2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2011-02-25 16:34:44 +03:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
2008-06-17 20:27:32 +04:00
/* */
/* 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 POOL_OBJECT_SQL_H_
# define POOL_OBJECT_SQL_H_
# include "ObjectSQL.h"
2011-02-24 20:12:26 +03:00
# include "ObjectXML.h"
2011-04-08 03:02:55 +04:00
# include "Template.h"
2008-06-17 20:27:32 +04:00
# include <pthread.h>
2011-02-24 20:12:26 +03:00
# include <string.h>
2008-06-17 20:27:32 +04:00
using namespace std ;
/**
2010-04-03 18:54:54 +04:00
* PoolObject class . Provides a SQL backend interface for Pool components . Each
2008-06-17 20:27:32 +04:00
* object is identified with and unique OID
2010-04-03 18:54:54 +04:00
*
* Note : The PoolObject provides a synchronization mechanism ( mutex ) . This
2008-06-17 20:27:32 +04:00
* implementation assumes that the mutex IS LOCKED when the class destructor
2010-04-03 18:54:54 +04:00
* is called .
2008-06-17 20:27:32 +04:00
*/
2011-02-24 20:12:26 +03:00
class PoolObjectSQL : public ObjectSQL , public ObjectXML
2008-06-17 20:27:32 +04:00
{
public :
2011-06-30 13:31:00 +04:00
PoolObjectSQL ( int id ,
const string & _name ,
int _uid ,
int _gid ,
const string & _uname ,
const string & _gname ,
const char * _table )
: ObjectSQL ( ) ,
ObjectXML ( ) ,
oid ( id ) ,
name ( _name ) ,
uid ( _uid ) ,
gid ( _gid ) ,
uname ( _uname ) ,
gname ( _gname ) ,
valid ( true ) ,
public_obj ( 0 ) ,
obj_template ( 0 ) ,
table ( _table )
2008-06-17 20:27:32 +04:00
{
pthread_mutex_init ( & mutex , 0 ) ;
} ;
virtual ~ PoolObjectSQL ( )
{
pthread_mutex_unlock ( & mutex ) ;
2010-04-03 18:54:54 +04:00
2008-06-17 20:27:32 +04:00
pthread_mutex_destroy ( & mutex ) ;
} ;
2010-04-03 18:54:54 +04:00
2011-03-05 00:37:21 +03:00
/* --------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
int get_oid ( ) const
{
return oid ;
} ;
2011-03-05 00:37:21 +03:00
const string & get_name ( ) const
{
return name ;
} ;
2011-05-26 14:20:56 +04:00
/**
* Returns true if the image is public
* @ return true if the image is public
*/
bool isPublic ( )
{
return ( public_obj = = 1 ) ;
} ;
2011-06-03 20:26:59 +04:00
int get_uid ( )
{
return uid ;
} ;
2011-06-09 02:58:57 +04:00
int get_gid ( )
{
return gid ;
} ;
2011-06-07 18:55:23 +04:00
/**
* Changes the object ' s owner id
* @ param _uid New User ID
*/
2011-06-09 02:58:57 +04:00
void set_uid ( int _uid )
2011-05-17 21:13:59 +04:00
{
uid = _uid ;
}
2011-06-07 18:55:23 +04:00
/**
* Changes the object ' s group id
* @ param _gid New Group ID
*/
2011-06-09 02:58:57 +04:00
void set_gid ( int _gid )
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
{
gid = _gid ;
} ;
2011-03-05 00:37:21 +03:00
/* --------------------------------------------------------------------- */
2009-07-13 16:21:14 +04:00
/**
* Check if the object is valid
* @ return true if object is valid
*/
const bool & isValid ( ) const
{
return valid ;
} ;
/**
2010-04-03 18:54:54 +04:00
* Set the object valid flag
2009-07-13 16:21:14 +04:00
* @ param _valid new valid flag
*/
void set_valid ( const bool _valid )
{
valid = _valid ;
2011-02-24 20:12:26 +03:00
} ;
2009-07-13 16:21:14 +04:00
2008-06-17 20:27:32 +04:00
/**
* Function to lock the object
*/
void lock ( )
{
pthread_mutex_lock ( & mutex ) ;
} ;
/**
* Function to unlock the object
*/
void unlock ( )
{
pthread_mutex_unlock ( & mutex ) ;
} ;
2010-04-03 18:54:54 +04:00
2011-05-13 02:47:21 +04:00
/**
* Function to print the object into a string in XML format
* base64 encoded
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
virtual string & to_xml64 ( string & xml64 ) ;
2011-02-24 20:12:26 +03:00
/**
* Function to print the object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2011-03-04 19:04:28 +03:00
virtual string & to_xml ( string & xml ) const = 0 ;
2011-02-24 20:12:26 +03:00
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
2011-03-04 19:04:28 +03:00
virtual int from_xml ( const string & xml_str ) = 0 ;
2011-02-24 20:12:26 +03:00
2011-04-08 03:02:55 +04:00
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @ param name of the attribute
* @ param values of the attribute
* @ return the number of values
*/
int get_template_attribute (
string & name ,
vector < const Attribute * > & values ) const
{
return obj_template - > get ( name , values ) ;
} ;
/**
* Gets the values of a template attribute
* @ param name of the attribute
* @ param values of the attribute
* @ return the number of values
*/
int get_template_attribute (
const char * name ,
vector < const Attribute * > & values ) const
{
string str = name ;
return obj_template - > get ( str , values ) ;
} ;
/**
* Gets a string based attribute ( single )
* @ param name of the attribute
* @ param value of the attribute ( a string ) , will be " " if not defined or
* not a single attribute
*/
void get_template_attribute (
const char * name ,
string & value ) const
{
string str = name ;
obj_template - > get ( str , value ) ;
}
/**
* Gets an int based attribute ( single )
* @ param name of the attribute
* @ param value of the attribute ( an int ) , will be 0 if not defined or
* not a single attribute
*/
void get_template_attribute (
const char * name ,
int & value ) const
{
string str = name ;
obj_template - > get ( str , value ) ;
}
/**
* Adds a new attribute to the template ( replacing it if
* already defined ) , the object ' s mutex SHOULD be locked
* @ param name of the new attribute
* @ param value of the new attribute
* @ return 0 on success
*/
int replace_template_attribute (
const string & name ,
const string & value )
{
2011-05-08 04:13:37 +04:00
SingleAttribute * sattr = new SingleAttribute ( name , value ) ;
2011-04-08 03:02:55 +04:00
2011-05-08 04:13:37 +04:00
obj_template - > erase ( sattr - > name ( ) ) ;
2011-04-08 03:02:55 +04:00
obj_template - > set ( sattr ) ;
return 0 ;
}
/**
* Generates a XML string for the template of the Object
* @ param xml the string to store the XML description .
*/
void template_to_xml ( string & xml ) const
{
obj_template - > to_xml ( xml ) ;
}
/**
* Removes an Image attribute
* @ param name of the attribute
*/
int remove_template_attribute ( const string & name )
{
return obj_template - > erase ( name ) ;
}
2011-04-15 03:51:38 +04:00
/**
* Sets an error message for the VM in the template
* @ param message
* @ return 0 on success
*/
void set_template_error_message ( const string & message ) ;
2011-06-02 01:53:09 +04:00
/**
* Factory method for templates , it should be implemented
* by classes that uses templates
* @ return a new template
*/
virtual Template * get_new_template ( )
{
return 0 ;
}
/**
2011-06-02 02:51:42 +04:00
* Replace template for this object . Object should be updated
* after calling this method
2011-06-02 01:53:09 +04:00
* @ param tmpl string representation of the template
*/
2011-06-02 02:20:42 +04:00
int replace_template ( const string & tmpl_str , string & error ) ;
2011-06-02 01:53:09 +04:00
2008-06-17 20:27:32 +04:00
protected :
2010-04-03 18:54:54 +04:00
2008-06-17 20:27:32 +04:00
/**
2011-02-24 20:12:26 +03:00
* Callback function to unmarshall a PoolObjectSQL
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2011-02-24 20:12:26 +03:00
int select_cb ( void * nil , int num , char * * values , char * * names )
{
if ( ( ! values [ 0 ] ) | | ( num ! = 1 ) )
{
return - 1 ;
}
2011-02-25 01:30:39 +03:00
return from_xml ( values [ 0 ] ) ;
2011-02-24 20:12:26 +03:00
} ;
2009-07-13 16:21:14 +04:00
/**
2011-02-24 20:12:26 +03:00
* Reads the PoolObjectSQL ( identified by its OID ) from the database .
* @ param db pointer to the db
* @ return 0 on success
2009-07-13 16:21:14 +04:00
*/
2011-03-09 14:44:39 +03:00
virtual int select ( SqlDB * db ) ;
2011-02-24 20:12:26 +03:00
2011-03-05 05:24:11 +03:00
/**
* Reads the PoolObjectSQL ( identified by its OID ) from the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2011-03-09 14:44:39 +03:00
virtual int select ( SqlDB * db , const string & _name , int _uid ) ;
2011-03-05 05:24:11 +03:00
2011-02-24 20:12:26 +03:00
/**
* Drops object from the database
* @ param db pointer to the db
* @ return 0 on success
*/
2011-03-09 14:44:39 +03:00
virtual int drop ( SqlDB * db ) ;
2011-02-24 20:12:26 +03:00
/**
* Function to output a pool object into a stream in XML format
* @ param oss the output stream
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
static int dump ( ostringstream & oss , int num , char * * values , char * * names )
{
if ( ( ! values [ 0 ] ) | | ( num ! = 1 ) )
{
return - 1 ;
}
oss < < values [ 0 ] ;
return 0 ;
} ;
/**
* The object ' s unique ID
2008-06-17 20:27:32 +04:00
*/
2011-03-05 00:37:21 +03:00
int oid ;
/**
* The object ' s name
*/
string name ;
/**
2011-03-05 05:24:11 +03:00
* Object ' s owner , set it to - 1 if owner is not used
2011-03-05 00:37:21 +03:00
*/
int uid ;
2009-07-13 16:21:14 +04:00
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
/**
* Object ' s group , set it to - 1 if group is not used
*/
int gid ;
2011-06-30 13:31:00 +04:00
/**
* Name of the object ' s owner , empty if owner is not used
*/
string uname ;
/**
* Name of the object ' s group , , empty if group is not used
*/
string gname ;
2009-07-13 16:21:14 +04:00
/**
2011-02-24 20:12:26 +03:00
* The contents of this object are valid
2009-07-13 16:21:14 +04:00
*/
2011-03-05 00:37:21 +03:00
bool valid ;
2008-06-17 20:27:32 +04:00
2011-05-26 14:20:56 +04:00
/**
* Set if the object is public
*/
int public_obj ;
2011-04-08 03:02:55 +04:00
/**
* Template for this object , will be allocated if needed
*/
Template * obj_template ;
2008-06-17 20:27:32 +04:00
private :
/**
* The PoolSQL , friend to easily manipulate its Objects
*/
friend class PoolSQL ;
/**
2010-04-03 18:54:54 +04:00
* The mutex for the PoolObject . This implementation assumes that the mutex
* IS LOCKED when the class destructor is called .
2008-06-17 20:27:32 +04:00
*/
pthread_mutex_t mutex ;
2011-02-25 01:30:39 +03:00
/**
* Pointer to the SQL table for the PoolObjectSQL
*/
const char * table ;
2011-04-15 03:51:38 +04:00
/**
* Name for the error messages attribute
*/
static const char * error_attribute_name ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*POOL_OBJECT_SQL_H_*/