Files
one/src/rm/RequestManagerCluster.cc
T
Ruben S. Montero 44e44b47a8 F #3859: New monitoring architecture
co-authored-by: Pavel Czerny <pczerny@opennebula.systems>
  co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.systems>
2020-03-04 16:05:57 +01:00

349 lines
8.3 KiB
C++

/* -------------------------------------------------------------------------- */
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "RequestManagerCluster.h"
#include "HostPool.h"
using namespace std;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void RequestManagerCluster::action_generic(
int cluster_id,
int object_id,
RequestAttributes& att,
PoolSQL * pool,
PoolObjectSQL::ObjectType type,
bool add)
{
int rc;
string cluster_name;
string obj_name;
Cluster * cluster = nullptr;
Clusterable * cluster_obj = nullptr;
PoolObjectSQL * object = nullptr;
PoolObjectAuth c_perms;
PoolObjectAuth obj_perms;
rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, c_perms,
cluster_name, true);
if ( rc == -1 )
{
return;
}
rc = get_info(pool, object_id, type, att, obj_perms, obj_name, true);
if ( rc == -1 )
{
return;
}
AuthRequest ar(att.uid, att.group_ids);
ar.add_auth(att.auth_op, c_perms); // ADMIN CLUSTER
ar.add_auth(AuthRequest::ADMIN, obj_perms); // ADMIN OBJECT
if (UserPool::authorize(ar) == -1)
{
att.resp_msg = ar.message;
failure_response(AUTHORIZATION, att);
return;
}
// ------------- Set new cluster id in object ---------------------
get(object_id, &object, &cluster_obj);
if ( object == nullptr )
{
att.resp_obj = type;
att.resp_id = object_id;
failure_response(NO_EXISTS, att);
return;
}
if (add)
{
rc = cluster_obj->add_cluster(cluster_id);
}
else
{
rc = cluster_obj->del_cluster(cluster_id);
}
if ( rc == -1 )
{
object->unlock();
success_response(cluster_id, att);
return;
}
pool->update(object);
object->unlock();
// ------------- Add/del object to new cluster ---------------------
cluster = clpool->get(cluster_id);
if ( cluster == nullptr )
{
att.resp_obj = PoolObjectSQL::CLUSTER;
att.resp_id = cluster_id;
failure_response(NO_EXISTS, att);
// Rollback
get(object_id, &object, &cluster_obj);
if ( object != nullptr )
{
if (add)
{
cluster_obj->del_cluster(cluster_id);
}
else
{
cluster_obj->add_cluster(cluster_id);
}
pool->update(object);
object->unlock();
}
return;
}
if (add)
{
rc = add_object(cluster, object_id, att.resp_msg);
}
else
{
rc = del_object(cluster, object_id, att.resp_msg);
}
if ( rc < 0 )
{
cluster->unlock();
failure_response(ACTION, att);
// Rollback
get(object_id, &object, &cluster_obj);
if ( object != nullptr )
{
if (add)
{
cluster_obj->del_cluster(cluster_id);
}
else
{
cluster_obj->add_cluster(cluster_id);
}
pool->update(object);
object->unlock();
}
return;
}
clpool->update(cluster);
cluster->unlock();
success_response(cluster_id, att);
return;
}
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void RequestManagerClusterHost::add_generic(
int cluster_id,
int host_id,
RequestAttributes& att)
{
int rc;
string cluster_name;
string obj_name;
string err_msg;
Cluster * cluster = nullptr;
Host * host = nullptr;
PoolObjectAuth c_perms;
PoolObjectAuth obj_perms;
int old_cluster_id;
string old_cluster_name;
rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, c_perms,
cluster_name, true);
if ( rc == -1 )
{
return;
}
rc = get_info(hpool, host_id, PoolObjectSQL::HOST, att, obj_perms, obj_name, true);
if ( rc == -1 )
{
return;
}
AuthRequest ar(att.uid, att.group_ids);
ar.add_auth(att.auth_op, c_perms); // ADMIN CLUSTER
ar.add_auth(AuthRequest::ADMIN, obj_perms); // ADMIN HOST
if (UserPool::authorize(ar) == -1)
{
att.resp_msg = ar.message;
failure_response(AUTHORIZATION, att);
return;
}
cluster = clpool->get_ro(cluster_id);
if ( cluster == nullptr )
{
att.resp_obj = PoolObjectSQL::CLUSTER;
att.resp_id = cluster_id;
failure_response(NO_EXISTS, att);
return;
}
string ccpu;
string cmem;
cluster->get_reserved_capacity(ccpu, cmem);
cluster->unlock();
// ------------- Set new cluster id in object ---------------------
host = hpool->get(host_id);
if ( host == nullptr )
{
att.resp_obj = PoolObjectSQL::HOST;
att.resp_id = host_id;
failure_response(NO_EXISTS, att);
return;
}
old_cluster_id = host->get_cluster_id();
old_cluster_name = host->get_cluster_name();
if ( old_cluster_id == cluster_id )
{
host->unlock();
success_response(cluster_id, att);
return;
}
host->set_cluster(cluster_id, cluster_name);
host->update_reserved_capacity(ccpu, cmem);
hpool->update(host);
host->unlock();
// ------------- Add object to new cluster ---------------------
cluster = clpool->get(cluster_id);
if ( clpool->add_to_cluster(PoolObjectSQL::HOST, cluster, host_id, att.resp_msg) < 0 )
{
cluster->unlock();
failure_response(INTERNAL, att);
// Rollback
cluster = clpool->get_ro(old_cluster_id);
if ( cluster != nullptr )
{
cluster->get_reserved_capacity(ccpu, cmem);
cluster->unlock();
}
else
{
old_cluster_id = ClusterPool::DEFAULT_CLUSTER_ID;
old_cluster_name = ClusterPool::DEFAULT_CLUSTER_NAME;
ccpu = "0";
cmem = "0";
}
host = hpool->get(host_id);
if ( host != nullptr )
{
host->set_cluster(old_cluster_id, old_cluster_name);
host->update_reserved_capacity(ccpu, cmem);
hpool->update(host);
host->unlock();
}
return;
}
cluster->unlock();
// ------------- Remove host from old cluster ---------------------
cluster = clpool->get(old_cluster_id);
if ( cluster == nullptr )
{
// This point should be unreachable.
// The old cluster is not empty (at least has the host_id),
// so it cannot be deleted
success_response(cluster_id, att);
return;
}
if ( clpool->del_from_cluster(PoolObjectSQL::HOST, cluster, host_id, att.resp_msg) < 0 )
{
cluster->unlock();
failure_response(INTERNAL, att);
return;
}
cluster->unlock();
success_response(cluster_id, att);
return;
}