diff --git a/include/AddressRange.h b/include/AddressRange.h index c26b629827..7446febe9a 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -121,9 +121,11 @@ public: * A vector containing just -1 means all VMs. * @param vnet_ids list of VNET the user can access reservation info from. * A vector containing just -1 means all VNETs. + * @param vrs list of VRouter the user can access VNET usage info from. + * A vector containing just -1 means all VRouters. */ void to_xml(ostringstream &oss, const vector& vms, - const vector& vnets) const; + const vector& vnets, const vector& vrs) const; // ************************************************************************* // Address allocation functions diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 91d66f656a..7c2dc7ff5d 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -341,10 +341,12 @@ public: * A vector containing just -1 means all VMs. * @param vnet_ids list of VNET the user can access reservation info from. * A vector containing just -1 means all VNETs. + * @param vrs list of VRouter the user can access VNET usage info from. + * A vector containing just -1 means all VRouters. * @return the string with the XML */ string& to_xml(string& sstream, bool extended, const vector& vms, - const vector& vnets) const; + const vector& vnets, const vector& vrs) const; private: /** diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h index e676f09f55..38bf7b550c 100644 --- a/include/NebulaUtil.h +++ b/include/NebulaUtil.h @@ -201,6 +201,17 @@ namespace one_util * @return trimed string */ std::string trim(const std::string& str); + + /** + * Returns a copy of st with the all occurrences of "find" substituted + * for "replacement" + * @param st string input + * @param sfind string to search for + * @param replacement string to replace occurrences with + * @return a string copy + */ + std::string gsub(const std::string& st, const std::string& sfind, + const std::string& replacement); }; #endif /* _NEBULA_UTIL_H_ */ diff --git a/include/ObjectCollection.h b/include/ObjectCollection.h index 115563a912..03f304d78e 100644 --- a/include/ObjectCollection.h +++ b/include/ObjectCollection.h @@ -80,11 +80,19 @@ public: /** * Returns a copy of the IDs set */ - set get_collection_copy() + set get_collection_copy() const { return set (collection_set); }; + /** + * Returns a reference to the IDs set + */ + const set& get_collection() const + { + return collection_set; + }; + /** * Returns true if the collection contains the given id * @param id ID to search diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index ec4124d1b0..b832a43075 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -49,6 +49,7 @@ public: */ enum ObjectType { + NONE = 0x0000000000000000LL, VM = 0x0000001000000000LL, HOST = 0x0000002000000000LL, NET = 0x0000004000000000LL, diff --git a/include/Quota.h b/include/Quota.h index e1b79e9c77..10d0b426e4 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -49,7 +49,7 @@ public: * @param error string * @return true if the operation can be performed */ - virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0; + //virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0; /** * Check if a resource update in usage counters will exceed the @@ -69,7 +69,7 @@ public: * Decrement usage counters when deallocating image * @param tmpl template for the resource */ - virtual void del(Template* tmpl) = 0; + //virtual void del(Template* tmpl) = 0; /** * Returns the name that identifies the quota in a template diff --git a/include/QuotaNetwork.h b/include/QuotaNetwork.h index ff18a2bd83..1d3087f213 100644 --- a/include/QuotaNetwork.h +++ b/include/QuotaNetwork.h @@ -18,6 +18,7 @@ #define QUOTA_NETWORK_H_ #include "Quota.h" +#include "PoolObjectSQL.h" /** * DataStore Quotas, defined as: @@ -47,18 +48,21 @@ public: /** * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated + * @param otype object type, VM or VRouter * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - bool check(Template* tmpl, Quotas& default_quotas, string& error); + bool check(PoolObjectSQL::ObjectType otype, Template* tmpl, + Quotas& default_quotas, string& error); /** * Decrement usage counters when deallocating image + * @param otype object type, VM or VRouter * @param tmpl template for the resource */ - void del(Template* tmpl); + void del(PoolObjectSQL::ObjectType otype, Template* tmpl); protected: diff --git a/include/Quotas.h b/include/Quotas.h index bbcd348658..93261ae39b 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -35,7 +35,8 @@ public: VM, /**< Checks VM usage (MEMORY, CPU and VMS) */ NETWORK, /**< Checks Network usage (leases) */ IMAGE, /**< Checks Image usage (RVMs using it) */ - VIRTUALMACHINE /**< Checks all VM associated resources VM, NETWORK, IMAGE */ + VIRTUALMACHINE, /**< Checks all VM associated resources VM, NETWORK, IMAGE */ + VIRTUALROUTER /**< Checks the Virtual Router NETWORK usage (leases) */ }; /** @@ -69,17 +70,6 @@ public: return datastore_quota.get_quota(id, va); } - /** - * Delete VM related usage (network, image and compute) from quota counters. - * @param tmpl template for the image, with usage - */ - void vm_del(Template * tmpl) - { - network_quota.del(tmpl); - vm_quota.del(tmpl); - image_quota.del(tmpl); - } - /** * Gets a VM quota identified by its ID. * diff --git a/include/Request.h b/include/Request.h index f606def3c0..2263c7218c 100644 --- a/include/Request.h +++ b/include/Request.h @@ -27,6 +27,77 @@ using namespace std; +/** + * This class represents the dynamic attributes: specific for a request of the + * same method. + */ +struct RequestAttributes +{ +public: + int uid; /**< id of the user */ + int gid; /**< id of the user's group */ + + string uname; /**< name of the user */ + string gname; /**< name of the user's group */ + + string password; /**< password of the user */ + + set group_ids; /**< set of user's group ids */ + + string session; /**< Session from ONE XML-RPC API */ + int req_id; /**< Request ID for log messages */ + + int umask; /**< User umask for new objects */ + + xmlrpc_c::value * retval; /**< Return value from libxmlrpc-c */ + + PoolObjectSQL::ObjectType resp_obj; /**< object type */ + int resp_id; /**< Id of the object */ + string resp_msg; /**< Additional response message */ + + RequestAttributes(){}; + + RequestAttributes(const RequestAttributes& ra) + { + uid = ra.uid; + gid = ra.gid; + + uname = ra.uname; + gname = ra.gname; + + password = ra.password; + + session = ra.session; + retval = ra.retval; + + umask = ra.umask; + + resp_obj = ra.resp_obj; + resp_id = ra.resp_id; + resp_msg = ra.resp_msg; + }; + + RequestAttributes(int _uid, int _gid, const RequestAttributes& ra) + { + uid = _uid; + gid = _gid; + + password = ""; + + uname = ""; + gname = ""; + + umask = 0; + + session = ra.session; + retval = ra.retval; + + resp_obj = PoolObjectSQL::NONE; + resp_id = -1; + resp_msg = ""; + }; +}; + /** * The Request Class represents the basic abstraction for the OpenNebula * XML-RPC API. This interface must be implemented by any XML-RPC API call @@ -34,16 +105,6 @@ using namespace std; class Request: public xmlrpc_c::method { public: - /** - * Wraps the actual execution function by authorizing the user - * and calling the request_execute virtual function - * @param _paramlist list of XML parameters - * @param _retval value to be returned to the client - */ - virtual void execute( - xmlrpc_c::paramList const& _paramList, - xmlrpc_c::value * const _retval); - /** * Error codes for the XML-RPC API */ @@ -55,8 +116,17 @@ public: ACTION = 0x0800, XML_RPC_API = 0x1000, INTERNAL = 0x2000, + ALLOCATE = 0x4000 }; + /** + * Gets a string representation for the Auth object in the + * request. + * @param ob object for the auth operation + * @return string equivalent of the object + */ + static string object_name(PoolObjectSQL::ObjectType ob); + /** * Sets the format string to log xml-rpc method calls. The format string * interprets the following sequences: @@ -77,69 +147,9 @@ public: } protected: - - /* ---------------------------------------------------------------------*/ - /* Attributes of the Request */ - /* ---------------------------------------------------------------------*/ - - /* -------- Dynamic (specific for a request of the same method) -------- */ - - struct RequestAttributes - { - int uid; /**< id of the user */ - int gid; /**< id of the user's group */ - - string uname; /**< name of the user */ - string gname; /**< name of the user's group */ - - string password; /**< password of the user */ - - set group_ids; /**< set of user's group ids */ - - string session; /**< Session from ONE XML-RPC API */ - int req_id; /**< Request ID for log messages */ - - int umask; /**< User umask for new objects */ - - xmlrpc_c::value * retval; /**< Return value from libxmlrpc-c */ - - RequestAttributes(){}; - - RequestAttributes(const RequestAttributes& ra) - { - uid = ra.uid; - gid = ra.gid; - - uname = ra.uname; - gname = ra.gname; - - password = ra.password; - - session = ra.session; - retval = ra.retval; - - umask = ra.umask; - }; - - RequestAttributes(int _uid, int _gid, const RequestAttributes& ra) - { - uid = _uid; - gid = _gid; - - password = ""; - - uname = ""; - gname = ""; - - umask = 0; - - session = ra.session; - retval = ra.retval; - }; - }; - - /* -------- Static (shared among request of the same method) -------- */ - + /* ---------------------------------------------------------------------- */ + /* Static Request Attributes: shared among request of the same method */ + /* ---------------------------------------------------------------------- */ PoolSQL * pool; /**< Pool of objects */ string method_name; /**< The name of the XML-RPC method */ @@ -150,11 +160,11 @@ protected: static string format_str; - /* -------------------- Constructors ---------------------------------- */ - - Request(const string& mn, - const string& signature, - const string& help): pool(0),method_name(mn) + /* ---------------------------------------------------------------------- */ + /* Class Constructors */ + /* ---------------------------------------------------------------------- */ + Request(const string& mn, const string& signature, const string& help): + pool(0),method_name(mn) { _signature = signature; _help = help; @@ -164,9 +174,88 @@ protected: virtual ~Request(){}; - /* -------------------------------------------------------------------- */ - /* -------------------------------------------------------------------- */ + /* ---------------------------------------------------------------------- */ + /* Methods to execute the request when received at the server */ + /* ---------------------------------------------------------------------- */ + /** + * Wraps the actual execution function by authorizing the user + * and calling the request_execute virtual function + * @param _paramlist list of XML parameters + * @param _retval value to be returned to the client + */ + virtual void execute(xmlrpc_c::paramList const& _paramList, + xmlrpc_c::value * const _retval); + /** + * Actual Execution method for the request. Must be implemented by the + * XML-RPC requests + * @param _paramlist of the XML-RPC call (complete list) + * @param att the specific request attributes + */ + virtual void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att) = 0; + /** + * 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 + * @param throw_error send error response to client if object not found + * + * @return 0 on success, -1 otherwise + */ + int get_info(PoolSQL * pool, + int id, + PoolObjectSQL::ObjectType type, + RequestAttributes& att, + PoolObjectAuth& perms, + string& name, + bool throw_error); + + /* ---------------------------------------------------------------------- */ + /* Methods to send response to xml-rpc client */ + /* ---------------------------------------------------------------------- */ + /** + * Builds an XML-RPC response updating retval. After calling this function + * the xml-rpc excute method should return + * @param val to be returned to the client + * @param att the specific request attributes + */ + void success_response(int val, RequestAttributes& att); + + /** + * Builds an XML-RPC response updating retval. After calling this function + * the xml-rpc excute method should return + * @param val string to be returned to the client + * @param att the specific request attributes + */ + void success_response(const string& val, RequestAttributes& att); + + /** + * Builds an XML-RPC response updating retval. After calling this function + * the xml-rpc execute method should return + * @param val to be returned to the client + * @param att the specific request attributes + */ + void success_response(bool val, RequestAttributes& att); + + /** + * Builds an XML-RPC response updating retval. After calling this function + * the xml-rpc excute method should return. A descriptive error message + * is constructed using att.resp_obj, att.resp_id and/or att.resp_msg and + * the ErrorCode + * @param ec error code for this call + * @param ra the specific request attributes + */ + void failure_response(ErrorCode ec, RequestAttributes& ra); + + /* ---------------------------------------------------------------------- */ + /* Authorization methods for requests */ + /* ---------------------------------------------------------------------- */ /** * Performs a basic authorization for this request using the uid/gid * from the request. The function gets the object from the pool to get @@ -196,7 +285,7 @@ protected: * @return true if the user is authorized. */ bool basic_authorization(int oid, AuthRequest::Operation op, - RequestAttributes& att); + RequestAttributes& att); /** * Performs a basic quota check for this request using the uid/gid @@ -209,10 +298,8 @@ protected: * * @return true if the user is authorized. */ - bool quota_authorization( - Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att); + bool quota_authorization(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att); /** * Performs a basic quota check for this request using the uid/gid @@ -227,11 +314,8 @@ protected: * @param error_str Error reason, if any * @return true if the user is authorized. */ - bool quota_authorization( - Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att, - string& error_str); + static bool quota_authorization(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att, string& error_str); /** * Performs rollback on usage counters for a previous quota check operation @@ -239,136 +323,53 @@ protected: * @param tmpl describing the object * @param att the specific request attributes */ - void quota_rollback(Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att); + static void quota_rollback(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att); - /** - * Actual Execution method for the request. Must be implemented by the - * XML-RPC requests - * @param _paramlist of the XML-RPC call (complete list) - * @param att the specific request attributes - */ - virtual void request_execute(xmlrpc_c::paramList const& _paramList, - RequestAttributes& att) = 0; +private: + /* ---------------------------------------------------------------------- */ + /* Functions to manage user and group quotas */ + /* ---------------------------------------------------------------------- */ + static bool user_quota_authorization(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att, string& error_str); - /** - * Builds an XML-RPC response updating retval. After calling this function - * the xml-rpc excute method should return - * @param val to be returned to the client - * @param att the specific request attributes - */ - void success_response(int val, RequestAttributes& att); + static bool group_quota_authorization(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att, string& error_str); - /** - * Builds an XML-RPC response updating retval. After calling this function - * the xml-rpc excute method should return - * @param val string to be returned to the client - * @param att the specific request attributes - */ - void success_response(const string& val, RequestAttributes& att); + static void user_quota_rollback(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att); - /** - * Builds an XML-RPC response updating retval. After calling this function - * the xml-rpc execute method should return - * @param val to be returned to the client - * @param att the specific request attributes - */ - void success_response(bool val, RequestAttributes& att); + static void group_quota_rollback(Template * tmpl, Quotas::QuotaType qtype, + RequestAttributes& att); /** * Builds an XML-RPC response updating retval. After calling this function * the xml-rpc excute method should return * @param ec error code for this call - * @param val string representation of the error - * @param att the specific request attributes + * @param va string representation of the error + * @param ra the specific request attributes */ - void failure_response(ErrorCode ec, - const string& val, - RequestAttributes& att); + void failure_response(ErrorCode ec, const string& va, RequestAttributes& ra); /** - * Gets a string representation for the Auth object in the - * request. - * @param ob object for the auth operation - * @return string equivalent of the object - */ - static string object_name(PoolObjectSQL::ObjectType ob); - - /** - * Logs authorization errors - * @param message with the authorization error details - * @return string for logging - * @param att the specific request attributes - */ - string authorization_error (const string &message, RequestAttributes& att); - - /** - * Logs authenticate errors - * @return string for logging - */ - string authenticate_error (); - - /** - * Logs get object errors - * @param object over which the get failed - * @param id of the object over which the get failed - * @return string for logging - */ - string get_error (const string &object, int id); - - /** - * Logs action errors - * @param err_desc brief description of the error - * @param err_detail additional error details from Managers & Pools - * @return string for logging - */ - string request_error (const string &err_desc, const string &err_detail); - - /** - * Logs allocate errors - * @param message with the allocate error details - * @return string for logging - */ - string allocate_error (const string& error); - - /** - * Logs allocate errors for a given resource - * @param obj the resource - * @param message with the allocate error details - * @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 + * Logs the method invocation, including the arguments * @param att the specific request attributes - * - * @param perms returns the object's permissions - * @param name returns the object's name - * @param throw_error send error response to client if object not found - * - * @return 0 on success, -1 otherwise + * @param paramList list of XML parameters + * @param format_str for the log + * @param hidden_params params not to be shown */ - int get_info (PoolSQL * pool, - int id, - PoolObjectSQL::ObjectType type, - RequestAttributes& att, - PoolObjectAuth& perms, - string& name, - bool throw_error); + static void log_method_invoked(const RequestAttributes& att, + const xmlrpc_c::paramList& paramList, const string& format_str, + const std::string& method_name, const std::set& hidden_params); /** * Logs the method result, including the output data or error message * * @param att the specific request attributes + * @param method_name that produced the error */ - virtual void log_result( - const RequestAttributes& att); + static void log_result(const RequestAttributes& att, + const std::string& method_name); /** * Formats and adds a xmlrpc_c::value to oss. @@ -376,41 +377,7 @@ protected: * @param v value to format * @param oss stream to write v */ - virtual void log_xmlrpc_value( - const xmlrpc_c::value& v, - ostringstream& oss); - -private: - - /** - * Logs the method invocation, including the arguments - * - * @param att the specific request attributes - * @param paramList list of XML parameters - */ - void log_method_invoked( - const RequestAttributes& att, - const xmlrpc_c::paramList& paramList); - - /* ------------- Functions to manage user and group quotas -------------- */ - - bool user_quota_authorization(Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att, - string& error_str); - - bool group_quota_authorization(Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att, - string& error_str); - - void user_quota_rollback(Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att); - - void group_quota_rollback(Template * tmpl, - Quotas::QuotaType qtype, - RequestAttributes& att); + static void log_xmlrpc_value(const xmlrpc_c::value& v, std::ostringstream& oss); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index 08aced6a80..7bfb33108c 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -64,7 +64,6 @@ protected: virtual int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att) { return -1; @@ -73,12 +72,11 @@ protected: virtual int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att, int cluster_id, const string& cluster_name) { - return pool_allocate(_paramList, tmpl, id, error_str, att); + return pool_allocate(_paramList, tmpl, id, att); }; virtual int get_cluster_id(xmlrpc_c::paramList const& paramList) @@ -138,7 +136,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); bool allocate_authorization(Template * obj_template, @@ -175,7 +172,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att, int cluster_id, const string& cluster_name); @@ -249,7 +245,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); bool allocate_authorization(Template * obj_template, @@ -281,7 +276,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att, int cluster_id, const string& cluster_name); @@ -325,7 +319,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -353,7 +346,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); private: @@ -389,7 +381,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att, int cluster_id, const string& cluster_name); @@ -445,7 +436,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -478,7 +468,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -514,7 +503,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -547,7 +535,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -580,7 +567,6 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); }; @@ -614,8 +600,11 @@ public: int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, int& id, - string& error_str, RequestAttributes& att); + + bool allocate_authorization(Template * obj_template, + RequestAttributes& att, + PoolObjectAuth * cluster_perms); }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerClone.h b/include/RequestManagerClone.h index dcc85c937e..2d03a5efa1 100644 --- a/include/RequestManagerClone.h +++ b/include/RequestManagerClone.h @@ -48,7 +48,6 @@ protected: int source_id, Template * tmpl, int& id, - string& error_str, RequestAttributes& att) = 0; }; @@ -82,7 +81,6 @@ public: int source_id, Template * tmpl, int& id, - string& error_str, RequestAttributes& att) { VMTemplatePool * tpool = static_cast(pool); @@ -91,7 +89,7 @@ public: static_cast(tmpl); return tpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, - ttmpl, &id, error_str); + ttmpl, &id, att.resp_msg); }; }; @@ -126,14 +124,13 @@ public: int source_id, Template * tmpl, int& id, - string& error_str, RequestAttributes& att) { DocumentPool * docpool = static_cast(pool); Document * doc = docpool->get(source_id, true); return docpool->allocate(att.uid, att.gid, att.uname, att.gname, - att.umask, doc->get_document_type(), tmpl, &id, error_str); + att.umask, doc->get_document_type(), tmpl, &id, att.resp_msg); }; }; @@ -167,13 +164,12 @@ public: int source_id, Template * tmpl, int& id, - string& error_str, RequestAttributes& att) { SecurityGroupPool * secgrouppool = static_cast(pool); return secgrouppool->allocate(att.uid, att.gid, att.uname, att.gname, - att.umask, tmpl, &id, error_str); + att.umask, tmpl, &id, att.resp_msg); }; }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerVMTemplate.h b/include/RequestManagerVMTemplate.h index e495eb45bf..f53f4808bb 100644 --- a/include/RequestManagerVMTemplate.h +++ b/include/RequestManagerVMTemplate.h @@ -66,6 +66,24 @@ public: void request_execute(xmlrpc_c::paramList const& _paramList, RequestAttributes& att); + + /** + * Instantiates the VM Template, checking permissions, quotas, etc + * @param id VM Template ID + * @param name Name for the new VM. Can be empty + * @param on_hold True to start the VM on HOLD state + * @param str_uattrs Template supplied by user to merge with the original + * contents. Can be empty + * @param extra_attrs Template to be merged. It should contain internal + * configuration, and it won't be authenticated or checked for restricted + * attributes. Can be 0 + * @param vmid on success of the new VM + * @param att the specific request attributes + * + * @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); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerVirtualMachine.h b/include/RequestManagerVirtualMachine.h index 88bd255120..5c3bbc3872 100644 --- a/include/RequestManagerVirtualMachine.h +++ b/include/RequestManagerVirtualMachine.h @@ -253,8 +253,16 @@ public: ~VirtualMachineAttachNic(){}; - void request_execute(xmlrpc_c::paramList const& _paramList, - RequestAttributes& att); + void request_execute(xmlrpc_c::paramList const& pl, RequestAttributes& ra); + + /** + * Process a NIC attahment request to a Virtual Machine + * @param id of the VirtualMachine + * @param tmpl with the new NIC description + * @param att attributes of this request + * @return ErroCode as defined in Request + */ + static ErrorCode attach(int id, VirtualMachineTemplate& tmpl, RequestAttributes& att); }; /* -------------------------------------------------------------------------- */ @@ -272,6 +280,15 @@ public: void request_execute(xmlrpc_c::paramList const& _paramList, RequestAttributes& att); + + /** + * Process a NIC detach request to a Virtual Machine + * @param id of the VirtualMachine + * @param nic_id id of the NIC + * @param att attributes of this request + * @return ErroCode as defined in Request + */ + static ErrorCode detach(int id, int nic_id, RequestAttributes& att); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerVirtualRouter.h b/include/RequestManagerVirtualRouter.h new file mode 100644 index 0000000000..a48f942398 --- /dev/null +++ b/include/RequestManagerVirtualRouter.h @@ -0,0 +1,118 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */ +/* */ +/* 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_VIRTUAL_ROUTER_H +#define REQUEST_MANAGER_VIRTUAL_ROUTER_H + +#include "Request.h" +#include "Nebula.h" + +using namespace std; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class RequestManagerVirtualRouter: public Request +{ +protected: + RequestManagerVirtualRouter(const string& method_name, + const string& help, + const string& params) + :Request(method_name,params,help) + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vrouterpool(); + + auth_object = PoolObjectSQL::VROUTER; + }; + + ~RequestManagerVirtualRouter(){}; + + /* -------------------------------------------------------------------- */ + + virtual void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att) = 0; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VirtualRouterInstantiate : public RequestManagerVirtualRouter +{ +public: + VirtualRouterInstantiate(): + RequestManagerVirtualRouter("VirtualRouterInstantiate", + "Instantiates a new virtual machine associated to a virtual router", + "A:siiisbs") + { + auth_op = AuthRequest::MANAGE; + }; + + ~VirtualRouterInstantiate(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VirtualRouterAttachNic : public RequestManagerVirtualRouter +{ +public: + VirtualRouterAttachNic(): + RequestManagerVirtualRouter("VirtualRouterAttachNic", + "Attaches a new NIC to the virtual router, and its virtual machines", + "A:sis") + { + auth_op = AuthRequest::MANAGE; + }; + + ~VirtualRouterAttachNic(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VirtualRouterDetachNic : public RequestManagerVirtualRouter +{ +public: + VirtualRouterDetachNic(): + RequestManagerVirtualRouter("VirtualRouterDetachNic", + "Detaches a NIC from a virtual router, and its virtual machines", + "A:sii") + { + auth_op = AuthRequest::MANAGE; + }; + + ~VirtualRouterDetachNic(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +#endif diff --git a/include/Template.h b/include/Template.h index 18ef02d38b..07658fa286 100644 --- a/include/Template.h +++ b/include/Template.h @@ -301,6 +301,34 @@ public: const string& name, vector& values); + /** + * Gets the first VectorAttribute with the given name + * @param name the attribute name. + * @param vatt stores the first vector attribute found, or 0 + * @return true if a vector attribute was found + */ + virtual bool get( + const string& name, + VectorAttribute*& vatt); + + /** + * Gets all the Vector Attributes with the given name + * @param name the attribute name. + * @return the number of elements in the vector + */ + virtual int get( + const string& name, + vector& values) const; + + /** + * Gets all the Vector Attributes with the given name, non-const version + * @param name the attribute name. + * @return the number of elements in the vector + */ + virtual int get( + const string& name, + vector& values); + /** * Gets the value of a Single attributes (string) with the given name. * @param name the attribute name. diff --git a/include/TransferManager.h b/include/TransferManager.h index 530d4bdd9b..fd75f67eb6 100644 --- a/include/TransferManager.h +++ b/include/TransferManager.h @@ -123,6 +123,22 @@ public: ostream& xfr, ostringstream& error); + /** + * Inserts a context command in the xfs stream + * + * @param vm The VM + * @param token_password Owner user's token password + * @param system_tm_mad The Transfer Manager for the system datastore + * @param xfr Stream where the transfer command will be written + * + * @return 0 on success + */ + int prolog_context_command( + VirtualMachine * vm, + const string& token_password, + string& system_tm_mad, + ostream& xfr); + /** * Inserts a transfer command in the xfs stream * diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index edde6df239..43c7de0e32 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -1318,6 +1318,8 @@ public: */ const VectorAttribute* get_disk(int disk_id) const; + const VectorAttribute* get_nic(int nic_id) const; + // ------------------------------------------------------------------------ // Virtual Router related functions // ------------------------------------------------------------------------ @@ -1334,6 +1336,13 @@ public: */ bool is_vrouter(); + /** + * Checks if the given action is supported for Virtual Router VMs + * @param action VM action to check + * @return true if the action is supported for Virtual Router VMs + */ + static bool is_vrouter_action_supported(History::VMAction action); + // ------------------------------------------------------------------------ // Context related functions // ------------------------------------------------------------------------ @@ -1345,7 +1354,15 @@ public: * @param token_password Password to encrypt the token, if it is set * @return -1 in case of error, 0 if the VM has no context, 1 on success */ - int generate_context(string &files, int &disk_id, string& token_password); + int generate_context(string &files, int &disk_id, const string& token_password); + + const VectorAttribute* get_context_disk() const; + + /** + * Returns the CREATED_BY template attribute, or the uid if it does not exist + * @return uid + */ + int get_created_by_uid() const; // ------------------------------------------------------------------------- // "Save as" Disk related functions (save_as hot) @@ -1567,18 +1584,6 @@ public: int uid, string& error_str); - /** - * Cleans the ATTACH = YES attribute from the NICs - */ - void clear_attach_nic(); - - /** - * Deletes the NIC that was in the process of being attached - * - * @return the deleted NIC or 0 if none was deleted - */ - VectorAttribute * delete_attach_nic(); - /** * Adds a new NIC to the virtual machine template. The NIC should be * generated by the build_attach_nic @@ -1587,12 +1592,37 @@ public: */ void set_attach_nic(VectorAttribute * new_nic, vector &rules); + /** + * Cleans the ATTACH = YES attribute from the NICs + */ + void attach_nic_success(); + + /** + * Deletes the NIC that was in the process of being attached + * + * @return the deleted NIC or 0 if none was deleted + */ + VectorAttribute * attach_nic_failure(); + /** * Sets the attach attribute to the given NIC * @param nic_id of the NIC * @return 0 if the nic_id was found, -1 otherwise */ - int set_attach_nic(int nic_id); + int set_detach_nic(int nic_id); + + /** + * Deletes the NIC that was in the process of being detached + * + * @return the deleted NIC or 0 if none was deleted + */ + VectorAttribute * detach_nic_success(); + + /** + * Cleans the ATTACH = YES attribute from the NIC, restores the NIC context + * variables + */ + void detach_nic_failure(); // ------------------------------------------------------------------------ // Snapshot related functions @@ -1711,7 +1741,6 @@ public: */ void delete_snapshots(); - private: // ------------------------------------------------------------------------- @@ -1978,6 +2007,13 @@ private: */ int parse_vrouter(string& error_str); + /** + * Known Virtual Router attributes, to be moved from the user template + * to the template + */ + static const char* VROUTER_ATTRIBUTES[]; + static const int NUM_VROUTER_ATTRIBUTES; + /** * Known attributes for network contextualization rendered as: * ETH__ = $NETWORK[context[1], vnet_name] @@ -2021,6 +2057,15 @@ private: */ int parse_context(string& error_str); + /** + * Parses the current contents of the context vector attribute, + * without adding any attributes. Substitutes $VARIABLE, + * $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE] + * + * @return 0 on success + */ + int reparse_context(); + /** * Parse the "SCHED_REQUIREMENTS" attribute of the template by substituting * $VARIABLE, $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE] @@ -2123,6 +2168,13 @@ private: static_cast(*this).get_disk(disk_id)); }; + /** + * Returns the NIC that is waiting for an attachment action + * + * @return the NIC waiting for an attachment action, or 0 + */ + VectorAttribute* get_attach_nic(); + // ------------------------------------------------------------------------ // Public cloud templates related functions // ------------------------------------------------------------------------ diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index ad1c2df112..9c63d2a665 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -359,7 +359,14 @@ public: * * @param vid VM id */ - void delete_attach_nic(int vid); + void attach_nic_failure(int vid); + + /** + * Deletes the NIC that was in the process of being detached + * + * @param vid VM id + */ + void detach_nic_success(int vid); /** * Deletes an entry in the HV-2-vmid mapping table for imported VMs @@ -417,6 +424,15 @@ private: * @return 0 on success */ int insert_index(const string& deploy_id, int vm_id, bool replace); + + // ------------------------------------------------------------------------- + + /** + * Helper method for delete attach/detach + * @param vid VM id + * @param attach true for an attach action, false for detach + */ + void delete_hotplug_nic(int vid, bool attach); }; #endif /*VIRTUAL_MACHINE_POOL_H_*/ diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 1a431576fb..d4ba3ce8c2 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -409,10 +409,12 @@ public: * A vector containing just -1 means all VMs. * @param vnet_ids list of VNET the user can access reservation info from. * A vector containing just -1 means all VNETs. + * @param vrs list of VRouter the user can access reservation info from. + * A vector containing just -1 means all VRouters. * @return a reference to the generated string */ string& to_xml_extended(string& xml, const vector& vms, - const vector& vnets) const; + const vector& vnets, const vector& vrs) const; /** * Gets a string based attribute (single) from an address range. If the @@ -527,7 +529,8 @@ private: * @return a reference to the generated string */ string& to_xml_extended(string& xml, bool extended, - const vector& vm_ids, const vector& vnet_oids) const; + const vector& vm_ids, const vector& vnet_oids, + const vector& vr_ids) const; /** * Rebuilds the object from an xml formatted string diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index b211e143ac..c4142e48e5 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -113,35 +113,23 @@ public: * -2 not using the pool */ int nic_attribute( - VectorAttribute* nic, - int nic_id, - int uid, - int vid, - string& error_str); - - /** - * Generates a NIC attribute for VRouters using the VirtualNetwork - * metadata - * @param nic the nic attribute to be generated - * @param uid of the VM owner - * @param vrid of the VRouter requesting the lease - * @param error_str string describing the error - * @return 0 on success, - * -1 error, - * -2 not using the pool - */ - int vrouter_nic_attribute( - VectorAttribute * nic, - int uid, - int vrid, - string& error_str); + PoolObjectSQL::ObjectType ot, + VectorAttribute* nic, + int nic_id, + int uid, + int vid, + string& error_str); /** * Generates an Authorization token for a NIC attribute * @param nic the nic to be authorized * @param ar the AuthRequest */ - void authorize_nic(VectorAttribute * nic, int uid, AuthRequest * ar); + void authorize_nic( + PoolObjectSQL::ObjectType ot, + VectorAttribute * nic, + int uid, + AuthRequest * ar); /** * Bootstraps the database table(s) associated to the VirtualNetwork pool diff --git a/include/VirtualRouter.h b/include/VirtualRouter.h index e4e938d1e4..c4be7d524c 100644 --- a/include/VirtualRouter.h +++ b/include/VirtualRouter.h @@ -20,6 +20,8 @@ #include "PoolObjectSQL.h" #include "Template.h" #include "ObjectCollection.h" +#include "VirtualMachineTemplate.h" +#include "AuthRequest.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -38,10 +40,14 @@ public: */ string& to_xml(string& xml) const; - int add_vmid(int vmid) - { - return vms.add_collection_id(vmid); - } + int add_vmid(int vmid); + + bool has_vmids() const; + + /** + * Returns a copy of the VM IDs set + */ + set get_vms() const; // ------------------------------------------------------------------------ // Template Contents @@ -65,7 +71,44 @@ public: *(static_cast