From 930c13ae76e216afe8c85a718aef3f4071ac5810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 20 Oct 2014 16:05:44 +0200 Subject: [PATCH] Bug #3168: process attributes in update --append --- include/Datastore.h | 19 +++++++--------- include/PoolObjectSQL.h | 11 ++++++++++ include/VirtualNetwork.h | 18 ++++++--------- include/Zone.h | 19 +++++++--------- src/datastore/Datastore.cc | 41 ++++------------------------------ src/pool/PoolObjectSQL.cc | 45 ++++++++++++++++++++++++++++++++++++-- src/vnm/VirtualNetwork.cc | 38 +------------------------------- src/zone/Zone.cc | 40 +++------------------------------ 8 files changed, 85 insertions(+), 146 deletions(-) diff --git a/include/Datastore.h b/include/Datastore.h index 489c3bfe17..9738d7d483 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -163,17 +163,6 @@ public: VectorAttribute * disk, const vector& inherit_attrs); - /** - * Replace template for this object. Object should be updated - * after calling this method - * @param tmpl_str new contents - * @param keep_restricted If true, the restricted attributes of the - * current template will override the new template - * @param error string describing the error if any - * @return 0 on success - */ - int replace_template(const string& tmpl_str, bool keep_restricted, string& error); - /** * Set monitor information for the Datastore * @param total_mb @@ -336,6 +325,14 @@ private: } int set_tm_mad(string &tm_mad, string &error_str); + + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + */ + int post_update_template(string& error); }; #endif /*DATASTORE_H_*/ diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index c235b3a4e0..c95b2ee0b0 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -692,6 +692,17 @@ protected: */ virtual void set_template_error_message(const string& name, const string& message); + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + */ + virtual int post_update_template(string& error) + { + return 0; + }; + /** * The object's unique ID */ diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 1939f680fe..179b0c5ee4 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -370,17 +370,6 @@ public: string& to_xml_extended(string& xml, const vector& vms, const vector& vnets) const; - /** - * Replace the template of the virtual network it also updates the BRIDGE, - * PHY_DEV, VLAN_ID and VLAN attributes. - * @param tmpl_str new contents - * @param keep_restricted If true, the restricted attributes of the - * current template will override the new template - * @param error string describing the error if any - * @return 0 on success - */ - int replace_template(const string& tmpl_str, bool keep_restricted, string& error); - /** * Gets a string based attribute (single) from an address range. If the * attribute is not found in the address range, the VNET template will be @@ -494,6 +483,13 @@ private: */ int from_xml(const string &xml_str); + /** + * Updates the BRIDGE, PHY_DEV, VLAN_ID and VLAN attributes. + * @param error string describing the error if any + * @return 0 on success + */ + int post_update_template(string& error); + //************************************************************************** // Constructor //************************************************************************** diff --git a/include/Zone.h b/include/Zone.h index b111bddd61..4553ac5bbf 100644 --- a/include/Zone.h +++ b/include/Zone.h @@ -44,17 +44,6 @@ public: */ int from_xml(const string &xml_str); - /** - * Replace template for this object. Object should be updated - * after calling this method - * @param tmpl_str new contents - * @param keep_restricted If true, the restricted attributes of the - * current template will override the new template - * @param error string describing the error if any - * @return 0 on success - */ - int replace_template(const string& tmpl_str, bool keep_restricted, string& error); - private: // ------------------------------------------------------------------------- @@ -126,6 +115,14 @@ private: { return new Template; } + + /** + * Child classes can process the new template set with replace_template or + * append_template with this method + * @param error string describing the error if any + * @return 0 on success + */ + int post_update_template(string& error); }; #endif /*ZONE_H_*/ diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index 8fb8252c6c..7a8ed8f665 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -581,11 +581,10 @@ int Datastore::from_xml(const string& xml) return 0; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ -int Datastore::replace_template( - const string& tmpl_str, bool keep_restricted, string& error_str) +int Datastore::post_update_template(string& error_str) { string new_ds_mad; string new_tm_mad; @@ -596,38 +595,12 @@ int Datastore::replace_template( Image::DiskType new_disk_type; DatastoreType new_ds_type; - Template * new_tmpl = new DatastoreTemplate; - - if ( new_tmpl == 0 ) - { - error_str = "Cannot allocate a new template"; - return -1; - } - - if ( new_tmpl->parse_str_or_xml(tmpl_str, error_str) != 0 ) - { - delete new_tmpl; - return -1; - } - - if (keep_restricted) - { - new_tmpl->remove_restricted(); - - if (obj_template != 0) - { - obj_template->remove_all_except_restricted(); - - string aux_error; - new_tmpl->merge(obj_template, aux_error); - } - } /* ---------------------------------------------------------------------- */ /* Set the TYPE of the Datastore (class & template) */ /* ---------------------------------------------------------------------- */ - new_tmpl->get("TYPE", s_ds_type); + get_template_attribute("TYPE", s_ds_type); if (!s_ds_type.empty()) { @@ -638,12 +611,6 @@ int Datastore::replace_template( new_ds_type = type; } - /* --- Update the Datastore template --- */ - - delete obj_template; - - obj_template = new_tmpl; - /* ---------------------------------------------------------------------- */ /* Set the TYPE of the Datastore (class & template) */ /* ---------------------------------------------------------------------- */ diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 453f9062f2..27e0c900ce 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -170,7 +170,8 @@ void PoolObjectSQL::clear_template_error_message() int PoolObjectSQL::replace_template( const string& tmpl_str, bool keep_restricted, string& error) { - Template * new_tmpl = get_new_template(); + Template * old_tmpl = 0; + Template * new_tmpl = get_new_template(); if ( new_tmpl == 0 ) { @@ -184,6 +185,11 @@ int PoolObjectSQL::replace_template( return -1; } + if (obj_template != 0) + { + old_tmpl = new Template(*obj_template); + } + if (keep_restricted && new_tmpl->has_restricted()) { new_tmpl->remove_restricted(); @@ -201,6 +207,22 @@ int PoolObjectSQL::replace_template( obj_template = new_tmpl; + if (post_update_template(error) == -1) + { + delete obj_template; + + if (old_tmpl != 0) + { + obj_template = old_tmpl; + } + else + { + obj_template = 0; + } + + return -1; + } + return 0; } @@ -210,7 +232,8 @@ int PoolObjectSQL::replace_template( int PoolObjectSQL::append_template( const string& tmpl_str, bool keep_restricted, string& error) { - Template * new_tmpl = get_new_template(); + Template * old_tmpl = 0; + Template * new_tmpl = get_new_template(); if ( new_tmpl == 0 ) { @@ -231,6 +254,8 @@ int PoolObjectSQL::append_template( if ( obj_template != 0 ) { + old_tmpl = new Template(*obj_template); + obj_template->merge(new_tmpl, error); delete new_tmpl; } @@ -239,6 +264,22 @@ int PoolObjectSQL::append_template( obj_template = new_tmpl; } + if (post_update_template(error) == -1) + { + delete obj_template; + + if (old_tmpl != 0) + { + obj_template = old_tmpl; + } + else + { + obj_template = 0; + } + + return -1; + } + return 0; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 3eabb5a102..8b69363e31 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -221,47 +221,11 @@ error_common: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::replace_template( - const string& tmpl_str, bool keep_restricted, string& error_str) +int VirtualNetwork::post_update_template(string& error) { string new_bridge; bool b_vlan; - /* ---------------------------------------------------------------------- */ - /* Parse & Update VirtualNetwork Template */ - /* ---------------------------------------------------------------------- */ - - Template * new_tmpl = new VirtualNetworkTemplate; - - if ( new_tmpl == 0 ) - { - error_str = "Cannot allocate a new template"; - return -1; - } - - if ( new_tmpl->parse_str_or_xml(tmpl_str, error_str) != 0 ) - { - delete new_tmpl; - return -1; - } - - if (keep_restricted && is_reservation()) - { - new_tmpl->remove_restricted(); - - if (obj_template != 0) - { - obj_template->remove_all_except_restricted(); - - string aux_error; - new_tmpl->merge(obj_template, aux_error); - } - } - - delete obj_template; - - obj_template = new_tmpl; - /* ---------------------------------------------------------------------- */ /* Update Configuration Attributes (class & template) */ /* - PHYDEV */ diff --git a/src/zone/Zone.cc b/src/zone/Zone.cc index aab8718ed8..378ecf7d9a 100644 --- a/src/zone/Zone.cc +++ b/src/zone/Zone.cc @@ -251,50 +251,16 @@ int Zone::from_xml(const string& xml) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ -int Zone::replace_template(const string& tmpl_str, bool keep_restricted, string& error_str) +int Zone::post_update_template(string& error) { - Template * new_tmpl = get_new_template(); - - if ( new_tmpl == 0 ) - { - error_str = "Cannot allocate a new template"; - return -1; - } - - if ( new_tmpl->parse_str_or_xml(tmpl_str, error_str) != 0 ) - { - delete new_tmpl; - return -1; - } - - if (keep_restricted) - { - new_tmpl->remove_restricted(); - - if (obj_template != 0) - { - obj_template->remove_all_except_restricted(); - - string aux_error; - new_tmpl->merge(obj_template, aux_error); - } - } - string new_endpoint; - new_tmpl->get("ENDPOINT", new_endpoint); + get_template_attribute("ENDPOINT", new_endpoint); if (new_endpoint.empty()) { - new_tmpl->replace("ENDPOINT", "-"); + replace_template_attribute("ENDPOINT", "-"); } - if ( obj_template != 0 ) - { - delete obj_template; - } - - obj_template = new_tmpl; - return 0; }