From dad6caf0ca02b598743802fbf084a98098b336f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 26 Nov 2012 16:12:05 +0100 Subject: [PATCH] Feature #1611: DefaultQuotas methods to & from xml --- include/DefaultQuotas.h | 28 ++++++++-- include/Nebula.h | 22 ++++---- include/Quotas.h | 1 - include/RequestManagerSystem.h | 7 +-- src/rm/RequestManagerSystem.cc | 42 +++------------ src/um/DefaultQuotas.cc | 93 ++++++++++++++++++++++++++++++++++ src/um/SConstruct | 3 +- 7 files changed, 142 insertions(+), 54 deletions(-) create mode 100644 src/um/DefaultQuotas.cc diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index 209df83b3c..892f23415c 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -22,11 +22,14 @@ class DefaultQuotas : public Quotas { public: - DefaultQuotas(const char * _ds_xpath, - const char * _net_xpath, - const char * _img_xpath, - const char * _vm_xpath): - Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath) + DefaultQuotas( + const char * _root_elem, + const char * _ds_xpath, + const char * _net_xpath, + const char * _img_xpath, + const char * _vm_xpath): + Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath), + root_elem(_root_elem) {}; ~DefaultQuotas(){}; @@ -43,7 +46,22 @@ public: return Quotas::set(tmpl, false, error); }; + /** + * Generates a string representation of the quotas in XML format + * @param xml the string to store the XML + * @return the same xml string to use it in << compounds + */ + string& to_xml(string& xml) const; + + /** + * Builds quota object from an ObjectXML + * @param xml The xml-formatted string + * @return 0 if success + */ + int from_xml(const string& xml); + private: + const char * root_elem; }; #endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index ea0580c668..39430cc5d5 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -313,7 +313,7 @@ public: return nebula_configuration->to_xml(xml); }; - const Quotas& get_default_user_quota() + const DefaultQuotas& get_default_user_quota() { return default_user_quota; }; @@ -323,7 +323,7 @@ public: return default_user_quota.set(tmpl, error); }; - const Quotas& get_default_group_quota() + const DefaultQuotas& get_default_group_quota() { return default_group_quota; }; @@ -340,14 +340,16 @@ private: // ----------------------------------------------------------------------- Nebula():nebula_configuration(0), - default_user_quota("/USER/DATASTORE_QUOTA", - "/USER/NETWORK_QUOTA", - "/USER/IMAGE_QUOTA", - "/USER/VM_QUOTA"), - default_group_quota("/GROUP/DATASTORE_QUOTA", - "/GROUP/NETWORK_QUOTA", - "/GROUP/IMAGE_QUOTA", - "/GROUP/VM_QUOTA"), + default_user_quota( "DEFAULT_USER_QUOTAS", + "/DEFAULT_USER_QUOTAS/DATASTORE_QUOTA", + "/DEFAULT_USER_QUOTAS/NETWORK_QUOTA", + "/DEFAULT_USER_QUOTAS/IMAGE_QUOTA", + "/DEFAULT_USER_QUOTAS/VM_QUOTA"), + default_group_quota("DEFAULT_GROUP_QUOTAS", + "/DEFAULT_GROUP_QUOTAS/DATASTORE_QUOTA", + "/DEFAULT_GROUP_QUOTAS/NETWORK_QUOTA", + "/DEFAULT_GROUP_QUOTAS/IMAGE_QUOTA", + "/DEFAULT_GROUP_QUOTAS/VM_QUOTA"), db(0),vmpool(0),hpool(0),vnpool(0), upool(0),ipool(0),gpool(0),tpool(0),dspool(0),clpool(0),docpool(0), lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0),aclm(0),imagem(0) diff --git a/include/Quotas.h b/include/Quotas.h index e819e5681c..5e2344171f 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -213,7 +213,6 @@ protected: */ int set(Template *tmpl, bool default_allowed, string& error); -private: //-------------------------------------------------------------------------- // Usage Counters and Quotas //-------------------------------------------------------------------------- diff --git a/include/RequestManagerSystem.h b/include/RequestManagerSystem.h index 22d3c17d5d..4e436b7d4e 100644 --- a/include/RequestManagerSystem.h +++ b/include/RequestManagerSystem.h @@ -18,6 +18,7 @@ #define REQUEST_MANAGER_SYSTEM_H #include "Request.h" +#include "DefaultQuotas.h" using namespace std; @@ -140,7 +141,7 @@ public: virtual int set_default_quota(Template *tmpl, string& error) = 0; - virtual string get_default_quota() = 0; + const virtual DefaultQuotas* get_default_quota() = 0; }; /* ------------------------------------------------------------------------- */ @@ -158,7 +159,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string get_default_quota(); + const DefaultQuotas* get_default_quota(); }; /* ------------------------------------------------------------------------- */ @@ -176,7 +177,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string get_default_quota(); + const DefaultQuotas* get_default_quota(); }; /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerSystem.cc b/src/rm/RequestManagerSystem.cc index 2d7701d23f..f020f4e84a 100644 --- a/src/rm/RequestManagerSystem.cc +++ b/src/rm/RequestManagerSystem.cc @@ -62,13 +62,7 @@ void UserQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, { string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_user_quota().to_xml(xml) - << ""; - - success_response(oss.str(), att); + success_response(Nebula::instance().get_default_user_quota().to_xml(xml), att); return; } @@ -81,13 +75,7 @@ void GroupQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, { string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_group_quota().to_xml(xml) - << ""; - - success_response(oss.str(), att); + success_response(Nebula::instance().get_default_group_quota().to_xml(xml), att); return; } @@ -99,7 +87,7 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { string quota_str = xmlrpc_c::value_string(paramList.getString(1)); - string error_str; + string error_str, xml; Template quota_tmpl; int rc; @@ -128,7 +116,7 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, return; } - success_response(get_default_quota(), att); + success_response(get_default_quota()->to_xml(xml), att); return; } @@ -144,16 +132,9 @@ int UserQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string UserQuotaUpdate::get_default_quota() +const DefaultQuotas * UserQuotaUpdate::get_default_quota() { - string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_user_quota().to_xml(xml) - << ""; - - return oss.str(); + return &Nebula::instance().get_default_user_quota(); } /* ------------------------------------------------------------------------- */ @@ -167,16 +148,9 @@ int GroupQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string GroupQuotaUpdate::get_default_quota() +const DefaultQuotas * GroupQuotaUpdate::get_default_quota() { - string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_group_quota().to_xml(xml) - << ""; - - return oss.str(); + return &Nebula::instance().get_default_group_quota(); } /* ------------------------------------------------------------------------- */ diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc new file mode 100644 index 0000000000..fc8877dc24 --- /dev/null +++ b/src/um/DefaultQuotas.cc @@ -0,0 +1,93 @@ +/* -------------------------------------------------------------------------- */ +/* 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 "DefaultQuotas.h" +#include "ObjectXML.h" + +string& DefaultQuotas::to_xml(string& xml) const +{ + ostringstream oss; + + string ds_quota_xml; + string net_quota_xml; + string vm_quota_xml; + string image_quota_xml; + + oss << "<" << root_elem << ">" + << datastore_quota.to_xml(ds_quota_xml) + << network_quota.to_xml(net_quota_xml) + << vm_quota.to_xml(vm_quota_xml) + << image_quota.to_xml(image_quota_xml) + << ""; + + xml = oss.str(); + + return xml; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::from_xml(const string& xml) +{ + vector content; + int rc = 0; + + ObjectXML *object_xml = new ObjectXML(xml); + + object_xml->get_nodes(ds_xpath, content); + + if (!content.empty()) + { + rc += datastore_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(net_xpath, content); + + if (!content.empty()) + { + rc += network_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(vm_xpath, content); + + if (!content.empty()) + { + rc += vm_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(img_xpath, content); + + if (!content.empty()) + { + rc += image_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + + delete object_xml; + + return rc; +} diff --git a/src/um/SConstruct b/src/um/SConstruct index 0c8ea406dd..122767aff1 100644 --- a/src/um/SConstruct +++ b/src/um/SConstruct @@ -29,7 +29,8 @@ source_files=[ 'QuotaNetwork.cc', 'QuotaVirtualMachine.cc', 'QuotaImage.cc', - 'Quotas.cc' + 'Quotas.cc', + 'DefaultQuotas.cc' ] # Build library