1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

F #2347: VMGroup Roles managed in a separate clas

This commit is contained in:
Ruben S. Montero 2017-01-03 04:06:51 +01:00
parent 17749cb5e7
commit ef25d1821f
4 changed files with 366 additions and 228 deletions

View File

@ -18,56 +18,10 @@
#define VMGROUP_H_
#include "PoolObjectSQL.h"
#include "VMGroupRole.h"
class VMGroupPool;
/**
* A VMGroupRole defines a VM type that typically implements a role in a
* multi-vm application.
*
* ROLE = [
* NAME = "Web application servers",
* ID = 12,
* VMS = "1,2,45,21"
* ]
*
*/
class VMGroupRole
{
public:
VMGroupRole(VectorAttribute *_va);
virtual ~VMGroupRole(){};
/* ---------------------------------------------------------------------- */
/* VMS set Interface */
/* ---------------------------------------------------------------------- */
const std::set<int>& get_vms()
{
return vms;
};
void add_vm(int vm_id);
void del_vm(int vm_id);
private:
/**
* The set of vms in the role
*/
std::set<int> vms;
/**
* The associated vector attribute
*/
VectorAttribute * va;
/**
* Set the VMS attribute for the role (list of VM IDs)
*/
void set_vms();
};
/**
* A VM group is a set of related VMs that may impose placement constraints.
*
@ -130,101 +84,6 @@ private:
~VMGroup();
// -------------------------------------------------------------------------
// Role Map management
// -------------------------------------------------------------------------
/**
* A role map indexed by different key types
*/
template<class T>
class RoleMap
{
public:
/**
* Inserts a new role in the map
* @param k the key
* @param r pointer to yhe VMGroupRole
* @return true if the role was successfully inserted
*/
bool insert(const T& k, VMGroupRole * r)
{
std::pair<T, VMGroupRole *> rpair(k, r);
std::pair<roles_it, bool> rc;
rc = roles.insert(rpair);
return rc.second;
}
/**
* Frees the memory associated to the map and clears it
*/
void delete_roles()
{
for (roles_it it = roles.begin() ; it != roles.end() ; ++it )
{
delete it->second;
}
clear();
}
/**
* Clears the contents of the map
*/
void clear()
{
roles.clear();
}
size_t erase(const T& k)
{
return roles.erase(k);
}
/**
* Check id a set of keys are in the map.
* @param key_str a comma separated list of keys
* @return true if all the keys are in the map
*/
bool in_map(const string& key_str)
{
std::set<T> key_set;
typename std::set<T>::iterator it;
one_util::split_unique(key_str, ',', key_set);
if ( key_set.empty() )
{
return true;
}
for ( it = key_set.begin(); it != key_set.end() ; ++it )
{
if ( roles.find(*it) == roles.end() )
{
return false;
}
}
return true;
}
private:
typedef typename std::map<T, VMGroupRole *>::iterator roles_it;
std::map<T, VMGroupRole *> roles;
};
/**
* Add a new role to the VM group
* @param role to add as a vector attribute
* @param error if any
*
* @return 0 on success
*/
int add_role(VectorAttribute * vrole, string& error);
// -------------------------------------------------------------------------
// DataBase implementation
// -------------------------------------------------------------------------
@ -299,16 +158,11 @@ private:
*/
std::string name;
/**
* Map to access the roles by their name
*/
RoleMap<std::string> by_name;
/**
* Map to access the roles by their id
*/
RoleMap<int> by_id;
/**
* The role set
*/
VMGroupRoles roles;
};
#endif /*SECURITYGROUP_H_*/
#endif /*VMGROUP_H_*/

226
include/VMGroupRole.h Normal file
View File

