diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index e41658c1c9..dafdba97ce 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -30,14 +30,9 @@ public: const char * _net_xpath, const char * _img_xpath, const char * _vm_xpath): - Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath), + Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath, true), root_elem(_root_elem) - { - datastore_quota.make_default(); - network_quota.make_default(); - image_quota.make_default(); - vm_quota.make_default(); - }; + {}; ~DefaultQuotas(){}; @@ -80,7 +75,12 @@ public: * Bootstraps the database table(s) associated to the default quotas * @return 0 on success */ - static int bootstrap(SqlDB * db); + static int bootstrap(SqlDB * db) + { + ostringstream oss(DefaultQuotas::db_bootstrap); + + return db->exec(oss); + } private: const char * root_elem; diff --git a/include/Quota.h b/include/Quota.h index 1f6106c798..ac7a74b1cf 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -24,14 +24,14 @@ class Quotas; /** * Base class for resource quotas, it provides basic storage and management of - * the quotas. Each resource MUST inherit from it to implement check and + * the quotas. Each resource MUST inherit from it to implement check and * update methods. Quotas are stored in a template form, each class store the * limits and usage in a resource specific format. */ class Quota: public Template { public: - + /** * Set the quotas. If the quota previously exists its limit is updated. * @param quota_str the quota template in ASCII or XML formats @@ -42,11 +42,11 @@ public: int set(vector * quotas, string& error); /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0; @@ -78,31 +78,23 @@ public: return get_quota(id, va, it); } - /** - * Sets this Quota as a Default Quota. Default quotas do not have usage, - * and can't have a limit of -1 - */ - void make_default() - { - is_default = true; - }; - protected: Quota(const char * quota_name, const char * _template_name, const char ** _metrics, - int _num_metrics) + int _num_metrics, + bool _is_default) : Template(false, '=', quota_name), template_name(_template_name), metrics(_metrics), num_metrics(_num_metrics), - is_default(false){}; - + is_default(_is_default){}; + virtual ~Quota(){}; /** - * Generic Quota Names + * Generic Quota Names * * template_name = [ * ID = "ID to identify the resource", @@ -128,8 +120,14 @@ protected: */ int num_metrics; - /** - * Check a given quota for an usage request and update counters if the + /** + * Whether or not this is a default quota. Default quotas do not have usage, + * and can't have a limit of -1 + */ + bool is_default; + + /** + * Check a given quota for an usage request and update counters if the * request does not exceed quota limits * @param qid id that identifies the quota, to be used by get_quota * @param usage_req usage for each metric @@ -137,7 +135,7 @@ protected: * @param error string describing the error * @return true if the request does not exceed current limits */ - bool check_quota(const string& qid, + bool check_quota(const string& qid, map& usage_req, Quotas& default_quotas, string& error); @@ -147,7 +145,7 @@ protected: * @param qid id that identifies the quota, to be used by get_quota * @param usage_req usage for each metric */ - void del_quota(const string& qid, + void del_quota(const string& qid, map& usage_req); /** @@ -184,12 +182,6 @@ protected: */ void cleanup_quota(const string& qid); - /** - * Whether or not this is a default quota. Default quotas do not have usage, - * and can't have a limit of -1 - */ - bool is_default; - private: /** * Creates an empty quota based on the given attribute. The attribute va @@ -218,7 +210,7 @@ private: */ void add_to_quota(VectorAttribute * attr, const string& va_name, float num); - /** + /** * Sets new limit values for the quota * @param quota to be updated * @param va attribute with the new limits diff --git a/include/QuotaDatastore.h b/include/QuotaDatastore.h index fcd5a7f7ab..b37fe53c68 100644 --- a/include/QuotaDatastore.h +++ b/include/QuotaDatastore.h @@ -35,21 +35,23 @@ class QuotaDatastore : public Quota { public: - - QuotaDatastore():Quota("DATASTORE_QUOTA", - "DATASTORE", - DS_METRICS, - NUM_DS_METRICS) + + QuotaDatastore(bool is_default): + Quota("DATASTORE_QUOTA", + "DATASTORE", + DS_METRICS, + NUM_DS_METRICS, + is_default) {}; ~QuotaDatastore(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaImage.h b/include/QuotaImage.h index 39b87ce6ac..57a8c8e759 100644 --- a/include/QuotaImage.h +++ b/include/QuotaImage.h @@ -33,21 +33,23 @@ class QuotaImage : public Quota { public: - - QuotaImage():Quota("IMAGE_QUOTA", - "IMAGE", - IMAGE_METRICS, - NUM_IMAGE_METRICS) + + QuotaImage(bool is_default): + Quota("IMAGE_QUOTA", + "IMAGE", + IMAGE_METRICS, + NUM_IMAGE_METRICS, + is_default) {}; ~QuotaImage(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaNetwork.h b/include/QuotaNetwork.h index afa92e5874..2bb4d6d6d1 100644 --- a/include/QuotaNetwork.h +++ b/include/QuotaNetwork.h @@ -33,21 +33,23 @@ class QuotaNetwork : public Quota { public: - - QuotaNetwork():Quota("NETWORK_QUOTA", - "NETWORK", - NET_METRICS, - NUM_NET_METRICS) + + QuotaNetwork(bool is_default): + Quota("NETWORK_QUOTA", + "NETWORK", + NET_METRICS, + NUM_NET_METRICS, + is_default) {}; ~QuotaNetwork(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaVirtualMachine.h b/include/QuotaVirtualMachine.h index fa085c0421..3d33eb9f10 100644 --- a/include/QuotaVirtualMachine.h +++ b/include/QuotaVirtualMachine.h @@ -36,21 +36,23 @@ class QuotaVirtualMachine : public Quota { public: - - QuotaVirtualMachine():Quota("VM_QUOTA", - "VM", - VM_METRICS, - NUM_VM_METRICS) + + QuotaVirtualMachine(bool is_default): + Quota("VM_QUOTA", + "VM", + VM_METRICS, + NUM_VM_METRICS, + is_default) {}; ~QuotaVirtualMachine(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); @@ -72,7 +74,6 @@ public: protected: - /** * Gets a quota, overrides base to not to use ID. * @@ -100,7 +101,10 @@ protected: * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va); + int get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va); static const char * VM_METRICS[]; diff --git a/include/Quotas.h b/include/Quotas.h index 2280362744..fb6392540f 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -31,10 +31,14 @@ public: const char * _net_xpath, const char * _img_xpath, const char * _vm_xpath): - ds_xpath(_ds_xpath), - net_xpath(_net_xpath), - img_xpath(_img_xpath), - vm_xpath(_vm_xpath) + datastore_quota(false), + network_quota(false), + image_quota(false), + vm_quota(false), + ds_xpath(_ds_xpath), + net_xpath(_net_xpath), + img_xpath(_img_xpath), + vm_xpath(_vm_xpath) {}; virtual ~Quotas(){}; @@ -200,6 +204,26 @@ public: static void quota_del(QuotaType type, int uid, int gid, Template * tmpl); protected: + /** + * This is an specialized constructor only for derived Quotas classes. + * It allows to set the defaultness attribute + */ + Quotas(const char * _ds_xpath, + const char * _net_xpath, + const char * _img_xpath, + const char * _vm_xpath, + bool is_deafult): + datastore_quota(is_deafult), + network_quota(is_deafult), + image_quota(is_deafult), + vm_quota(is_deafult), + ds_xpath(_ds_xpath), + net_xpath(_net_xpath), + img_xpath(_img_xpath), + vm_xpath(_vm_xpath) + {}; + +private: //-------------------------------------------------------------------------- // Usage Counters and Quotas //-------------------------------------------------------------------------- diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index 993f999108..e88e6a83eb 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -23,6 +23,9 @@ const char * DefaultQuotas::table = "system_attributes"; const char * DefaultQuotas::db_names = "name, body"; +const char * DefaultQuotas::db_bootstrap = "CREATE TABLE IF NOT EXISTS" + " system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -139,13 +142,3 @@ error_body: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ - -int DefaultQuotas::bootstrap(SqlDB * db) -{ - ostringstream oss; - - oss << "CREATE TABLE IF NOT EXISTS system_attributes (" - "name VARCHAR(128) PRIMARY KEY, body TEXT)"; - - return db->exec(oss); -} diff --git a/src/um/QuotaImage.cc b/src/um/QuotaImage.cc index 0b6774524f..dd3f1e563a 100644 --- a/src/um/QuotaImage.cc +++ b/src/um/QuotaImage.cc @@ -51,7 +51,7 @@ bool QuotaImage::check(Template * tmpl, Quotas& default_quotas, string& error) } image_id = disk->vector_value("IMAGE_ID"); - + if ( !image_id.empty() ) { if ( !check_quota(image_id, image_request, default_quotas, error) ) @@ -92,7 +92,7 @@ void QuotaImage::del(Template * tmpl) } image_id = disk->vector_value("IMAGE_ID"); - + del_quota(image_id, image_request); } } @@ -100,7 +100,10 @@ void QuotaImage::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaImage::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) +int QuotaImage::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { return default_quotas.image_get(id, va); } diff --git a/src/um/QuotaNetwork.cc b/src/um/QuotaNetwork.cc index 97dd50b1b1..b1ca09b45d 100644 --- a/src/um/QuotaNetwork.cc +++ b/src/um/QuotaNetwork.cc @@ -92,7 +92,7 @@ void QuotaNetwork::del(Template * tmpl) } net_id = nic->vector_value("NETWORK_ID"); - + del_quota(net_id, net_request); } } diff --git a/src/um/QuotaVirtualMachine.cc b/src/um/QuotaVirtualMachine.cc index 14c0335aee..34378c41dc 100644 --- a/src/um/QuotaVirtualMachine.cc +++ b/src/um/QuotaVirtualMachine.cc @@ -73,7 +73,7 @@ bool QuotaVirtualMachine::check(Template * tmpl, vm_request.insert(make_pair("VMS",1)); vm_request.insert(make_pair("MEMORY", memory)); vm_request.insert(make_pair("CPU", cpu)); - + return check_quota("", vm_request, default_quotas, error); } @@ -100,14 +100,17 @@ void QuotaVirtualMachine::del(Template * tmpl) vm_request.insert(make_pair("VMS",1)); vm_request.insert(make_pair("MEMORY", memory)); vm_request.insert(make_pair("CPU", cpu)); - + del_quota("", vm_request); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaVirtualMachine::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) +int QuotaVirtualMachine::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { return default_quotas.vm_get(id, va); }