Skip to content

Commit

Permalink
F OpenNebula#1750: Added method to oca for replacing TEMPLATE attributes
Browse files Browse the repository at this point in the history
 F OpenNebula#1750: added method xmlelement to set content

F OpenNebula#1750: replace method works for all one objects
  • Loading branch information
Semedi committed Feb 16, 2018
1 parent 1c30a04 commit fb61588
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/oca/ruby/opennebula/pool_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def chmod_octet(xml_method, octet)
other_m, other_a)
end


# Calls to the corresponding chmod method to modify
# the object's permission bits
# Each [Integer] parameter must be 1 to allow, 0 deny, -1 do not change
Expand Down Expand Up @@ -234,6 +235,26 @@ def to_str

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
Expand Down Expand Up @@ -264,6 +285,25 @@ def self.process_monitoring(xmldoc, root_elem, timestamp_elem, oid, xpath_expres

return hash
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


# Alternative method with better performance for huge number of timestamps.
Expand Down
3 changes: 3 additions & 0 deletions src/oca/ruby/opennebula/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ def user_template_xml
end
end

def replace(opts = {})
super(opts, "USER_TEMPLATE")
end

# Initiates the instance of the VM on the target host.
#
Expand Down
9 changes: 9 additions & 0 deletions src/oca/ruby/opennebula/xml_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def add_element(xpath, elems)
}
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)
Expand Down

0 comments on commit fb61588

Please sign in to comment.