1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00

Feature #872: one.template.clone method in core, ruby OCA & CLI

This commit is contained in:
Carlos Martín 2012-04-13 18:39:06 +02:00
parent daf28b963a
commit eddf3353f6
5 changed files with 128 additions and 3 deletions

View File

@ -68,6 +68,26 @@ public:
RequestAttributes& att);
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class VMTemplateClone : public RequestManagerVMTemplate
{
public:
VMTemplateClone():
RequestManagerVMTemplate("VMTemplateClone",
"Clone an existing virtual machine template",
"A:sis")
{
auth_op = AuthRequest::USE;
};
~VMTemplateClone(){};
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -88,6 +88,20 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
clone_desc = <<-EOT.unindent
Creates a new Template from an existing one
EOT
command :clone, clone_desc, :templateid, :name do
helper.perform_action(args[0],options,"cloned") do |t|
res = t.clone(args[1])
if !OpenNebula.is_error?(res)
puts "ID: #{res}"
end
end
end
delete_desc = <<-EOT.unindent
Deletes the given Image
EOT

View File

@ -32,7 +32,8 @@ module OpenNebula
:publish => "template.publish",
:delete => "template.delete",
:chown => "template.chown",
:chmod => "template.chmod"
:chmod => "template.chmod",
:clone => "template.clone"
}
# Creates a Template description with just its identifier
@ -143,6 +144,20 @@ module OpenNebula
group_m, group_a, other_u, other_m, other_a)
end
# Clones this template into a new one
#
# @param name [String] Name for the new Template.
#
# @return [Integer, OpenNebula::Error] The new Template ID in case
# of success, Error otherwise
def clone(name)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(TEMPLATE_METHODS[:clone], @pe_id, name)
return rc
end
#######################################################################
# Helpers to get Template information
#######################################################################

View File

@ -236,6 +236,7 @@ void RequestManager::register_xml_methods()
// VMTemplate Methods
xmlrpc_c::methodPtr template_instantiate(new VMTemplateInstantiate());
xmlrpc_c::methodPtr template_clone(new VMTemplateClone());
// VirtualMachine Methods
xmlrpc_c::methodPtr vm_deploy(new VirtualMachineDeploy());
@ -356,6 +357,7 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.template.info", template_info);
RequestManagerRegistry.addMethod("one.template.chown", template_chown);
RequestManagerRegistry.addMethod("one.template.chmod", template_chmod);
RequestManagerRegistry.addMethod("one.template.clone", template_clone);
RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info);

View File

@ -61,7 +61,7 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
// Check template for restricted attributes, but only if the Template owner
// is not oneadmin
if ( perms.uid != 0 && perms.gid != GroupPool::ONEADMIN_ID )
if ( perms.uid != UserPool::ONEADMIN_ID && perms.gid != GroupPool::ONEADMIN_ID )
{
if (tmpl->check(aname))
{
@ -84,7 +84,6 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
if ( att.uid != 0 )
{
AuthRequest ar(att.uid, att.gid);
string tmpl_txt;
ar.add_auth(auth_op, perms); //USE TEMPLATE
@ -119,3 +118,78 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VMTemplateClone::request_execute(xmlrpc_c::paramList const& paramList,
RequestAttributes& att)
{
int source_id = xmlrpc_c::value_int(paramList.getInt(1));
string name = xmlrpc_c::value_string(paramList.getString(2));
int rc, new_id;
PoolObjectAuth perms;
VMTemplatePool * tpool = static_cast<VMTemplatePool *>(pool);
VirtualMachineTemplate * tmpl;
VMTemplate * source_tmpl;
string error_str;
source_tmpl = tpool->get(source_id,true);
if ( source_tmpl == 0 )
{
failure_response(NO_EXISTS,
get_error(object_name(auth_object),source_id),
att);
return;
}
tmpl = source_tmpl->clone_template();
source_tmpl->get_permissions(perms);
source_tmpl->unlock();
tmpl->erase("NAME");
tmpl->set(new SingleAttribute("NAME",name));
if ( att.uid != 0 )
{
string tmpl_str = "";
AuthRequest ar(att.uid, att.gid);
ar.add_auth(auth_op, perms); //USE TEMPLATE
tmpl->to_xml(tmpl_str);
ar.add_create_auth(auth_object, tmpl_str);
if (UserPool::authorize(ar) == -1)
{
failure_response(AUTHORIZATION,
authorization_error(ar.message, att),
att);
delete tmpl;
return;
}
}
rc = tpool->allocate(att.uid, att.gid, att.uname, att.gname,
tmpl, &new_id, error_str);
if ( rc < 0 )
{
failure_response(INTERNAL, allocate_error(error_str), att);
return;
}
success_response(new_id, att);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */