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