lib/vra/client.rb in vmware-vra-1.0.0 vs lib/vra/client.rb in vmware-vra-1.1.0
- old
+ new
@@ -16,23 +16,24 @@
# limitations under the License.
#
require 'ffi_yajl'
require 'rest-client'
+require 'passwordmasker'
module Vra
# rubocop:disable ClassLength
class Client
attr_accessor :bearer_token
def initialize(opts)
@base_url = opts[:base_url]
@username = opts[:username]
- @password = opts[:password]
+ @password = PasswordMasker.new(opts[:password])
@tenant = opts[:tenant]
@verify_ssl = opts.fetch(:verify_ssl, true)
- @bearer_token = nil
+ @bearer_token = PasswordMasker.new(nil)
validate_client_options!
end
#########################
@@ -58,50 +59,50 @@
#
def bearer_token_request_body
{
'username' => @username,
- 'password' => @password,
+ 'password' => @password.value,
'tenant' => @tenant
}
end
def request_headers
headers = {}
headers['Accept'] = 'application/json'
headers['Content-Type'] = 'application/json'
- headers['Authorization'] = "Bearer #{@bearer_token}" unless @bearer_token.nil?
+ headers['Authorization'] = "Bearer #{@bearer_token.value}" unless @bearer_token.value.nil?
headers
end
def authorize!
generate_bearer_token unless authorized?
raise Vra::Exception::Unauthorized, 'Unable to authorize against vRA' unless authorized?
end
def authorized?
- return false if @bearer_token.nil?
+ return false if @bearer_token.value.nil?
- response = http_head("/identity/api/tokens/#{@bearer_token}", :skip_auth)
+ response = http_head("/identity/api/tokens/#{@bearer_token.value}", :skip_auth)
if response.code == 204
true
else
false
end
end
def generate_bearer_token
- @bearer_token = nil
+ @bearer_token.value = nil
validate_client_options!
response = http_post('/identity/api/tokens', bearer_token_request_body.to_json, :skip_auth)
if response.code != 200
raise Vra::Exception::Unauthorized, "Unable to get bearer token: #{response.body}"
end
- @bearer_token = FFI_Yajl::Parser.parse(response.body)['id']
+ @bearer_token.value = FFI_Yajl::Parser.parse(response.body)['id']
end
def full_url(path)
"#{@base_url}#{path}"
end
@@ -189,10 +190,10 @@
message = exception.errors.empty? ? caught_exception.message : exception.errors.join(', ')
raise exception, message
end
def validate_client_options!
- raise ArgumentError, 'Username and password are required' if @username.nil? || @password.nil?
+ raise ArgumentError, 'Username and password are required' if @username.nil? || @password.value.nil?
raise ArgumentError, 'A tenant is required' if @tenant.nil?
raise ArgumentError, 'A base URL is required' if @base_url.nil?
raise ArgumentError, "Base URL #{@base_url} is not a valid URI." unless valid_uri?(@base_url)
end