From 2e46a4043f9dddb730071b5c0c8c690f719e15fa Mon Sep 17 00:00:00 2001 From: Sergio Semedi Barranco Date: Mon, 19 Feb 2018 10:14:39 +0100 Subject: [PATCH] F #1750: Added method to oca for replacing TEMPLATE attributes (#1751) F #1750: added method xmlelement to set content F #1750: replace method works for all one objects --- src/oca/ruby/opennebula/pool_element.rb | 21 ++++++++++++++++++++- src/oca/ruby/opennebula/virtual_machine.rb | 3 +++ src/oca/ruby/opennebula/xml_element.rb | 9 +++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/oca/ruby/opennebula/pool_element.rb b/src/oca/ruby/opennebula/pool_element.rb index 404cd9886f..715da1d41f 100644 --- a/src/oca/ruby/opennebula/pool_element.rb +++ b/src/oca/ruby/opennebula/pool_element.rb @@ -234,6 +234,26 @@ module OpenNebula return str end + + # Replace the xml pointed by xpath using a Hash object + # one object will be modified taking hash object pairs + # + # @param [String] xpath + # @param [Hash] options object containing pair key-value + # + # @returns the new xml representation + def replace(opts, xpath = "TEMPLATE") + if self[xpath] + opts.each do |att, value| + xpath_u = xpath+"/#{att}" + docs = retrieve_xmlelements(xpath_u) + if docs.size == 1 + docs[0].set_content(value) + end + end + update(template_like_str(xpath)) + end + end end # Processes the monitoring data in XML returned by OpenNebula @@ -265,7 +285,6 @@ module OpenNebula return hash end - # Alternative method with better performance for huge number of timestamps. # For reasonable amounts of data, the current method is quicker =begin diff --git a/src/oca/ruby/opennebula/virtual_machine.rb b/src/oca/ruby/opennebula/virtual_machine.rb index 4a4cfd23b9..a1749d753b 100644 --- a/src/oca/ruby/opennebula/virtual_machine.rb +++ b/src/oca/ruby/opennebula/virtual_machine.rb @@ -325,6 +325,9 @@ module OpenNebula end end + def replace(opts = {}) + super(opts, "USER_TEMPLATE") + end # Initiates the instance of the VM on the target host. # diff --git a/src/oca/ruby/opennebula/xml_element.rb b/src/oca/ruby/opennebula/xml_element.rb index 689d989e1e..a89dae5765 100644 --- a/src/oca/ruby/opennebula/xml_element.rb +++ b/src/oca/ruby/opennebula/xml_element.rb @@ -145,6 +145,15 @@ module OpenNebula } end + # Update the content of the current doc + def set_content(content) + if NOKOGIRI + @xml.content = content + else + @xml.text = content + end + end + # Gets an array of text from elements extracted # using the XPATH expression passed as filter def retrieve_elements(filter)