vmware-vra 3.1.2 → 3.2.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.
data/lib/vra/client.rb CHANGED
@@ -17,15 +17,15 @@
17
17
  # limitations under the License.
18
18
  #
19
19
 
20
- require 'ffi_yajl' unless defined?(FFI_Yajl)
21
- require 'passwordmasker'
22
- require 'vra/http'
20
+ require "ffi_yajl" unless defined?(FFI_Yajl)
21
+ require "passwordmasker"
22
+ require "vra/http"
23
23
 
24
24
  module Vra
25
25
  class Client
26
- REFRESH_TOKEN_URL = '/csp/gateway/am/api/login?access_token'
27
- ACCESS_TOKEN_URL = '/iaas/api/login'
28
- ROLES_URL = '/csp/gateway/am/api/loggedin/user/orgs'
26
+ REFRESH_TOKEN_URL = "/csp/gateway/am/api/login?access_token"
27
+ ACCESS_TOKEN_URL = "/iaas/api/login"
28
+ ROLES_URL = "/csp/gateway/am/api/loggedin/user/orgs"
29
29
 
30
30
  attr_accessor :page_size
31
31
 
@@ -33,7 +33,8 @@ module Vra
33
33
  @base_url = opts[:base_url]
34
34
  @username = opts[:username]
35
35
  @password = PasswordMasker.new(opts[:password])
36
- @tenant = opts[:tenant]
36
+ @domain = fetch_domain(opts)
37
+
37
38
  @verify_ssl = opts.fetch(:verify_ssl, true)
38
39
  @refresh_token = PasswordMasker.new(nil)
39
40
  @access_token = PasswordMasker.new(nil)
@@ -80,15 +81,15 @@ module Vra
80
81
  {
81
82
  'username': @username,
82
83
  'password': @password.value,
83
- 'tenant': @tenant
84
+ 'domain': @domain,
84
85
  }
85
86
  end
86
87
 
87
88
  def request_headers
88
89
  headers = {}
89
- headers['Accept'] = 'application/json'
90
- headers['Content-Type'] = 'application/json'
91
- headers['Authorization'] = 'Bearer ' + @access_token.value unless @access_token.value.nil?
90
+ headers["Accept"] = "application/json"
91
+ headers["Content-Type"] = "application/json"
92
+ headers["Authorization"] = "Bearer " + @access_token.value unless @access_token.value.nil?
92
93
 
93
94
  headers
94
95
  end
@@ -96,7 +97,7 @@ module Vra
96
97
  def authorize!
97
98
  generate_access_token unless authorized?
98
99
 
99
- raise Vra::Exception::Unauthorized, 'Unable to authorize against vRA' unless authorized?
100
+ raise Vra::Exception::Unauthorized, "Unable to authorize against vRA" unless authorized?
100
101
  end
101
102
 
102
103
  def authorized?
@@ -119,14 +120,14 @@ module Vra
119
120
  raise Vra::Exception::Unauthorized, "Unable to get the refresh token: #{refresh_response.body}" unless refresh_response.success_ok?
120
121
 
121
122
  refresh_response_body = FFI_Yajl::Parser.parse(refresh_response.body)
122
- @refresh_token.value = refresh_response_body['refresh_token']
123
+ @refresh_token.value = refresh_response_body["refresh_token"]
123
124
 
124
125
  # Second Step: Sending the refresh token to a separate endpoint to get an Access Token
125
126
  access_response = http_post(ACCESS_TOKEN_URL, "{ \"refreshToken\": \"#{@refresh_token.value}\" }", :skip_auth)
126
127
  raise Vra::Exception::Unauthorized, "Unable to get the access token: #{access_response.body}" unless access_response.success_ok?
127
128
 
128
129
  access_response_body = FFI_Yajl::Parser.parse(access_response.body)
129
- @access_token.value = access_response_body['token']
130
+ @access_token.value = access_response_body["token"]
130
131
  end
131
132
 
132
133
  def full_url(path)
@@ -231,7 +232,7 @@ module Vra
231
232
 
232
233
  def validate_client_options!
233
234
  raise ArgumentError, "Username and password are required" if @username.nil? || @password.value.nil?
234
- raise ArgumentError, "A tenant is required" if @tenant.nil?
235
+ raise ArgumentError, "A domain is required" if @domain.nil?
235
236
  raise ArgumentError, "A base URL is required" if @base_url.nil?
236
237
  raise ArgumentError, "Base URL #{@base_url} is not a valid URI." unless valid_uri?(@base_url)
237
238
  end
@@ -242,5 +243,15 @@ module Vra
242
243
  rescue URI::InvalidURIError
243
244
  false
244
245
  end
