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

feature #1304: Removed name uniqueness for document pool. Simplify related functions because of this

This commit is contained in:
Ruben S. Montero 2012-06-13 12:44:04 +02:00
parent 85644bfe0e
commit cb9e8bdb82
22 changed files with 86 additions and 270 deletions

View File

@ -21,6 +21,9 @@
#include <string>
#include <libxml/tree.h>
#include "PoolObjectSQL.h"
#include "AuthManager.h"
using namespace std;
/**
@ -224,6 +227,20 @@ private:
* Builds the human representation of the ACL
*/
void build_str();
/**
* Array of PoolObjectSQL types to iterate over all types
*/
static const int num_pool_objects;
static const PoolObjectSQL::ObjectType pool_objects[];
/**
* Array of Auth operation types to iterate over all types
*/
static const int num_auth_operations;
static const AuthRequest::Operation auth_operations[];
};
#endif /*ACL_RULE_H*/

View File

@ -80,11 +80,6 @@ private:
// Document Attributes
// -------------------------------------------------------------------------
/**
* Registration time
*/
time_t regtime;
/**
* Document type, to implement generic objects.
*/

View File

@ -27,7 +27,7 @@ class DocumentPool : public PoolSQL
{
public:
DocumentPool(SqlDB * db) : PoolSQL(db, Document::table, true){};
DocumentPool(SqlDB * db) : PoolSQL(db, Document::table, false){};
~DocumentPool(){};
@ -52,48 +52,26 @@ public:
int type,
Template * template_contents,
int * oid,
string& error_str);
string& error_str)
{
*oid = PoolSQL::allocate(
new Document(-1, uid, gid, uname, gname, type, template_contents),
error_str);
return *oid;
}
/**
* Gets an object from the pool (if needed the object is loaded from the
* database).
* @param oid the object unique identifier
* @param type the document type
* @param lock locks the object if true
*
* @return a pointer to the object, 0 in case of failure
*/
Document * get(int oid, int type, bool lock)
Document * get(int oid, bool lock)
{
Document* tmpl = static_cast<Document *>(PoolSQL::get(oid,lock));
if ( tmpl != 0 && tmpl->get_document_type() != type )
{
if ( lock )
{
tmpl->unlock();
}
return 0;
}
return tmpl;
};
/**
* Gets an object from the pool (if needed the object is loaded from the
* database).
* @param name of the object
* @param uid id of owner
* @param type the document type
* @param lock locks the object if true
*
* @return a pointer to the object, 0 in case of failure
*/
Document * get(const string& name, int uid, int type, bool lock)
{
// TODO: use type
return static_cast<Document *>(PoolSQL::get(name,uid,lock));
return static_cast<Document *>(PoolSQL::get(oid,lock));
};
/**

View File

@ -30,9 +30,8 @@ class RequestManagerChmod : public Request
{
protected:
RequestManagerChmod(const string& method_name,
const string& help,
const string& params = "A:siiiiiiiiii")
:Request(method_name,params,help){};
const string& help)
:Request(method_name, "A:siiiiiiiiii", help){};
~RequestManagerChmod(){};
@ -40,12 +39,6 @@ protected:
virtual void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
};
/* ------------------------------------------------------------------------- */
@ -149,8 +142,7 @@ class DocumentChmod : public RequestManagerChmod
public:
DocumentChmod():
RequestManagerChmod("DocumentChmod",
"Changes permission bits of a generic document",
"A:siiiiiiiiiii")
"Changes permission bits of a generic document")
{
Nebula& nd = Nebula::instance();
pool = nd.get_docpool();
@ -158,16 +150,8 @@ public:
};
~DocumentChmod(){};
/* -------------------------------------------------------------------- */
PoolObjectSQL * get_obj(int oid, xmlrpc_c::paramList const& paramList)
{
int obj_type = xmlrpc_c::value_int(paramList.getInt(11));
return static_cast<DocumentPool*>(pool)->get(oid, obj_type, true);
};
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -52,13 +52,6 @@ protected:
virtual void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
};
/* ------------------------------------------------------------------------- */
@ -196,15 +189,6 @@ public:
};
~DocumentChown(){};
/* -------------------------------------------------------------------- */
PoolObjectSQL * get_obj(int oid, xmlrpc_c::paramList const& paramList)
{
int obj_type = xmlrpc_c::value_int(paramList.getInt(4));
return static_cast<DocumentPool*>(pool)->get(oid, obj_type, true);
};
};
/* -------------------------------------------------------------------------- */

