Class: Vra::Client
- Inherits:
-
Object
- Object
- Vra::Client
- Defined in:
- lib/vra/client.rb
Constant Summary collapse
- ACCESS_TOKEN_URL =
'/csp/gateway/am/api/login?access_token'- ROLES_URL =
'/csp/gateway/am/api/loggedin/user/orgs'
Instance Attribute Summary collapse
-
#page_size ⇒ Object
Returns the value of attribute page_size.
Instance Method Summary collapse
-
#access_token ⇒ Object
client methods.
- #access_token=(value) ⇒ Object
- #authorize! ⇒ Object
- #authorized? ⇒ Boolean
-
#catalog ⇒ Object
methods to other classes.
- #deployments ⇒ Object
- #full_url(path) ⇒ Object
- #generate_access_token ⇒ Object
- #get_parsed(path) ⇒ Object
- #http_delete(path, skip_auth = nil) ⇒ 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, filter = nil) ⇒ 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
- #refresh_token ⇒ Object
- #refresh_token=(value) ⇒ Object
- #request_headers ⇒ Object
- #token_params ⇒ Object
- #valid_uri?(uri) ⇒ Boolean
- #validate_client_options! ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vra/client.rb', line 31 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) @refresh_token = PasswordMasker.new(nil) @access_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.
29 30 31 |
# File 'lib/vra/client.rb', line 29 def page_size @page_size end |
Instance Method Details
#access_token ⇒ Object
client methods
62 63 64 |
# File 'lib/vra/client.rb', line 62 def access_token @access_token.value end |
#access_token=(value) ⇒ Object
70 71 72 |
# File 'lib/vra/client.rb', line 70 def access_token=(value) @access_token.value = value end |
#authorize! ⇒ Object
95 96 97 98 99 |
# File 'lib/vra/client.rb', line 95 def generate_access_token unless raise Vra::Exception::Unauthorized, 'Unable to authorize against vRA' unless end |
#authorized? ⇒ Boolean
101 102 103 104 105 106 |
# File 'lib/vra/client.rb', line 101 def return false if @access_token.value.nil? response = http_head(ROLES_URL, :skip_auth) response.success? end |
#catalog ⇒ Object
methods to other classes
49 50 51 |
# File 'lib/vra/client.rb', line 49 def catalog @catalog ||= Vra::Catalog.new(self) end |
#deployments ⇒ Object
53 54 55 |
# File 'lib/vra/client.rb', line 53 def deployments @deployments ||= Vra::Deployments.new(self) end |
#full_url(path) ⇒ Object
122 123 124 |
# File 'lib/vra/client.rb', line 122 def full_url(path) "#{@base_url}#{path}" end |
#generate_access_token ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/vra/client.rb', line 108 def generate_access_token @access_token.value = nil response = http_post(ACCESS_TOKEN_URL, FFI_Yajl::Encoder.encode(token_params), :skip_auth) raise Vra::Exception::Unauthorized, "Unable to get the access token: #{response.body}" unless response.success_ok? response_body = FFI_Yajl::Parser.parse(response.body) @access_token.value = response_body['access_token'] @refresh_token.value = response_body['refresh_token'] end |
#get_parsed(path) ⇒ Object
156 157 158 |
# File 'lib/vra/client.rb', line 156 def get_parsed(path) FFI_Yajl::Parser.parse(http_get!(path)) end |
#http_delete(path, skip_auth = nil) ⇒ Object
152 153 154 |
# File 'lib/vra/client.rb', line 152 def http_delete(path, skip_auth = nil) http_fetch(:delete, path, skip_auth) end |
#http_fetch(method, path, skip_auth = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/vra/client.rb', line 126 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
143 144 145 |
# File 'lib/vra/client.rb', line 143 def http_get(path, skip_auth = nil) http_fetch(:get, path, skip_auth) end |
#http_get!(path) ⇒ Object
147 148 149 150 |
# File 'lib/vra/client.rb', line 147 def http_get!(path) response = http_get(path) response.body end |
#http_get_paginated_array!(path, filter = nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/vra/client.rb', line 160 def http_get_paginated_array!(path, filter = nil) items = [] page = 0 base_path = path + "?$top=#{page_size}" base_path += "&#{filter}" if filter loop do response = get_parsed("#{base_path}&$skip=#{page * page_size}") items += response["content"] page += 1 break if page >= response["totalPages"] 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
139 140 141 |
# File 'lib/vra/client.rb', line 139 def http_head(path, skip_auth = nil) http_fetch(:head, path, skip_auth) end |
#http_post(path, payload, skip_auth = nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/vra/client.rb', line 185 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
199 200 201 202 |
# File 'lib/vra/client.rb', line 199 def http_post!(path, payload) response = http_post(path, payload) response.body end |
#raise_http_exception(caught_exception, path) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/vra/client.rb', line 204 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 |
#refresh_token ⇒ Object
66 67 68 |
# File 'lib/vra/client.rb', line 66 def refresh_token @refresh_token.value end |
#refresh_token=(value) ⇒ Object
74 75 76 |
# File 'lib/vra/client.rb', line 74 def refresh_token=(value) @refresh_token.value = value end |
#request_headers ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/vra/client.rb', line 86 def request_headers headers = {} headers['Accept'] = 'application/json' headers['Content-Type'] = 'application/json' headers['csp-auth-token'] = @access_token.value unless @access_token.value.nil? headers end |
#token_params ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/vra/client.rb', line 78 def token_params { 'username': @username, 'password': @password.value, 'tenant': @tenant } end |
#valid_uri?(uri) ⇒ Boolean
229 230 231 232 233 234 |
# File 'lib/vra/client.rb', line 229 def valid_uri?(uri) uri = URI.parse(uri) uri.is_a?(URI::HTTP) rescue URI::InvalidURIError false end |
#validate_client_options! ⇒ Object
222 223 224 225 226 227 |
# File 'lib/vra/client.rb', line 222 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 |