@ -0,0 +1,226 @@
/* ------------------------------------------------------------------------ */
/* Copyright 2002-2016, 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. */
/* -------------------------------------------------------------------------*/
#ifndef VMGROUP_ROLE_H_
#define VMGROUP_ROLE_H_
#include "PoolObjectSQL.h"
class VMGroupPool;
/**
* A VMGroupRole defines a VM type that typically implements a role in a
* multi-vm application.
*
* ROLE = [
* NAME = "Web application servers",
* ID = 12,
* VMS = "1,2,45,21"
* ]
*
*/
class VMGroupRole
{
public:
VMGroupRole(VectorAttribute *_va);
virtual ~VMGroupRole(){};
/* ---------------------------------------------------------------------- */
/* VMS set Interface */
/* ---------------------------------------------------------------------- */
const std::set<int>& get_vms()
{
return vms;
};
void add_vm(int vm_id);
void del_vm(int vm_id);
private:
/**
* The set of vms in the role
*/
std::set<int> vms;
/**
* The associated vector attribute
*/
VectorAttribute * va;
/**
* Set the VMS attribute for the role (list of VM IDs)
*/
void set_vms();
};
/**
* The VMGroupRoles class represents a set of ROLES stored in a Template
*/
class VMGroupRoles
{
public:
VMGroupRoles():roles_template(false,'=',"ROLES"), next_role(0){};
~VMGroupRoles()
{
by_id.delete_roles();
};
/**
* Function to print the VMGroupRoles into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
std::string& to_xml(std::string& xml_str) const
{
return roles_template.to_xml(xml_str);
}
/**
* Builds the object from an xml node
* @param node for the ROLES template
* @return 0 on success, -1 otherwise
*/
int from_xml_node(const xmlNodePtr node);
/**
* Adds a new role to the set
* @param vrole VectorAttribute of the role
* @param error string if any
*
* @return 0 on success
*/
int add_role(VectorAttribute * vrole, string& error);
/**
* Check that a key list is defined in the name map
* @param key_str separated list of keys
* @param true if the keys are in the map
*/
bool in_map(const std::string& key_str)
{
return by_name.in_map(key_str);
}
private:
/**
* A role map indexed by different key types
*/
template<class T>
class RoleMap
{
public:
/**
* Inserts a new role in the map
* @param k the key
* @param r pointer to yhe VMGroupRole
* @return true if the role was successfully inserted
*/
bool insert(const T& k, VMGroupRole * r)
{
std::pair<T, VMGroupRole *> rpair(k, r);
std::pair<roles_it, bool> rc;
rc = roles.insert(rpair);
return rc.second;
}
/**
* Frees the memory associated to the map and clears it
*/
void delete_roles()
{
for (roles_it it = roles.begin() ; it != roles.end() ; ++it )
{
delete it->second;
}
clear();
}
/**
* Clears the contents of the map
*/
void clear()
{
roles.clear();
}
size_t erase(const T& k)
{
return roles.erase(k);
}
/**
* Check id a set of keys are in the map.
* @param key_str a comma separated list of keys
* @return true if all the keys are in the map
*/
bool in_map(const string& key_str)
{
std::set<T> key_set;
typename std::set<T>::iterator it;
one_util::split_unique(key_str, ',', key_set);
if ( key_set.empty() )
{
return true;
}
for ( it = key_set.begin(); it != key_set.end() ; ++it )
{
if ( roles.find(*it) == roles.end() )
{
return false;
}
}
return true;
}
private:
typedef typename std::map<T, VMGroupRole *>::iterator roles_it;
std::map<T, VMGroupRole *> roles;
};
/**
* The role template to store the VMGroupRole
*/
Template roles_template;
/**
* The next role id
*/
int next_role;
/**
* Map to access the roles by their name
*/
RoleMap<std::string> by_name;
/**
* Map to access the roles by their id
*/
RoleMap<int> by_id;
};
#endif /*VMGROUP_ROLE_H*/

View File

@ -95,8 +95,8 @@ class OneVMGroupHelper < OpenNebulaHelper::OneHelper
CLIHelper.print_header(str_h1 % "ROLES", false)
if !vmgroup.to_hash['VM_GROUP']['TEMPLATE']['ROLE'].nil?
roles = [vmgroup.to_hash['VM_GROUP']['TEMPLATE']['ROLE']].flatten
if !vmgroup.to_hash['VM_GROUP']['ROLES']['ROLE'].nil?
roles = [vmgroup.to_hash['VM_GROUP']['ROLES']['ROLE']].flatten
end
CLIHelper::ShowTable.new(nil, self) do

View File

