Class: IbmPowerHmc::AbstractNonRest Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_power_hmc/schema/parser.rb

Overview

This class is abstract.

HMC generic K2 non-REST object.

Constant Summary collapse

ATTRS =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ AbstractNonRest

Returns a new instance of AbstractNonRest.



85
86
87
88
# File 'lib/ibm_power_hmc/schema/parser.rb', line 85

def initialize(xml)
  @xml = xml
  self.class::ATTRS.each { |varname, xpath| define_attr(varname, xpath) }
end

Instance Attribute Details

#xmlREXML::Document (readonly)

The XML document representing this object.

Returns:

  • (REXML::Document)

    the current value of xml



81
82
83
# File 'lib/ibm_power_hmc/schema/parser.rb', line 81

def xml
  @xml
end

Class Method Details

.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object

XML marshaling of object.

Parameters:

  • attrs (Hash) (defaults to: {})

    The initial properties of the object.

  • namespace (String) (defaults to: UOM_XMLNS)

    The XML namespace to use.

  • version (String) (defaults to: "V1_1_0")

    The XML schema version to use.



96
97
98
99
100
101
102
103
104
105
# File 'lib/ibm_power_hmc/schema/parser.rb', line 96

def self.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0")
  doc = REXML::Document.new("")
  doc.add_element(name.split("::").last, "schemaVersion" => version)
  doc.root.add_namespace(namespace)
  obj = new(doc.root)
  attrs.each do |varname, value|
    obj.send("#{varname}=", value)
  end
  obj
end

Instance Method Details

#collection_of(name, type) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/ibm_power_hmc/schema/parser.rb', line 183

def collection_of(name, type)
  xml.get_elements([name, type].compact.join("/")).map do |elem|
    Module.const_get("IbmPowerHmc::#{elem.name}").new(elem)
  rescue NameError
    nil
  end.compact
end

#create_element(xpath) ⇒ Object

Create a new XML element.

Parameters:

  • xpath (String)

    The XPath of the XML element to create.



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ibm_power_hmc/schema/parser.rb', line 132

def create_element(xpath)
  cur = xml
  xpath.split("/").each do |el|
    p = cur.elements[el]
    if p.nil?
      cur = cur.add_element(el)
    else
      cur = p
    end
  end
end

#marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object

XML marshaling of object.

Parameters:

  • attrs (Hash) (defaults to: {})

    The initial properties of the object.

  • namespace (String) (defaults to: UOM_XMLNS)

    The XML namespace to use.

  • version (String) (defaults to: "V1_1_0")

    The XML schema version to use.



96
97
98
99
100
101
102
103
104
105
# File 'lib/ibm_power_hmc/schema/parser.rb', line 96

def self.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0")
  doc = REXML::Document.new("")
  doc.add_element(name.split("::").last, "schemaVersion" => version)
  doc.root.add_namespace(namespace)
  obj = new(doc.root)
  attrs.each do |varname, value|
    obj.send("#{varname}=", value)
  end
  obj
end

#singleton(xpath, attr = nil) ⇒ String?

Get the text (or the value of a specified attribute) of an XML element.

Examples:

lpar.singleton(“PartitionProcessorConfiguration/*/MaximumVirtualProcessors”).to_i

Parameters:

  • xpath (String)

    The XPath of the XML element.

  • attr (String) (defaults to: nil)

    The name of the attribute.

Returns:

  • (String, nil)

    The text or attribute value of the XML element or nil.



151
152
153
154
155
156
# File 'lib/ibm_power_hmc/schema/parser.rb', line 151

def singleton(xpath, attr = nil)
  elem = xml.elements[xpath]
  return if elem.nil?

  attr.nil? ? elem.text&.strip : elem.attributes[attr]
end

#timestamp(xpath) ⇒ Object



178
179
180
181
# File 'lib/ibm_power_hmc/schema/parser.rb', line 178

def timestamp(xpath)
  # XML element containing a number of milliseconds since the Epoch.
  Time.at(0, singleton(xpath).to_i, :millisecond).utc
end

#to_sObject



158
159
160
161
162
163
164
165
166
# File 'lib/ibm_power_hmc/schema/parser.rb', line 158

def to_s
  str = +"#{self.class.name}:\n"
  self.class::ATTRS.each do |varname, _|
    value = instance_variable_get("@#{varname}")
    value = value.nil? ? "null" : "'#{value}'"
    str << "  #{varname}: #{value}\n"
  end
  str
end

#uuid_from_href(href, index = -1)) ⇒ Object



168
169
170
# File 'lib/ibm_power_hmc/schema/parser.rb', line 168

def uuid_from_href(href, index = -1)
  URI(href).path.split('/')[index]
end


172
173
174
175
176
# File 'lib/ibm_power_hmc/schema/parser.rb', line 172

def uuids_from_links(elem, index = -1)
  xml.get_elements("#{elem}/link[@href]").map do |link|
    uuid_from_href(link.attributes["href"], index)
  end.compact
end