class Vra::Client
Attributes
page_size[RW]
Public Class Methods
new(opts)
click to toggle source
# File lib/vra/client.rb, line 28 def initialize(opts) @base_url = opts[:base_url] @username = opts[:username] @password = PasswordMasker.new(opts[:password]) @tenant = opts[:tenant] @verify_ssl = opts.fetch(:verify_ssl, true) @bearer_token = PasswordMasker.new(nil) @page_size = opts.fetch(:page_size, 20) validate_client_options! end
Public Instance Methods
bearer_token()
click to toggle source
client methods
# File lib/vra/client.rb, line 62 def bearer_token @bearer_token.value end
bearer_token=(value)
click to toggle source
# File lib/vra/client.rb, line 66 def bearer_token=(value) @bearer_token.value = value end
bearer_token_request_body()
click to toggle source
# File lib/vra/client.rb, line 70 def bearer_token_request_body { "username" => @username, "password" => @password.value, "tenant" => @tenant, } end
catalog()
click to toggle source
methods to other classes
# File lib/vra/client.rb, line 45 def catalog Vra::Catalog.new(self) end
fetch_subtenant_items(tenant, subtenant_name)
click to toggle source
# File lib/vra/client.rb, line 223 def fetch_subtenant_items(tenant, subtenant_name) http_get("/identity/api/tenants/#{tenant}/subtenants?%24filter=name+eq+'#{subtenant_name}'") end
full_url(path)
click to toggle source
# File lib/vra/client.rb, line 113 def full_url(path) "#{@base_url}#{path}" end
generate_bearer_token()
click to toggle source
# File lib/vra/client.rb, line 99 def generate_bearer_token @bearer_token.value = nil validate_client_options! response = http_post("/identity/api/tokens", FFI_Yajl::Encoder.encode(bearer_token_request_body), :skip_auth) unless response.success_ok? raise Vra::Exception::Unauthorized, "Unable to get bearer token: #{response.body}" end @bearer_token.value = FFI_Yajl::Parser.parse(response.body)["id"] end
get_parsed(path)
click to toggle source
# File lib/vra/client.rb, line 143 def get_parsed(path) FFI_Yajl::Parser.parse(http_get!(path)) end
http_fetch(method, path, skip_auth = nil)
click to toggle source
# File lib/vra/client.rb, line 117 def http_fetch(method, path, skip_auth = nil) authorize! unless skip_auth response = Vra::Http.execute(method: method, url: full_url(path), headers: request_headers, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else response end
http_get(path, skip_auth = nil)
click to toggle source
# File lib/vra/client.rb, line 134 def http_get(path, skip_auth = nil) http_fetch(:get, path, skip_auth) end
http_get!(path)
click to toggle source
# File lib/vra/client.rb, line 138 def http_get!(path) response = http_get(path) response.body end
http_get_paginated_array!(path)
click to toggle source
# File lib/vra/client.rb, line 147 def http_get_paginated_array!(path) items = [] page = 1 base_path = path + "?limit=#{page_size}" loop do response = get_parsed("#{base_path}&page=#{page}") items += response["content"] break if page >= response["metadata"]["totalPages"] page += 1 end if items.uniq! raise Vra::Exception::DuplicateItemsDetected, "Duplicate items were returned by the vRA API. " \ "Increase your page size to avoid this vRA API bug. " \ "See https://github.com/chef-partners/vmware-vra-gem#pagination " \ "for more information." end items end
http_head(path, skip_auth = nil)
click to toggle source
# File lib/vra/client.rb, line 130 def http_head(path, skip_auth = nil) http_fetch(:head, path, skip_auth) end
http_post(path, payload, skip_auth = nil)
click to toggle source
# File lib/vra/client.rb, line 172 def http_post(path, payload, skip_auth = nil) authorize! unless skip_auth response = Vra::Http.execute(method: :post, url: full_url(path), headers: request_headers, payload: payload, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else response end
http_post!(path, payload)
click to toggle source
# File lib/vra/client.rb, line 186 def http_post!(path, payload) response = http_post(path, payload) response.body end
raise_http_exception(caught_exception, path)
click to toggle source
# File lib/vra/client.rb, line 191 def raise_http_exception(caught_exception, path) raise unless caught_exception.respond_to?(:http_code) klass = if caught_exception.http_code == 404 Vra::Exception::HTTPNotFound else Vra::Exception::HTTPError end exception = klass.new(code: caught_exception.http_code, body: caught_exception.response, klass: caught_exception.class, path: path) message = exception.errors.empty? ? caught_exception.message : exception.errors.join(", ") raise exception, message end
request_headers()
click to toggle source
# File lib/vra/client.rb, line 78 def request_headers headers = {} headers["Accept"] = "application/json" headers["Content-Type"] = "application/json" headers["Authorization"] = "Bearer #{@bearer_token.value}" unless @bearer_token.value.nil? headers end
requests(*args)
click to toggle source
# File lib/vra/client.rb, line 49 def requests(*args) Vra::Requests.new(self, *args) end
resources(*args)
click to toggle source
# File lib/vra/client.rb, line 53 def resources(*args) Vra::Resources.new(self, *args) end
valid_uri?(uri)
click to toggle source
# File lib/vra/client.rb, line 216 def valid_uri?(uri) uri = URI.parse(uri) uri.is_a?(URI::HTTP) rescue URI::InvalidURIError false end
validate_client_options!()
click to toggle source
# File lib/vra/client.rb, line 209 def validate_client_options! 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