@ -15,6 +15,7 @@
/* -------------------------------------------------------------------------*/
#include "VMGroup.h"
#include "VMGroupRole.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -60,7 +61,6 @@ void VMGroupRole::del_vm(int vm_id)
set_vms();
}
void VMGroupRole::set_vms()
{
std::string vms_str = one_util::join(vms.begin(), vms.end(), ',');
@ -68,13 +68,108 @@ void VMGroupRole::set_vms()
va->replace("VMS", vms_str);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VMGroupRoles::from_xml_node(const xmlNodePtr node)
{
std::vector<VectorAttribute *> roles;
std::vector<VectorAttribute *>::iterator it;
if ( roles_template.from_xml_node(node) == -1 )
{
return -1;
}
roles_template.get("ROLE", roles);
for (it = roles.begin(); it != roles.end(); ++it)
{
std::string rname = (*it)->vector_value("NAME");
int rid;
int rc = (*it)->vector_value("ID", rid);
if ( rname.empty() || rc == -1 )
{
return -1;
}
if ( rid >= next_role )
{
next_role = rid + 1;
}
VMGroupRole * role = new VMGroupRole((*it));
if ( by_id.insert(rid, role) == false )
{
delete role;
return -1;
}
if ( by_name.insert(rname, role) == false )
{
by_id.erase(rid);
delete role;
return -1;
}
}
return 0;
}
/* -------------------------------------------------------------------------- */
int VMGroupRoles::add_role(VectorAttribute * vrole, string& error)
{
std::string rname = vrole->vector_value("NAME");
if ( rname.empty() )
{
error = "Missing NAME in VM group role";
return -1;
}
// Remove internal attributes before inserting
vrole->replace("ID", next_role);
vrole->remove("VMS");
VMGroupRole * role = new VMGroupRole(vrole);
if ( by_id.insert(next_role, role) == false )
{
delete role;
error = "Role ID already exists";
return -1;
}
if ( by_name.insert(rname, role) == false )
{
by_id.erase(next_role);
delete role;
error = "Role NAME already exists";
return -1;
}
next_role += 1;
roles_template.set(vrole);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* VMGroup */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * VMGroup::table = "vmgroup_pool";
const char * VMGroup::db_names = "oid, name, body, uid, gid, owner_u, group_u, "
@ -105,8 +200,6 @@ VMGroup::VMGroup(int _uid, int _gid, const string& _uname, const string& _gname,
VMGroup::~VMGroup()
{
by_id.delete_roles();
delete obj_template;
}
@ -116,8 +209,9 @@ VMGroup::~VMGroup()
string& VMGroup::to_xml(string& xml) const
{
ostringstream oss;
string template_xml;
string perms_xml;
string template_xml;
string perms_xml;
string roles_xml;
oss <<
"<VM_GROUP>" <<
@ -128,6 +222,7 @@ string& VMGroup::to_xml(string& xml) const
"<GNAME>" << gname << "</GNAME>" <<
"<NAME>" << name << "</NAME>" <<
perms_to_xml(perms_xml) <<
roles.to_xml(roles_xml) <<
obj_template->to_xml(template_xml) <<
"</VM_GROUP>";
@ -165,74 +260,29 @@ int VMGroup::from_xml(const string &xml_str)
return -1;
}
// Template contents
rc += obj_template->from_xml_node(content[0]);
ObjectXML::free_nodes(content);
content.clear();
// VMGroup roles
ObjectXML::get_nodes("/VM_GROUP/ROLES", content);
if (!content.empty())
{
rc += roles.from_xml_node(content[0]);
}
ObjectXML::free_nodes(content);
content.clear();
if (rc != 0)
{
return -1;
}
// VMGroup roles
vector<VectorAttribute *> roles;
vector<VectorAttribute *>::iterator it;
std::string error;
obj_template->get("ROLE", roles);
for (it = roles.begin(); it != roles.end(); ++it)
{
if ( add_role(*it, error) == -1 )
{
return -1;
}
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VMGroup::add_role(VectorAttribute * vrole, string& error)
{
int rid;
std::string rname = vrole->vector_value("NAME");
if ( vrole->vector_value("ID", rid) == -1 )
{
error = "Missing ID in VM group role";
return -1;
}
else if ( rname.empty() )
{
error = "Missing NAME in VM group role";
return -1;
}
VMGroupRole * role = new VMGroupRole(vrole);
if ( by_id.insert(rid, role) == false )
{
delete role;
error = "Role ID already exists";
return -1;
}
if ( by_name.insert(rname, role) == false )
{
by_id.erase(rid);
delete role;
error = "Role NAME already exists";
return -1;
}
return 0;
}
@ -322,10 +372,8 @@ error_common:
int VMGroup::insert(SqlDB *db, string& error_str)
{
int role_id;
vector<VectorAttribute*> va_roles;
vector<VectorAttribute*>::iterator it;
vector<VectorAttribute*> roles;
vector<const SingleAttribute*> affined;
vector<const SingleAttribute*> anti_affined;
@ -340,16 +388,26 @@ int VMGroup::insert(SqlDB *db, string& error_str)
return -1;
}
get_template_attribute("ROLE", roles);
remove_template_attribute("ROLE", va_roles);
for ( it = roles.begin(), role_id = 0; it != roles.end(); ++it, ++role_id )
bool error = false;
for ( it = va_roles.begin(); it != va_roles.end(); ++it )
{
(*it)->replace("ID", role_id);
if (add_role(*it, error_str) == -1)
if ( error )
{
return -1;
delete *it;
}
else if ( roles.add_role(*it, error_str) == -1 )
{
delete *it;
error = true;
}
}
if ( error )
{
return -1;
}
get_template_attribute("AFFINED", affined);
@ -358,7 +416,7 @@ int VMGroup::insert(SqlDB *db, string& error_str)
{
std::string a_str = (*jt)->value();
if ( !by_name.in_map(a_str) )
if ( !roles.in_map(a_str) )
{
error_str = "Some AFFINED roles: " + a_str + ", not defined";
return -1;
@ -371,7 +429,7 @@ int VMGroup::insert(SqlDB *db, string& error_str)
{
std::string a_str = (*jt)->value();
if ( !by_name.in_map(a_str) )
if ( !roles.in_map(a_str) )
{
error_str = "Some ANTI_AFFINED roles: " + a_str + ", not defined";
return -1;