diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 220e66b51a..31428d4577 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -140,6 +140,11 @@ public: return name; }; + void set_name(const string& _name) + { + name = _name; + }; + int get_uid() const { return uid; diff --git a/include/RequestManagerRename.h b/include/RequestManagerRename.h new file mode 100644 index 0000000000..f459c8b91f --- /dev/null +++ b/include/RequestManagerRename.h @@ -0,0 +1,180 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* 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_RENAME_H_ +#define REQUEST_MANAGER_RENAME_H_ + +#include "Request.h" +#include "Nebula.h" + +using namespace std; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class RequestManagerRename : public Request +{ +protected: + RequestManagerRename(const string& method_name, + const string& help, + const string& params = "A:sis") + :Request(method_name,params,help) + { + auth_op = AuthRequest::MANAGE; + }; + + ~RequestManagerRename(){}; + + /* -------------------------------------------------------------------- */ + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + + PoolObjectSQL * get_and_quota(int oid, + int new_uid, + int new_gid, + RequestAttributes& att); + + virtual PoolObjectSQL * get(const string& name, int uid, bool lock) = 0; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VirtualMachineRename : public RequestManagerRename +{ +public: + VirtualMachineRename(): + RequestManagerRename("VirtualMachineRename", + "Renames a virtual machine") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmpool(); + auth_object = PoolObjectSQL::VM; + }; + + ~VirtualMachineRename(){}; + + int check_name_unique(int oid, int noid, RequestAttributes& att) + { + return 0; + }; + + PoolObjectSQL * get(const string& name, int uid, bool lock) + { + return 0; + }; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class TemplateRename : public RequestManagerRename +{ +public: + TemplateRename(): + RequestManagerRename("TemplateRename", + "Renames a virtual machine template") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_tpool(); + auth_object = PoolObjectSQL::TEMPLATE; + }; + + ~TemplateRename(){}; + + PoolObjectSQL * get(const string& name, int uid, bool lock) + { + return static_cast(pool)->get(name, uid, lock); + }; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + + +class VirtualNetworkRename: public RequestManagerRename +{ +public: + VirtualNetworkRename(): + RequestManagerRename("VirtualNetworkRename", + "Renames a virtual network") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vnpool(); + auth_object = PoolObjectSQL::NET; + }; + + ~VirtualNetworkRename(){}; + + PoolObjectSQL * get(const string& name, int uid, bool lock) + { + return static_cast(pool)->get(name, uid, lock); + }; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class ImageRename: public RequestManagerRename +{ +public: + ImageRename(): + RequestManagerRename("ImageRename", + "Renames an image") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_ipool(); + auth_object = PoolObjectSQL::IMAGE; + }; + + ~ImageRename(){}; + + PoolObjectSQL * get(const string& name, int uid, bool lock) + { + return static_cast(pool)->get(name, uid, lock); + }; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class DocumentRename : public RequestManagerRename +{ +public: + DocumentRename(): + RequestManagerRename("DocumentRename", + "Renames a generic document") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_docpool(); + auth_object = PoolObjectSQL::DOCUMENT; + }; + + ~DocumentRename(){}; + + PoolObjectSQL * get(const string& name, int uid, bool lock) + { + return 0; + }; +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +#endif diff --git a/src/oca/ruby/OpenNebula/Template.rb b/src/oca/ruby/OpenNebula/Template.rb index aa209d3f3d..c8a8cb886d 100644 --- a/src/oca/ruby/OpenNebula/Template.rb +++ b/src/oca/ruby/OpenNebula/Template.rb @@ -32,7 +32,8 @@ module OpenNebula :delete => "template.delete", :chown => "template.chown", :chmod => "template.chmod", - :clone => "template.clone" + :clone => "template.clone", + :rename => "template.rename" } # Creates a Template description with just its identifier @@ -157,6 +158,16 @@ module OpenNebula return rc end + # Renames this Template + # + # @param name [String] New name for the new Template. + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def rename(name) + return call(TEMPLATE_METHODS[:rename], @pe_id, name) + end + ####################################################################### # Helpers to get Template information ####################################################################### diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 1ba7f4b8bf..004c3e2d82 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -26,6 +26,7 @@ #include "RequestManagerChown.h" #include "RequestManagerChmod.h" #include "RequestManagerClone.h" +#include "RequestManagerRename.h" #include "RequestManagerVirtualNetwork.h" #include "RequestManagerVirtualMachine.h" @@ -374,6 +375,13 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr group_get_default_quota(new GroupQuotaInfo()); xmlrpc_c::methodPtr group_set_default_quota(new GroupQuotaUpdate()); + // Rename Methods + xmlrpc_c::methodPtr vm_rename(new VirtualMachineRename()); + xmlrpc_c::methodPtr template_rename(new TemplateRename()); + xmlrpc_c::methodPtr vn_rename(new VirtualNetworkRename()); + xmlrpc_c::methodPtr image_rename(new ImageRename()); + xmlrpc_c::methodPtr doc_rename(new DocumentRename()); + /* VM related methods */ RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy); RequestManagerRegistry.addMethod("one.vm.action", vm_action); @@ -386,6 +394,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.vm.monitoring", vm_monitoring); RequestManagerRegistry.addMethod("one.vm.attach", vm_attach); RequestManagerRegistry.addMethod("one.vm.detach", vm_detach); + RequestManagerRegistry.addMethod("one.vm.rename", vm_rename); RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info); RequestManagerRegistry.addMethod("one.vmpool.accounting", vm_pool_acct); @@ -400,6 +409,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.template.chown", template_chown); RequestManagerRegistry.addMethod("one.template.chmod", template_chmod); RequestManagerRegistry.addMethod("one.template.clone", template_clone); + RequestManagerRegistry.addMethod("one.template.rename", template_rename); RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info); @@ -436,6 +446,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.vn.info", vn_info); RequestManagerRegistry.addMethod("one.vn.chown", vn_chown); RequestManagerRegistry.addMethod("one.vn.chmod", vn_chmod); + RequestManagerRegistry.addMethod("one.vn.rename", vn_rename); RequestManagerRegistry.addMethod("one.vnpool.info", vnpool_info); @@ -465,6 +476,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.image.chmod", image_chmod); RequestManagerRegistry.addMethod("one.image.chtype", image_chtype); RequestManagerRegistry.addMethod("one.image.clone", image_clone); + RequestManagerRegistry.addMethod("one.image.rename", image_rename); RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info); @@ -506,6 +518,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.document.chown", doc_chown); RequestManagerRegistry.addMethod("one.document.chmod", doc_chmod); RequestManagerRegistry.addMethod("one.document.clone", doc_clone); + RequestManagerRegistry.addMethod("one.document.rename", doc_rename); RequestManagerRegistry.addMethod("one.documentpool.info",docpool_info); diff --git a/src/rm/RequestManagerRename.cc b/src/rm/RequestManagerRename.cc new file mode 100644 index 0000000000..e681f96fe3 --- /dev/null +++ b/src/rm/RequestManagerRename.cc @@ -0,0 +1,111 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManagerRename.h" +#include "PoolObjectSQL.h" + +#include "NebulaLog.h" +#include "Nebula.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManagerRename::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int oid = xmlrpc_c::value_int(paramList.getInt(1)); + string new_name = xmlrpc_c::value_string(paramList.getString(2)); + + int rc; + + string old_name; + + PoolObjectAuth operms; + + PoolObjectSQL * object; + + string obj_name; + + rc = get_info(pool, oid, auth_object, att, operms, old_name); + + if ( rc == -1 ) + { + return; + } + + // ------------- Set authorization request for non-oneadmin's -------------- + + if ( att.uid != 0 ) + { + AuthRequest ar(att.uid, att.gid); + + ar.add_auth(auth_op, operms); // MANAGE OBJECT + + if (UserPool::authorize(ar) == -1) + { + failure_response(AUTHORIZATION, + authorization_error(ar.message, att), + att); + + return; + } + } + + // --------------- Check name uniqueness ----------------------------------- + + object = get(new_name, operms.uid, true); + + if ( object != 0 ) + { + ostringstream oss; + + int duplicate_obj_oid = object->get_oid(); + object->unlock(); + + oss << PoolObjectSQL::type_to_str(auth_object) << " [" + << oid << "] cannot be renamed to " << new_name + << " because it collides with " + << PoolObjectSQL::type_to_str(auth_object) << " [" + << duplicate_obj_oid << "]"; + + failure_response(INTERNAL, request_error(oss.str(), ""), att); + return; + } + + // --------------- Update the object --------------------------------------- + + object = pool->get(oid,true); + + if ( object == 0 ) + { + failure_response(NO_EXISTS, + get_error(object_name(auth_object), oid), + att); + } + + object->set_name(new_name); + + pool->update(object); + + object->unlock(); + + pool->update_cache_index(old_name, operms.uid, new_name, operms.uid); + + success_response(oid, att); + + return; +} + diff --git a/src/rm/SConstruct b/src/rm/SConstruct index 4420593001..a4e9bea234 100644 --- a/src/rm/SConstruct +++ b/src/rm/SConstruct @@ -41,7 +41,8 @@ source_files=[ 'RequestManagerChmod.cc', 'RequestManagerCluster.cc', 'RequestManagerClone.cc', - 'RequestManagerSystem.cc' + 'RequestManagerSystem.cc', + 'RequestManagerRename.cc' ] # Build library