ruby_vcloud_sdk 0.4.8 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +296 -0
  3. data/lib/ruby_vcloud_sdk.rb +0 -4
  4. data/lib/ruby_vcloud_sdk/catalog.rb +377 -0
  5. data/lib/ruby_vcloud_sdk/catalog_item.rb +26 -0
  6. data/lib/ruby_vcloud_sdk/client.rb +25 -882
  7. data/lib/ruby_vcloud_sdk/config.rb +1 -7
  8. data/lib/ruby_vcloud_sdk/connection/connection.rb +114 -54
  9. data/lib/ruby_vcloud_sdk/cpu.rb +11 -0
  10. data/lib/ruby_vcloud_sdk/disk.rb +55 -0
  11. data/lib/ruby_vcloud_sdk/edge_gateway.rb +32 -0
  12. data/lib/ruby_vcloud_sdk/infrastructure.rb +135 -0
  13. data/lib/ruby_vcloud_sdk/ip_ranges.rb +95 -0
  14. data/lib/ruby_vcloud_sdk/memory.rb +11 -0
  15. data/lib/ruby_vcloud_sdk/network.rb +32 -0
  16. data/lib/ruby_vcloud_sdk/powerable.rb +78 -0
  17. data/lib/ruby_vcloud_sdk/resources.rb +13 -0
  18. data/lib/ruby_vcloud_sdk/session.rb +46 -0
  19. data/lib/ruby_vcloud_sdk/vapp.rb +122 -0
  20. data/lib/ruby_vcloud_sdk/vdc.rb +210 -0
  21. data/lib/ruby_vcloud_sdk/vdc_storage_profile.rb +23 -0
  22. data/lib/ruby_vcloud_sdk/version.rb +1 -1
  23. data/lib/ruby_vcloud_sdk/vm.rb +132 -0
  24. data/lib/ruby_vcloud_sdk/xml/constants.rb +6 -4
  25. data/lib/ruby_vcloud_sdk/xml/wrapper.rb +109 -30
  26. data/lib/ruby_vcloud_sdk/xml/wrapper_classes.rb +45 -2
  27. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/admin_catalog.rb +13 -4
  28. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/allocated_ip_addresses.rb +9 -0
  29. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/catalog_item.rb +6 -14
  30. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/disk.rb +24 -14
  31. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/disk_attach_or_detach_params.rb +0 -2
  32. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/disk_create_params.rb +1 -3
  33. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/edge_gateway.rb +13 -0
  34. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/gateway_interface.rb +22 -0
  35. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/hard_disk_item_wrapper.rb +4 -4
  36. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/ip_address.rb +10 -0
  37. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/ip_range.rb +13 -0
  38. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/ip_ranges.rb +9 -0
  39. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/ip_scope.rb +6 -32
  40. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/item.rb +1 -7
  41. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/link.rb +14 -0
  42. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/media.rb +4 -20
  43. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/media_insert_or_eject_params.rb +1 -3
  44. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/network_connection_section.rb +0 -4
  45. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/nic_item_wrapper.rb +0 -2
  46. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/org.rb +44 -0
  47. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/org_vdc_network.rb +11 -3
  48. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/query_result_records.rb +14 -0
  49. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/recompose_vapp_params.rb +42 -0
  50. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/session.rb +5 -5
  51. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/supported_versions.rb +19 -0
  52. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/task.rb +1 -1
  53. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/upload_vapp_template_params.rb +2 -4
  54. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vapp.rb +13 -22
  55. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vapp_template.rb +0 -2
  56. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vcloud.rb +5 -3
  57. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vdc.rb +72 -15
  58. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/virtual_hardware_section.rb +8 -6
  59. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vm.rb +29 -25
  60. data/lib/ruby_vcloud_sdk/xml/wrapper_classes/vms.rb +12 -0
  61. data/lib/ruby_vcloud_sdk/xml/xml_templates/AdminCatalog.xml +6 -1
  62. data/lib/ruby_vcloud_sdk/xml/xml_templates/RecomposeVAppParams.xml +16 -1
  63. metadata +116 -233
  64. data/README +0 -1
  65. data/Rakefile +0 -50
  66. data/lib/ruby_vcloud_sdk/util.rb +0 -21
  67. data/spec/assets/admin_org_response.xml +0 -75
  68. data/spec/assets/catalog_add_item_response.xml +0 -8
  69. data/spec/assets/catalog_add_vapp_request.xml +0 -5
  70. data/spec/assets/catalog_item_added_response.xml +0 -19
  71. data/spec/assets/catalog_response.xml +0 -19
  72. data/spec/assets/existing_media_busy_response.xml +0 -19
  73. data/spec/assets/existing_media_catalog_item.xml +0 -8
  74. data/spec/assets/existing_media_delete_task_done.xml +0 -6
  75. data/spec/assets/existing_media_done_response.xml +0 -11
  76. data/spec/assets/existing_vapp_resolver_response.xml +0 -3
  77. data/spec/assets/existing_vapp_template_catalog_resolver_response.xml +0 -3
  78. data/spec/assets/existing_vapp_template_instantiate_response.xml +0 -20
  79. data/spec/assets/existing_vapp_template_instantiate_task_error_response.xml +0 -7
  80. data/spec/assets/existing_vapp_template_instantiate_task_start_response.xml +0 -7
  81. data/spec/assets/existing_vapp_template_instantiate_task_success_response.xml +0 -7
  82. data/spec/assets/existing_vapp_template_item_response.xml +0 -8
  83. data/spec/assets/existing_vapp_template_ready_response.xml +0 -79
  84. data/spec/assets/finalize_upload_task_done_response.xml +0 -7
  85. data/spec/assets/finalize_upload_task_response.xml +0 -7
  86. data/spec/assets/indy_disk_attach_request.xml +0 -3
  87. data/spec/assets/indy_disk_attach_task.xml +0 -6
  88. data/spec/assets/indy_disk_attach_task_error.xml +0 -6
  89. data/spec/assets/indy_disk_create_error.xml +0 -1
  90. data/spec/assets/indy_disk_create_request.xml +0 -4
  91. data/spec/assets/indy_disk_create_response.xml +0 -19
  92. data/spec/assets/indy_disk_delete_task.xml +0 -6
  93. data/spec/assets/indy_disk_detach_request.xml +0 -3
  94. data/spec/assets/indy_disk_detach_task.xml +0 -6
  95. data/spec/assets/indy_disk_response.xml +0 -11
  96. data/spec/assets/instantiated_suspended_vapp_response.xml +0 -209
  97. data/spec/assets/instantiated_vapp_delelete_done_task.xml +0 -5
  98. data/spec/assets/instantiated_vapp_delelete_running_task.xml +0 -6
  99. data/spec/assets/instantiated_vapp_network_config_add_network_request.xml +0 -36
  100. data/spec/assets/instantiated_vapp_network_config_modify_network_task_success.xml +0 -6
  101. data/spec/assets/instantiated_vapp_network_config_remove_network_request.xml +0 -6
  102. data/spec/assets/instantiated_vapp_network_config_section_response.xml +0 -17
  103. data/spec/assets/instantiated_vapp_off_response.xml +0 -206
  104. data/spec/assets/instantiated_vapp_on_response.xml +0 -205
  105. data/spec/assets/instantiated_vapp_power_task_running.xml +0 -6
  106. data/spec/assets/instantiated_vapp_power_task_success.xml +0 -6
  107. data/spec/assets/instantiated_vapp_response.xml +0 -205
  108. data/spec/assets/instantiated_vm_change_task_running.xml +0 -6
  109. data/spec/assets/instantiated_vm_change_task_success.xml +0 -6
  110. data/spec/assets/instantiated_vm_cpu_response.xml +0 -11
  111. data/spec/assets/instantiated_vm_insert_media_task_done.xml +0 -6
  112. data/spec/assets/instantiated_vm_memory_response.xml +0 -11
  113. data/spec/assets/instantiated_vm_modify_task_running.xml +0 -6
  114. data/spec/assets/instantiated_vm_modify_task_success.xml +0 -5
  115. data/spec/assets/instantiated_vm_network_section_response.xml +0 -11
  116. data/spec/assets/instantiated_vm_response.xml +0 -149
  117. data/spec/assets/media_add_to_catalog_request.xml +0 -5
  118. data/spec/assets/media_add_to_catalog_response.xml +0 -8
  119. data/spec/assets/media_delete_task_done.xml +0 -6
  120. data/spec/assets/media_upload_pending_response.xml +0 -13
  121. data/spec/assets/media_upload_request.xml +0 -2
  122. data/spec/assets/metadata_set_request.xml +0 -3
  123. data/spec/assets/metadata_set_task_done.xml +0 -6
  124. data/spec/assets/org_network_response.xml +0 -22
  125. data/spec/assets/reconfigure_vm_request.xml +0 -133
  126. data/spec/assets/reconfigure_vm_task.xml +0 -8
  127. data/spec/assets/session.xml +0 -7
  128. data/spec/assets/test-config.yml +0 -38
  129. data/spec/assets/undeploy_params.xml +0 -1
  130. data/spec/assets/vapp_template_catalog_resolver_response.xml +0 -3
  131. data/spec/assets/vapp_template_delelete_done_task.xml +0 -5
  132. data/spec/assets/vapp_template_delelete_running_task.xml +0 -6
  133. data/spec/assets/vapp_template_instantiate_request.xml +0 -8
  134. data/spec/assets/vapp_template_instantiate_with_locality_request.xml +0 -14
  135. data/spec/assets/vapp_template_no_disk_response.xml +0 -27
  136. data/spec/assets/vapp_template_ready_response.xml +0 -79
  137. data/spec/assets/vapp_template_upload_complete.xml +0 -28
  138. data/spec/assets/vapp_template_upload_failed.xml +0 -28
  139. data/spec/assets/vapp_template_upload_request.xml +0 -4
  140. data/spec/assets/vapp_template_upload_response.xml +0 -25
  141. data/spec/assets/vcloud_response.xml +0 -56
  142. data/spec/assets/vdc_response.xml +0 -57
  143. data/spec/spec_helper.rb +0 -107
  144. data/spec/unit/client_response.rb +0 -700
  145. data/spec/unit/client_spec.rb +0 -1152
