vmware-vra 2.7.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +79 -144
  5. data/Rakefile +0 -11
  6. data/lib/vra/catalog.rb +39 -8
  7. data/lib/vra/catalog_base.rb +62 -0
  8. data/lib/vra/catalog_item.rb +28 -74
  9. data/lib/vra/catalog_source.rb +116 -0
  10. data/lib/vra/catalog_type.rb +56 -0
  11. data/lib/vra/client.rb +62 -54
  12. data/lib/vra/deployment.rb +155 -0
  13. data/lib/vra/deployment_request.rb +117 -0
  14. data/lib/vra/{resources.rb → deployments.rb} +26 -17
  15. data/lib/vra/exceptions.rb +1 -1
  16. data/lib/vra/http.rb +11 -6
  17. data/lib/vra/request.rb +28 -36
  18. data/lib/vra/request_parameters.rb +12 -12
  19. data/lib/vra/resource.rb +32 -203
  20. data/lib/vra/version.rb +2 -2
  21. data/lib/vra.rb +15 -12
  22. data/spec/catalog_item_spec.rb +64 -222
  23. data/spec/catalog_source_spec.rb +178 -0
  24. data/spec/catalog_spec.rb +112 -72
  25. data/spec/catalog_type_spec.rb +114 -0
  26. data/spec/client_spec.rb +271 -226
  27. data/spec/deployment_request_spec.rb +192 -0
  28. data/spec/deployment_spec.rb +227 -0
  29. data/spec/deployments_spec.rb +80 -0
  30. data/spec/fixtures/resource/sample_catalog_item.json +18 -0
  31. data/spec/fixtures/resource/sample_catalog_item_2.json +18 -0
  32. data/spec/fixtures/resource/sample_catalog_source.json +20 -0
  33. data/spec/fixtures/resource/sample_catalog_type.json +49 -0
  34. data/spec/fixtures/resource/sample_dep_actions.json +58 -0
  35. data/spec/fixtures/resource/sample_dep_request.json +19 -0
  36. data/spec/fixtures/resource/sample_dep_resource.json +112 -0
  37. data/spec/fixtures/resource/sample_deployment.json +26 -0
  38. data/spec/fixtures/resource/sample_entitlements.json +25 -0
  39. data/spec/http_spec.rb +63 -61
  40. data/spec/request_spec.rb +62 -68
  41. data/spec/resource_spec.rb +71 -390
  42. data/spec/spec_helper.rb +10 -4
  43. data/vmware-vra.gemspec +0 -1
  44. metadata +40 -30
  45. data/.travis.yml +0 -14
  46. data/lib/vra/catalog_request.rb +0 -137
  47. data/lib/vra/requests.rb +0 -41
  48. data/spec/catalog_request_spec.rb +0 -268
  49. data/spec/requests_spec.rb +0 -60
  50. data/spec/resources_spec.rb +0 -71
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  #
4
- # Author:: Chef Partner Engineering (<partnereng@chef.io>)
5
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
3
+ # Author:: Ashique Saidalavi (<ashique.saidalavi@progress.com>)
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
6
5
  # License:: Apache License, Version 2.0
7
6
  #
8
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,29 +16,39 @@
17
16
  # See the License for the specific language governing permissions and
18
17
  # limitations under the License.
19
18
  #
20
-
21
19
  module Vra
22
- class Resources
23
- attr_reader :client
24
-
20
+ # class that represents the Deployments Object
21
+ class Deployments
25
22
  def initialize(client)
26
23
  @client = client
27
24
  end
28
25
 
29
- # @return [Array[Vra::Resource]] - returns an array of all the resources owned by the user
30
- # @param [Vra::Client]
31
- def self.all(client)
32
- items = client.http_get_paginated_array!("/catalog-service/api/consumer/resources")
33
- items.map { |item| Vra::Resource.new(client, data: item) }
26
+ def by_id(dep_id)
27
+ Vra::Deployment.new(client, id: dep_id)
28
+ end
29
+
30
+ def all
31
+ fetch_all_resources
34
32
  end
35
33
 
36
- # @return [Array[Vra::Resource]] - returns an array of all the resources owned by the user
37
- def all_resources
38
- self.class.all(client)
34
+ class << self
35
+ def all(client)
36
+ new(client).all
37
+ end
38
+
39
+ def by_id(client, id)
40
+ new(client).by_id(id)
41
+ end
39
42
  end