View File

@ -42,12 +42,6 @@ protected:
virtual void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
virtual Template * clone_template(PoolObjectSQL* obj) = 0;
virtual int pool_allocate(

View File

@ -50,17 +50,10 @@ protected:
RequestAttributes& att);
bool delete_authorization(int oid,
RequestAttributes& att,
xmlrpc_c::paramList const& paramList);
RequestAttributes& att);
/* -------------------------------------------------------------------- */
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
virtual int drop(int oid, PoolObjectSQL * object, string& error_msg);
virtual int get_cluster_id(PoolObjectSQL * object)
@ -295,15 +288,6 @@ public:
};
~DocumentDelete(){};
/* -------------------------------------------------------------------- */
PoolObjectSQL * get_obj(int oid, xmlrpc_c::paramList const& paramList)
{
int obj_type = xmlrpc_c::value_int(paramList.getInt(2));
return static_cast<DocumentPool*>(pool)->get(oid, obj_type, true);
};
};
/* -------------------------------------------------------------------------- */

View File

@ -50,12 +50,6 @@ protected:
{
object->to_xml(str);
};
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
};
/* ------------------------------------------------------------------------- */
@ -255,15 +249,6 @@ public:
};
~DocumentInfo(){};
/* -------------------------------------------------------------------- */
PoolObjectSQL * get_obj(int oid, xmlrpc_c::paramList const& paramList)
{
int obj_type = xmlrpc_c::value_int(paramList.getInt(2));
return static_cast<DocumentPool*>(pool)->get(oid, obj_type, true);
};
};
/* -------------------------------------------------------------------------- */

View File

@ -43,12 +43,6 @@ protected:
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
virtual PoolObjectSQL * get_obj(
int oid, xmlrpc_c::paramList const& paramList)
{
return pool->get(oid,true);
};
};
/* ------------------------------------------------------------------------- */
@ -176,15 +170,6 @@ public:
};
~DocumentUpdateTemplate(){};
/* -------------------------------------------------------------------- */
PoolObjectSQL * get_obj(int oid, xmlrpc_c::paramList const& paramList)
{
int obj_type = xmlrpc_c::value_int(paramList.getInt(3));
return static_cast<DocumentPool*>(pool)->get(oid, obj_type, true);
};
};
/* -------------------------------------------------------------------------- */

View File

