2012-06-08 18:00:16 +04:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2012-06-08 18:00:16 +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 REQUEST_MANAGER_CLONE_H
# define REQUEST_MANAGER_CLONE_H
# include "Request.h"
2016-04-21 17:08:28 +03:00
# include "RequestManagerVMTemplate.h"
2012-06-08 18:00:16 +04:00
# include "Nebula.h"
2019-12-10 13:45:15 +03:00
# include "DocumentPool.h"
# include "SecurityGroupPool.h"
# include "VNTemplatePool.h"
2012-06-08 18:00:16 +04:00
using namespace std ;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class RequestManagerClone : public Request
{
protected :
2016-04-21 17:08:28 +03:00
RequestManagerClone ( const string & method_name , const string & help ,
const string & params = " A:sis " ) :
Request ( method_name , params , help ) { } ;
2012-06-08 18:00:16 +04:00
~ RequestManagerClone ( ) { } ;
/* -------------------------------------------------------------------- */
2016-04-21 17:08:28 +03:00
void request_execute ( xmlrpc_c : : paramList const & _paramList ,
2019-07-05 18:23:21 +03:00
RequestAttributes & att ) override ;
2016-04-11 13:25:02 +03:00
2016-04-21 17:08:28 +03:00
/* -------------------------------------------------------------------- */
/* Especialization Functions for specific Clone actions */
/* -------------------------------------------------------------------- */
/**
* Function to clone the object
*/
virtual ErrorCode clone ( int source_id , const string & name , int & new_id ,
bool recursive , const string & s_uattr , RequestAttributes & att ) ;
2012-06-08 18:00:16 +04:00
2016-04-21 17:08:28 +03:00
/**
* Function to clone the base template
*/
virtual Template * clone_template ( PoolObjectSQL * obj ) = 0 ;
2016-04-20 18:39:44 +03:00
2016-04-21 17:08:28 +03:00
/**
* Function to merge user / additional attributes in the cloned object
*/
virtual ErrorCode merge ( Template * tmpl , const string & str_uattrs ,
2016-04-20 18:39:44 +03:00
RequestAttributes & att )
{
return SUCCESS ;
}
2016-04-21 17:08:28 +03:00
/**
* Function to allocated the new clone object
*/
virtual int pool_allocate ( int source_id , Template * tmpl , int & id ,
RequestAttributes & att ) = 0 ;
2012-06-08 18:00:16 +04:00
} ;
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class VMTemplateClone : public RequestManagerClone
{
public :
2016-04-21 17:08:28 +03:00
VMTemplateClone ( ) :
2017-05-07 22:52:34 +03:00
RequestManagerClone ( " one.template.clone " ,
" Clone a virtual machine template " , " A:sisb " )
2012-06-08 18:00:16 +04:00
{
2016-04-21 17:08:28 +03:00
Nebula & nd = Nebula : : instance ( ) ;
pool = nd . get_tpool ( ) ;
2012-06-08 18:00:16 +04:00
2016-04-21 17:08:28 +03:00
auth_object = PoolObjectSQL : : TEMPLATE ;
auth_op = AuthRequest : : USE ;
2012-06-08 18:00:16 +04:00
} ;
2016-04-21 17:08:28 +03:00
~ VMTemplateClone ( ) { } ;
2012-06-08 18:00:16 +04:00
2016-04-21 17:08:28 +03:00
ErrorCode request_execute ( int source_id , const string & name , int & new_id ,
bool recursive , const string & s_uattrs , RequestAttributes & att )
{
return clone ( source_id , name , new_id , recursive , s_uattrs , att ) ;
} ;
protected :
2016-04-11 13:25:02 +03:00
2016-04-21 17:08:28 +03:00
ErrorCode clone ( int source_id , const string & name , int & new_id ,
2019-07-05 18:23:21 +03:00
bool recursive , const string & s_a , RequestAttributes & att ) override ;
2016-04-19 15:58:22 +03:00
2019-07-05 18:23:21 +03:00
Template * clone_template ( PoolObjectSQL * obj ) override
2012-06-08 18:00:16 +04:00
{
return static_cast < VMTemplate * > ( obj ) - > clone_template ( ) ;
} ;
2019-07-05 18:23:21 +03:00
int pool_allocate ( int sid , Template * tmpl , int & id , RequestAttributes & att ) override
2012-06-08 18:00:16 +04:00
{
2016-04-21 17:08:28 +03:00
VMTemplatePool * tpool = static_cast < VMTemplatePool * > ( pool ) ;
VirtualMachineTemplate * t = static_cast < VirtualMachineTemplate * > ( tmpl ) ;
2012-06-08 18:00:16 +04:00
2014-05-27 14:58:46 +04:00
return tpool - > allocate ( att . uid , att . gid , att . uname , att . gname , att . umask ,
2016-04-21 17:08:28 +03:00
t , & id , att . resp_msg ) ;
2012-06-08 18:00:16 +04:00
} ;
2016-04-19 15:58:22 +03:00
2019-07-05 18:23:21 +03:00
ErrorCode merge ( Template * tmpl , const string & s_a , RequestAttributes & att ) override
2019-09-09 14:13:52 +03:00
{
VMTemplateInstantiate vm_instantiate ;
2016-04-19 15:58:22 +03:00
2019-09-09 14:13:52 +03:00
return vm_instantiate . merge ( tmpl , s_a , att ) ;
} ;
2012-06-08 18:00:16 +04:00
} ;
2018-11-20 19:24:59 +03:00
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class VNTemplateClone : public RequestManagerClone
{
public :
VNTemplateClone ( ) :
RequestManagerClone ( " one.vntemplate.clone " ,
2019-01-28 21:36:43 +03:00
" Clone a virtual network template " , " A:sis " )
2018-11-20 19:24:59 +03:00
{
Nebula & nd = Nebula : : instance ( ) ;
pool = nd . get_vntpool ( ) ;
auth_object = PoolObjectSQL : : VNTEMPLATE ;
auth_op = AuthRequest : : USE ;
} ;
~ VNTemplateClone ( ) { } ;
ErrorCode request_execute ( int source_id , const string & name , int & new_id ,
2019-01-28 21:36:43 +03:00
const string & s_uattrs , RequestAttributes & att )
2018-11-20 19:24:59 +03:00
{
2019-01-28 21:36:43 +03:00
return clone ( source_id , name , new_id , false , s_uattrs , att ) ;
2018-11-20 19:24:59 +03:00
} ;
protected :
2019-07-05 18:23:21 +03:00
Template * clone_template ( PoolObjectSQL * obj ) override
2018-11-20 19:24:59 +03:00
{
return static_cast < VNTemplate * > ( obj ) - > clone_template ( ) ;
} ;
2019-07-05 18:23:21 +03:00
int pool_allocate ( int sid , Template * tmpl , int & id , RequestAttributes & att ) override
2018-11-20 19:24:59 +03:00
{
VNTemplatePool * tpool = static_cast < VNTemplatePool * > ( pool ) ;
VirtualNetworkTemplate * t = static_cast < VirtualNetworkTemplate * > ( tmpl ) ;
return tpool - > allocate ( att . uid , att . gid , att . uname , att . gname , att . umask ,
t , & id , att . resp_msg ) ;
} ;
} ;
2012-06-08 18:00:16 +04:00
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class DocumentClone : public RequestManagerClone
{
public :
DocumentClone ( ) :
2017-05-07 22:52:34 +03:00
RequestManagerClone ( " one.document.clone " , " Clone existing document " )
2012-06-08 18:00:16 +04:00
{
Nebula & nd = Nebula : : instance ( ) ;
pool = nd . get_docpool ( ) ;
auth_object = PoolObjectSQL : : DOCUMENT ;
auth_op = AuthRequest : : USE ;
} ;
~ DocumentClone ( ) { } ;
2016-04-21 17:08:28 +03:00
protected :
2012-06-08 18:00:16 +04:00
2019-07-05 18:23:21 +03:00
Template * clone_template ( PoolObjectSQL * obj ) override
2012-06-08 18:00:16 +04:00
{
return static_cast < Document * > ( obj ) - > clone_template ( ) ;
} ;
2019-07-05 18:23:21 +03:00
int pool_allocate ( int sid , Template * tmpl , int & id , RequestAttributes & att ) override
2012-06-08 18:00:16 +04:00
{
DocumentPool * docpool = static_cast < DocumentPool * > ( pool ) ;
2018-10-09 12:05:08 +03:00
Document * doc = docpool - > get_ro ( sid ) ;
2016-04-21 17:08:28 +03:00
if ( doc = = 0 )
{
return - 1 ;
}
int dtype = doc - > get_document_type ( ) ;
2016-01-21 19:59:52 +03:00
doc - > unlock ( ) ;
2012-06-08 18:00:16 +04:00
2014-05-27 14:58:46 +04:00
return docpool - > allocate ( att . uid , att . gid , att . uname , att . gname ,
2016-04-21 17:08:28 +03:00
att . umask , dtype , tmpl , & id , att . resp_msg ) ;
2012-06-08 18:00:16 +04:00
} ;
} ;
2014-09-08 13:50:25 +04:00
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class SecurityGroupClone : public RequestManagerClone
{
public :
SecurityGroupClone ( ) :
2017-05-07 22:52:34 +03:00
RequestManagerClone ( " one.secgroup.clone " , " Clone a security group " )
2014-09-08 13:50:25 +04:00
{
Nebula & nd = Nebula : : instance ( ) ;
pool = nd . get_secgrouppool ( ) ;
auth_object = PoolObjectSQL : : SECGROUP ;
auth_op = AuthRequest : : USE ;
} ;
~ SecurityGroupClone ( ) { } ;
2016-04-21 17:08:28 +03:00
protected :
2014-09-08 13:50:25 +04:00
2019-07-05 18:23:21 +03:00
Template * clone_template ( PoolObjectSQL * obj ) override
2014-09-08 13:50:25 +04:00
{
return static_cast < SecurityGroup * > ( obj ) - > clone_template ( ) ;
} ;
2019-07-05 18:23:21 +03:00
int pool_allocate ( int sid , Template * tmpl , int & id , RequestAttributes & att ) override
2014-09-08 13:50:25 +04:00
{
2016-04-21 17:08:28 +03:00
SecurityGroupPool * sg = static_cast < SecurityGroupPool * > ( pool ) ;
2014-09-08 13:50:25 +04:00
2016-04-21 17:08:28 +03:00
return sg - > allocate ( att . uid , att . gid , att . uname , att . gname , att . umask ,
tmpl , & id , att . resp_msg ) ;
2014-09-08 13:50:25 +04:00
} ;
} ;
2016-04-21 17:08:28 +03:00
2012-06-08 18:00:16 +04:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
# endif