2011-03-30 21:03:49 +04:00
/* -------------------------------------------------------------------------- */
2024-07-29 15:25:20 +03:00
/* Copyright 2002-2024, 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 :
2020-09-10 10:08:29 +03:00
virtual ~ VMTemplate ( ) = default ;
2011-03-30 21:03:49 +04:00
/**
* 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
*/
2020-07-02 23:42:10 +03:00
std : : string & to_xml ( std : : string & xml ) const override ;
2011-03-30 21:03:49 +04:00
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
*/
2020-07-02 23:42:10 +03:00
std : : string & to_xml ( std : : string & xml , const Template * tmpl ) const ;
2015-07-21 17:24:43 +03:00
2011-03-30 21:03:49 +04:00
// ------------------------------------------------------------------------
// Template Contents
// ------------------------------------------------------------------------
2011-06-02 01:53:09 +04:00
/**
* Factory method for virtual machine templates
*/
2020-09-15 12:16:00 +03:00
std : : unique_ptr < Template > get_new_template ( ) const override
2011-06-02 01:53:09 +04:00
{
2020-09-15 12:16:00 +03:00
return std : : make_unique < VirtualMachineTemplate > ( ) ;
2011-06-02 01:53:09 +04:00
}
2011-04-05 17:58:18 +04:00
/**
* Returns a copy of the VirtualMachineTemplate
* @ return A copy of the VirtualMachineTemplate
*/
2020-09-15 12:16:00 +03:00
std : : unique_ptr < VirtualMachineTemplate > clone_template ( ) const
2011-04-01 19:17:26 +04:00
{
2020-09-15 12:16:00 +03:00
return std : : make_unique < VirtualMachineTemplate > ( * obj_template ) ;
}
2011-04-01 19:17:26 +04:00
2016-02-08 18:40:38 +03:00
/**
2016-03-01 14:09:15 +03:00
* Returns a copy of the DISK attributes of this template , the attributes
2020-05-18 03:23:29 +03:00
* are copied and must be freed by the calling function .
* @ param a vector to store the disks .
2016-02-08 18:40:38 +03:00
*/
2020-07-02 23:42:10 +03:00
void clone_disks ( std : : vector < VectorAttribute * > & disks )
2016-02-26 17:56:09 +03:00
{
2020-07-02 23:42:10 +03:00
std : : vector < const VectorAttribute * > _disks ;
2016-03-01 14:09:15 +03:00
2020-05-18 03:23:29 +03:00
obj_template - > get ( " DISK " , _disks ) ;
2016-03-01 14:09:15 +03:00
2020-07-02 23:42:10 +03:00
for ( auto disk : _disks )
2020-05-18 03:23:29 +03:00
{
2020-07-02 23:42:10 +03:00
disks . push_back ( new VectorAttribute ( disk ) ) ;
2020-05-18 03:23:29 +03:00
}
2016-02-26 17:56:09 +03:00
}
2016-02-08 18:40:38 +03:00
2016-04-21 17:08:28 +03:00
/**
* Replaces the current DISK attributes with the given ones . The objects
* must NOT be deleted by the calling function , and should be obtained
* through a clone_disks ( ) call .
* @ param disks a vector with the new disks
*
*/
2020-07-02 23:42:10 +03:00
void replace_disks ( std : : vector < VectorAttribute * > & disks )
2016-04-11 13:25:02 +03:00
{
obj_template - > erase ( " DISK " ) ;
2016-04-21 17:08:28 +03:00
obj_template - > set ( disks ) ;
2016-04-11 13:25:02 +03: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
*/
2020-07-02 23:42:10 +03:00
int insert_replace ( SqlDB * db , bool replace , std : : string & error_str ) ;
2011-03-30 21:03:49 +04:00
2017-07-18 12:04:27 +03:00
/**
* Execute this method after update the template .
* @ param error Returns the error reason , if any
* @ return 0 one success
*/
2020-07-02 23:42:10 +03:00
int post_update_template ( std : : string & error ) override ;
2017-07-18 12:04:27 +03:00
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
*/
2020-06-29 13:14:00 +03:00
static int bootstrap ( SqlDB * db ) ;
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
*/
2020-07-02 23:42:10 +03:00
int from_xml ( const std : : string & xml_str ) override ;
2011-03-30 21:03:49 +04:00
2017-07-19 12:27:57 +03:00
/**
* This method removes sched_action DONE / MESSAGE attributes
*/
2020-07-02 23:42:10 +03:00
int parse_sched_action ( std : : string & error_str ) ;
2017-07-19 12:27:57 +03:00
2011-03-30 21:03:49 +04:00
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 ,
2020-07-02 23:42:10 +03:00
const std : : string & uname ,
const std : : string & gname ,
2013-01-18 21:34:51 +04:00
int umask ,
2020-09-15 12:16:00 +03:00
std : : unique_ptr < Template > _template_contents ) ;
2011-03-30 21:03:49 +04:00
// *************************************************************************
// DataBase implementation
// *************************************************************************
/**
* 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
*/
2020-07-02 23:42:10 +03:00
int insert ( SqlDB * db , std : : string & error_str ) override ;
2011-03-30 21:03:49 +04:00
/**
* Writes / updates the VMTemplate data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int update ( SqlDB * db ) override
2011-03-30 21:03:49 +04:00
{
2020-07-02 23:42:10 +03:00
std : : string err ;
2011-12-19 20:07:32 +04:00
return insert_replace ( db , true , err ) ;
2011-03-30 21:03:49 +04:00
} ;
} ;
# endif /*VMTEMPLATE_H_*/