@ -27,6 +27,28 @@ const long long AclRule::ALL_ID = 0x0000000400000000LL;
const long long AclRule::NONE_ID = 0x1000000000000000LL;
const int AclRule::num_pool_objects = 10;
const PoolObjectSQL::ObjectType AclRule::pool_objects[] = {
PoolObjectSQL::VM,
PoolObjectSQL::HOST,
PoolObjectSQL::NET,
PoolObjectSQL::IMAGE,
PoolObjectSQL::USER,
PoolObjectSQL::TEMPLATE,
PoolObjectSQL::GROUP,
PoolObjectSQL::DATASTORE,
PoolObjectSQL::CLUSTER,
PoolObjectSQL::DOCUMENT
};
const int AclRule::num_auth_operations = 4;
const AuthRequest::Operation AclRule::auth_operations[] = {
AuthRequest::USE,
AuthRequest::MANAGE,
AuthRequest::ADMIN,
AuthRequest::CREATE
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -246,32 +268,18 @@ void AclRule::build_str()
oss << " ";
int n_objects = 10;
PoolObjectSQL::ObjectType objects[] = {
PoolObjectSQL::VM,
PoolObjectSQL::HOST,
PoolObjectSQL::NET,
PoolObjectSQL::IMAGE,
PoolObjectSQL::USER,
PoolObjectSQL::TEMPLATE,
PoolObjectSQL::GROUP,
PoolObjectSQL::DATASTORE,
PoolObjectSQL::CLUSTER,
PoolObjectSQL::DOCUMENT
};
bool prefix = false;
for ( int i = 0; i < n_objects; i++ )
for ( int i = 0; i < num_pool_objects; i++ )
{
if ( (resource & objects[i]) != 0 )
if ( (resource & pool_objects[i]) != 0 )
{
if ( prefix )
{
oss << "+";
}
oss << PoolObjectSQL::type_to_str( objects[i] );
oss << PoolObjectSQL::type_to_str( pool_objects[i] );
prefix = true;
}
}
@ -295,29 +303,20 @@ void AclRule::build_str()
oss << "??";
}
oss << " ";
AuthRequest::Operation operations[] = {
AuthRequest::USE,
AuthRequest::MANAGE,
AuthRequest::ADMIN,
AuthRequest::CREATE
};
prefix = false;
for ( int i = 0; i < 4; i++ )
for ( int i = 0; i < num_auth_operations; i++ )
{
if ( (rights & operations[i]) != 0 )
if ( (rights & auth_operations[i]) != 0 )
{
if ( prefix )
{
oss << "+";
}
oss << AuthRequest::operation_to_str( operations[i] );
oss << AuthRequest::operation_to_str( auth_operations[i] );
prefix = true;
}
}
@ -399,4 +398,3 @@ int AclRule::from_xml(xmlNodePtr node)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -27,9 +27,7 @@ Document::Document( int id,
const string& _gname,
int _type,
Template * _template_contents):
PoolObjectSQL(id,DOCUMENT,"",_uid,_gid,_uname,_gname,table),
regtime(time(0)),
type(_type)
PoolObjectSQL(id,DOCUMENT,"",_uid,_gid,_uname,_gname,table), type(_type)
{
if (_template_contents != 0)
{
@ -64,8 +62,7 @@ const char * Document::db_names =
const char * Document::db_bootstrap =
"CREATE TABLE IF NOT EXISTS document_pool (oid INTEGER PRIMARY KEY, "
"name VARCHAR(128), body TEXT, type INTEGER, uid INTEGER, gid INTEGER, "
"owner_u INTEGER, group_u INTEGER, other_u INTEGER,"
"UNIQUE(name,uid,type))";
"owner_u INTEGER, group_u INTEGER, other_u INTEGER)";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -204,7 +201,6 @@ string& Document::to_xml(string& xml) const
<< "<NAME>" << name << "</NAME>"
<< "<TYPE>" << type << "</TYPE>"
<< perms_to_xml(perm_str)
<< "<REGTIME>" << regtime << "</REGTIME>"
<< obj_template->to_xml(template_xml)
<< "</DOCUMENT>";
@ -232,7 +228,6 @@ int Document::from_xml(const string& xml)
rc += xpath(gname, "/DOCUMENT/GNAME", "not_found");
rc += xpath(name, "/DOCUMENT/NAME", "not_found");
rc += xpath(type, "/DOCUMENT/TYPE", 0);
rc += xpath(regtime, "/DOCUMENT/REGTIME", 0);
// Permissions
rc += perms_from_xml();

View File

@ -1,80 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
/* ************************************************************************** */
/* Document Pool */
/* ************************************************************************** */
#include "DocumentPool.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int DocumentPool::allocate (
int uid,
int gid,
const string& uname,
const string& gname,
int type,
Template * template_contents,
int * oid,
string& error_str)
{
Document * document;
Document * document_aux = 0;
string name;
ostringstream oss;
// ------------------------------------------------------------------------
// Build a new Document object
// ------------------------------------------------------------------------
document = new Document(-1, uid, gid, uname, gname,
type, template_contents);
// Check name
document->get_template_attribute("NAME", name);
if ( !name.empty() )
{
// Check for duplicates
document_aux = get(name, uid, type, false);
if( document_aux != 0 )
{
goto error_duplicated;
}
}
// ------------------------------------------------------------------------
// Insert the Object in the pool
// ------------------------------------------------------------------------
*oid = PoolSQL::allocate(document, error_str);
return *oid;
error_duplicated:
oss << "NAME is already taken by DOCUMENT "
<< document_aux->get_oid() << ".";
delete document;
*oid = -1;
error_str = oss.str();
return *oid;
}

View File

@ -22,8 +22,7 @@ lib_name='nebula_document'
# Sources to generate the library
source_files=[
'Document.cc',
'DocumentPool.cc'
'Document.cc'
]
# Build library

View File

@ -139,7 +139,7 @@ module OpenNebula
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chown(uid, gid)
super(DOCUMENT_METHODS[:chown], uid, gid, TYPE)
super(DOCUMENT_METHODS[:chown], uid, gid)
end
# Changes the Document permissions.
@ -149,7 +149,7 @@ module OpenNebula
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod_octet(octet)
super(DOCUMENT_METHODS[:chmod], octet, TYPE)
super(DOCUMENT_METHODS[:chmod], octet)
end
# Changes the Document permissions.
@ -160,7 +160,7 @@ module OpenNebula
def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a)
super(DOCUMENT_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
group_m, group_a, other_u, other_m, other_a, TYPE)
group_m, group_a, other_u, other_m, other_a)
end
# Clones this Document into a new one