@@ -0,0 +1,23 @@
1
+ module VCloudSdk
2
+
3
+ class VdcStorageProfile
4
+ attr_reader :name
5
+
6
+ def initialize(storage_profile_xml_obj)
7
+ @storage_profile_xml_obj = storage_profile_xml_obj
8
+ @name = @storage_profile_xml_obj[:name]
9
+ @storage_used_mb = @storage_profile_xml_obj[:storageUsedMB].to_i
10
+ @storage_limit_mb = @storage_profile_xml_obj[:storageLimitMB].to_i
11
+ @vdc_name = @storage_profile_xml_obj[:vdcName]
12
+ end
13
+
14
+ # Return storageLimitMB - storageUsedMB
15
+ # Return -1 if 'storageLimitMB' is 0
16
+ def available_storage
17
+ return -1 if @storage_limit_mb == 0
18
+
19
+ @storage_limit_mb - @storage_used_mb
20
+ end
21
+ end
22
+
23
+ end
@@ -1,3 +1,3 @@
1
1
  module VCloudSdk
2
- VERSION = "0.4.8"
2
+ VERSION = '0.5.0'
3
3
  end
@@ -0,0 +1,132 @@
1
+ require "forwardable"
2
+ require_relative "infrastructure"
3
+ require_relative "powerable"
4
+
5
+ module VCloudSdk
6
+ class VM
7
+ include Infrastructure
8
+ include Powerable
9
+
10
+ extend Forwardable
11
+ def_delegator :entity_xml, :name
12
+
13
+ def initialize(session, link)
14
+ @session = session
15
+ @link = link
16
+ end
17
+
18
+ def href
19
+ @link
20
+ end
21
+
22
+ def independent_disks
23
+ hardware_section = entity_xml.hardware_section
24
+ disks = []
25
+ hardware_section.hard_disks.each do |disk|
26
+ disk_link = disk.host_resource.attribute("disk")
27
+ unless disk_link.nil?
28
+ disks << VCloudSdk::Disk.new(@session, disk_link.to_s)
29
+ end
30
+ end
31
+ disks
32
+ end
33
+
34
+ def list_disks
35
+ entity_xml.hardware_section.hard_disks.map do |disk|
36
+ disk_link = disk.host_resource.attribute("disk")
37
+ if disk_link.nil?
38
+ disk.element_name
39
+ else
40
+ "#{disk.element_name} (#{VCloudSdk::Disk.new(@session, disk_link.to_s).name})"
41
+ end
42
+ end
43
+ end
44
+
45
+ def attach_disk(disk)
46
+ fail CloudError,
47
+ "Disk '#{disk.name}' of link #{disk.href} is attached to VM '#{disk.vm.name}'" if disk.attached?
48
+
49
+ task = connection.post(entity_xml.attach_disk_link.href,
50
+ disk_attach_or_detach_params(disk),
51
+ Xml::MEDIA_TYPE[:DISK_ATTACH_DETACH_PARAMS])
52
+ task = monitor_task(task)
53
+
54
+ Config.logger.info "Disk '#{disk.name}' is attached to VM '#{name}'"
55
+ task
56
+ end
57
+
58
+ def detach_disk(disk)
59
+ parent_vapp = vapp
60
+ if parent_vapp.status == "SUSPENDED"
61
+ fail VmSuspendedError,
62
+ "vApp #{parent_vapp.name} suspended, discard state before detaching disk."
63
+ end
64
+
65
+ unless (vm = disk.vm).href == href
66
+ fail CloudError,
67
+ "Disk '#{disk.name}' is attached to other VM - name: '#{vm.name}', link '#{vm.href}'"
68
+ end
69
+
70
+ task = connection.post(entity_xml.detach_disk_link.href,
71
+ disk_attach_or_detach_params(disk),
72
+ Xml::MEDIA_TYPE[:DISK_ATTACH_DETACH_PARAMS])
73
+ task = monitor_task(task)
74
+
75
+ Config.logger.info "Disk '#{disk.name}' is detached from VM '#{name}'"
76
+ task
77
+ end
78
+
79
+ def insert_media(catalog_name, media_file_name)
80
+ catalog = find_catalog_by_name(catalog_name)
81
+ media = catalog.find_item(media_file_name, Xml::MEDIA_TYPE[:MEDIA])
82
+
83
+ vm = entity_xml
84
+ media_xml = connection.get(media.href)
85
+ Config.logger.info("Inserting media #{media_xml.name} into VM #{vm.name}")
86
+
87
+ wait_for_running_tasks(media_xml, "Media '#{media_xml.name}'")
88
+
89
+ task = connection.post(vm.insert_media_link.href,
90
+ media_insert_or_eject_params(media),
91
+ Xml::MEDIA_TYPE[:MEDIA_INSERT_EJECT_PARAMS])
92
+ monitor_task(task)
93
+ end
94
+
95
+ def eject_media(catalog_name, media_file_name)
96
+ catalog = find_catalog_by_name(catalog_name)
97
+ media = catalog.find_item(media_file_name, Xml::MEDIA_TYPE[:MEDIA])
98
+
99
+ vm = entity_xml
100
+ media_xml = connection.get(media.href)
101
+ Config.logger.info("Ejecting media #{media_xml.name} from VM #{vm.name}")
102
+
103
+ wait_for_running_tasks(media_xml, "Media '#{media_xml.name}'")
104
+
105
+ task = connection.post(vm.eject_media_link.href,
106
+ media_insert_or_eject_params(media),
107
+ Xml::MEDIA_TYPE[:MEDIA_INSERT_EJECT_PARAMS])
108
+ monitor_task(task)
109
+ end
110
+
111
+ private
112
+
113
+ def disk_attach_or_detach_params(disk)
114
+ Xml::WrapperFactory
115
+ .create_instance("DiskAttachOrDetachParams")
116
+ .tap do |params|
117
+ params.disk_href = disk.href
118
+ end
119
+ end
120
+
121
+ def vapp
122
+ vapp_link = entity_xml.vapp_link
123
+ VCloudSdk::VApp.new(@session, vapp_link.href)
124
+ end
125
+
126
+ def media_insert_or_eject_params(media)
127
+ Xml::WrapperFactory.create_instance("MediaInsertOrEjectParams").tap do |params|
128
+ params.media_href = media.href
129
+ end
130
+ end
131
+ end
132
+ end
@@ -24,7 +24,8 @@ module VCloudSdk
24
24
  :ADDRESS_ON_PARENT => "AddressOnParent",