40
43
 
41
- def by_id(id)
42
- Vra::Resource.new(client, id: id)
44
+ private
45
+
46
+ attr_reader :client
47
+
48
+ def fetch_all_resources
49
+ client
50
+ .http_get_paginated_array!('/deployment/api/deployments')
51
+ .map! { |x| Vra::Deployment.new(client, data: x) }
43
52
  end
44
53
  end
45
54
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/vra/http.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
- require "net/http" unless defined?(Net::HTTP)
3
- require "openssl" unless defined?(OpenSSL)
2
+
3
+ require 'net/http' unless defined?(Net::HTTP)
4
+ require 'openssl' unless defined?(OpenSSL)
5
+ require 'ffi_yajl' unless defined?(FFI_Yajl)
6
+ require 'json'
4
7
 
5
8
  module Vra
6
9
  module Http
@@ -61,6 +64,7 @@ module Vra
61
64
  get: Net::HTTP::Get,
62
65
  head: Net::HTTP::Head,
63
66
  post: Net::HTTP::Post,
67
+ delete: Net::HTTP::Delete
64
68
  }.fetch(method, nil)
65
69
 
66
70
  raise "Unknown HTTP method #{method}!" unless type
@@ -142,13 +146,14 @@ module Vra
142
146
  end
143
147
 
144
148
  class Error < StandardError
149
+ attr_reader :http_code, :response
150
+
145
151
  def self.from_response(http_response)
146
- new(http_response.message, http_response.code, http_response.body)
152
+ body = FFI_Yajl::Parser.parse(http_response.body) || {}
153
+ message = body['message'] || body['serverMessage']
154
+ new(message, http_response.code, body)
147
155
  end
148
156
 
149
- attr_reader :http_code
150
- attr_reader :response
151
-
152
157
  def initialize(message, http_code, response)
153
158
  super(message)
154
159
  @http_code = http_code
data/lib/vra/request.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,23 +17,34 @@
17
17
  # limitations under the License.
18
18
  #
19
19
 
20
- require "ffi_yajl" unless defined?(FFI_Yajl)
21
-
22
20
  module Vra
21
+ # class to represent the Deployment request
23
22
  class Request
24
- attr_reader :client, :id
25
- def initialize(client, id)
26
- @client = client
27
- @id = id
23
+ attr_reader :id, :deployment_id
24
+
25
+ def initialize(client, deployment_id, opts = {})
26
+ @client = client
27
+ @deployment_id = deployment_id
28
+ @id = opts[:id]
29
+ @request_data = opts[:data]
30
+
31
+ if @request_data.nil?
32
+ refresh
33
+ else
34
+ @id = @request_data['id']
35
+ end
36
+ end
37
+
38
+ def requested_by
39
+ request_data['requestedBy']
40
+ end
28
41
 
29
- @request_data = nil
30
- @status = nil
31
- @completion_state = nil
32
- @completion_details = nil
42
+ def name
43
+ request_data['name']
33
44
  end
34
45
 
35
46
  def refresh
36
- @request_data = client.get_parsed("/catalog-service/api/consumer/requests/#{@id}")
47
+ @request_data = client.get_parsed("/deployment/api/deployments/#{deployment_id}/requests/#{id}?deleted=true")
37
48
  rescue Vra::Exception::HTTPNotFound
38
49
  raise Vra::Exception::NotFound, "request ID #{@id} is not found"
39
50
  end
@@ -50,7 +61,7 @@ module Vra
50
61
  refresh_if_empty
51
62
  return if request_empty?
52
63
 
53
- @request_data["phase"]
64
+ request_data['status']
54
65
  end
55
66
 
56
67
  def completed?
@@ -58,34 +69,15 @@ module Vra
58
69
  end
59
70
 
60
71
  def successful?
61
- status == "SUCCESSFUL"
72
+ status == 'SUCCESSFUL'
62
73
  end
63
74
 
64
75
  def failed?
65
- status == "FAILED"
66
- end
67
-
68
- def completion_state
69
- refresh_if_empty
70
- return if request_empty?
71
-
72
- @request_data["requestCompletion"]["requestCompletionState"]
76
+ status == 'FAILED'
73
77
  end
74
78
 
