Class: VCloudSdk::Xml::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_vcloud_sdk/xml/wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml, ns = nil, ns_definitions = nil) ⇒ Wrapper

Because we are wrapping individual nodes in a wrapper and turning them into XML docs, we need to preserve the namespace information with each node



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 63

def initialize(xml, ns = nil, ns_definitions = nil)
  if xml.is_a?(Nokogiri::XML::Document)
    @doc = xml
    @root = @doc.root
  else
    @root = xml
  end
  if ns
    @ns = ns
  else
    @ns = @root.namespace
  end

  # Use (server) supplied prefixes defaulting to the preset ones for
  # those not specified.
  @doc_namespaces = ns_definitions.nil? ? [] :
    Array.new(ns_definitions)
  if @root.namespace_definitions
    @doc_namespaces.concat(@root.namespace_definitions)
  end
end

Instance Method Details

#==(other) ⇒ Object



230
231
232
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 230

def ==(other)
  @root.to_s == other.node.to_s
end

#[](attr) ⇒ Object

There seem to be a Nokogiri bug such that attribute’s namespace prefixes are ignored. For example, to return an attribute named vcloud:capacity, use “capacity”, not “vcloud:capacity”.



209
210
211
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 209

def [](attr)
  @root[attr]
end

#[]=(attr, value) ⇒ Object



218
219
220
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 218

def []=(attr, value)
  @root[attr] = value
end

#add_child(child, namespace_prefix = nil, namespace_href = nil, parent = @root) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 243

def add_child(
      child,
      namespace_prefix = nil,
      namespace_href = nil,
      parent = @root)
  if child.is_a? Wrapper
    @root.add_child(child.node)
  elsif child.is_a? String
    node = Nokogiri::XML::Node.new(child, parent)
    set_namespace(node, namespace_prefix, namespace_href)
    parent.add_child(node)
  else
    fail CpiError, "Cannot add child.  Unknown object passed in."
  end
end

#attribute(name) ⇒ Object

get the attribute regardless what namespace the attribute has



214
215
216
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 214

def attribute(name)
  @root.attribute(name)
end

#contentObject



222
223
224
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 222

def content
  @root.content
end

#content=(value) ⇒ Object



226
227
228
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 226

def content=(value)
  @root.content = value
end

#create_child(tag, namespace_prefix = nil, namespace_href = nil) ⇒ Object

Creates a child node but does not add it to the document. Used when a new child node has to be in a specific location or order.



261
262
263
264
265
266
267
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 261

def create_child(tag,
                 namespace_prefix = nil,
                 namespace_href = nil)
  node = Nokogiri::XML::Node.new(tag, @root)
  set_namespace(node, namespace_prefix, namespace_href)
  node
end

#create_qualified_name(name, href) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 177

def create_qualified_name(name, href)
  namespace_wanted = nil
  ns_wanted_no_prefix = nil
  # Do it this way so the namespaces are searched in the order they are
  # added.  The first one is the one closest to the node, while the ones
  # at the document root are the last.
  @doc_namespaces.each do |ns|
    if ns.href == href
      if ns.prefix.nil?
        ns_wanted_no_prefix = ns
      else
        namespace_wanted = ns
        break
      end
    end
  end
  namespace_wanted = ns_wanted_no_prefix unless namespace_wanted
  fail CpiError,
       "Namespace #{href} not found." unless namespace_wanted
  ns_prefix = namespace_wanted.prefix.nil? ? "xmlns" :
    namespace_wanted.prefix
  "#{ns_prefix}:#{name}"
end

#create_xpath_query(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 161

def create_xpath_query(type_name, attrs = nil, only_immediate = false,
    namespace = VCLOUD_NAMESPACE)
  qualified_name = create_qualified_name(type_name, namespace)
  depth_prefix = only_immediate ? nil : ".//"
  if attrs && attrs.length > 0
    attrs_list = []
    attrs.each do |k, v|
      attrs_list.push(%Q[@#{k}="#{v}"])
    end

    "#{depth_prefix}#{qualified_name}[#{attrs_list.join(" and ")}]"
  else
    "#{depth_prefix}#{qualified_name}"
  end
end

#doc_namespacesObject



85
86
87
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 85

def doc_namespaces
  @doc_namespaces
end


132
133
134
135
136
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 132

def edit_link
  get_nodes(XML_TYPE[:LINK],
            { rel: XML_TYPE[:EDIT] },
            true).first
end

#get_nodes(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object



201
202
203
204
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 201

def get_nodes(type_name, attrs = nil, only_immediate = false,
    namespace = VCLOUD_NAMESPACE)
  xpath(create_xpath_query(type_name, attrs, only_immediate, namespace))
end

#hrefObject



93
94
95
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 93

def href
  @root["href"]
end

#href=(href) ⇒ Object



97
98
99
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 97

def href=(href)
  @root["href"] = href
end

#href_idObject



101
102
103
104
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 101

def href_id
  # Sample href: "https://10.147.0.0/api/org/a3783d64-0b9b-42d6-93cf-23bb08ec5520"
  URI.parse(href).path.split('/')[-1]
end

#nameObject



106
107
108
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 106

def name
  @root["name"]
end

#name=(name) ⇒ Object



110
111
112
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 110

def name=(name)
  @root["name"] = name
end


144
145
146
147
148
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 144

def power_off_link
  get_nodes(XML_TYPE[:LINK],
            { rel: "power:powerOff"},
            true).first
end


138
139
140
141
142
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 138

def power_on_link
  get_nodes(XML_TYPE[:LINK],
            { rel: "power:powerOn"},
            true).first
end


126
127
128
129
130
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 126

def remove_link
  get_nodes(XML_TYPE[:LINK],
            { rel: XML_TYPE[:REMOVE] },
            true).first
end

#running_tasksObject



156
157
158
159
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 156

def running_tasks
  get_nodes(XML_TYPE[:TASK],
            { status: TASK_STATUS[:RUNNING] })
end

#to_sObject



234
235
236
237
238
239
240
241
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 234

def to_s
  add_namespaces
    .to_xml
    .each_line
    .reduce("") do |xml, line|
    xml.concat(line.sub(/^\s+$/, ""))
  end
end

#typeObject



118
119
120
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 118

def type
  @root["type"]
end

#type=(type) ⇒ Object



122
123
124
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 122

def type=(type)
  @root["type"] = type
end


150
151
152
153
154
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 150

def undeploy_link
  get_nodes(XML_TYPE[:LINK],
            { rel: "undeploy" },
            true).first
end

#urnObject



114
115
116
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 114

def urn
  @root["id"]
end

#xpath(*args) ⇒ Object



89
90
91
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 89

def xpath(*args)
  WrapperFactory.wrap_nodes(@root.xpath(*args), @ns, @doc_namespaces)
end