25
25
  :ADDRESS => "Address",
26
26
  :CONNECTION => "Connection",
27
- :PARENT => "Parent"
27
+ :PARENT => "Parent",
28
+ :ELEMENT_NAME => "ElementName",
28
29
  }
29
30
 
30
31
  RESOURCE_SUB_TYPE = {
@@ -174,7 +175,6 @@ module VCloudSdk
174
175
 
175
176
  ADMIN_MEDIA_TYPE = {
176
177
  :ADMIN_MEDIA_TYPE_PREFIX => "application/vnd.vmware.admin.",
177
- :VCLOUD => "application/vnd.vmware.admin.vcloud+xml",
178
178
  :PROVIDER_VDC => "application/vnd.vmware.admin.providervdc+xml",
179
179
  :ADMIN_VDC => "application/vnd.vmware.admin.vdc+xml",
180
180
  :VDC => "application/vnd.vmware.vcloud.vdc+xml",
@@ -185,14 +185,13 @@ module VCloudSdk
185
185
  :SYSTEM_ADMIN_ORGANIZATION =>
186
186
  "application/vnd.vmware.admin.systemOrganization+xml",
187
187
  :ADMIN_ORGANIZATION => "application/vnd.vmware.admin.organization+xml",
188
- :ORGANIZATION => "application/vnd.vmware.vcloud.org+xml",
189
188
  :TASKS_LIST => "application/vnd.vmware.vcloud.tasksList+xml",
190
189
  :USER => "application/vnd.vmware.admin.user+xml",
191
190
  :GROUP => "application/vnd.vmware.admin.group+xml",
192
191
  :ROLE => "application/vnd.vmware.admin.role+xml",
193
192
  :RIGHT => "application/vnd.vmware.admin.right+xml",
194
193
  :ADMIN_CATALOG => "application/vnd.vmware.admin.catalog+xml",
195
- :CATALOG => "application/vnd.vmware.vcloud.catalog+xml",
194
+ :CATALOG => "application/vnd.vmware.admin.catalog+xml",
196
195
  :ADMIN_CATALOG_ITEM => "application/vnd.vmware.admin.catalogItem+xml",
197
196
  :CATALOG_ITEM => "application/vnd.vmware.vcloud.catalogItem+xml",
198
197
  :ADMIN_NETWORK => "application/vnd.vmware.admin.network+xml",
@@ -215,6 +214,7 @@ module VCloudSdk
215
214
  }
216
215
 
217
216
  XML_TYPE = {
217
+ :ADD => "add",
218
218
  :ADMINALLOCATEDEXTIPRECORD => "AdminAllocatedExtIpRecord",
219
219
  :ADMINCATALOG => "AdminCatalog",
220
220
  :ADMINCATALOGITEMRECORD => "AdminCatalogItemRecord",
@@ -265,6 +265,7 @@ module VCloudSdk
265
265
  :DEPLOYVAPPPARAMS => "DeployVAppParams",
266
266
  :DHCPSERVICE => "DhcpService",
267
267
  :DVSWITCHRECORD => "DvSwitchRecord",
268
+ :EDIT => "edit",
268
269
  :ENTITY => "Entity",
269
270
  :ERROR => "Error",
270
271
  :EVENTRECORD => "EventRecord",
@@ -341,6 +342,7 @@ module VCloudSdk
341
342
  :REFERENCE => "Reference",
342
343
  :REFERENCES => "References",
343
344
  :RELOCATEPARAMS => "RelocateParams",
345
+ :REMOVE => "remove",
344
346
  :RESOURCEENTITY => "ResourceEntity",
345
347
  :RESOURCEPOOLRECORD => "ResourcePoolRecord",
346
348
  :RIGHT => "Right",
@@ -2,7 +2,6 @@ require "nokogiri"
2
2
 
3
3
  module VCloudSdk
4
4
  module Xml
5
-
6
5
  class WrapperFactory
7
6
  @@xml_dictionary = {}
8
7
  class << self
@@ -20,8 +19,11 @@ module VCloudSdk
20
19
  end
21
20
 
22
21
  def wrap_nodes(nodes, ns, namespace_defintions)
23
- nodes.collect { |node| WrapperFactory.wrap_node(node, ns,
24
- namespace_defintions) }
22
+ nodes.map do |node|
23
+ WrapperFactory.wrap_node(node,
24
+ ns,
25
+ namespace_defintions)
26
+ end
25
27
  end
26
28
 
27
29
  # TODO: We might run into a bug later if there are ever XML node types
@@ -38,11 +40,11 @@ module VCloudSdk
38
40
  def create_instance(type_name, ns = nil, namespace_defintions = nil,
39
41
  *args)
40
42
  xml = @@xml_dictionary[type_name]
41
- if (xml)
43
+ if xml
42
44
  wrap_document(xml, ns, namespace_defintions, *args)
43
45
  else
44
- raise CpiError,
45
- "XML type #{type_name} not found in xml_templates dir."
46
+ fail CpiError,
47
+ "XML type #{type_name} not found in xml_templates dir."
46
48
  end
47
49
  end
48
50
 
@@ -73,7 +75,7 @@ module VCloudSdk
73
75
 
74
76
  # Use (server) supplied prefixes defaulting to the preset ones for
75
77
  # those not specified.
76
- @doc_namespaces = ns_definitions.nil? ? Array.new :
78
+ @doc_namespaces = ns_definitions.nil? ? [] :
77
79
  Array.new(ns_definitions)
78
80
  if @root.namespace_definitions
79
81
  @doc_namespaces.concat(@root.namespace_definitions)
@@ -85,17 +87,30 @@ module VCloudSdk
85
87
  end
86
88
 
87
89
  def xpath(*args)
88
- WrapperFactory::wrap_nodes(@root.xpath(*args), @ns, @doc_namespaces)
90
+ WrapperFactory.wrap_nodes(@root.xpath(*args), @ns, @doc_namespaces)
89
91
  end
90
92
 
91
93
  def href
92
94
  @root["href"]
93
95
  end
94
96
 
97
+ def href=(href)
98
+ @root["href"] = href
99
+ end
100
+
101
+ def href_id
102
+ # Sample href: "https://10.146.21.135/api/org/a3783d64-0b9b-42d6-93cf-23bb08ec5520"
103
+ URI.parse(href).path.split('/')[-1]
104
+ end
105
+
95
106
  def name
96
107
  @root["name"]
97
108
  end
98
109
 
110
+ def name=(name)
111
+ @root["name"] = name
112
+ end
113
+
99
114
  def urn
100
115
  @root["id"]
101
116
  end
@@ -104,13 +119,52 @@ module VCloudSdk
104
119
  @root["type"]
105
120
  end
106
121
 
122
+ def type=(type)
123
+ @root["type"] = type
124
+ end
125
+
126
+ def remove_link
127
+ get_nodes(XML_TYPE[:LINK],
128
+ { rel: XML_TYPE[:REMOVE] },
129
+ true).first
130
+ end
131
+
132
+ def edit_link
133
+ get_nodes(XML_TYPE[:LINK],
134
+ { rel: XML_TYPE[:EDIT] },
135
+ true).first
136
+ end
137
+
138
+ def power_on_link
139
+ get_nodes(XML_TYPE[:LINK],
140
+ { rel: "power:powerOn"},
141
+ true).first
142
+ end
143
+
144
+ def power_off_link
145
+ get_nodes(XML_TYPE[:LINK],
146
+ { rel: "power:powerOff"},
147
+ true).first
148
+ end
149
+
150
+ def undeploy_link
151
+ get_nodes(XML_TYPE[:LINK],
152
+ { rel: "undeploy" },
153
+ true).first
154
+ end
155
+
156
+ def running_tasks
157
+ get_nodes(XML_TYPE[:TASK],
158
+ { status: TASK_STATUS[:RUNNING] })
159
+ end
160
+
107
161
  def create_xpath_query(type_name, attrs = nil, only_immediate = false,
108
162
  namespace = VCLOUD_NAMESPACE)
109
163
  qualified_name = create_qualified_name(type_name, namespace)
110
164
  depth_prefix = only_immediate ? nil : ".//"
111
165
  if attrs && attrs.length > 0
112
166
  attrs_list = []
113
- attrs.each do |k,v|
167
+ attrs.each do |k, v|
114
168
  attrs_list.push(%Q[@#{k}="#{v}"])
115
169
  end
116
170
 
@@ -129,7 +183,7 @@ module VCloudSdk
129
183
  @doc_namespaces.each do |ns|
130
184
  if ns.href == href
131
185
  if ns.prefix.nil?
132
- ns_wanted_no_prefix = ns;
186
+ ns_wanted_no_prefix = ns
133
187
  else
134
188
  namespace_wanted = ns
135
189
  break
@@ -137,7 +191,8 @@ module VCloudSdk
137
191
  end
138
192
  end
139
193
  namespace_wanted = ns_wanted_no_prefix unless namespace_wanted
140
- raise CpiError, "Namespace #{href} not found." unless namespace_wanted
194
+ fail CpiError,
195
+ "Namespace #{href} not found." unless namespace_wanted
141
196
  ns_prefix = namespace_wanted.prefix.nil? ? "xmlns" :
142
197
  namespace_wanted.prefix
143
198
  "#{ns_prefix}:#{name}"
@@ -155,6 +210,11 @@ module VCloudSdk
155
210
  @root[attr]
156
211
  end
157
212
 
213
+ # get the attribute regardless what namespace the attribute has
214
+ def attribute(name)
215
+ @root.attribute(name)
216
+ end
217
+
158
218
  def []=(attr, value)
159
219
  @root[attr] = value
160
220
  end
@@ -172,37 +232,38 @@ module VCloudSdk
172
232
  end
173
233
 
174
234
  def to_s
175
- add_namespaces.to_xml.each_line.inject("") {
176
- |xml, line| xml.concat(line.sub(/^\s+$/, "")) }
235
+ add_namespaces
236
+ .to_xml
237
+ .each_line
238
+ .reduce("") do |xml, line|
239
+ xml.concat(line.sub(/^\s+$/, ""))
240
+ end
177
241
  end
178
242
 
179
- def add_child(child, namespace_prefix = nil, namespace_href = nil,
180
- parent = @root)
243
+ def add_child(
244
+ child,
245
+ namespace_prefix = nil,
246
+ namespace_href = nil,
247
+ parent = @root)
181
248
  if child.is_a? Wrapper
182
249
  @root.add_child(child.node)
183
250
  elsif child.is_a? String
184
251
  node = Nokogiri::XML::Node.new(child, parent)
185
- if (namespace_prefix.nil? ^ namespace_href.nil?)
186
- raise CpiError,
187
- "Namespace prefix must both be nil or defined together."
188
- end
189
- # This is a little more cumbersome but Nokogiri has problems
190
- # figuring out the right namespace prefix otherwise
191
- if namespace_prefix
192
- ns = node.add_namespace_definition(namespace_prefix,
193
- namespace_href)
194
- node.namespace = ns
195
- end
252
+ set_namespace(node, namespace_prefix, namespace_href)
196
253
  parent.add_child(node)
197
254
  else
198
- raise CpiError, "Cannot add child. Unknown object passed in."
255
+ fail CpiError, "Cannot add child. Unknown object passed in."
199
256
  end
200
257
  end
201
258
 
202
259
  # Creates a child node but does not add it to the document. Used when
203
260
  # a new child node has to be in a specific location or order.
204
- def create_child(tag)
205
- Nokogiri::XML::Node.new(tag, @root)
261
+ def create_child(tag,
262
+ namespace_prefix = nil,
263
+ namespace_href = nil)
264
+ node = Nokogiri::XML::Node.new(tag, @root)
265
+ set_namespace(node, namespace_prefix, namespace_href)
266
+ node
206
267
  end
207
268
 
208
269
  protected
@@ -236,7 +297,25 @@ module VCloudSdk
236
297
  clone.namespace = default_ns
237
298
  clone
238
299
  end
239
- end
240
300
 
301
+ def set_namespace(node, namespace_prefix, namespace_href)
302
+ if namespace_prefix.nil? && namespace_href.nil?
303
+ return
304
+ elsif namespace_prefix.nil? || namespace_href.nil?
305
+ fail CpiError,
306
+ "Namespace prefix must both be nil or defined together."
307
+ end
308
+
309
+ if !node.namespace.nil? &&
310
+ node.namespace.prefix == namespace_prefix &&
311
+ node.namespace.href == namespace_href
312
+ return
313
+ end
314
+
315
+ ns = node.add_namespace_definition(namespace_prefix,
316
+ namespace_href)
317
+ node.namespace = ns
318
+ end
319
+ end
241
320
  end
242
321
  end