75
- def completion_details
76
- refresh_if_empty
77
- return if request_empty?
78
-
79
- @request_data["requestCompletion"]["completionDetails"]
80
- end
79
+ private
81
80
 
82
- def resources
83
- begin
84
- request_resources = client.http_get_paginated_array!("/catalog-service/api/consumer/requests/#{@id}/resources")
85
- rescue Vra::Exception::HTTPNotFound
86
- raise Vra::Exception::NotFound, "resources for request ID #{@id} are not found"
87
- end
88
- request_resources.map { |resource| Vra::Resource.new(client, data: resource) }
89
- end
81
+ attr_reader :request_data, :client
90
82
  end
91
83
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -93,11 +93,11 @@ module Vra
93
93
 
94
94
  def to_vra
95
95
  hash = {
96
- "data" => {},
96
+ 'inputs': {}
97
97
  }
98
98
 
99
- @entries.each do |k, v|
100
- hash["data"].merge!(v.to_vra)
99
+ @entries.each do |_, v|
100
+ hash[:inputs].merge!(v.to_vra)
101
101
  end
102
102
 
103
103
  hash
@@ -120,7 +120,7 @@ module Vra
120
120
  def to_h
121
121
  hash = {}
122
122
 
123
- if @children.count > 0
123
+ if @children.count.positive?
124
124
  hash[@key] = {}
125
125
 
126
126
  @children.each do |c|
@@ -135,13 +135,13 @@ module Vra
135
135
 
136
136
  def to_vra
137
137
  hash = {}
138
- if @children.count > 0
138
+ if @children.count.positive?
139
139
  hash[@key] = {}
140
140
 
141
- hash[@key]["data"] = {}
141
+ hash[@key]['inputs'] = {}
142
142
 
143
143
  @children.each do |c|
144
- hash[@key]["data"].merge!(c.to_vra)
144
+ hash[@key]['inputs'].merge!(c.to_vra)
145
145
  end
146
146
  else
147
147
  hash[@key] = format_value
@@ -152,12 +152,12 @@ module Vra
152
152
 
153
153
  def format_value
154
154
  case @type
155
- when "integer"
155
+ when 'integer'
156
156
  @value.to_i
157
- when "string"
157
+ when 'string'
158
158
  @value
159
- when "boolean"
160
- @value.to_s == "true"
159
+ when 'boolean'
160
+ @value.to_s == 'true'
161
161
  else
162
162
  @value
163
163
  end
data/lib/vra/resource.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,252 +21,81 @@ require "ffi_yajl" unless defined?(FFI_Yajl)
21
21
 
22
22
  module Vra
23
23
  class Resource
24
- attr_reader :client, :id, :resource_data
24
+ VM_TYPES = %w[
25
+ Cloud.vSphere.Machine
26
+ Cloud.Machine
27
+ ].freeze
25
28
 
26
- def initialize(client, opts)
29
+ attr_reader :client, :deployment_id, :id, :resource_data
30
+
31
+ def initialize(client, deployment_id, opts = {})
27
32
  @client = client
33
+ @deployment_id = deployment_id
28
34
  @id = opts[:id]
29
35
  @resource_data = opts[:data]
30
36
  @resource_actions = []
31
37
 
32
- if @id.nil? && @resource_data.nil?
33
- raise ArgumentError, "must supply an id or a resource data hash"
34
- end
35
-
36
- if !@id.nil? && !@resource_data.nil?
37
- raise ArgumentError, "must supply an id OR a resource data hash, not both"
38
- end
38
+ raise ArgumentError, 'must supply an id or a resource data hash' if @id.nil? && @resource_data.nil?
39
+ raise ArgumentError, 'must supply an id OR a resource data hash, not both' if !@id.nil? && !@resource_data.nil?
39
40
 
40
41
  if @resource_data.nil?
41
42
  fetch_resource_data
42
43
  else
43
- @id = @resource_data["id"]
44
+ @id = @resource_data['id']
44
45
  end
45
46
  end
46
47
 
