1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
5
/* not use this file except in compliance with the License. You may obtain */
6
/* a copy of the License at */
8
/* http://www.apache.org/licenses/LICENSE-2.0 */
10
/* Unless required by applicable law or agreed to in writing, software */
11
/* distributed under the License is distributed on an "AS IS" BASIS, */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13
/* See the License for the specific language governing permissions and */
14
/* limitations under the License. */
15
/* -------------------------------------------------------------------------- */
17
#include "RequestManagerCluster.h"
21
/* ------------------------------------------------------------------------- */
22
/* ------------------------------------------------------------------------- */
24
void RequestManagerCluster::add_generic(
27
RequestAttributes& att,
29
PoolObjectSQL::ObjectType type)
38
Clusterable * cluster_obj = 0;
39
PoolObjectSQL * object = 0;
42
PoolObjectAuth c_perms;
43
PoolObjectAuth obj_perms;
46
string old_cluster_name;
48
if ( cluster_id != ClusterPool::NONE_CLUSTER_ID )
50
rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, c_perms, cluster_name);
59
cluster_name = ClusterPool::NONE_CLUSTER_NAME;
62
rc = get_info(pool, object_id, type, att, obj_perms, obj_name);
71
AuthRequest ar(att.uid, att.gid);
73
if ( cluster_id != ClusterPool::NONE_CLUSTER_ID )
75
ar.add_auth(auth_op, c_perms); // ADMIN CLUSTER
78
ar.add_auth(AuthRequest::ADMIN, obj_perms); // ADMIN OBJECT
80
if (UserPool::authorize(ar) == -1)
82
failure_response(AUTHORIZATION,
83
authorization_error(ar.message, att),
90
// ------------- Set new cluster id in object ---------------------
91
get(object_id, true, &object, &cluster_obj);
95
failure_response(NO_EXISTS,
96
get_error(object_name(type), object_id),
102
old_cluster_id = cluster_obj->get_cluster_id();
103
old_cluster_name = cluster_obj->get_cluster_name();
105
if ( old_cluster_id == cluster_id )
108
success_response(cluster_id, att);
112
cluster_obj->set_cluster(cluster_id, cluster_name);
114
pool->update(object);
118
// ------------- Add object to new cluster ---------------------
119
if ( cluster_id != ClusterPool::NONE_CLUSTER_ID )
121
cluster = clpool->get(cluster_id, true);
125
failure_response(NO_EXISTS,
126
get_error(object_name(PoolObjectSQL::CLUSTER),cluster_id),
130
get(object_id, true, &object, &cluster_obj);
134
cluster_obj->set_cluster(old_cluster_id, old_cluster_name);
136
pool->update(object);
144
if ( add_object(cluster, object_id, err_msg) < 0 )
148
failure_response(INTERNAL,
149
request_error("Cannot add object to cluster", err_msg),
155
clpool->update(cluster);
160
// ------------- Remove host from old cluster ---------------------
162
if ( old_cluster_id != ClusterPool::NONE_CLUSTER_ID )
164
cluster = clpool->get(old_cluster_id, true);
168
// This point should be unreachable.
169
// The old cluster is not empty (at least has the host_id),
170
// so it cannot be deleted
171
success_response(cluster_id, att);
175
if ( del_object(cluster, object_id, err_msg) < 0 )
179
failure_response(INTERNAL,
180
request_error("Cannot remove object from cluster", err_msg),
186
clpool->update(cluster);
191
success_response(cluster_id, att);