vcloud-core 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/.gitignore +1 -1
  2. data/CHANGELOG.md +12 -0
  3. data/lib/vcloud/core.rb +2 -1
  4. data/lib/vcloud/core/api_interface.rb +35 -0
  5. data/lib/vcloud/core/compute_metadata.rb +1 -1
  6. data/lib/vcloud/core/edge_gateway.rb +2 -2
  7. data/lib/vcloud/core/fog.rb +40 -0
  8. data/lib/vcloud/core/fog/fog_constants.rb +35 -0
  9. data/lib/vcloud/core/fog/login.rb +40 -0
  10. data/lib/vcloud/core/fog/model_interface.rb +41 -0
  11. data/lib/vcloud/core/fog/service_interface.rb +253 -0
  12. data/lib/vcloud/core/login_cli.rb +1 -1
  13. data/lib/vcloud/core/metadata_helper.rb +1 -1
  14. data/lib/vcloud/core/org_vdc_network.rb +3 -3
  15. data/lib/vcloud/core/query_runner.rb +1 -1
  16. data/lib/vcloud/core/vapp.rb +6 -6
  17. data/lib/vcloud/core/vapp_template.rb +1 -1
  18. data/lib/vcloud/core/vdc.rb +1 -1
  19. data/lib/vcloud/core/version.rb +1 -1
  20. data/lib/vcloud/core/vm.rb +9 -9
  21. data/lib/vcloud/fog.rb +2 -39
  22. data/lib/vcloud/temporary_fog_classes.rb +11 -0
  23. data/spec/integration/{fog → core/fog}/login_manual.rb +1 -1
  24. data/spec/integration/{fog → core/fog}/login_spec.rb +13 -9
  25. data/spec/integration/core/vm_spec.rb +1 -1
  26. data/spec/support/integration_helper.rb +1 -1
  27. data/spec/vcloud/core/edge_gateway_spec.rb +1 -1
  28. data/spec/vcloud/{fog → core/fog}/fog_model_interface_spec.rb +2 -2
  29. data/spec/vcloud/{fog → core/fog}/login_spec.rb +4 -4
  30. data/spec/vcloud/core/fog/service_interface_spec.rb +53 -0
  31. data/spec/vcloud/{fog_spec.rb → core/fog_spec.rb} +2 -2
  32. data/spec/vcloud/core/login_cli_spec.rb +6 -6
  33. data/spec/vcloud/core/org_vdc_network_spec.rb +1 -1
  34. data/spec/vcloud/core/query_runner_spec.rb +1 -1
  35. data/spec/vcloud/core/vapp_spec.rb +1 -1
  36. data/spec/vcloud/core/vapp_template_spec.rb +1 -1
  37. data/spec/vcloud/core/vdc_spec.rb +1 -1
  38. data/spec/vcloud/core/vm_spec.rb +1 -1
  39. metadata +22 -21
  40. data/jenkins_integration_tests.sh +0 -5
  41. data/lib/vcloud/fog/content_types.rb +0 -18
  42. data/lib/vcloud/fog/login.rb +0 -38
  43. data/lib/vcloud/fog/model_interface.rb +0 -33
  44. data/lib/vcloud/fog/relation.rb +0 -8
  45. data/lib/vcloud/fog/service_interface.rb +0 -245
  46. data/spec/vcloud/fog/service_interface_spec.rb +0 -51
data/.gitignore CHANGED
@@ -1,5 +1,5 @@
1
1
  /Gemfile.lock
2
2
  /coverage/
3
- .idea
4
3
  spec/integration/vcloud_tools_testing_config.yaml
5
4
  .bundle
5
+ .yardoc/
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 0.8.0 (2014-08-07)
2
+
3
+ API changes:
4
+
5
+ - Create new `Vcloud::Core::ApiInterface` that delegates calls to the fog
6
+ service interface and model interface, so that gems that depend on
7
+ vCloud Core do not need to know about the inner workings of fog, or
8
+ about fog at all.
9
+ - Move fog classes into Core. This API change is not backwards-compatible.
10
+ - Mark the fog classes `@api private` to clarify that they do not form
11
+ part of the public API.
12
+
1
13
  ## 0.7.0 (2014-07-28)
2
14
 
3
15
  Features:
data/lib/vcloud/core.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'open3'
2
- require 'vcloud/fog'
2
+ require 'vcloud/core/fog'
3
+ require 'vcloud/core/api_interface'
3
4
 
