vsphere-automation-vapi 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +18 -18
- data/README.md +1 -1
- data/lib/vsphere-automation-vapi/api_client.rb +260 -0
- data/lib/vsphere-automation-vapi/api_error.rb +34 -0
- data/lib/vsphere-automation-vapi/configuration.rb +212 -0
- data/lib/vsphere-automation-vapi/version.rb +1 -1
- data/pkg/vsphere-automation-vapi-0.4.4.gem +0 -0
- data/spec/api_client_spec.rb +248 -0
- data/spec/configuration_spec.rb +38 -0
- data/vsphere-automation-vapi.gemspec +2 -2
- metadata +14 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd6f6e710098d05a1c4a19a99d280643e1eb9afd8e190b17d680b7f3f27de6b7
|
4
|
+
data.tar.gz: eadb6e78446598111a5a999c62d9eab27e0df7138dd5d25b98be73d67848d2a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b50804d2a7332b74544579e3555b2eae403e58c6a4135b0f543b8dc092db029a2aa4a54df8e1b32500f5fb4ad20d0dfaf1d6032a1a374e1a522cd21fa855662
|
7
|
+
data.tar.gz: 64f28e0c75d7f76bb6b0eadb1ff56c61d7a06324a15a38007b7631238f2ee30ac5d00a703c413ac53ef12ec785c64ab5150b46836479bb414872f8c6319afe64
|
data/Gemfile.lock
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ../cis
|
3
3
|
specs:
|
4
|
-
vsphere-automation-cis (0.4.
|
5
|
-
vsphere-automation-runtime (~> 0.4.
|
4
|
+
vsphere-automation-cis (0.4.5)
|
5
|
+
vsphere-automation-runtime (~> 0.4.5)
|
6
6
|
|
7
7
|
PATH
|
8
8
|
remote: ../runtime
|
9
9
|
specs:
|
10
|
-
vsphere-automation-runtime (0.4.
|
10
|
+
vsphere-automation-runtime (0.4.5)
|
11
11
|
|
12
12
|
PATH
|
13
13
|
remote: .
|
14
14
|
specs:
|
15
|
-
vsphere-automation-vapi (0.4.
|
16
|
-
vsphere-automation-cis (~> 0.4.
|
17
|
-
vsphere-automation-runtime (~> 0.4.
|
15
|
+
vsphere-automation-vapi (0.4.5)
|
16
|
+
vsphere-automation-cis (~> 0.4.5)
|
17
|
+
vsphere-automation-runtime (~> 0.4.5)
|
18
18
|
|
19
19
|
GEM
|
20
20
|
remote: https://rubygems.org/
|
@@ -29,7 +29,7 @@ GEM
|
|
29
29
|
hashdiff (1.0.0)
|
30
30
|
jaro_winkler (1.5.3)
|
31
31
|
method_source (0.9.2)
|
32
|
-
parallel (1.
|
32
|
+
parallel (1.18.0)
|
33
33
|
parser (2.6.5.0)
|
34
34
|
ast (~> 2.4.0)
|
35
35
|
pry (0.12.2)
|
@@ -38,19 +38,19 @@ GEM
|
|
38
38
|
public_suffix (4.0.1)
|
39
39
|
rainbow (3.0.0)
|
40
40
|
rake (12.3.3)
|
41
|
-
rspec (3.
|
42
|
-
rspec-core (~> 3.
|
43
|
-
rspec-expectations (~> 3.
|
44
|
-
rspec-mocks (~> 3.
|
45
|
-
rspec-core (3.
|
46
|
-
rspec-support (~> 3.
|
47
|
-
rspec-expectations (3.
|
41
|
+
rspec (3.9.0)
|
42
|
+
rspec-core (~> 3.9.0)
|
43
|
+
rspec-expectations (~> 3.9.0)
|
44
|
+
rspec-mocks (~> 3.9.0)
|
45
|
+
rspec-core (3.9.0)
|
46
|
+
rspec-support (~> 3.9.0)
|
47
|
+
rspec-expectations (3.9.0)
|
48
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
-
rspec-support (~> 3.
|
50
|
-
rspec-mocks (3.
|
49
|
+
rspec-support (~> 3.9.0)
|
50
|
+
rspec-mocks (3.9.0)
|
51
51
|
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
-
rspec-support (~> 3.
|
53
|
-
rspec-support (3.
|
52
|
+
rspec-support (~> 3.9.0)
|
53
|
+
rspec-support (3.9.0)
|
54
54
|
rubocop (0.73.0)
|
55
55
|
jaro_winkler (~> 1.5.1)
|
56
56
|
parallel (~> 1.10)
|
data/README.md
CHANGED
@@ -0,0 +1,260 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
require 'net/http'
|
6
|
+
require 'openssl'
|
7
|
+
require 'uri'
|
8
|
+
require 'vsphere-automation-runtime/configuration'
|
9
|
+
require 'vsphere-automation-runtime/version'
|
10
|
+
|
11
|
+
module VSphereAutomation
|
12
|
+
# The client responsible for communicating with the API.
|
13
|
+
class ApiClient
|
14
|
+
# The Configuration object holding settings to be used in the API client.
|
15
|
+
attr_accessor :config
|
16
|
+
|
17
|
+
# Defines the headers to be used in HTTP requests of all API calls by
|
18
|
+
# default.
|
19
|
+
#
|
20
|
+
# @return [Hash]
|
21
|
+
attr_reader :default_headers
|
22
|
+
|
23
|
+
# Creates a new instance
|
24
|
+
#
|
25
|
+
# @param config [Configuration] configuration object with values to use
|
26
|
+
def initialize(config = Configuration.default)
|
27
|
+
@config = config
|
28
|
+
@http = create_http
|
29
|
+
@user_agent = default_user_agent
|
30
|
+
@default_headers = { 'Content-Type' => 'application/json',
|
31
|
+
'User-Agent' => @user_agent }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Retrieves an instance of the object in it's default state
|
35
|
+
#
|
36
|
+
# @return [ApiClient] an instance of the object in it's default state
|
37
|
+
def self.default
|
38
|
+
DEFAULT
|
39
|
+
end
|
40
|
+
|
41
|
+
# Build the collection of parameters
|
42
|
+
def build_collection_param(params, format)
|
43
|
+
params
|
44
|
+
end
|
45
|
+
|
46
|
+
# Make a request to an API endpoint with the given options
|
47
|
+
#
|
48
|
+
# @param http_method [Symbol] the HTTP method to be used
|
49
|
+
# @param path [String] the path request will be made to
|
50
|
+
# @param opts [Hash] any additional options needed
|
51
|
+
# @return [Array<(Object, Fixnum, Hash)>] the deserialized body, status
|
52
|
+
# code, and headers.
|
53
|
+
def call_api(http_method, path, opts = {})
|
54
|
+
query_params = opts.fetch(:query_params, {})
|
55
|
+
header_params = opts.fetch(:header_params, {})
|
56
|
+
form_params = opts.fetch(:form_params, {})
|
57
|
+
add_auth(query_params, header_params, opts.fetch(:auth_names, []))
|
58
|
+
|
59
|
+
uri = build_request_uri(path, query_params)
|
60
|
+
request = Net::HTTP.const_get(http_method.capitalize).new(uri)
|
61
|
+
|
62
|
+
add_form_params(request, form_params)
|
63
|
+
add_header_params(request, header_params)
|
64
|
+
request.body = opts[:body] if opts[:body]
|
65
|
+
request.content_type = request['Content-Type'] if request['Content-Type']
|
66
|
+
|
67
|
+
if @config.debugging
|
68
|
+
@config.logger.debug("Request Body:\n#{request.body}\n")
|
69
|
+
end
|
70
|
+
|
71
|
+
response = @http.request(request)
|
72
|
+
@cookie = cookie_from_response(response)
|
73
|
+
api_key_from_response(response)
|
74
|
+
|
75
|
+
return_type = opts.fetch(:return_type, {}).fetch(response.code, nil)
|
76
|
+
data = deserialize(response, return_type)
|
77
|
+
[data, Integer(response.code), response.each_header.to_h]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Takes an object and returns the object as an HTTP body
|
81
|
+
#
|
82
|
+
# @param object [Object] object to transform
|
83
|
+
# @return [String] object as JSON string
|
84
|
+
def object_to_http_body(object)
|
85
|
+
return object.map { |o| object_to_http_body(o) } if object.is_a?(Array)
|
86
|
+
|
87
|
+
return object unless object.respond_to?(:to_hash)
|
88
|
+
|
89
|
+
object.to_hash.to_json
|
90
|
+
end
|
91
|
+
|
92
|
+
# Select an Accept header to use
|
93
|
+
#
|
94
|
+
# @param types [Array] a list of suggested types
|
95
|
+
# @return [String] the Accept header value
|
96
|
+
def select_header_accept(types)
|
97
|
+
return DEFAULT_MIME_TYPE unless types.is_a?(Array)
|
98
|
+
|
99
|
+
types.find { |t| t.include?('json') } || types.join(', ')
|
100
|
+
end
|
101
|
+
|
102
|
+
# Select an Content-Type header to use
|
103
|
+
#
|
104
|
+
# @param types [Array] a list of suggested types
|
105
|
+
# @return [String] the Content-Type header value
|
106
|
+
def select_header_content_type(types)
|
107
|
+
return DEFAULT_MIME_TYPE unless types.is_a?(Array)
|
108
|
+
|
109
|
+
types.find { |t| t.include?('json') } || types.first
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def add_auth(query_params, header_params, auth_names)
|
115
|
+
auth_names.map do |name|
|
116
|
+
settings = @config.auth_settings.fetch(name, {})
|
117
|
+
case settings[:in]
|
118
|
+
when 'header'
|
119
|
+
header_params[settings[:key]] = settings[:value]
|
120
|
+
api_key_from_cookie(settings) unless settings[:value]
|
121
|
+
when 'query'
|
122
|
+
query_params[settings[:key]] = settings[:value]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def add_form_params(request, form_params)
|
128
|
+
request.set_form_data(form_params) unless form_params.empty?
|
129
|
+
end
|
130
|
+
|
131
|
+
def add_header_params(request, headers)
|
132
|
+
header_params = @default_headers.merge(headers)
|
133
|
+
header_params.merge!(Hash(@cookie))
|
134
|
+
header_params.map { |name, value| request[name] = value }
|
135
|
+
end
|
136
|
+
|
137
|
+
def add_query_params(uri, query_params)
|
138
|
+
uri.query = URI.encode_www_form(query_params)
|
139
|
+
end
|
140
|
+
|
141
|
+
def build_request_uri(path = '', query_params = {})
|
142
|
+
path = "/#{path}".gsub(%r{/+}, '/')
|
143
|
+
uri = URI.parse(@config.base_url + path)
|
144
|
+
add_query_params(uri, query_params)
|
145
|
+
forced_query_params = path.split('?')[1]
|
146
|
+
return URI.join(uri, '?' + forced_query_params) if forced_query_params
|
147
|
+
|
148
|
+
uri
|
149
|
+
end
|
150
|
+
|
151
|
+
def create_http
|
152
|
+
uri = build_request_uri
|
153
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
154
|
+
http.use_ssl = @config.scheme == 'https'
|
155
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @config.verify_ssl_host
|
156
|
+
http.read_timeout = @config.timeout
|
157
|
+
http
|
158
|
+
end
|
159
|
+
|
160
|
+
# The default user agent
|
161
|
+
#
|
162
|
+
# @return [String] the default user agent
|
163
|
+
def default_user_agent
|
164
|
+
"SDK/#{VSphereAutomation::Runtime::VERSION} "\
|
165
|
+
"Ruby/#{RUBY_VERSION} "\
|
166
|
+
"(#{Gem::Platform.local.os}; "\
|
167
|
+
"#{Gem::Platform.local.version}; "\
|
168
|
+
"#{Gem::Platform.local.cpu})"
|
169
|
+
end
|
170
|
+
|
171
|
+
# Deserialize the response to the given return type
|
172
|
+
#
|
173
|
+
# @param [Response] response the HTTP response
|
174
|
+
# @param [String] return_type the type to return
|
175
|
+
# @return [Object] the response represented as the return type
|
176
|
+
def deserialize(response, return_type)
|
177
|
+
body = response.body
|
178
|
+
|
179
|
+
return nil if body.nil? || body.empty? || return_type.nil?
|
180
|
+
|
181
|
+
begin
|
182
|
+
data = JSON.parse("[#{body}]", symbolize_names: true).first
|
183
|
+
rescue JSON::ParserError => e
|
184
|
+
raise e unless %w[String Date DateTime].include?(return_type)
|
185
|
+
|
186
|
+
data = body
|
187
|
+
end
|
188
|
+
|
189
|
+
convert_to_type(data, return_type)
|
190
|
+
end
|
191
|
+
|
192
|
+
def convert_to_type(data, return_type)
|
193
|
+
return nil if data.nil?
|
194
|
+
case return_type
|
195
|
+
when 'String'
|
196
|
+
data.to_s
|
197
|
+
when 'Integer'
|
198
|
+
data.to_i
|
199
|
+
when 'Float'
|
200
|
+
data.to_f
|
201
|
+
when 'BOOLEAN'
|
202
|
+
data == true
|
203
|
+
when 'DateTime'
|
204
|
+
# parse date time (expecting ISO 8601 format)
|
205
|
+
DateTime.parse data
|
206
|
+
when 'Date'
|
207
|
+
# parse date time (expecting ISO 8601 format)
|
208
|
+
Date.parse data
|
209
|
+
when 'Object'
|
210
|
+
# generic object (usually a Hash), return directly
|
211
|
+
data
|
212
|
+
when /\AArray<(.+)>\z/
|
213
|
+
# e.g. Array<Pet>
|
214
|
+
sub_type = $1
|
215
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
216
|
+
when /\AHash\<String, (.+)\>\z/
|
217
|
+
# e.g. Hash<String, Integer>
|
218
|
+
sub_type = $1
|
219
|
+
{}.tap do |hash|
|
220
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
221
|
+
end
|
222
|
+
else
|
223
|
+
# models, e.g. Pet
|
224
|
+
VSphereAutomation.const_get(return_type).new.tap do |model|
|
225
|
+
model.build_from_hash data
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
# Create the Cookie header from a response
|
231
|
+
#
|
232
|
+
# @param response [Net::HTTPResponse] the response
|
233
|
+
# @return [Hash, nil] the Cookie header
|
234
|
+
def cookie_from_response(response)
|
235
|
+
{ 'Cookie' => response['set-cookie'] } if response['set-cookie']
|
236
|
+
end
|
237
|
+
|
238
|
+
def api_key_from_response(response)
|
239
|
+
key = @config.auth_settings['api_key'][:key]
|
240
|
+
@config.api_key[key] = response[key] if response[key]
|
241
|
+
end
|
242
|
+
|
243
|
+
def api_key_from_cookie(auth)
|
244
|
+
return if @cookie.nil?
|
245
|
+
|
246
|
+
regex = /(?<key>#{auth[:key]})=(?<value>\w+)/
|
247
|
+
matches = Hash(@cookie)['Cookie'].match(regex)
|
248
|
+
key = @config.auth_settings['api_key'][:key]
|
249
|
+
@config.api_key[key] = matches[:value] if matches
|
250
|
+
end
|
251
|
+
|
252
|
+
# An instance of the object in it's default state
|
253
|
+
DEFAULT = new
|
254
|
+
|
255
|
+
# The default MIME type for Content-Type and Accept headers
|
256
|
+
DEFAULT_MIME_TYPE = 'application/json'
|
257
|
+
|
258
|
+
private_constant :DEFAULT, :DEFAULT_MIME_TYPE
|
259
|
+
end
|
260
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# vapi - vAPI is an extensible API Platform for modelling and delivering APIs/SDKs/CLIs.
|
7
|
+
|
8
|
+
|
9
|
+
module VSphereAutomation
|
10
|
+
class ApiError < StandardError
|
11
|
+
attr_reader :code, :response_headers, :response_body
|
12
|
+
|
13
|
+
# Usage examples:
|
14
|
+
# ApiError.new
|
15
|
+
# ApiError.new("message")
|
16
|
+
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
|
17
|
+
# ApiError.new(:code => 404, :message => "Not Found")
|
18
|
+
def initialize(arg = nil)
|
19
|
+
if arg.is_a? Hash
|
20
|
+
if arg.key?(:message) || arg.key?('message')
|
21
|
+
super(arg[:message] || arg['message'])
|
22
|
+
else
|
23
|
+
super arg
|
24
|
+
end
|
25
|
+
|
26
|
+
arg.each do |k, v|
|
27
|
+
instance_variable_set "@#{k}", v
|
28
|
+
end
|
29
|
+
else
|
30
|
+
super arg
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# vapi - vAPI is an extensible API Platform for modelling and delivering APIs/SDKs/CLIs.
|
7
|
+
|
8
|
+
|
9
|
+
require 'uri'
|
10
|
+
|
11
|
+
module VSphereAutomation
|
12
|
+
class Configuration
|
13
|
+
# Defines url scheme
|
14
|
+
attr_accessor :scheme
|
15
|
+
|
16
|
+
# Defines url host
|
17
|
+
attr_accessor :host
|
18
|
+
|
19
|
+
# Defines url base path
|
20
|
+
attr_accessor :base_path
|
21
|
+
|
22
|
+
# Defines API keys used with API Key authentications.
|
23
|
+
#
|
24
|
+
# @return [Hash] key: parameter name, value: parameter value (API key)
|
25
|
+
#
|
26
|
+
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
27
|
+
# config.api_key['api_key'] = 'xxx'
|
28
|
+
attr_accessor :api_key
|
29
|
+
|
30
|
+
# Defines API key prefixes used with API Key authentications.
|
31
|
+
#
|
32
|
+
# @return [Hash] key: parameter name, value: API key prefix
|
33
|
+
#
|
34
|
+
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
35
|
+
# config.api_key_prefix['api_key'] = 'Token'
|
36
|
+
attr_accessor :api_key_prefix
|
37
|
+
|
38
|
+
# Defines the username used with HTTP basic authentication.
|
39
|
+
#
|
40
|
+
# @return [String]
|
41
|
+
attr_accessor :username
|
42
|
+
|
43
|
+
# Defines the password used with HTTP basic authentication.
|
44
|
+
#
|
45
|
+
# @return [String]
|
46
|
+
attr_accessor :password
|
47
|
+
|
48
|
+
# Defines the access token (Bearer) used with OAuth2.
|
49
|
+
attr_accessor :access_token
|
50
|
+
|
51
|
+
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
52
|
+
# details will be logged with `logger.debug` (see the `logger` attribute).
|
53
|
+
# Default to false.
|
54
|
+
#
|
55
|
+
# @return [true, false]
|
56
|
+
attr_accessor :debugging
|
57
|
+
|
58
|
+
# Defines the logger used for debugging.
|
59
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
60
|
+
#
|
61
|
+
# @return [#debug]
|
62
|
+
attr_accessor :logger
|
63
|
+
|
64
|
+
# Defines the temporary folder to store downloaded files
|
65
|
+
# (for API endpoints that have file response).
|
66
|
+
# Default to use `Tempfile`.
|
67
|
+
#
|
68
|
+
# @return [String]
|
69
|
+
attr_accessor :temp_folder_path
|
70
|
+
|
71
|
+
# The time limit for HTTP request in seconds.
|
72
|
+
# Default to 0 (never times out).
|
73
|
+
attr_accessor :timeout
|
74
|
+
|
75
|
+
# Set this to false to skip client side validation in the operation.
|
76
|
+
# Default to true.
|
77
|
+
# @return [true, false]
|
78
|
+
attr_accessor :client_side_validation
|
79
|
+
|
80
|
+
### TLS/SSL setting
|
81
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
82
|
+
# Default to true.
|
83
|
+
#
|
84
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
85
|
+
#
|
86
|
+
# @return [true, false]
|
87
|
+
attr_accessor :verify_ssl
|
88
|
+
|
89
|
+
### TLS/SSL setting
|
90
|
+
# Set this to false to skip verifying SSL host name
|
91
|
+
# Default to true.
|
92
|
+
#
|
93
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
94
|
+
#
|
95
|
+
# @return [true, false]
|
96
|
+
attr_accessor :verify_ssl_host
|
97
|
+
|
98
|
+
### TLS/SSL setting
|
99
|
+
# Set this to customize the certificate file to verify the peer.
|
100
|
+
#
|
101
|
+
# @return [String] the path to the certificate file
|
102
|
+
#
|
103
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
104
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
105
|
+
attr_accessor :ssl_ca_cert
|
106
|
+
|
107
|
+
### TLS/SSL setting
|
108
|
+
# Client certificate file (for client certificate)
|
109
|
+
attr_accessor :cert_file
|
110
|
+
|
111
|
+
### TLS/SSL setting
|
112
|
+
# Client private key file (for client certificate)
|
113
|
+
attr_accessor :key_file
|
114
|
+
|
115
|
+
# Set this to customize parameters encoding of array parameter with multi collectionFormat.
|
116
|
+
# Default to nil.
|
117
|
+
#
|
118
|
+
# @see The params_encoding option of Ethon. Related source code:
|
119
|
+
# https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
|
120
|
+
attr_accessor :params_encoding
|
121
|
+
|
122
|
+
attr_accessor :inject_format
|
123
|
+
|
124
|
+
attr_accessor :force_ending_format
|
125
|
+
|
126
|
+
def initialize
|
127
|
+
@scheme = 'https'
|
128
|
+
@host = '<vcenter>'
|
129
|
+
@base_path = '/rest'
|
130
|
+
@api_key = {}
|
131
|
+
@api_key_prefix = {}
|
132
|
+
@timeout = nil
|
133
|
+
@client_side_validation = true
|
134
|
+
@verify_ssl = true
|
135
|
+
@verify_ssl_host = true
|
136
|
+
@params_encoding = :multi
|
137
|
+
@cert_file = nil
|
138
|
+
@key_file = nil
|
139
|
+
@debugging = false
|
140
|
+
@inject_format = false
|
141
|
+
@force_ending_format = false
|
142
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
143
|
+
|
144
|
+
yield(self) if block_given?
|
145
|
+
end
|
146
|
+
|
147
|
+
# The default Configuration object.
|
148
|
+
def self.default
|
149
|
+
@@default ||= Configuration.new
|
150
|
+
end
|
151
|
+
|
152
|
+
def configure
|
153
|
+
yield(self) if block_given?
|
154
|
+
end
|
155
|
+
|
156
|
+
def scheme=(scheme)
|
157
|
+
# remove :// from scheme
|
158
|
+
@scheme = scheme.sub(/:\/\//, '')
|
159
|
+
end
|
160
|
+
|
161
|
+
def host=(host)
|
162
|
+
# remove http(s):// and anything after a slash
|
163
|
+
@host = host.sub(/https?:\/\//, '').split('/').first
|
164
|
+
end
|
165
|
+
|
166
|
+
def base_path=(base_path)
|
167
|
+
# Add leading and trailing slashes to base_path
|
168
|
+
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
169
|
+
@base_path = '' if @base_path == '/'
|
170
|
+
end
|
171
|
+
|
172
|
+
def base_url
|
173
|
+
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
|
174
|
+
URI.encode(url)
|
175
|
+
end
|
176
|
+
|
177
|
+
# Gets API key (with prefix if set).
|
178
|
+
# @param [String] param_name the parameter name of API key auth
|
179
|
+
def api_key_with_prefix(param_name)
|
180
|
+
if @api_key_prefix[param_name]
|
181
|
+
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
182
|
+
else
|
183
|
+
@api_key[param_name]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Gets Basic Auth token string
|
188
|
+
def basic_auth_token
|
189
|
+
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
190
|
+
end
|
191
|
+
|
192
|
+
# Returns Auth Settings hash for api client.
|
193
|
+
def auth_settings
|
194
|
+
{
|
195
|
+
'api_key' =>
|
196
|
+
{
|
197
|
+
type: 'api_key',
|
198
|
+
in: 'header',
|
199
|
+
key: 'vmware-api-session-id',
|
200
|
+
value: api_key_with_prefix('vmware-api-session-id')
|
201
|
+
},
|
202
|
+
'basic_auth' =>
|
203
|
+
{
|
204
|
+
type: 'basic',
|
205
|
+
in: 'header',
|
206
|
+
key: 'Authorization',
|
207
|
+
value: basic_auth_token
|
208
|
+
},
|
209
|
+
}
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
Binary file
|
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe VSphereAutomation::ApiClient do
|
4
|
+
describe '.default' do
|
5
|
+
it 'returns the same instance every time' do
|
6
|
+
first_instance = VSphereAutomation::ApiClient.default
|
7
|
+
second_instance = VSphereAutomation::ApiClient.default
|
8
|
+
third_instance = VSphereAutomation::ApiClient.default
|
9
|
+
expect(first_instance).to be(second_instance)
|
10
|
+
expect(first_instance).to be(third_instance)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#build_collection_param' do
|
15
|
+
context 'when called with format :multi' do
|
16
|
+
it 'returns the collection' do
|
17
|
+
params = { foo: 'bar', baz: 'quux' }
|
18
|
+
expect(subject.build_collection_param(params, :multi)).to eq(params)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#call_api' do
|
24
|
+
let(:host) { 'example.com' }
|
25
|
+
let(:config) do
|
26
|
+
VSphereAutomation::Configuration.new.tap do |c|
|
27
|
+
c.scheme = 'https'
|
28
|
+
c.host = host
|
29
|
+
end
|
30
|
+
end
|
31
|
+
let(:url) { 'https://example.com/rest/test' }
|
32
|
+
subject { VSphereAutomation::ApiClient.new(config) }
|
33
|
+
|
34
|
+
it 'adds headers to request' do
|
35
|
+
headers = { 'foo' => 'bar' }
|
36
|
+
stub_request(:get, url).with(headers: headers)
|
37
|
+
|
38
|
+
subject.call_api(:GET, '/test', header_params: headers)
|
39
|
+
|
40
|
+
expect(a_request(:get, url).with(headers: headers)).to have_been_made
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'adds query parameters to request' do
|
44
|
+
query_params = { 'foo' => 'bar' }
|
45
|
+
stub_request(:get, url).with(query: query_params)
|
46
|
+
|
47
|
+
subject.call_api(:GET, '/test', query_params: query_params)
|
48
|
+
|
49
|
+
expect(a_request(:get, url).with(query: query_params)).to have_been_made
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'keeps query parameters from the path to request' do
|
53
|
+
# The specs have some paths that contain query parameters already. This
|
54
|
+
# test is to make sure they don't get overwritten.
|
55
|
+
key = '~foo'
|
56
|
+
value = 'bar'
|
57
|
+
query_params = { key => value }
|
58
|
+
stub_request(:post, url).with(query: query_params)
|
59
|
+
|
60
|
+
subject.call_api(:POST, "/test?#{key}=#{value}")
|
61
|
+
|
62
|
+
expect(a_request(:post, url).with(query: query_params)).to have_been_made
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'adds form parameters to the request body' do
|
66
|
+
form_params = { 'foo' => 'bar' }
|
67
|
+
body = form_params.to_a.map { |e| e.join('=') }.join('&')
|
68
|
+
stub_request(:get, url).with(body: body)
|
69
|
+
|
70
|
+
subject.call_api(:GET, '/test', form_params: form_params)
|
71
|
+
|
72
|
+
expect(a_request(:get, url).with(body: body)).to have_been_made
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'uses basic auth information from configuration' do
|
76
|
+
auth_name = 'basic_auth'
|
77
|
+
auth_header = { config.auth_settings[auth_name][:key] =>
|
78
|
+
config.auth_settings[auth_name][:value] }
|
79
|
+
stub_request(:get, url).with(headers: auth_header)
|
80
|
+
|
81
|
+
subject.call_api(:GET, '/test', auth_names: [auth_name])
|
82
|
+
|
83
|
+
expect(a_request(:get, url)
|
84
|
+
.with(headers: auth_header)).to have_been_made
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'use API key information from configuration' do
|
88
|
+
auth_name = 'api_key'
|
89
|
+
config.api_key[config.auth_settings[auth_name][:key]] = 'foo'
|
90
|
+
auth_header = { config.auth_settings[auth_name][:key] =>
|
91
|
+
config.auth_settings[auth_name][:value] }
|
92
|
+
stub_request(:get, url).with(headers: auth_header)
|
93
|
+
|
94
|
+
subject.call_api(:GET, '/test', auth_names: [auth_name])
|
95
|
+
|
96
|
+
expect(a_request(:get, url)
|
97
|
+
.with(headers: auth_header)).to have_been_made
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'updates api_key from responses with set-cookie header' do
|
101
|
+
key = config.auth_settings['api_key'][:key]
|
102
|
+
value = 'foo'
|
103
|
+
cookie = "#{key}=#{value};Path=/rest;Secure;HttpOnly"
|
104
|
+
set_cookie_header = { 'set-cookie' => cookie }
|
105
|
+
stub_request(:get, url + '1').to_return(headers: set_cookie_header)
|
106
|
+
stub_request(:get, url + '2')
|
107
|
+
|
108
|
+
subject.call_api(:GET, '/test1')
|
109
|
+
subject.call_api(:GET, '/test2', auth_names: ['api_key'])
|
110
|
+
|
111
|
+
expect(a_request(:get, url + '1')).to have_been_made
|
112
|
+
expect(a_request(:get, url + '2')).to have_been_made
|
113
|
+
expect(subject.config.api_key[key]).to eq(value)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'updates api_key from responses with api_key header' do
|
117
|
+
key = config.auth_settings['api_key'][:key]
|
118
|
+
value = 'foo'
|
119
|
+
auth_header = { key => value }
|
120
|
+
|
121
|
+
stub_request(:get, url + '1').to_return(headers: auth_header)
|
122
|
+
stub_request(:get, url + '2').with(headers: auth_header)
|
123
|
+
|
124
|
+
subject.call_api(:GET, '/test1')
|
125
|
+
subject.call_api(:GET, '/test2', auth_names: ['api_key'])
|
126
|
+
|
127
|
+
expect(a_request(:get, url + '1')).to have_been_made
|
128
|
+
expect(a_request(:get, url + '2')
|
129
|
+
.with(headers: auth_header)).to have_been_made
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'adds the body to requests when available' do
|
133
|
+
body = { foo: 'bar' }.to_json
|
134
|
+
stub_request(:post, url).with(body: body)
|
135
|
+
|
136
|
+
subject.call_api(:POST, '/test', body: body)
|
137
|
+
|
138
|
+
expect(a_request(:post, url).with(body: body)).to have_been_made
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'sets the Content-Type on requests when available' do
|
142
|
+
content_type = { 'Content-Type' => 'application/json' }
|
143
|
+
body = { foo: 'bar' }.to_json
|
144
|
+
|
145
|
+
stub_request(:post, url).with(body: body, headers: content_type)
|
146
|
+
|
147
|
+
subject.call_api(:POST, '/test', body: body, header_params: content_type)
|
148
|
+
|
149
|
+
expect(a_request(:post, url)
|
150
|
+
.with(body: body, headers: content_type)).to have_been_made
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#object_to_http_body' do
|
155
|
+
context 'when given nil' do
|
156
|
+
it 'returns the object as is' do
|
157
|
+
expect(subject.object_to_http_body(nil)).to be_nil
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when given a string' do
|
162
|
+
it 'returns the object as is' do
|
163
|
+
expect(subject.object_to_http_body('asdf')).to eq('asdf')
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'when given an object that responds to `to_hash`' do
|
168
|
+
it 'returns that object as JSON' do
|
169
|
+
obj = OpenStruct.new(foo: 'bar', baz: 'quux')
|
170
|
+
obj.to_hash = obj.to_h
|
171
|
+
json = obj.to_hash.to_json
|
172
|
+
|
173
|
+
expect(subject.object_to_http_body(obj)).to eq(json)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'when given an array' do
|
178
|
+
it 'returns a JSON array of objects' do
|
179
|
+
obj = OpenStruct.new(foo: 'bar', baz: 'quux')
|
180
|
+
obj.to_hash = obj.to_h
|
181
|
+
json = obj.to_hash.to_json
|
182
|
+
|
183
|
+
expect(subject.object_to_http_body([obj])).to eq([json])
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe '#select_header_accept' do
|
189
|
+
context 'when given anything other than an array' do
|
190
|
+
it 'returns application/json as a default' do
|
191
|
+
expect(subject.select_header_accept(nil)).to eq('application/json')
|
192
|
+
expect(subject.select_header_accept('')).to eq('application/json')
|
193
|
+
expect(subject.select_header_accept(1)).to eq('application/json')
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context 'when given a list of types' do
|
198
|
+
context 'that includes a JSON type' do
|
199
|
+
it 'returns the first type containing JSON' do
|
200
|
+
xml = 'application/xml'
|
201
|
+
json = 'application/json'
|
202
|
+
|
203
|
+
expect(subject.select_header_accept([xml, json])).to eq(json)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context 'that does not include a JSON type' do
|
208
|
+
it 'returns the all of the types' do
|
209
|
+
xml = 'application/xml'
|
210
|
+
html = 'text/html'
|
211
|
+
result = [xml, html].join(', ')
|
212
|
+
|
213
|
+
expect(subject.select_header_accept([xml, html])).to eq(result)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe '#select_header_content_type' do
|
220
|
+
context 'when given anything other than an array' do
|
221
|
+
it 'returns application/json as a default' do
|
222
|
+
expect(subject.select_header_content_type(nil)).to eq('application/json')
|
223
|
+
expect(subject.select_header_content_type('')).to eq('application/json')
|
224
|
+
expect(subject.select_header_content_type(1)).to eq('application/json')
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context 'when given a list of types' do
|
229
|
+
context 'that includes a JSON type' do
|
230
|
+
it 'returns the first type containing JSON' do
|
231
|
+
xml = 'application/xml'
|
232
|
+
json = 'application/json'
|
233
|
+
|
234
|
+
expect(subject.select_header_content_type([xml, json])).to eq(json)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
context 'that does not include a JSON type' do
|
239
|
+
it 'returns the first type' do
|
240
|
+
xml = 'application/xml'
|
241
|
+
html = 'text/html'
|
242
|
+
|
243
|
+
expect(subject.select_header_content_type([xml, html])).to eq(xml)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright (c) 2018-2019 VMware, Inc. All Rights Reserved.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
|
4
|
+
# DO NOT MODIFY. THIS CODE IS GENERATED. CHANGES WILL BE OVERWRITTEN.
|
5
|
+
|
6
|
+
# vapi - vAPI is an extensible API Platform for modelling and delivering APIs/SDKs/CLIs.
|
7
|
+
|
8
|
+
|
9
|
+
require 'spec_helper'
|
10
|
+
|
11
|
+
describe VSphereAutomation::Configuration do
|
12
|
+
let(:config) { VSphereAutomation::Configuration.default }
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
# uncomment below to setup host and base_path
|
16
|
+
# require 'URI'
|
17
|
+
# uri = URI.parse("https://<vcenter>/rest")
|
18
|
+
# VSphereAutomation.configure do |c|
|
19
|
+
# c.host = uri.host
|
20
|
+
# c.base_path = uri.path
|
21
|
+
# end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#base_url' do
|
25
|
+
it 'should have the default value' do
|
26
|
+
# uncomment below to test default value of the base path
|
27
|
+
# expect(config.base_url).to eq("https://<vcenter>/rest")
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should remove trailing slashes' do
|
31
|
+
[nil, '', '/', '//'].each do |base_path|
|
32
|
+
config.base_path = base_path
|
33
|
+
# uncomment below to test trailing slashes
|
34
|
+
# expect(config.base_url).to eq("https://<vcenter>/rest")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.description = "A Ruby SDK for the vSphere APIs (VAPI)"
|
23
23
|
s.license = 'MIT'
|
24
24
|
s.required_ruby_version = ">= 2.3"
|
25
|
-
s.add_runtime_dependency 'vsphere-automation-runtime', '~> 0.4.
|
26
|
-
s.add_runtime_dependency 'vsphere-automation-cis', '~> 0.4.
|
25
|
+
s.add_runtime_dependency 'vsphere-automation-runtime', '~> 0.4.5'
|
26
|
+
s.add_runtime_dependency 'vsphere-automation-cis', '~> 0.4.5'
|
27
27
|
|
28
28
|
s.add_development_dependency 'bundler', '~> 2.0'
|
29
29
|
s.add_development_dependency 'pry', '~> 0.12.2'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vsphere-automation-vapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J.R. Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: vsphere-automation-runtime
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.4.
|
19
|
+
version: 0.4.5
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.4.
|
26
|
+
version: 0.4.5
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: vsphere-automation-cis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.4.
|
33
|
+
version: 0.4.5
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.4.
|
40
|
+
version: 0.4.5
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -328,6 +328,9 @@ files:
|
|
328
328
|
- lib/vsphere-automation-vapi/api/rest_navigation_root_api.rb
|
329
329
|
- lib/vsphere-automation-vapi/api/rest_navigation_service_api.rb
|
330
330
|
- lib/vsphere-automation-vapi/api/vcenter_activation_api.rb
|
331
|
+
- lib/vsphere-automation-vapi/api_client.rb
|
332
|
+
- lib/vsphere-automation-vapi/api_error.rb
|
333
|
+
- lib/vsphere-automation-vapi/configuration.rb
|
331
334
|
- lib/vsphere-automation-vapi/models/vapi_metadata_authentication_authentication_info.rb
|
332
335
|
- lib/vsphere-automation-vapi/models/vapi_metadata_authentication_authentication_info_scheme_type.rb
|
333
336
|
- lib/vsphere-automation-vapi/models/vapi_metadata_authentication_component_data.rb
|
@@ -463,6 +466,7 @@ files:
|
|
463
466
|
- pkg/vsphere-automation-vapi-0.4.1.gem
|
464
467
|
- pkg/vsphere-automation-vapi-0.4.2.gem
|
465
468
|
- pkg/vsphere-automation-vapi-0.4.3.gem
|
469
|
+
- pkg/vsphere-automation-vapi-0.4.4.gem
|
466
470
|
- spec/api/metadata_authentication_component_api_spec.rb
|
467
471
|
- spec/api/metadata_authentication_package_api_spec.rb
|
468
472
|
- spec/api/metadata_authentication_service_api_spec.rb
|
@@ -487,6 +491,8 @@ files:
|
|
487
491
|
- spec/api/rest_navigation_root_api_spec.rb
|
488
492
|
- spec/api/rest_navigation_service_api_spec.rb
|
489
493
|
- spec/api/vcenter_activation_api_spec.rb
|
494
|
+
- spec/api_client_spec.rb
|
495
|
+
- spec/configuration_spec.rb
|
490
496
|
- spec/models/vapi_metadata_authentication_authentication_info_scheme_type_spec.rb
|
491
497
|
- spec/models/vapi_metadata_authentication_authentication_info_spec.rb
|
492
498
|
- spec/models/vapi_metadata_authentication_component_data_spec.rb
|
@@ -668,6 +674,8 @@ test_files:
|
|
668
674
|
- spec/api/rest_navigation_root_api_spec.rb
|
669
675
|
- spec/api/metadata_metamodel_resource_api_spec.rb
|
670
676
|
- spec/api/vcenter_activation_api_spec.rb
|
677
|
+
- spec/api_client_spec.rb
|
678
|
+
- spec/configuration_spec.rb
|
671
679
|
- spec/models/vapi_rest_navigation_link_spec.rb
|
672
680
|
- spec/models/vapi_rest_navigation_root_result_spec.rb
|
673
681
|
- spec/models/vapi_metadata_cli_command_formatter_type_spec.rb
|