47
- # @param client [Vra::Client]
48
- # @param name [String] - the hostname of the client you wish to lookup
49
- # @preturn [Vra::Resource] - return nil if not found, otherwise the resource associated with the name
50
- def self.by_name(client, name)
51
- raise ArgumentError.new("name cannot be nil") if name.nil?
52
- raise ArgumentError.new("client cannot be nil") if client.nil?
53
-
54
- Resources.all(client).find { |r| r.name.downcase =~ /#{name.downcase}/ }
55
- end
56
-
57
48
  def fetch_resource_data
58
- @resource_data = client.get_parsed("/catalog-service/api/consumer/resources/#{@id}")
49
+ @resource_data = client.get_parsed("/deployment/api/deployments/#{deployment_id}/resources/#{id}")
59
50
  rescue Vra::Exception::HTTPNotFound
60
51
  raise Vra::Exception::NotFound, "resource ID #{@id} does not exist"
61
52
  end
53
+
62
54
  alias refresh fetch_resource_data
63
55
 
64
56
  def name
65
- resource_data["name"]
66
- end
67
-
68
- def description
69
- resource_data["description"]
57
+ resource_data['name']
70
58
  end
71
59
 
72
60
  def status
73
- resource_data["status"]
61
+ resource_data['syncStatus']
74
62
  end
75
63
 
76
- def vm?
77
- %w{Infrastructure.Virtual Infrastructure.Cloud}.include?(resource_data["resourceTypeRef"]["id"])
64
+ def properties
65
+ resource_data['properties']
78
66
  end
79
67
 
80
- def organization
81
- return {} if resource_data["organization"].nil?
82
-
83
- resource_data["organization"]
84
- end
85
-
86
- def tenant_id
87
- organization["tenantRef"]
88
- end
89
-
90
- def tenant_name
91
- organization["tenantLabel"]
92
- end
93
-
94
- def subtenant_id
95
- organization["subtenantRef"]
96
- end
97
-
98
- def subtenant_name
99
- organization["subtenantLabel"]
100
- end
101
-
102
- def catalog_item
103
- return {} if resource_data["catalogItem"].nil?
104
-
105
- resource_data["catalogItem"]
106
- end
107
-
108
- def catalog_id
109
- catalog_item["id"]
110
- end
111
-
112
- def catalog_name
113
- catalog_item["label"]
114
- end
115
-
116
- def owner_ids
117
- resource_data["owners"].map { |x| x["ref"] }
68
+ def vm?
69
+ VM_TYPES.include?(resource_data['type'])
118
70
  end
119
71
 
120
72
  def owner_names
121
- resource_data["owners"].map { |x| x["value"] }
122
- end
123
-
124
- def machine_status
125
- status = resource_data["resourceData"]["entries"].find { |x| x["key"] == "MachineStatus" }
126
- raise "No MachineStatus entry available for resource" if status.nil?
127
-
128
- status["value"]["value"]
129
- end
130
-
131
- def machine_on?
132
- machine_status == "On"
133
- end
134
-
135
- def machine_off?
136
- machine_status == "Off"
73
+ properties['Owner']
137
74
  end
138
75
 
139
- def machine_turning_on?
140
- machine_status == "TurningOn" || machine_status == "MachineActivated"
141
- end
142
-
143
- def machine_turning_off?
144
- %w{TurningOff ShuttingDown}.include?(machine_status)
145
- end
146
-
147
- def machine_in_provisioned_state?
148
- machine_status == "MachineProvisioned"
76
+ def project_id
77
+ properties['project']
149
78
  end
150
79
 
151
80
  def network_interfaces
152
81
  return unless vm?
153
82
 
154
- network_list = resource_data["resourceData"]["entries"].find { |x| x["key"] == "NETWORK_LIST" }
83
+ network_list = properties['networks']
155
84
  return if network_list.nil?
156
85
 
157
- network_list["value"]["items"].each_with_object([]) do |item, nics|
158
- nic = {}
159
- item["values"]["entries"].each do |entry|
160
- key = entry["key"]
161
- value = entry["value"]["value"]
162
- nic[key] = value
163
- end
164
-
165
- nics << nic
86
+ network_list.each_with_object([]) do |item, nics|
87
+ nics << {
88
+ 'NETWORK_NAME' => item['name'],
89
+ 'NETWORK_ADDRESS' => item['address'],
90
+ 'NETWORK_MAC_ADDRESS' => item['mac_address']
91
+ }
166
92
  end
167
93
  end
168
94
 
169
- def ip_addresses
95
+ def ip_address
170
96
  return if !vm? || network_interfaces.nil?
171
97
 
172
- addrs = []
173
-
174
- request_id = @resource_data["requestId"]
175
-
176
- print "Waiting For vRA to collect the IP"
177
-
178
- loop do
179
- resource_views = @client.http_get("/catalog-service/api/consumer/requests/#{request_id}/resourceViews")
180
-
181
- JSON.parse(resource_views.body)["content"].each do |content|
182
- if content.key?("data") &&
183
- !(content["data"]["ip_address"].nil? ||
184
- content["data"]["ip_address"] == "")
185
- addrs << content["data"]["ip_address"]
186
- end
187
- end
188
-
189
- break unless addrs.empty?
190
-
191
- sleep 10
192
- end
193
-
194
- addrs
195
- end
196
-
197
- def actions
198
- # if this Resource instance was created with data from a "all_resources" fetch,
199
- # it is likely missing operations data because the vRA API is not pleasant sometimes.
200
- fetch_resource_data if resource_data["operations"].nil?
201
-
202
- resource_data["operations"]
203
- end
204
-
205
- def action_id_by_name(name)
206
- return if actions.nil?
207
-
208
- action = actions.find { |x| x["name"] == name }
209
- return if action.nil?
210
-
211
- action["id"]
212
- end
213
-
214
- def destroy
215
- action_id = action_id_by_name("Destroy")
216
- raise Vra::Exception::NotFound, "No destroy action found for resource #{@id}" if action_id.nil?
217
-
218
- submit_action_request(action_id)
219
- end
220
-
221
- def shutdown
222
- action_id = action_id_by_name("Shutdown")
223
- raise Vra::Exception::NotFound, "No shutdown action found for resource #{@id}" if action_id.nil?
224
-
225
- submit_action_request(action_id)
226
- end
227
-
228
- def poweroff
229
- action_id = action_id_by_name("Power Off")
230
- raise Vra::Exception::NotFound, "No power-off action found for resource #{@id}" if action_id.nil?
231
-
232
- submit_action_request(action_id)
233
- end
234
-
235
- def poweron
236
- action_id = action_id_by_name("Power On")
237
- raise Vra::Exception::NotFound, "No power-on action found for resource #{@id}" if action_id.nil?
238
-
239
- submit_action_request(action_id)
240
- end
241
-
242
- def action_request_payload(action_id)
243
- {
244
- "@type" => "ResourceActionRequest",
245
- "resourceRef" => {
246
- "id" => @id,
247
- },
248
- "resourceActionRef" => {
249
- "id" => action_id,
250
- },
251
- "organization" => {
252
- "tenantRef" => tenant_id,
253
- "tenantLabel" => tenant_name,
254
- "subtenantRef" => subtenant_id,
255
- "subtenantLabel" => subtenant_name,
256
- },
257
- "state" => "SUBMITTED",
258
- "requestNumber" => 0,
259
- "requestData" => {
260
- "entries" => [],
261
- },
262
- }
263
- end
264
-
265
- def submit_action_request(action_id)
266
- payload = action_request_payload(action_id).to_json
267
- response = client.http_post("/catalog-service/api/consumer/requests", payload)
268
- request_id = response.location.split("/")[-1]
269
- Vra::Request.new(client, request_id)
98
+ properties['address']
270
99
  end
271
100
  end
272
101
  end
data/lib/vra/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,5 +18,5 @@
18
18
  #
19
19
 
20
20
  module Vra
21
- VERSION = "2.7.2"
21
+ VERSION = "3.0.0"
22
22
  end
data/lib/vra.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,14 +17,17 @@
17
17
  # limitations under the License.
18
18
  #
19
19
 
20
- require "vra/catalog"
21
- require "vra/catalog_item"
22
- require "vra/catalog_request"
23
- require "vra/client"
24
- require "vra/exceptions"
25
- require "vra/request"
26
- require "vra/request_parameters"
27
- require "vra/requests"
28
- require "vra/resource"
29
- require "vra/resources"
30
- require "vra/version"
20
+ require 'vra/catalog_base'
21
+ require 'vra/catalog'
22
+ require 'vra/catalog_item'
23
+ require 'vra/catalog_source'
24
+ require 'vra/catalog_type'
25
+ require 'vra/deployment_request'
26
+ require 'vra/deployment'
27
+ require 'vra/deployments'
28
+ require 'vra/client'
29
+ require 'vra/exceptions'
30
+ require 'vra/request'
31
+ require 'vra/request_parameters'
32
+ require 'vra/resource'
33
+ require 'vra/version'