mirror of
https://github.com/OpenNebula/one.git
synced 2025-08-24 17:49:28 +03:00
Feature #4400: Instantiate-persistent merges the user provided template
This commit is contained in:
@ -45,6 +45,7 @@ protected:
|
||||
ErrorCode clone(
|
||||
int source_id,
|
||||
const string &name,
|
||||
const string &str_uattrs,
|
||||
int &new_id,
|
||||
RequestAttributes& att);
|
||||
|
||||
@ -55,6 +56,14 @@ protected:
|
||||
Template * tmpl,
|
||||
int& id,
|
||||
RequestAttributes& att) = 0;
|
||||
|
||||
virtual ErrorCode merge(
|
||||
Template * tmpl,
|
||||
const string &str_uattrs,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
return SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
@ -77,11 +86,12 @@ public:
|
||||
xmlrpc_c::paramList const& paramList, RequestAttributes& att);
|
||||
|
||||
ErrorCode request_execute(
|
||||
int source_id,
|
||||
string name,
|
||||
bool recursive,
|
||||
int &new_id,
|
||||
RequestAttributes &att);
|
||||
int source_id,
|
||||
string name,
|
||||
bool recursive,
|
||||
const string &str_uattrs,
|
||||
int &new_id,
|
||||
RequestAttributes &att);
|
||||
|
||||
Template * clone_template(PoolObjectSQL* obj)
|
||||
{
|
||||
@ -103,6 +113,11 @@ public:
|
||||
ttmpl, &id, att.resp_msg);
|
||||
};
|
||||
|
||||
ErrorCode merge(
|
||||
Template * tmpl,
|
||||
const string &str_uattrs,
|
||||
RequestAttributes& att);
|
||||
|
||||
private:
|
||||
VMTemplateClone():
|
||||
RequestManagerClone("VMTemplateClone",
|
||||
|
@ -83,7 +83,13 @@ public:
|
||||
* @return ErroCode for the request.
|
||||
*/
|
||||
static ErrorCode instantiate(int id, string name, bool on_hold,
|
||||
string str_uattrs, Template* extra_attrs, int& vid, RequestAttributes& att);
|
||||
const string &str_uattrs, Template* extra_attrs, int& vid,
|
||||
RequestAttributes& att);
|
||||
|
||||
static ErrorCode merge(
|
||||
Template * tmpl,
|
||||
const string &str_uattrs,
|
||||
RequestAttributes& att);
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "RequestManagerClone.h"
|
||||
#include "RequestManagerImage.h"
|
||||
#include "RequestManagerDelete.h"
|
||||
#include "RequestManagerVMTemplate.h"
|
||||
#include "PoolObjectAuth.h"
|
||||
#include "Nebula.h"
|
||||
|
||||
@ -32,7 +33,7 @@ void RequestManagerClone::request_execute(
|
||||
|
||||
int new_id;
|
||||
|
||||
ErrorCode ec = clone(source_id, name, new_id, att);
|
||||
ErrorCode ec = clone(source_id, name, "", new_id, att);
|
||||
|
||||
if ( ec == SUCCESS )
|
||||
{
|
||||
@ -50,6 +51,7 @@ void RequestManagerClone::request_execute(
|
||||
Request::ErrorCode RequestManagerClone::clone(
|
||||
int source_id,
|
||||
const string &name,
|
||||
const string &str_uattrs,
|
||||
int &new_id,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
@ -74,6 +76,14 @@ Request::ErrorCode RequestManagerClone::clone(
|
||||
|
||||
source_obj->unlock();
|
||||
|
||||
ErrorCode ec = merge(tmpl, str_uattrs, att);
|
||||
|
||||
if (ec != SUCCESS)
|
||||
{
|
||||
delete tmpl;
|
||||
return ec;
|
||||
}
|
||||
|
||||
tmpl->erase("NAME");
|
||||
tmpl->set(new SingleAttribute("NAME",name));
|
||||
|
||||
@ -126,7 +136,7 @@ void VMTemplateClone::request_execute(
|
||||
|
||||
int new_id;
|
||||
|
||||
ErrorCode ec = request_execute(source_id, name, recursive, new_id, att);
|
||||
ErrorCode ec = request_execute(source_id, name, recursive, "", new_id, att);
|
||||
|
||||
if ( ec == SUCCESS )
|
||||
{
|
||||
@ -142,11 +152,12 @@ void VMTemplateClone::request_execute(
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
Request::ErrorCode VMTemplateClone::request_execute(
|
||||
int source_id,
|
||||
string name,
|
||||
bool recursive,
|
||||
int &new_id,
|
||||
RequestAttributes &att)
|
||||
int source_id,
|
||||
string name,
|
||||
bool recursive,
|
||||
const string &str_uattrs,
|
||||
int &new_id,
|
||||
RequestAttributes &att)
|
||||
{
|
||||
VMTemplate * vmtmpl;
|
||||
VMTemplatePool* tpool = static_cast<VMTemplatePool*>(pool);
|
||||
@ -160,7 +171,7 @@ Request::ErrorCode VMTemplateClone::request_execute(
|
||||
RequestAttributes img_att(att);
|
||||
img_att.resp_obj = PoolObjectSQL::IMAGE;
|
||||
|
||||
ec = clone(source_id, name, new_id, att);
|
||||
ec = clone(source_id, name, str_uattrs, new_id, att);
|
||||
|
||||
if ( ec != SUCCESS )
|
||||
{
|
||||
@ -293,3 +304,13 @@ error_template:
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
Request::ErrorCode VMTemplateClone::merge(
|
||||
Template * tmpl,
|
||||
const string &str_uattrs,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
return VMTemplateInstantiate::merge(tmpl, str_uattrs, att);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -54,6 +54,8 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
|
||||
|
||||
bool is_vrouter = tmpl->is_vrouter();
|
||||
|
||||
string original_tmpl_name = tmpl->get_name();
|
||||
|
||||
tmpl->unlock();
|
||||
|
||||
if (is_vrouter)
|
||||
@ -69,7 +71,15 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
|
||||
{
|
||||
int new_id;
|
||||
|
||||
ErrorCode ec = VMTemplateClone::instance().request_execute(id, name, true, new_id, att);
|
||||
string tmpl_name = name;
|
||||
|
||||
if (tmpl_name.empty())
|
||||
{
|
||||
tmpl_name = original_tmpl_name + "-copy";
|
||||
}
|
||||
|
||||
ErrorCode ec = VMTemplateClone::instance().request_execute(
|
||||
id, tmpl_name, true, str_uattrs, new_id, att);
|
||||
|
||||
if (ec != SUCCESS)
|
||||
{
|
||||
@ -78,6 +88,7 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
|
||||
}
|
||||
|
||||
instantiate_id = new_id;
|
||||
str_uattrs = "";
|
||||
}
|
||||
|
||||
int vid;
|
||||
@ -99,7 +110,7 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
Request::ErrorCode VMTemplateInstantiate::instantiate(int id, string name,
|
||||
bool on_hold, string str_uattrs, Template* extra_attrs, int& vid,
|
||||
bool on_hold, const string &str_uattrs, Template* extra_attrs, int& vid,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
int rc;
|
||||
@ -139,46 +150,12 @@ Request::ErrorCode VMTemplateInstantiate::instantiate(int id, string name,
|
||||
|
||||
rtmpl->unlock();
|
||||
|
||||
// Parse & merge user attributes (check if the request user is not oneadmin)
|
||||
if (!str_uattrs.empty())
|
||||
ErrorCode ec = merge(tmpl, str_uattrs, att);
|
||||
|
||||
if (ec != SUCCESS)
|
||||
{
|
||||
rc = uattrs.parse_str_or_xml(str_uattrs, att.resp_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
delete tmpl;
|
||||
return INTERNAL;
|
||||
}
|
||||
|
||||
if (att.uid!=UserPool::ONEADMIN_ID && att.gid!=GroupPool::ONEADMIN_ID)
|
||||
{
|
||||
if (uattrs.check(aname))
|
||||
{
|
||||
att.resp_msg ="User Template includes a restricted attribute " + aname;
|
||||
|
||||
delete tmpl;
|
||||
return AUTHORIZATION;
|
||||
}
|
||||
}
|
||||
|
||||
rc = tmpl->merge(&uattrs, att.resp_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
delete tmpl;
|
||||
return INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (extra_attrs != 0)
|
||||
{
|
||||
rc = tmpl->merge(extra_attrs, att.resp_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
delete tmpl;
|
||||
return INTERNAL;
|
||||
}
|
||||
delete tmpl;
|
||||
return ec;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -262,3 +239,43 @@ Request::ErrorCode VMTemplateInstantiate::instantiate(int id, string name,
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
Request::ErrorCode VMTemplateInstantiate::merge(
|
||||
Template * tmpl,
|
||||
const string &str_uattrs,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
// Parse & merge user attributes (check if the request user is not oneadmin)
|
||||
if (!str_uattrs.empty())
|
||||
{
|
||||
int rc;
|
||||
|
||||
VirtualMachineTemplate uattrs;
|
||||
string aname;
|
||||
|
||||
rc = uattrs.parse_str_or_xml(str_uattrs, att.resp_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return INTERNAL;
|
||||
}
|
||||
|
||||
if (att.uid!=UserPool::ONEADMIN_ID && att.gid!=GroupPool::ONEADMIN_ID)
|
||||
{
|
||||
if (uattrs.check(aname))
|
||||
{
|
||||
att.resp_msg ="User Template includes a restricted attribute " + aname;
|
||||
|
||||
return AUTHORIZATION;
|
||||
}
|
||||
}
|
||||
|
||||
rc = tmpl->merge(&uattrs, att.resp_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user