4
5
  require 'vcloud/core/version'
5
6
 
@@ -0,0 +1,35 @@
1
+ module Vcloud
2
+ module Core
3
+
4
+ # Public interface to allow direct access to the API
5
+ # if functionality does not exist in Core
6
+ class ApiInterface
7
+
8
+ def initialize
9
+ @fog_service_interface = Vcloud::Core::Fog::ServiceInterface.new
10
+ @fog_model_interface = Vcloud::Core::Fog::ModelInterface.new
11
+ end
12
+
13
+ def get_vapp_by_name_and_vdc_name(name, vdc_name)
14
+ @fog_service_interface.get_vapp_by_name_and_vdc_name(name, vdc_name)
15
+ end
16
+
17
+ def get_vapp(id)
18
+ @fog_service_interface.get_vapp(id)
19
+ end
20
+
21
+ def delete_vapp(id)
22
+ @fog_service_interface.delete_vapp(id)
23
+ end
24
+
25
+ def delete_network(id)
26
+ @fog_service_interface.delete_network(id)
27
+ end
28
+
29
+ def current_organization
30
+ @fog_model_interface.current_organization
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -3,7 +3,7 @@ module Vcloud
3
3
  module ComputeMetadata
4
4
 
5
5
  def get_metadata id
6
- vcloud_compute_metadata = Vcloud::Fog::ServiceInterface.new.get_vapp_metadata(id)
6
+ vcloud_compute_metadata = Vcloud::Core::Fog::ServiceInterface.new.get_vapp_metadata(id)
7
7
  MetadataHelper.extract_metadata(vcloud_compute_metadata[:MetadataEntry])
8
8
  end
9
9
 
@@ -21,7 +21,7 @@ module Vcloud
21
21
  end
22
22
 
23
23
  def update_configuration(config)
24
- fsi = Vcloud::Fog::ServiceInterface.new
24
+ fsi = Vcloud::Core::Fog::ServiceInterface.new
25
25
  fsi.post_configure_edge_gateway_services(id, config)
26
26
  end
27
27
 
@@ -42,7 +42,7 @@ module Vcloud
42
42
  end
43
43
 
44
44
  def vcloud_attributes
45
- fsi = Vcloud::Fog::ServiceInterface.new
45
+ fsi = Vcloud::Core::Fog::ServiceInterface.new
46
46
  fsi.get_edge_gateway(id)
47
47
  end
48
48
 