246
+
247
+ private
248
+
249
+ # This is for backward compatibility, if someone still uses tenant key to pass the domain,
250
+ # it should also work.
251
+ def fetch_domain(opts)
252
+ return opts[:tenant] if opts.key?(:tenant) && !opts.key?(:domain)
253
+
254
+ opts[:domain]
255
+ end
245
256
  end
246
257
  end
@@ -22,7 +22,7 @@ require "ffi_yajl" unless defined?(FFI_Yajl)
22
22
  module Vra
23
23
  # Class that represents the Deployment Object
24
24
  class Deployment
25
- INDEX_URL = '/deployment/api/deployments'
25
+ INDEX_URL = "/deployment/api/deployments"
26
26
 
27
27
  attr_reader :id
28
28
 
@@ -35,40 +35,44 @@ module Vra
35
35
  if @data.nil?
36
36
  refresh
37
37
  elsif @id.nil?
38
- @id = @data['id']
38
+ @id = @data["id"]
39
39
  end
40
40
  end
41
41
 
42
42
  def name
43
- @data['name']
43
+ @data["name"]
44
44
  end
45
45
 
46
46
  def description
47
- @data['description']
47
+ @data["description"]
48
48
  end
49
49
 
50
50
  def org_id
51
- @data['orgId']
51
+ @data["orgId"]
52
52
  end
53
53
 
54
54
  def blueprint_id
55
- @data['blueprintId']
55
+ @data["blueprintId"]
56
56
  end
57
57
 
58
58
  def owner
59
- @data['ownedBy']
59
+ @data["ownedBy"]
60
60
  end
61
61
 
62
62
  def status
63
- @data['status']
63
+ @data["status"]
64
64
  end
65
65
 
66
66
  def successful?
67
- status == 'CREATE_SUCCESSFUL'
67
+ status == "CREATE_SUCCESSFUL"
68
68
  end
69
69
 
70
70
  def failed?
71
- status == 'CREATE_FAILED'
71
+ status == "CREATE_FAILED"
72
+ end
73
+
74
+ def completion_details
75
+ requests.last.details
72
76
  end
73
77
 
74
78
  def completed?
@@ -80,16 +84,16 @@ module Vra
80
84
  end
81
85
 
82
86
  def action_id_by_name(action_name)
83
- action = actions.find { |x| x['name'] == action_name}
87
+ action = actions.find { |x| x["name"] == action_name }
84
88
  return if action.nil?
85
89
 
86
- action['id']
90
+ action["id"]
87
91
  end
88
92
 
89
93
  def resources
90
94
  response = client.get_parsed("/deployment/api/deployments/#{id}/resources")
91
95
 
92
- response['content'].map! { |x| Vra::Resource.new(client, id, data: x) }
96
+ response["content"].map! { |x| Vra::Resource.new(client, id, data: x) }
93
97
  end
94
98
 
95
99
  def resource_by_id(res_id)
@@ -99,7 +103,7 @@ module Vra
99
103
  def requests
100
104
  response = client.get_parsed("/deployment/api/deployments/#{id}/requests")
101
105
 
102
- response['content'].map! { |x| Vra::Request.new(client, id, id: x['id'], data: x) }
106
+ response["content"].map! { |x| Vra::Request.new(client, id, id: x["id"], data: x) }
103
107
  end
104
108
 
105
109
  def refresh
@@ -108,22 +112,22 @@ module Vra
108
112
  raise Vra::Exception::NotFound, "deployment with ID #{id} does not exist"
109
113
  end
110
114
 
111
- def destroy(reason = '')
112
- action_id = action_id_by_name('Delete')
115
+ def destroy(reason = "")
116
+ action_id = action_id_by_name("Delete")
113
117
  raise Vra::Exception::NotFound, "No destroy action found for resource #{@id}" if action_id.nil?
114
118
 
115
119
  submit_action_request(action_id, reason)
116
120
  end
117
121
 
118
- def power_off(reason = '')
119
- action_id = action_id_by_name('PowerOff')
122
+ def power_off(reason = "")
123
+ action_id = action_id_by_name("PowerOff")
120
124
  raise Vra::Exception::NotFound, "No power-off action found for resource #{@id}" if action_id.nil?
121
125
 
122
126
  submit_action_request(action_id, reason)
123
127
  end
124
128
 
125
129
  def power_on(reason = nil)
126
- action_id = action_id_by_name('PowerOn')
130
+ action_id = action_id_by_name("PowerOn")
127
131
  raise Vra::Exception::NotFound, "No power-on action found for resource #{@id}" if action_id.nil?
128
132
 
129
133
  submit_action_request(action_id, reason)
@@ -134,7 +138,7 @@ module Vra
134
138
  attr_reader :client, :data
135
139
 
136
140
  def validate!
137
- raise ArgumentError, 'must supply id or data hash' if @id.nil? && @data.nil?
141
+ raise ArgumentError, "must supply id or data hash" if @id.nil? && @data.nil?
138
142
  end
