vsphere-automation-content 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78f6b35831e4eb59d6a72b98a44cfe39a9da8e1516d4a83aca215703449d363d
4
- data.tar.gz: 9ccefaa1e93441875e02b92e0a2e40c41ecceadb6a55734b9e6703cb1e59507f
3
+ metadata.gz: 9e66274297cb65e4f44c975ceda45b8381af6cdf9bbfe3cc9108f5e7760b793a
4
+ data.tar.gz: a32cfc105c54b190b3af976b9663a445fe9181e50084a7e85f919199eee6020c
5
5
  SHA512:
6
- metadata.gz: 37a9fda73fbc2f33fe22cb36fbb44655afddf9cd1917493ce02bc561bd47bd7e6a090c16b5b4dc0b5f5f09cfdc5386564e6ed63e98e2d18fec0e9607f84035a0
7
- data.tar.gz: 19a8bf09d00606e2b3c2171394b4fadab8b833594fc334678f5c2a50947079039248fc18bac4eb1d4c8691f548da2c8487a57d0bad7128a93209eabb89b91940
6
+ metadata.gz: 183a6c840fc324e451d3512e2771d1e910a534cdd82ce609a41afe09b16effcd3dea977071d43b7ef9a5a4228ecdd3ff67e735c6413f546d434c55d5509ad728
7
+ data.tar.gz: 5370fa3ea1419bf959d214f8d7ff29fd6521eaa3f4402f47254d97ae6fbf8af86e176e8d7888294582324c2b03b5dc3119b53600603dadf50c1595d56caeee22
data/Gemfile.lock CHANGED
@@ -1,20 +1,20 @@
1
1
  PATH
2
2
  remote: ../cis
3
3
  specs:
4
- vsphere-automation-cis (0.4.0)
5
- vsphere-automation-runtime (~> 0.4.0)
4
+ vsphere-automation-cis (0.4.1)
5
+ vsphere-automation-runtime (~> 0.4.1)
6
6
 
7
7
  PATH
8
8
  remote: ../runtime
9
9
  specs:
10
- vsphere-automation-runtime (0.4.0)
10
+ vsphere-automation-runtime (0.4.1)
11
11
 
12
12
  PATH
13
13
  remote: .
14
14
  specs:
15
- vsphere-automation-content (0.4.0)
16
- vsphere-automation-cis (~> 0.4.0)
17
- vsphere-automation-runtime (~> 0.4.0)
15
+ vsphere-automation-content (0.4.1)
16
+ vsphere-automation-cis (~> 0.4.1)
17
+ vsphere-automation-runtime (~> 0.4.1)
18
18
 
19
19
  GEM
20
20
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  The Ruby gem for the vSphere Content API
4
4
 
5
5
  - API version: 2.0.0
6
- - Package version: 0.4.0
6
+ - Package version: 0.4.1
7
7
 
8
8
  ## Installation
9
9
 
