2011-03-30 21:03:49 +04:00
/* -------------------------------------------------------------------------- */
2015-09-23 16:03:22 +03:00
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */
2011-03-30 21:03:49 +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 VMTEMPLATE_H_
# define VMTEMPLATE_H_
# include "PoolObjectSQL.h"
# include "VirtualMachineTemplate.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* The VMTemplate class .
*/
class VMTemplate : public PoolObjectSQL
{
public :
/**
* Function to print the VMTemplate object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
string & to_xml ( string & xml ) const ;
2015-07-21 17:24:43 +03:00
/**
* Function to print the VMTemplate object into a string in XML format
* @ param xml the resulting XML string
* @ param tmpl a template to replace the internal obj_template . It is only
* used to create the resulting xml string , the internal obj_template is
* not altered
* @ return a reference to the generated string
*/
string & to_xml ( string & xml , const Template * tmpl ) const ;
2011-03-30 21:03:49 +04:00
// ------------------------------------------------------------------------
// Template Contents
// ------------------------------------------------------------------------
2011-06-02 01:53:09 +04:00
/**
* Factory method for virtual machine templates
*/
2012-03-14 18:48:06 +04:00
Template * get_new_template ( ) const
2011-06-02 01:53:09 +04:00
{
return new VirtualMachineTemplate ;
}
2011-04-05 17:58:18 +04:00
/**
* Returns a copy of the VirtualMachineTemplate
* @ return A copy of the VirtualMachineTemplate
*/
2011-04-11 01:55:49 +04:00
VirtualMachineTemplate * clone_template ( ) const
2011-04-01 19:17:26 +04:00
{
2011-04-11 01:55:49 +04:00
return new VirtualMachineTemplate (
* ( static_cast < VirtualMachineTemplate * > ( obj_template ) ) ) ;
2011-04-05 17:58:18 +04:00
} ;
2011-04-01 19:17:26 +04:00
2016-02-03 20:29:39 +03:00
// ------------------------------------------------------------------------
// Virtual Router
// ------------------------------------------------------------------------
/**
* Returns true if this Template is a Virtual Router Template
* @ return true if this Template is a Virtual Router Template
*/
bool is_vrouter ( ) ;
2011-03-30 21:03:49 +04:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class VMTemplatePool ;
// -------------------------------------------------------------------------
// VMTemplate Attributes
// -------------------------------------------------------------------------
2011-04-01 19:17:26 +04:00
/**
* Registration time
*/
time_t regtime ;
2011-03-30 21:03:49 +04:00
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
/**
* Execute an INSERT or REPLACE Sql query .
* @ param db The SQL DB
* @ param replace Execute an INSERT or a REPLACE
2011-12-19 20:07:32 +04:00
* @ param error_str Returns the error reason , if any
2011-03-30 21:03:49 +04:00
* @ return 0 one success
*/
2011-12-19 20:07:32 +04:00
int insert_replace ( SqlDB * db , bool replace , string & error_str ) ;
2011-03-30 21:03:49 +04:00
/**
* Bootstraps the database table ( s ) associated to the VMTemplate
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2011-03-30 21:03:49 +04:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * db )
2011-03-30 21:03:49 +04:00
{
ostringstream oss ( VMTemplate : : db_bootstrap ) ;
2011-10-10 17:14:46 +04:00
return db - > exec ( oss ) ;
2011-03-30 21:03:49 +04:00
} ;
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
int from_xml ( const string & xml_str ) ;
protected :
// *************************************************************************
// Constructor
// *************************************************************************
2015-07-01 22:15:40 +03:00
VMTemplate ( int id ,
int uid ,
2011-06-30 13:31:00 +04:00
int gid ,
const string & uname ,
const string & gname ,
2013-01-18 21:34:51 +04:00
int umask ,
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
VirtualMachineTemplate * _template_contents ) ;
2011-03-30 21:03:49 +04:00
~ VMTemplate ( ) ;
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names ;
static const char * db_bootstrap ;
static const char * table ;
/**
* Writes the VMTemplate in the database .
* @ param db pointer to the db
2011-12-19 20:07:32 +04:00
* @ param error_str Returns the error reason , if any
2011-03-30 21:03:49 +04:00
* @ return 0 on success
*/
int insert ( SqlDB * db , string & error_str ) ;
/**
* Writes / updates the VMTemplate data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
int update ( SqlDB * db )
{
2011-12-19 20:07:32 +04:00
string err ;
return insert_replace ( db , true , err ) ;
2011-03-30 21:03:49 +04:00
} ;
} ;
# endif /*VMTEMPLATE_H_*/