139
143
 
140
144
  def submit_action_request(action_id, reason)
@@ -148,7 +152,7 @@ module Vra
148
152
  {
149
153
  "actionId": action_id,
150
154
  "inputs": {},
151
- "reason": reason
155
+ "reason": reason,
152
156
  }
153
157
  end
154
158
  end
@@ -16,7 +16,7 @@
16
16
  # See the License for the specific language governing permissions and
17
17
  # limitations under the License.
18
18
  #
19
- require 'ffi_yajl' unless defined?(FFI_Yajl)
19
+ require "ffi_yajl" unless defined?(FFI_Yajl)
20
20
 
21
21
  # Overriding the hash object to add the deep_merge method
22
22
  class ::Hash
@@ -50,12 +50,12 @@ module Vra
50
50
  begin
51
51
  response = send_request!
52
52
  rescue Vra::Exception::HTTPError => e
53
- raise Vra::Exception::RequestError, "Unable to submit request: #{e.message}, trace: #{e.errors.join(', ')}"
53
+ raise Vra::Exception::RequestError, "Unable to submit request: #{e.message}, trace: #{e.errors.join(", ")}"
54
54
  rescue StandardError => e
55
55
  raise e, e.message
56
56
  end
57
57
 
58
- request_id = FFI_Yajl::Parser.parse(response)[0]['deploymentId']
58
+ request_id = FFI_Yajl::Parser.parse(response)[0]["deploymentId"]
59
59
  Vra::Deployment.new(client, id: request_id)
60
60
  end
61
61
 
@@ -85,21 +85,20 @@ module Vra
85
85
 
86
86
  def validate!
87
87
  missing_params = []
88
- %i[image_mapping flavor_mapping name project_id].each do |arg|
88
+ %i{image_mapping flavor_mapping name project_id}.each do |arg|
89
89
  missing_params << arg if send(arg).nil?
90
90
  end
91
91
 
92
92
  unless missing_params.empty?
93
- raise ArgumentError, "Unable to submit request, required param(s) missing => #{missing_params.join(', ')}"
93
+ raise ArgumentError, "Unable to submit request, required param(s) missing => #{missing_params.join(", ")}"
94
94
  end
95
95
 
96
96
  # If the user doesn't supply the catalog version, fetch the latest version and use it
97
97
  # and if the API was unable to find a valid version, alert the user.
98
98
  return unless @version.nil?
99
99
 
100
-
101
100
  @version = CatalogItem.fetch_latest_version(client, catalog_id)
102
- raise ArgumentError, 'Unable to fetch a valid catalog version' if @version.nil?
101
+ raise ArgumentError, "Unable to fetch a valid catalog version" if @version.nil?
103
102
  end
104
103
 
105
104
  def send_request!
@@ -117,8 +116,8 @@ module Vra
117
116
  'inputs': {
118
117
  'count': count,
119
118
  'image': image_mapping,
120
- 'flavor': flavor_mapping
121
- }
119
+ 'flavor': flavor_mapping,
120
+ },
122
121
  }.deep_merge(parameters)
123
122
  end
124
123
  end
@@ -47,7 +47,7 @@ module Vra
47
47
 
48
48
  def fetch_all_resources
49
49
  client
50
- .http_get_paginated_array!('/deployment/api/deployments')
50
+ .http_get_paginated_array!("/deployment/api/deployments")
51
51
  .map! { |x| Vra::Deployment.new(client, data: x) }
52
52
  end
53
53
  end
data/lib/vra/http.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
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'
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" unless defined?(JSON)
7
7
 
8
8
  module Vra
9
9
  module Http
@@ -64,7 +64,7 @@ module Vra
64
64
  get: Net::HTTP::Get,
65
65
  head: Net::HTTP::Head,
66
66
  post: Net::HTTP::Post,
67
- delete: Net::HTTP::Delete
67
+ delete: Net::HTTP::Delete,
68
68
  }.fetch(method, nil)
69
69
 
70
70
  raise "Unknown HTTP method #{method}!" unless type
@@ -150,7 +150,7 @@ module Vra
150
150
 
151
151
  def self.from_response(http_response)
152
152
  body = FFI_Yajl::Parser.parse(http_response.body) || {}
153
- message = body['message'] || body['serverMessage']
153
+ message = body["message"] || body["serverMessage"]
154
154
  new(message, http_response.code, body)
155
155
  end
156
156
 
data/lib/vra/request.rb CHANGED
@@ -31,16 +31,16 @@ module Vra
31
31
  if @request_data.nil?
32
32
  refresh
33
33
  else
34
- @id = @request_data['id']
34
+ @id = @request_data["id"]
35
35
  end
36
36
  end
37
37
 
38
38
  def requested_by
39
- request_data['requestedBy']
39
+ request_data["requestedBy"]
40
40
  end