@@ -0,0 +1,253 @@
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
+
10
+ module VSphereAutomation
11
+ # The client responsible for communicating with the API.
12
+ class ApiClient
13
+ # The Configuration object holding settings to be used in the API client.
14
+ attr_accessor :config
15
+
16
+ # Defines the headers to be used in HTTP requests of all API calls by
17
+ # default.
18
+ #
19
+ # @return [Hash]
20
+ attr_reader :default_headers
21
+
22
+ # Creates a new instance
23
+ #
24
+ # @param config [Configuration] configuration object with values to use
25
+ def initialize(config = Configuration.default)
26
+ @config = config
27
+ @http = create_http
28
+ @user_agent = default_user_agent
29
+ @default_headers = { 'Content-Type' => 'application/json',
30
+ 'User-Agent' => @user_agent }
31
+ end
32
+
33
+ # Retrieves an instance of the object in it's default state
34
+ #
35
+ # @return [ApiClient] an instance of the object in it's default state
36
+ def self.default
37
+ DEFAULT
38
+ end
39
+
40
+ # Build the collection of parameters
41
+ def build_collection_param(params, format)
42
+ params
43
+ end
44
+
45
+ # Make a request to an API endpoint with the given options
46
+ #
47
+ # @param http_method [Symbol] the HTTP method to be used
48
+ # @param path [String] the path request will be made to
49
+ # @param opts [Hash] any additional options needed
50
+ # @return [Array<(Object, Fixnum, Hash)>] the deserialized body, status
51
+ # code, and headers.
52
+ def call_api(http_method, path, opts = {})
53
+ query_params = opts.fetch(:query_params, {})
54
+ header_params = opts.fetch(:header_params, {})
55
+ form_params = opts.fetch(:form_params, {})
56
+ add_auth(query_params, header_params, opts.fetch(:auth_names, []))
57
+
58
+ uri = build_request_uri(path, query_params)
59
+ request = Net::HTTP.const_get(http_method.capitalize).new(uri)
60
+
61
+ add_form_params(request, form_params)
62
+ add_header_params(request, header_params)
63
+ request.body = opts[:body] if opts[:body]
64
+ request.content_type = request['Content-Type'] if request['Content-Type']
65
+
66
+ if @config.debugging
67
+ @config.logger.debug("Request Body:\n#{request.body}\n")
68
+ end
69
+
70
+ response = @http.request(request)
71
+ @cookie = cookie_from_response(response)
72
+ api_key_from_response(response)
73
+
74
+ return_type = opts.fetch(:return_type, {}).fetch(response.code, nil)
75
+ data = deserialize(response, return_type)
76
+ [data, Integer(response.code), response.each_header.to_h]
77
+ end
78
+
79
+ # Takes an object and returns the object as an HTTP body
80
+ #
81
+ # @param object [Object] object to transform
82
+ # @return [String] object as JSON string
83
+ def object_to_http_body(object)
84
+ return object.map { |o| object_to_http_body(o) } if object.is_a?(Array)
85
+
86
+ return object unless object.respond_to?(:to_hash)
87
+
88
+ object.to_hash.to_json
89
+ end
90
+
91
+ # Select an Accept header to use
92
+ #
93
+ # @param types [Array] a list of suggested types
94
+ # @return [String] the Accept header value
95
+ def select_header_accept(types)
96
+ return DEFAULT_MIME_TYPE unless types.is_a?(Array)
97
+
98
+ types.find { |t| t.include?('json') } || types.join(', ')
99
+ end
100
+
101
+ # Select an Content-Type header to use
102
+ #
103
+ # @param types [Array] a list of suggested types
104
+ # @return [String] the Content-Type header value
105
+ def select_header_content_type(types)
106
+ return DEFAULT_MIME_TYPE unless types.is_a?(Array)
107
+
108
+ types.find { |t| t.include?('json') } || types.first
109
+ end
110
+
111
+ private
112
+
113
+ def add_auth(query_params, header_params, auth_names)
114
+ auth_names.map do |name|
115
+ settings = @config.auth_settings.fetch(name, {})
116
+ case settings[:in]
117
+ when 'header'
118
+ header_params[settings[:key]] = settings[:value]
119
+ api_key_from_cookie(header_params, settings) unless settings[:value]
120
+ when 'query'
121
+ query_params[settings[:key]] = settings[:value]
122
+ end
123
+ end
124
+ end
125
+
126
+ def add_form_params(request, form_params)
127
+ request.set_form_data(form_params) unless form_params.empty?
128
+ end
129
+
130
+ def add_header_params(request, headers)
131
+ header_params = @default_headers.merge(headers, Hash(@cookie))
132
+ header_params.map { |name, value| request[name] = value }
133
+ end
134
+
135
+ def add_query_params(uri, query_params)
136
+ uri.query = URI.encode_www_form(query_params)
137
+ end
138
+
139
+ def build_request_uri(path = '', query_params = {})
140
+ path = "/#{path}".gsub(%r{/+}, '/')
141
+ uri = URI.parse(@config.base_url + path)
142
+ add_query_params(uri, query_params)
143
+ uri
144
+ end
145
+
146
+ def create_http
147
+ uri = build_request_uri
148
+ http = Net::HTTP.new(uri.host, uri.port)
149
+ http.use_ssl = @config.scheme == 'https'
150
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @config.verify_ssl_host
151
+ http
152
+ end
153
+
154
+ # The default user agent
155
+ #
156
+ # @return [String] the default user agent
157
+ def default_user_agent
158
+ "SDK/#{VSphereAutomation::Runtime::VERSION} "\
159
+ "Ruby/#{RUBY_VERSION} "\
160
+ "(#{Gem::Platform.local.os}; "\
161
+ "#{Gem::Platform.local.version}; "\
162
+ "#{Gem::Platform.local.cpu})"
163
+ end
164
+
165
+ # Deserialize the response to the given return type
166
+ #
167
+ # @param [Response] response the HTTP response
168
+ # @param [String] return_type the type to return
169
+ # @return [Object] the response represented as the return type
170
+ def deserialize(response, return_type)
171
+ body = response.body
172
+
173
+ return nil if body.nil? || body.empty? || return_type.nil?
174
+
175
+ begin
176
+ data = JSON.parse("[#{body}]", symbolize_names: true).first
177
+ rescue JSON::ParserError => e
178
+ raise e unless %w[String Date DateTime].include?(return_type)
179
+
180
+ data = body
181
+ end
182
+
183
+ convert_to_type(data, return_type)
184
+ end
185
+
186
+ def convert_to_type(data, return_type)
187
+ return nil if data.nil?
188
+ case return_type
189
+ when 'String'
190
+ data.to_s
191
+ when 'Integer'
192
+ data.to_i
193
+ when 'Float'
194
+ data.to_f
195
+ when 'BOOLEAN'
196
+ data == true
197
+ when 'DateTime'
198
+ # parse date time (expecting ISO 8601 format)
199
+ DateTime.parse data
200
+ when 'Date'
201
+ # parse date time (expecting ISO 8601 format)
202
+ Date.parse data
203
+ when 'Object'
204
+ # generic object (usually a Hash), return directly
205
+ data
206
+ when /\AArray<(.+)>\z/
207
+ # e.g. Array<Pet>
208
+ sub_type = $1
209
+ data.map { |item| convert_to_type(item, sub_type) }
210
+ when /\AHash\<String, (.+)\>\z/
211
+ # e.g. Hash<String, Integer>
212
+ sub_type = $1
213
+ {}.tap do |hash|
214
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
215
+ end
216
+ else
217
+ # models, e.g. Pet
218
+ VSphereAutomation.const_get(return_type).new.tap do |model|
219
+ model.build_from_hash data
220
+ end
221
+ end
222
+ end
223
+
224
+ # Create the Cookie header from a response
225
+ #
226
+ # @param response [Net::HTTPResponse] the response
227
+ # @return [Hash, nil] the Cookie header
228
+ def cookie_from_response(response)
229
+ { 'Cookie' => response['set-cookie'] } if response['set-cookie']
230
+ end
231
+
232
+ def api_key_from_response(response)
233
+ key = @config.auth_settings['api_key'][:key]
234
+ @config.api_key[key] = response[key] if response[key]
235
+ end
236
+
237
+ def api_key_from_cookie(headers, auth)
238
+ return if @cookie.nil?
239
+
240
+ regex = /(?<key>#{auth[:key]})=(?<value>\w+)/
241
+ matches = Hash(@cookie)['Cookie'].match(regex)
242
+ headers[matches[:key]] = matches[:value] if matches
243
+ end
244
+
245
+ # An instance of the object in it's default state
246
+ DEFAULT = new
247
+
248
+ # The default MIME type for Content-Type and Accept headers
249
+ DEFAULT_MIME_TYPE = 'application/json'
250
+
251
+ private_constant :DEFAULT, :DEFAULT_MIME_TYPE
252
+ end
253
+ 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
+ # content - VMware vSphere® Content Library empowers vSphere Admins to effectively manage VM templates, vApps, ISO images and scripts with ease.
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
+ # content - VMware vSphere® Content Library empowers vSphere Admins to effectively manage VM templates, vApps, ISO images and scripts with ease.
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 = '&lt;vcenter&gt;'
129
+ @base_path = '/rest'
130
+ @api_key = {}
131
+ @api_key_prefix = {}
132
+ @timeout = 0
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
@@ -8,6 +8,6 @@
8
8
 
9
9
  module VSphereAutomation
10
10
  module Content
11
- VERSION = '0.4.0'
11
+ VERSION = '0.4.1'
12
12
  end
13
13
  end
@@ -0,0 +1,238 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vsphere-automation-runtime/new_api_client'
4
+
5
+ describe VSphereAutomation::NewApiClient do
6
+ describe '.default' do
7
+ it 'returns the same instance every time' do
8
+ first_instance = VSphereAutomation::NewApiClient.default
9
+ second_instance = VSphereAutomation::NewApiClient.default
10
+ third_instance = VSphereAutomation::NewApiClient.default
11
+ expect(first_instance).to be(second_instance)
12
+ expect(first_instance).to be(third_instance)
13
+ end
14
+ end
15
+
16
+ describe '#build_collection_param' do
17
+ context 'when called with format :multi' do
18
+ it 'returns the collection' do
19
+ params = { foo: 'bar', baz: 'quux' }
20
+ expect(subject.build_collection_param(params, :multi)).to eq(params)
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#call_api' do
26
+ let(:host) { 'example.com' }
27
+ let(:config) do
28
+ VSphereAutomation::Configuration.new.tap do |c|
29
+ c.scheme = 'https'
30
+ c.host = host
31
+ end
32
+ end
33
+ let(:url) { 'https://example.com/rest/test' }
34
+ subject { VSphereAutomation::NewApiClient.new(config) }
35
+
36
+ it 'adds headers to request' do
37
+ headers = { 'foo' => 'bar' }
38
+ stub_request(:get, url).with(headers: headers)
39
+
40
+ subject.call_api(:GET, '/test', header_params: headers)
41
+
42
+ expect(a_request(:get, url).with(headers: headers)).to have_been_made
43
+ end
44
+
45
+ it 'adds query parameters to request' do
46
+ query_params = { 'foo' => 'bar' }
47
+ stub_request(:get, url).with(query: query_params)
48
+
49
+ subject.call_api(:GET, '/test', query_params: query_params)
50
+
51
+ expect(a_request(:get, url).with(query: query_params)).to have_been_made
52
+ end
53
+
54
+ it 'adds form parameters to the request body' do
55
+ form_params = { 'foo' => 'bar' }
56
+ body = form_params.to_a.map { |e| e.join('=') }.join('&')
57
+ stub_request(:get, url).with(body: body)
58
+
59
+ subject.call_api(:GET, '/test', form_params: form_params)
60
+
61
+ expect(a_request(:get, url).with(body: body)).to have_been_made
62
+ end
63
+
64
+ it 'uses basic auth information from configuration' do
65
+ auth_name = 'basic_auth'
66
+ auth_header = { config.auth_settings[auth_name][:key] =>
67
+ config.auth_settings[auth_name][:value] }
68
+ stub_request(:get, url).with(headers: auth_header)
69
+
70
+ subject.call_api(:GET, '/test', auth_names: [auth_name])
71
+
72
+ expect(a_request(:get, url)
73
+ .with(headers: auth_header)).to have_been_made
74
+ end
75
+
76
+ it 'use API key information from configuration' do
77
+ auth_name = 'api_key'
78
+ config.api_key[config.auth_settings[auth_name][:key]] = 'foo'
79
+ auth_header = { config.auth_settings[auth_name][:key] =>
80
+ config.auth_settings[auth_name][:value] }
81
+ stub_request(:get, url).with(headers: auth_header)
82
+
83
+ subject.call_api(:GET, '/test', auth_names: [auth_name])
84
+
85
+ expect(a_request(:get, url)
86
+ .with(headers: auth_header)).to have_been_made
87
+ end
88
+
89
+ it 'updates api_key from responses with set-cookie header' do
90
+ key = config.auth_settings['api_key'][:key]
91
+ value = 'foo'
92
+ cookie = "#{key}=#{value};Path=/rest;Secure;HttpOnly"
93
+ set_cookie_header = { 'set-cookie' => cookie }
94
+ auth_header = { key => value }
95
+ stub_request(:get, url + '1').to_return(headers: set_cookie_header)
96
+ stub_request(:get, url + '2').with(headers: auth_header)
97
+
98
+ subject.call_api(:GET, '/test1')
99
+ subject.call_api(:GET, '/test2', auth_names: ['api_key'])
100
+
101
+ expect(a_request(:get, url + '1')).to have_been_made
102
+ expect(a_request(:get, url + '2')
103
+ .with(headers: auth_header)).to have_been_made
104
+ end
105
+
106
+ it 'updates api_key from responses with api_key header' do
107
+ key = config.auth_settings['api_key'][:key]
108
+ value = 'foo'
109
+ auth_header = { key => value }
110
+
111
+ stub_request(:get, url + '1').to_return(headers: auth_header)
112
+ stub_request(:get, url + '2').with(headers: auth_header)
113
+
114
+ subject.call_api(:GET, '/test1')
115
+ subject.call_api(:GET, '/test2', auth_names: ['api_key'])
116
+
117
+ expect(a_request(:get, url + '1')).to have_been_made
118
+ expect(a_request(:get, url + '2')
119
+ .with(headers: auth_header)).to have_been_made
120
+ end
121
+
122
+ it 'adds the body to requests when available' do
123
+ body = { foo: 'bar' }.to_json
124
+ stub_request(:post, url).with(body: body)
125
+
126
+ subject.call_api(:POST, '/test', body: body)
127
+
128
+ expect(a_request(:post, url).with(body: body)).to have_been_made
129
+ end
130
+
131
+ it 'sets the Content-Type on requests when available' do
132
+ content_type = { 'Content-Type' => 'application/json' }
133
+ body = { foo: 'bar' }.to_json
134
+
135
+ stub_request(:post, url).with(body: body, headers: content_type)
136
+
137
+ subject.call_api(:POST, '/test', body: body, header_params: content_type)
138
+
139
+ expect(a_request(:post, url)
140
+ .with(body: body, headers: content_type)).to have_been_made
141
+ end
142
+ end
143
+
144
+ describe '#object_to_http_body' do
145
+ context 'when given nil' do
146
+ it 'returns the object as is' do
147
+ expect(subject.object_to_http_body(nil)).to be_nil
148
+ end
149
+ end
150
+
151
+ context 'when given a string' do
152
+ it 'returns the object as is' do
153
+ expect(subject.object_to_http_body('asdf')).to eq('asdf')
154
+ end
155
+ end
156
+
157
+ context 'when given an object that responds to `to_hash`' do
158
+ it 'returns that object as JSON' do
159
+ obj = OpenStruct.new(foo: 'bar', baz: 'quux')
160
+ obj.to_hash = obj.to_h
161
+ json = obj.to_hash.to_json
162
+
163
+ expect(subject.object_to_http_body(obj)).to eq(json)
164
+ end
165
+ end
166
+
167
+ context 'when given an array' do
168
+ it 'returns a JSON array of objects' 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
+ end
177
+
178
+ describe '#select_header_accept' do
179
+ context 'when given anything other than an array' do
180
+ it 'returns application/json as a default' do
181
+ expect(subject.select_header_accept(nil)).to eq('application/json')
182
+ expect(subject.select_header_accept('')).to eq('application/json')
183
+ expect(subject.select_header_accept(1)).to eq('application/json')
184
+ end
185
+ end
186
+
187
+ context 'when given a list of types' do
188
+ context 'that includes a JSON type' do
189
+ it 'returns the first type containing JSON' do
190
+ xml = 'application/xml'
191
+ json = 'application/json'
192
+
193
+ expect(subject.select_header_accept([xml, json])).to eq(json)
194
+ end
195
+ end
196
+
197
+ context 'that does not include a JSON type' do
198
+ it 'returns the all of the types' do
199
+ xml = 'application/xml'
200
+ html = 'text/html'
201
+ result = [xml, html].join(', ')
202
+
203
+ expect(subject.select_header_accept([xml, html])).to eq(result)
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ describe '#select_header_content_type' do
210
+ context 'when given anything other than an array' do
211
+ it 'returns application/json as a default' do
212
+ expect(subject.select_header_content_type(nil)).to eq('application/json')
213
+ expect(subject.select_header_content_type('')).to eq('application/json')
214
+ expect(subject.select_header_content_type(1)).to eq('application/json')
215
+ end
216
+ end
217
+
218
+ context 'when given a list of types' do
219
+ context 'that includes a JSON type' do
220
+ it 'returns the first type containing JSON' do
221
+ xml = 'application/xml'
222
+ json = 'application/json'
223
+
224
+ expect(subject.select_header_content_type([xml, json])).to eq(json)
225
+ end
226
+ end
227
+
228
+ context 'that does not include a JSON type' do
229
+ it 'returns the first type' do
230
+ xml = 'application/xml'
231
+ html = 'text/html'
232
+
233
+ expect(subject.select_header_content_type([xml, html])).to eq(xml)
234
+ end
235
+ end
236
+ end
237
+ end
238
+ 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
+ # content - VMware vSphere® Content Library empowers vSphere Admins to effectively manage VM templates, vApps, ISO images and scripts with ease.
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 (Content)"
23
23
  s.license = 'MIT'
24
24
  s.required_ruby_version = ">= 2.3"
25
- s.add_runtime_dependency 'vsphere-automation-runtime', '~> 0.4.0'
26
- s.add_runtime_dependency 'vsphere-automation-cis', '~> 0.4.0'
25
+ s.add_runtime_dependency 'vsphere-automation-runtime', '~> 0.4.1'
26
+ s.add_runtime_dependency 'vsphere-automation-cis', '~> 0.4.1'
27
27
 
28
28
  s.add_development_dependency 'bundler', '~> 2.0'
29
29
  s.add_development_dependency 'pry', '~> 0.12.2'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vsphere-automation-content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - J.R. Garcia
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.4.0
19
+ version: 0.4.1
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.0
26
+ version: 0.4.1
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.0
33
+ version: 0.4.1
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.0
40
+ version: 0.4.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -326,6 +326,9 @@ files:
326
326
  - lib/vsphere-automation-content/api/library_subscriptions_api.rb
327
327
  - lib/vsphere-automation-content/api/local_library_api.rb
328
328
  - lib/vsphere-automation-content/api/subscribed_library_api.rb
329
+ - lib/vsphere-automation-content/api_client.rb
330
+ - lib/vsphere-automation-content/api_error.rb
331
+ - lib/vsphere-automation-content/configuration.rb
329
332
  - lib/vsphere-automation-content/models/content_configuration_model.rb
330
333
  - lib/vsphere-automation-content/models/content_configuration_result.rb
331
334
  - lib/vsphere-automation-content/models/content_configuration_update.rb
@@ -478,7 +481,6 @@ files:
478
481
  - lib/vsphere-automation-content/models/vapi_std_errors_unsupported_error.rb
479
482
  - lib/vsphere-automation-content/models/vapi_std_localizable_message.rb
480
483
  - lib/vsphere-automation-content/version.rb
481
- - pkg/vsphere-automation-content-0.4.0.gem
482
484
  - spec/api/configuration_api_spec.rb
483
485
  - spec/api/library_api_spec.rb
484
486
  - spec/api/library_item_api_spec.rb
@@ -492,6 +494,8 @@ files:
492
494
  - spec/api/library_subscriptions_api_spec.rb
493
495
  - spec/api/local_library_api_spec.rb
494
496
  - spec/api/subscribed_library_api_spec.rb
497
+ - spec/api_client_spec.rb
498
+ - spec/configuration_spec.rb
495
499
  - spec/models/content_configuration_model_spec.rb
496
500
  - spec/models/content_configuration_result_spec.rb
497
501
  - spec/models/content_configuration_update_spec.rb
@@ -682,6 +686,8 @@ test_files:
682
686
  - spec/api/library_item_file_api_spec.rb
683
687
  - spec/api/library_item_download_session_api_spec.rb
684
688
  - spec/api/library_item_downloadsession_file_api_spec.rb
689
+ - spec/api_client_spec.rb
690
+ - spec/configuration_spec.rb
685
691
  - spec/models/content_library_item_file_list_result_spec.rb
686
692
  - spec/models/content_library_find_result_spec.rb
687
693
  - spec/models/content_library_item_downloadsession_file_list_result_spec.rb