Class: Vra::Client
- Inherits:
-
Object
- Object
- Vra::Client
- Defined in:
- lib/vra/client.rb
Instance Attribute Summary collapse
-
#page_size ⇒ Object
Returns the value of attribute page_size.
Instance Method Summary collapse
- #authorize! ⇒ Object
- #authorized? ⇒ Boolean
-
#bearer_token ⇒ Object
client methods.
- #bearer_token=(value) ⇒ Object
- #bearer_token_request_body ⇒ Object
-
#catalog ⇒ Object
methods to other classes.
- #fetch_subtenant_items(tenant, subtenant_name) ⇒ Object
- #full_url(path) ⇒ Object
- #generate_bearer_token ⇒ Object
- #get_parsed(path) ⇒ Object
- #http_fetch(method, path, skip_auth = nil) ⇒ Object
- #http_get(path, skip_auth = nil) ⇒ Object
- #http_get!(path) ⇒ Object
- #http_get_paginated_array!(path) ⇒ Object
- #http_head(path, skip_auth = nil) ⇒ Object
- #http_post(path, payload, skip_auth = nil) ⇒ Object
- #http_post!(path, payload) ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #raise_http_exception(caught_exception, path) ⇒ Object
- #request_headers ⇒ Object
- #requests(*args) ⇒ Object
- #resources(*args) ⇒ Object
- #valid_uri?(uri) ⇒ Boolean
- #validate_client_options! ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
28 29 30 31 32 33 34 35 36 37 38 |
# 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) end |
Instance Attribute Details
#page_size ⇒ Object
Returns the value of attribute page_size.
26 27 28 |
# File 'lib/vra/client.rb', line 26 def page_size @page_size end |
Instance Method Details
#authorize! ⇒ Object
86 87 88 89 90 |
# File 'lib/vra/client.rb', line 86 def generate_bearer_token unless raise Vra::Exception::Unauthorized, "Unable to authorize against vRA" unless end |
#authorized? ⇒ Boolean
92 93 94 95 96 97 |
# File 'lib/vra/client.rb', line 92 def return false if @bearer_token.value.nil? response = http_head("/identity/api/tokens/#{@bearer_token.value}", :skip_auth) response.success_no_content? end |
#bearer_token ⇒ Object
client methods
62 63 64 |
# File 'lib/vra/client.rb', line 62 def bearer_token @bearer_token.value end |
#bearer_token=(value) ⇒ Object
66 67 68 |
# File 'lib/vra/client.rb', line 66 def bearer_token=(value) @bearer_token.value = value end |
#bearer_token_request_body ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/vra/client.rb', line 70 def bearer_token_request_body { "username" => @username, "password" => @password.value, "tenant" => @tenant, } end |
#catalog ⇒ Object
methods to other classes
45 46 47 |
# File 'lib/vra/client.rb', line 45 def catalog Vra::Catalog.new(self) end |
#fetch_subtenant_items(tenant, subtenant_name) ⇒ Object
222 223 224 |
# File 'lib/vra/client.rb', line 222 def fetch_subtenant_items(tenant, subtenant_name) http_get("/identity/api/tenants/#{tenant}/subtenants?%24filter=name+eq+'#{subtenant_name}'") end |
#full_url(path) ⇒ Object
113 114 115 |
# File 'lib/vra/client.rb', line 113 def full_url(path) "#{@base_url}#{path}" end |
#generate_bearer_token ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vra/client.rb', line 99 def generate_bearer_token @bearer_token.value = nil 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) ⇒ Object
143 144 145 |
# 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) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/vra/client.rb', line 117 def http_fetch(method, path, skip_auth = nil) 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) ⇒ Object
134 135 136 |
# File 'lib/vra/client.rb', line 134 def http_get(path, skip_auth = nil) http_fetch(:get, path, skip_auth) end |
#http_get!(path) ⇒ Object
138 139 140 141 |
# File 'lib/vra/client.rb', line 138 def http_get!(path) response = http_get(path) response.body end |
#http_get_paginated_array!(path) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# 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) ⇒ Object
130 131 132 |
# 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) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/vra/client.rb', line 171 def http_post(path, payload, skip_auth = nil) 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) ⇒ Object
185 186 187 188 |
# File 'lib/vra/client.rb', line 185 def http_post!(path, payload) response = http_post(path, payload) response.body end |
#raise_http_exception(caught_exception, path) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vra/client.rb', line 190 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) = exception.errors.empty? ? caught_exception. : exception.errors.join(", ") raise exception, end |
#request_headers ⇒ Object
78 79 80 81 82 83 84 |
# 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) ⇒ Object
49 50 51 |
# File 'lib/vra/client.rb', line 49 def requests(*args) Vra::Requests.new(self, *args) end |
#resources(*args) ⇒ Object
53 54 55 |
# File 'lib/vra/client.rb', line 53 def resources(*args) Vra::Resources.new(self, *args) end |
#valid_uri?(uri) ⇒ Boolean
215 216 217 218 219 220 |
# File 'lib/vra/client.rb', line 215 def valid_uri?(uri) uri = URI.parse(uri) uri.is_a?(URI::HTTP) rescue URI::InvalidURIError false end |
#validate_client_options! ⇒ Object
208 209 210 211 212 213 |
# File 'lib/vra/client.rb', line 208 def 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 |