41
41
 
42
42
  def name
43
- request_data['name']
43
+ request_data["name"]
44
44
  end
45
45
 
46
46
  def refresh
@@ -61,7 +61,7 @@ module Vra
61
61
  refresh_if_empty
62
62
  return if request_empty?
63
63
 
64
- request_data['status']
64
+ request_data["status"]
65
65
  end
66
66
 
67
67
  def completed?
@@ -69,11 +69,15 @@ module Vra
69
69
  end
70
70
 
71
71
  def successful?
72
- status == 'SUCCESSFUL'
72
+ status == "SUCCESSFUL"
73
73
  end
74
74
 
75
75
  def failed?
76
- status == 'FAILED'
76
+ status == "FAILED"
77
+ end
78
+
79
+ def details
80
+ request_data["details"]
77
81
  end
78
82
 
79
83
  private
@@ -93,7 +93,7 @@ module Vra
93
93
 
94
94
  def to_vra
95
95
  hash = {
96
- 'inputs': {}
96
+ 'inputs': {},
97
97
  }
98
98
 
99
99
  @entries.each do |_, v|
@@ -120,7 +120,7 @@ module Vra
120
120
  def to_h
121
121
  hash = {}
122
122
 
123
- if @children.count.positive?
123
+ if @children.count > 0
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.positive?
138
+ if @children.count > 0
139
139
  hash[@key] = {}
140
140
 
141
- hash[@key]['inputs'] = {}
141
+ hash[@key]["inputs"] = {}
142
142
 
143
143
  @children.each do |c|
144
- hash[@key]['inputs'].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
@@ -21,10 +21,10 @@ require "ffi_yajl" unless defined?(FFI_Yajl)
21
21
 
22
22
  module Vra
23
23
  class Resource
24
- VM_TYPES = %w[
24
+ VM_TYPES = %w{
25
25
  Cloud.vSphere.Machine
26
26
  Cloud.Machine
27
- ].freeze
27
+ }.freeze
28
28
 
29
29
  attr_reader :client, :deployment_id, :id, :resource_data
30
30
 
@@ -35,13 +35,13 @@ module Vra
35
35
  @resource_data = opts[:data]
36
36
  @resource_actions = []
37
37
 
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?
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?
40
40
 
41
41
  if @resource_data.nil?
42
42
  fetch_resource_data
43
43
  else
44
- @id = @resource_data['id']
44
+ @id = @resource_data["id"]
45
45
  end
46
46
  end
47
47
 
@@ -54,40 +54,40 @@ module Vra
54
54
  alias refresh fetch_resource_data
55
55
 
56
56
  def name
57
- resource_data['name']
57
+ resource_data["name"]
58
58
  end
59
59
 
60
60
  def status
61
- resource_data['syncStatus']
61
+ resource_data["syncStatus"]
62
62
  end
63
63
 
64
64
  def properties
65
- resource_data['properties']
65
+ resource_data["properties"]
66
66
  end
67
67
 
68
68
  def vm?
69
- VM_TYPES.include?(resource_data['type'])
69
+ VM_TYPES.include?(resource_data["type"])
70
70
  end
71
71
 
72
72
  def owner_names
73
- properties['Owner']
73
+ properties["Owner"]
74
74
  end
75
75
 
76
76
  def project_id
77
- properties['project']
77
+ properties["project"]
78
78
  end
79
79
 
80
80
  def network_interfaces
81
81
  return unless vm?
82
82
 
83
- network_list = properties['networks']
83
+ network_list = properties["networks"]
84
84
  return if network_list.nil?
85
85
 
86
86
  network_list.each_with_object([]) do |item, nics|
87
87
  nics << {
88
- 'NETWORK_NAME' => item['name'],
89
- 'NETWORK_ADDRESS' => item['address'],
90
- 'NETWORK_MAC_ADDRESS' => item['mac_address']
88
+ "NETWORK_NAME" => item["name"],
89
+ "NETWORK_ADDRESS" => item["address"],
90
+ "NETWORK_MAC_ADDRESS" => item["mac_address"],
91
91
  }
92
92
  end
93
93
  end
@@ -95,7 +95,7 @@ module Vra
95
95
  def ip_address
96
96
  return if !vm? || network_interfaces.nil?
97
97
 
98
- properties['address']
98
+ properties["address"]
99
99
  end
100
100
  end
101
101
  end
data/lib/vra/version.rb CHANGED
@@ -18,5 +18,5 @@
18
18
  #
19
19
 
20
20
  module Vra
21
- VERSION = "3.1.2"
21
+ VERSION = "3.2.0"
22
22
  end
data/lib/vra.rb CHANGED
@@ -17,17 +17,17 @@
17
17
  # limitations under the License.
18
18
  #
19
19
 
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'
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"