diff --git a/include/Request.h b/include/Request.h index 1078fd8898..9c479d97b6 100644 --- a/include/Request.h +++ b/include/Request.h @@ -219,6 +219,26 @@ protected: * @return string for logging */ string allocate_error (PoolObjectSQL::ObjectType obj, const string& error); + + /** + * Locks the requested object, gets information, and unlocks it + * + * @param pool object pool + * @param id of the object + * @param type of the object + * @param att the specific request attributes + * + * @param perms returns the object's permissions + * @param name returns the object's name + * + * @return 0 on success, -1 otherwise + */ + int get_info (PoolSQL * pool, + int id, + PoolObjectSQL::ObjectType type, + RequestAttributes& att, + PoolObjectAuth& perms, + string& name); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index 1249407a73..d22e0d52be 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -207,7 +207,7 @@ public: HostAllocate(): RequestManagerAllocate("HostAllocate", "Allocates a new host", - "A:sssss", + "A:sssssi", false) { Nebula& nd = Nebula::instance(); @@ -217,11 +217,10 @@ public: ~HostAllocate(){}; - int pool_allocate(xmlrpc_c::paramList const& _paramList, - Template * tmpl, - int& id, - string& error_str, - RequestAttributes& att); + /* --------------------------------------------------------------------- */ + + void request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerChown.h b/include/RequestManagerChown.h index 78569079e0..c0591bb36c 100644 --- a/include/RequestManagerChown.h +++ b/include/RequestManagerChown.h @@ -52,13 +52,6 @@ protected: virtual void request_execute(xmlrpc_c::paramList const& _paramList, RequestAttributes& att); - - int get_info (PoolSQL * pool, - int id, - PoolObjectSQL::ObjectType type, - RequestAttributes& att, - PoolObjectAuth& perms, - string& name); }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerCluster.h b/include/RequestManagerCluster.h index 94958600d7..c90cb124e4 100644 --- a/include/RequestManagerCluster.h +++ b/include/RequestManagerCluster.h @@ -67,13 +67,6 @@ protected: virtual int del_object(Cluster* cluster, int id, string& error_msg) = 0; virtual void get(int oid, bool lock, PoolObjectSQL ** object, Clusterable ** cluster_obj) = 0; - - int get_info (PoolSQL * pool, - int id, - PoolObjectSQL::ObjectType type, - RequestAttributes& att, - PoolObjectAuth& perms, - string& name); }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerDelete.h b/include/RequestManagerDelete.h index 037b29dd2e..32c2896220 100644 --- a/include/RequestManagerDelete.h +++ b/include/RequestManagerDelete.h @@ -135,6 +135,10 @@ public: }; ~HostDelete(){}; + + /* -------------------------------------------------------------------- */ + + int drop(int oid, PoolObjectSQL * object, string& error_msg); }; /* ------------------------------------------------------------------------- */ diff --git a/src/cli/onehost b/src/cli/onehost index 115e2f7098..9da4453de8 100755 --- a/src/cli/onehost +++ b/src/cli/onehost @@ -61,9 +61,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :create, create_desc, :hostname, :im_mad, :vmm_mad, - :vnm_mad do + :vnm_mad, [:clusterid, nil] do helper.create_resource(options) do |host| - host.allocate(args[0], args[1], args[2], args[3]) + if args[4] + host.allocate(args[0], args[1], args[2], args[3]) + else + host.allocate(args[0], args[1], args[2], args[3], args[4].to_i) + end end end diff --git a/src/oca/ruby/OpenNebula/ClusterPool.rb b/src/oca/ruby/OpenNebula/ClusterPool.rb index 8885c399c7..b080514628 100644 --- a/src/oca/ruby/OpenNebula/ClusterPool.rb +++ b/src/oca/ruby/OpenNebula/ClusterPool.rb @@ -23,6 +23,8 @@ module OpenNebula # Constants and Class attribute accessors ####################################################################### + DEFAULT_CLUSTER_ID = 0 + CLUSTER_POOL_METHODS = { :info => "clusterpool.info" } diff --git a/src/oca/ruby/OpenNebula/Host.rb b/src/oca/ruby/OpenNebula/Host.rb index 4a77bd2756..e869959396 100644 --- a/src/oca/ruby/OpenNebula/Host.rb +++ b/src/oca/ruby/OpenNebula/Host.rb @@ -82,11 +82,12 @@ module OpenNebula # @param im [String] Name of the im_driver (information/monitoring) # @param vmm [String] Name of the vmm_driver (hypervisor) # @param tm [String] Name of the vnm_driver (networking) + # @param cluster_id [Integer] Id of the cluster # # @return [Integer, OpenNebula::Error] the new ID in case of # success, error otherwise - def allocate(hostname,im,vmm,vnm) - super(HOST_METHODS[:allocate],hostname,im,vmm,vnm) + def allocate(hostname,im,vmm,vnm,cluster_id=ClusterPool::DEFAULT_CLUSTER_ID) + super(HOST_METHODS[:allocate],hostname,im,vmm,vnm,cluster_id) end # Deletes the Host diff --git a/src/rm/Request.cc b/src/rm/Request.cc index cf50bec1ed..bd5b9a9f82 100644 --- a/src/rm/Request.cc +++ b/src/rm/Request.cc @@ -297,3 +297,31 @@ string Request::allocate_error (const string& error) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int Request::get_info( + PoolSQL * pool, + int id, + PoolObjectSQL::ObjectType type, + RequestAttributes& att, + PoolObjectAuth& perms, + string& name) +{ + PoolObjectSQL * ob; + + if ((ob = pool->get(id,true)) == 0 ) + { + failure_response(NO_EXISTS, get_error(object_name(type), id), att); + return -1; + } + + ob->get_permissions(perms); + + name = ob->get_name(); + + ob->unlock(); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index b049568133..909bfcfcc4 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -306,27 +306,66 @@ int TemplateAllocate::pool_allocate(xmlrpc_c::paramList const& _paramList, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostAllocate::pool_allocate(xmlrpc_c::paramList const& paramList, - Template * tmpl, - int& id, - string& error_str, - RequestAttributes& att) +void HostAllocate::request_execute( + xmlrpc_c::paramList const& paramList, + RequestAttributes& att) { + string error_str; + string cluster_name; + string ds_data; + int rc, id; + + PoolObjectAuth cluster_perms; + string host = xmlrpc_c::value_string(paramList.getString(1)); string im_mad = xmlrpc_c::value_string(paramList.getString(2)); string vmm_mad = xmlrpc_c::value_string(paramList.getString(3)); string vnm_mad = xmlrpc_c::value_string(paramList.getString(4)); + int cluster_id = xmlrpc_c::value_int(paramList.getInt(5)); - // TODO: include another int parameter for the cluster? - int cluster_id = ClusterPool::DEFAULT_CLUSTER_ID; - string cluster_name = ClusterPool::DEFAULT_CLUSTER_NAME; + Nebula& nd = Nebula::instance(); - // TODO: Add to auth request CLUSTER MANAGE or ADMIN + ClusterPool * clpool = nd.get_clpool(); + HostPool * hpool = static_cast(pool); - HostPool * hpool = static_cast(pool); + // ------------------------- Check Cluster exists ------------------------ - return hpool->allocate(&id, host, im_mad, vmm_mad, vnm_mad, - cluster_id, cluster_name, error_str); + get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, + cluster_perms, cluster_name); + + // ------------- Set authorization request for non-oneadmin's ------------- + + if ( att.uid != 0 ) + { + AuthRequest ar(att.uid, att.gid); + string tmpl_str = ""; + + ar.add_create_auth(auth_object, tmpl_str); // CREATE HOST + + ar.add_auth(AuthRequest::ADMIN, cluster_perms); // ADMIN CLUSTER + + if (UserPool::authorize(ar) == -1) + { + failure_response(AUTHORIZATION, + authorization_error(ar.message, att), + att); + + return; + } + } + + // ------------- Allocate Host -------------------------------------------- + + rc = hpool->allocate(&id, host, im_mad, vmm_mad, vnm_mad, + cluster_id, cluster_name, error_str); + + if ( rc < 0 ) + { + failure_response(INTERNAL, allocate_error(error_str), att); + return; + } + + success_response(id, att); } /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerChown.cc b/src/rm/RequestManagerChown.cc index 4ecbf9ee48..768a87c5e8 100644 --- a/src/rm/RequestManagerChown.cc +++ b/src/rm/RequestManagerChown.cc @@ -23,33 +23,6 @@ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int RequestManagerChown::get_info (PoolSQL * pool, - int id, - PoolObjectSQL::ObjectType type, - RequestAttributes& att, - PoolObjectAuth& perms, - string& name) -{ - PoolObjectSQL * ob; - - if ((ob = pool->get(id,true)) == 0 ) - { - failure_response(NO_EXISTS, get_error(object_name(type), id), att); - return -1; - } - - ob->get_permissions(perms); - - name = ob->get_name(); - - ob->unlock(); - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - void RequestManagerChown::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { diff --git a/src/rm/RequestManagerCluster.cc b/src/rm/RequestManagerCluster.cc index 91584334ea..09089e110c 100644 --- a/src/rm/RequestManagerCluster.cc +++ b/src/rm/RequestManagerCluster.cc @@ -21,35 +21,6 @@ using namespace std; /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -// TODO: same method in RequestManagerChown, should be moved to Request - -int RequestManagerCluster::get_info (PoolSQL * pool, - int id, - PoolObjectSQL::ObjectType type, - RequestAttributes& att, - PoolObjectAuth& perms, - string& name) -{ - PoolObjectSQL * ob; - - if ((ob = pool->get(id,true)) == 0 ) - { - failure_response(NO_EXISTS, get_error(object_name(type), id), att); - return -1; - } - - ob->get_permissions(perms); - - name = ob->get_name(); - - ob->unlock(); - - return 0; -} - -/* ------------------------------------------------------------------------- */ -/* ------------------------------------------------------------------------- */ - void RequestManagerCluster::add_generic( xmlrpc_c::paramList const& paramList, RequestAttributes& att, diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index 9c56df8de5..c6a0f627cc 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -153,6 +153,43 @@ int ImageDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) /* ------------------------------------------------------------------------- */ +int HostDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) +{ + Host * host = static_cast(object); + int cluster_id = host->get_cluster_id(); + + int rc = pool->drop(object, error_msg); + + object->unlock(); + + if ( rc == 0 ) + { + Nebula& nd = Nebula::instance(); + ClusterPool * clpool = nd.get_clpool(); + + Cluster * cluster = clpool->get(cluster_id, true); + + if( cluster != 0 ) + { + rc = cluster->del_host(oid, error_msg); + + if ( rc < 0 ) + { + cluster->unlock(); + return rc; + } + + clpool->update(cluster); + + cluster->unlock(); + } + } + + return rc; +} + +/* ------------------------------------------------------------------------- */ + int UserDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) { User * user = static_cast(object);