View File

@ -270,12 +270,11 @@ module OpenNebula
# @param [String] xml_method the name of the XML-RPC method
# @param [Integer] uid the new owner id. Set to -1 to leave the current one
# @param [Integer] gid the new goup id. Set to -1 to leave the current one
# @param [Array] args any extra arguments for the xml-rpc method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chown(xml_method, uid, gid, *args)
return call(xml_method, @pe_id, uid, gid, *args)
def chown(xml_method, uid, gid)
return call(xml_method, @pe_id, uid, gid)
end
# Calls to the corresponding chmod method to modify
@ -286,7 +285,7 @@ module OpenNebula
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod_octet(xml_method, octet, *args)
def chmod_octet(xml_method, octet)
owner_u = octet[0..0].to_i & 4 != 0 ? 1 : 0
owner_m = octet[0..0].to_i & 2 != 0 ? 1 : 0
owner_a = octet[0..0].to_i & 1 != 0 ? 1 : 0
@ -298,7 +297,7 @@ module OpenNebula
other_a = octet[2..2].to_i & 1 != 0 ? 1 : 0
chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a, *args)
other_m, other_a)
end
# Calls to the corresponding chmod method to modify
@ -310,10 +309,10 @@ module OpenNebula
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod(xml_method, owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a, *args)
other_m, other_a)
return call(xml_method, @pe_id, owner_u, owner_m,
owner_a, group_u, group_m, group_a, other_u,
other_m, other_a, *args)
other_m, other_a)
end

View File

@ -144,7 +144,7 @@ module Migrator
@db.run "DROP TABLE old_history;"
@db.run "CREATE TABLE document_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, type INTEGER, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, UNIQUE(name,uid,type));"
@db.run "CREATE TABLE document_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, type INTEGER, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER);"
return true
end

View File

@ -49,7 +49,7 @@ void RequestManagerChmod::request_execute(xmlrpc_c::paramList const& paramList,
AuthRequest::Operation op = AuthRequest::MANAGE;
PoolObjectAuth perms;
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{
@ -128,7 +128,7 @@ void RequestManagerChmod::request_execute(xmlrpc_c::paramList const& paramList,
// ------------- Update the object ---------------------
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{

View File

@ -104,7 +104,7 @@ void RequestManagerChown::request_execute(xmlrpc_c::paramList const& paramList,
// ------------- Update the object ---------------------
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{

View File

@ -37,12 +37,12 @@ void RequestManagerClone::request_execute(
string error_str;
source_obj = get_obj(source_id, paramList);
source_obj = pool->get(source_id, true);
if ( source_obj == 0 )
{
failure_response(NO_EXISTS,
get_error(object_name(auth_object),source_id),
get_error(object_name(auth_object), source_id),
att);
return;

View File

@ -23,8 +23,7 @@ using namespace std;
bool RequestManagerDelete::delete_authorization(
int oid,
RequestAttributes& att,
xmlrpc_c::paramList const& paramList)
RequestAttributes& att)
{
PoolObjectSQL * object;
PoolObjectAuth perms;
@ -34,7 +33,7 @@ bool RequestManagerDelete::delete_authorization(
return true;
}
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{
@ -74,12 +73,12 @@ void RequestManagerDelete::request_execute(xmlrpc_c::paramList const& paramList,
PoolObjectSQL * object;
string error_msg;
if ( delete_authorization(oid, att, paramList) == false )
if ( delete_authorization(oid, att) == false )
{
return;
}
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{

View File

@ -45,7 +45,7 @@ void RequestManagerInfo::request_execute(xmlrpc_c::paramList const& paramList,
return;
}
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{

View File

@ -38,7 +38,7 @@ void RequestManagerUpdateTemplate::request_execute(
return;
}
object = get_obj(oid, paramList);
object = pool->get(oid,true);
if ( object == 0 )
{