@@ -0,0 +1,40 @@
1
+ require 'fog'
2
+ require 'vcloud/core/fog/fog_constants'
3
+ require 'vcloud/core/fog/login'
4
+ require 'vcloud/core/fog/service_interface'
5
+ require 'vcloud/core/fog/model_interface'
6
+
7
+ module Vcloud
8
+ module Core
9
+ module Fog
10
+ TOKEN_ENV_VAR_NAME = 'FOG_VCLOUD_TOKEN'
11
+ FOG_CREDS_PASS_NAME = :vcloud_director_password
12
+
13
+ def self.check_credentials
14
+ pass = fog_credentials_pass
15
+ unless pass.nil? or pass.empty?
16
+ warn <<EOF
17
+ [WARNING] Storing :vcloud_director_password in your plaintext FOG_RC file is
18
+ insecure. Future releases of vcloud-core (and tools that depend on
19
+ it) will prevent you from doing this. Please use vcloud-login to
20
+ get a session token instead.
21
+ EOF
22
+ end
23
+ end
24
+
25
+ def self.fog_credentials_pass
26
+ begin
27
+ pass = ::Fog.credentials[FOG_CREDS_PASS_NAME]
28
+ rescue ::Fog::Errors::LoadError
29
+ # Assume no password if Fog has been unable to load creds.
30
+ # Suppresses a noisy error about missing credentials.
31
+ pass = nil
32
+ end
33
+
34
+ pass
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ Vcloud::Core::Fog.check_credentials
@@ -0,0 +1,35 @@
1
+ module Vcloud
2
+ module Core
3
+ module Fog
4
+
5
+ # Private helper constants for use with the vCloud API.
6
+ #
7
+ # @api private
8
+ module ContentTypes
9
+ ORG = 'application/vnd.vmware.vcloud.org+xml'
10
+ VDC = 'application/vnd.vmware.vcloud.vdc+xml'
11
+ NETWORK = 'application/vnd.vmware.vcloud.network+xml'
12
+ METADATA = 'application/vnd.vmware.vcloud.metadata.value+xml'
13
+ end
14
+
15
+ # Private helper constants for use with the vCloud API.
16
+ #
17
+ # @api private
18
+ module MetadataValueType
19
+ String = 'MetadataStringValue'
20
+ Number = 'MetadataNumberValue'
21
+ DateTime = 'MetadataDateTimeValue'
22
+ Boolean = 'MetadataBooleanValue'
23
+ end
24
+
25
+ # Private helper constants for use with the vCloud API.
26
+ #
27
+ # @api private
28
+ module RELATION
29
+ PARENT = 'up'
30
+ CHILD = 'down'
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ require 'fog'
2
+
3
+ module Vcloud
4
+ module Core
5
+ module Fog
6
+ module Login
7
+ class << self
8
+ def token(pass)
9
+ check_plaintext_pass
10
+ token = get_token(pass)
11
+
12
+ return token
13
+ end
14
+
15
+ def token_export(*args)
16
+ return "export #{Vcloud::Core::Fog::TOKEN_ENV_VAR_NAME}=#{token(*args)}"
17
+ end
18
+
19
+ private
20
+
21
+ def check_plaintext_pass
22
+ pass = Vcloud::Core::Fog::fog_credentials_pass
23
+ unless pass.nil? || pass.empty?
24
+ raise "Found plaintext #{Vcloud::Core::Fog::FOG_CREDS_PASS_NAME} entry. Please set it to an empty string"
25
+ end
26
+ end
27
+
28
+ def get_token(pass)
29
+ ENV.delete(Vcloud::Core::Fog::TOKEN_ENV_VAR_NAME)
30
+ vcloud = ::Fog::Compute::VcloudDirector.new({
31
+ Vcloud::Core::Fog::FOG_CREDS_PASS_NAME => pass,
32
+ })
33
+
34
+ return vcloud.vcloud_token
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,41 @@
1
+ module Vcloud
2
+ module Core
3
+ module Fog
4
+
5
+ # Private interface to the fog model layer.
6
+ # You should not use this directly. Expose required
7
+ # functionality in {Vcloud::Core::ApiInterface}
8
+ #
9
+ # @api private
10
+ class ModelInterface
11
+ def initialize
12
+ @vcloud = ::Fog::Compute::VcloudDirector.new
13
+ end
14
+
15
+ def org_name
16
+ @vcloud.org_name
17
+ end
18
+
19
+ def current_organization
20
+ @vcloud.organizations.get_by_name org_name
21
+ end
22
+
23
+ def current_vdc vdc_id
24
+ current_organization.vdcs.detect { |v| v.id == vdc_id }
25
+ end
26
+
27
+ def get_vm_by_href href
28
+ vm = @vcloud.get_vms_in_lease_from_query(
29
+ {
30
+ :filter => "href==#{href}"
31
+ }).body[:VMRecord].first
32
+ return nil unless vm
33
+ vdc = current_vdc(vm[:vdc].split('/').last)
34
+ vapp = vdc.vapps.get_by_name(vm[:containerName])
35
+ vapp.vms.first
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,253 @@
1
+ require 'forwardable'
2
+
3
+ module Vcloud
4
+ module Core
5
+ module Fog
6
+
7
+ # Private interface to the fog service layer.
8
+ # You should not use this directly. Expose required
9
+ # functionality in {Vcloud::Core::ApiInterface}
10
+ #
11
+ # @api private
12
+ class ServiceInterface
13
+ extend Forwardable
14
+
15
+ def_delegators :@fog, :get_vapp, :organizations, :org_name, :delete_vapp, :vcloud_token, :end_point,
16
+ :get_execute_query, :get_vapp_metadata, :power_off_vapp, :shutdown_vapp, :session,
17
+ :post_instantiate_vapp_template, :put_memory, :put_cpu, :power_on_vapp, :put_vapp_metadata_value,
18
+ :put_vm, :get_edge_gateway, :get_network_complete, :delete_network, :post_create_org_vdc_network,
19
+ :post_configure_edge_gateway_services, :get_vdc, :post_undeploy_vapp
20
+
21
+ #########################
22
+ # FogFacade Inner class to represent a logic free facade over our interactions with Fog
23
+
24
+ class FogFacade
25
+ def initialize
26
+ @vcloud = ::Fog::Compute::VcloudDirector.new
27
+ end
28
+
29
+ def get_vdc(id)
30
+ @vcloud.get_vdc(id).body
31
+ end
32
+
33
+ def get_organization (id)
34
+ @vcloud.get_organization(id).body
35
+ end
36
+
37
+ def session
38
+ @vcloud.get_current_session.body
39
+ end
40
+
41
+ def get_vapps_in_lease_from_query(options)
42
+ @vcloud.get_vapps_in_lease_from_query(options).body
43
+ end
44
+
45
+ def post_instantiate_vapp_template(vdc, template, name, params)
46
+ Vcloud::Core.logger.debug("instantiating #{name} vapp in #{vdc[:name]}")
47
+ vapp = @vcloud.post_instantiate_vapp_template(extract_id(vdc), template, name, params).body
48
+ @vcloud.process_task(vapp[:Tasks][:Task])
49
+ @vcloud.get_vapp(extract_id(vapp)).body
50
+ end
51
+
52
+ def put_memory(vm_id, memory)
53
+ Vcloud::Core.logger.debug("putting #{memory}MB memory into VM #{vm_id}")
54
+ task = @vcloud.put_memory(vm_id, memory).body
55
+ @vcloud.process_task(task)
56
+ end
57
+
58
+ def get_vapp(id)
59
+ @vcloud.get_vapp(id).body
60
+ end
61
+
62
+ def put_network_connection_system_section_vapp(vm_id, section)
63
+ task = @vcloud.put_network_connection_system_section_vapp(vm_id, section).body
64
+ @vcloud.process_task(task)
65
+ end
66
+
67
+ def put_cpu(vm_id, cpu)
68
+ Vcloud::Core.logger.debug("putting #{cpu} CPU(s) into VM #{vm_id}")
69
+ task = @vcloud.put_cpu(vm_id, cpu).body
70
+ @vcloud.process_task(task)
71
+ end
72
+
73
+ def put_vm(id, name, options={})
74
+ Vcloud::Core.logger.debug("updating name : #{name}, :options => #{options} in vm : #{id}")
75
+ task = @vcloud.put_vm(id, name, options).body
76
+ @vcloud.process_task(task)
77
+ end
78
+
79
+ def vcloud_token
80
+ @vcloud.vcloud_token
81
+ end
82
+
83
+ def end_point
84
+ @vcloud.end_point
85
+ end
86
+
87
+ def put_guest_customization_section_vapp(vm_id, customization_req)
88
+ task = @vcloud.put_guest_customization_section_vapp(vm_id, customization_req).body
89
+ @vcloud.process_task(task)
90
+ end
91
+
92
+ def get_execute_query(type=nil, options={})
93
+ @vcloud.get_execute_query(type, options).body
94
+ end
95
+
96
+ def get_vapp_metadata(id)
97
+ @vcloud.get_vapp_metadata(id).body
98
+ end
99
+
100
+
101
+ def organizations
102
+ @vcloud.organizations
103
+ end
104
+
105
+ def org_name
106
+ @vcloud.org_name
107
+ end
108
+
109
+ def post_undeploy_vapp(vapp_id)
110
+ task = @vcloud.post_undeploy_vapp(vapp_id).body
111
+ @vcloud.process_task(task)
112
+ end
113
+
114
+ def delete_vapp(vapp_id)
115
+ task = @vcloud.delete_vapp(vapp_id).body
116
+ @vcloud.process_task(task)
117
+ end
118
+
119
+ def get_network_complete(id)
120
+ @vcloud.get_network_complete(id).body
121
+ end
122
+
123
+ def delete_network(id)
124
+ task = @vcloud.delete_network(id).body
125
+ @vcloud.process_task(task)
126
+ end
127
+
128
+ def post_create_org_vdc_network(vdc_id, name, options)
129
+ Vcloud::Core.logger.debug("creating #{options[:fence_mode]} OrgVdcNetwork #{name} in vDC #{vdc_id}")
130
+ attrs = @vcloud.post_create_org_vdc_network(vdc_id, name, options).body
131
+ @vcloud.process_task(attrs[:Tasks][:Task])
132
+ get_network_complete(extract_id(attrs))
133
+ end
134
+
135
+ def post_configure_edge_gateway_services(edgegw_id, config)
136
+ Vcloud::Core.logger.info("Updating EdgeGateway #{edgegw_id}")
137
+ begin
138
+ task = @vcloud.post_configure_edge_gateway_services(edgegw_id, config).body
139
+ @vcloud.process_task(task)
140
+ rescue => ex
141
+ Vcloud::Core.logger.error("Could not update EdgeGateway #{edgegw_id} : #{ex}")
142
+ raise
143
+ end
144
+ end
145
+
146
+ def power_off_vapp(vapp_id)
147
+ task = @vcloud.post_power_off_vapp(vapp_id).body
148
+ @vcloud.process_task(task)
149
+ end
150
+
151
+ def power_on_vapp(vapp_id)
152
+ Vcloud::Core.logger.debug("Powering on vApp #{vapp_id}")
153
+ task = @vcloud.post_power_on_vapp(vapp_id).body
154
+ @vcloud.process_task(task)
155
+ end
156
+
157
+ def shutdown_vapp(vapp_id)
158
+ task = @vcloud.post_shutdown_vapp(vapp_id).body
159
+ @vcloud.process_task(task)
160
+ end
161
+
162
+ def put_vapp_metadata_value(id, k, v)
163
+ Vcloud::Core.logger.debug("putting metadata pair '#{k}'=>'#{v}' to #{id}")
164
+ # need to convert key to_s since Fog 0.17 borks on symbol key
165
+ task = @vcloud.put_vapp_metadata_item_metadata(id, k.to_s, v).body
166
+ @vcloud.process_task(task)
167
+ end
168
+
169
+ def get_edge_gateway(id)
170
+ @vcloud.get_edge_gateway(id).body
171
+ end
172
+
173
+ private
174
+ def extract_id(link)
175
+ link[:href].split('/').last
176
+ end
177
+ end
178
+ #
179
+ #########################
180
+
181
+
182
+
183
+ def initialize (fog = FogFacade.new)
184
+ @fog = fog
185
+ end
186
+
187
+ def org
188
+ link = session[:Link].select { |l| l[:rel] == RELATION::CHILD }.detect do |l|
189
+ l[:type] == ContentTypes::ORG
190
+ end
191
+ @fog.get_organization(link[:href].split('/').last)
192
+ end
193
+
194
+ def get_vapp_by_name_and_vdc_name name, vdc_name
195
+ response_body = @fog.get_vapps_in_lease_from_query({:filter => "name==#{name}"})
196
+ response_body[:VAppRecord].detect { |record| record[:vdcName] == vdc_name }
197
+ end
198
+
199
+ def vdc(name)
200
+ link = org[:Link].select { |l| l[:rel] == RELATION::CHILD }.detect do |l|
201
+ l[:type] == ContentTypes::VDC && l[:name] == name
202
+ end
203
+ raise "vdc #{name} cannot be found" unless link
204
+ @fog.get_vdc(link[:href].split('/').last)
205
+
206
+ end
207
+
208
+ def put_network_connection_system_section_vapp(vm_id, section)
209
+ begin
210
+ Vcloud::Core.logger.debug("adding NIC into VM #{vm_id}")
211
+ @fog.put_network_connection_system_section_vapp(vm_id, section)
212
+ rescue => ex
213
+ Vcloud::Core.logger.error("failed to put_network_connection_system_section_vapp for vm #{vm_id}: #{ex}")
214
+ Vcloud::Core.logger.debug("requested network section : #{section.inspect}")
215
+ raise
216
+ end
217
+ end
218
+
219
+ def find_networks(network_names, vdc_name)
220
+ network_names.collect do |network|
221
+ vdc(vdc_name)[:AvailableNetworks][:Network].detect do |l|
222
+ l[:type] == ContentTypes::NETWORK && l[:name] == network
223
+ end
224
+ end
225
+ end
226
+
227
+ def put_guest_customization_section(vm_id, vm_name, script)
228
+ begin
229
+ Vcloud::Core.logger.debug("configuring guest customization section for vm : #{vm_id}")
230
+ customization_req = {
231
+ :Enabled => true,
232
+ :CustomizationScript => script,
233
+ :ComputerName => vm_name
234
+ }
235
+ @fog.put_guest_customization_section_vapp(vm_id, customization_req)
236
+ rescue => ex
237
+ Vcloud::Core.logger.error("Failed to update guest customization section: #{ex}")
238
+ Vcloud::Core.logger.debug("=== interpolated preamble:")
239
+ Vcloud::Core.logger.debug(script)
240
+ raise
241
+ end
242
+ end
243
+
244
+ private
245
+ def extract_id(link)
246
+ link[:href].split('/').last
247
+ end
248
+
249
+ end
250
+
251
+ end
252
+ end
253
+ end