Class: OracleCloud::Client
- Inherits:
-
Object
- Object
- OracleCloud::Client
- Defined in:
- lib/oraclecloud/client.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#identity_domain ⇒ Object
readonly
Returns the value of attribute identity_domain.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #asset_delete(asset_type, path) ⇒ Object
- #asset_get(request_type, asset_type, path) ⇒ Object
- #asset_put(asset_type, path, payload = nil) ⇒ Object
- #authenticate! ⇒ Object
- #authenticate_payload ⇒ Object
- #authenticated? ⇒ Boolean
- #directory(asset_type, path) ⇒ Object
- #full_identity_domain ⇒ Object
- #full_url(path) ⇒ Object
- #http_delete(path) ⇒ Object
- #http_get(request_type, url) ⇒ Object
- #http_post(path, payload) ⇒ Object
- #http_put(path, payload = nil) ⇒ Object
-
#imagelists ⇒ Object
methods to other API objects.
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #instance_request(*args) ⇒ Object
- #instances ⇒ Object
- #ip_associations ⇒ Object
- #orchestrations ⇒ Object
- #private_cloud? ⇒ Boolean
- #process_auth_cookies(cookies) ⇒ Object
- #raise_http_exception(caught_exception, path) ⇒ Object
- #request_headers(opts = {}) ⇒ Object
- #shapes ⇒ Object
- #single_item(asset_type, path) ⇒ Object
- #sshkeys ⇒ Object
- #url_with_identity_domain(type, path = '') ⇒ Object
- #username_with_domain ⇒ Object
- #valid_uri?(uri) ⇒ Boolean
-
#validate_client_options! ⇒ Object
client methods.
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/oraclecloud/client.rb', line 27 def initialize(opts) @api_url = opts[:api_url] @identity_domain = opts[:identity_domain] @private_cloud = opts[:private_cloud] || false @username = opts[:username] @password = opts[:password] @verify_ssl = opts.fetch(:verify_ssl, true) @cookie = nil end |
Instance Attribute Details
#identity_domain ⇒ Object (readonly)
Returns the value of attribute identity_domain.
25 26 27 |
# File 'lib/oraclecloud/client.rb', line 25 def identity_domain @identity_domain end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
25 26 27 |
# File 'lib/oraclecloud/client.rb', line 25 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
25 26 27 |
# File 'lib/oraclecloud/client.rb', line 25 def username @username end |
Instance Method Details
#asset_delete(asset_type, path) ⇒ Object
167 168 169 170 |
# File 'lib/oraclecloud/client.rb', line 167 def asset_delete(asset_type, path) url = url_with_identity_domain(asset_type, path) http_delete(url) end |
#asset_get(request_type, asset_type, path) ⇒ Object
157 158 159 160 |
# File 'lib/oraclecloud/client.rb', line 157 def asset_get(request_type, asset_type, path) url = url_with_identity_domain(asset_type, path) http_get(request_type, url) end |
#asset_put(asset_type, path, payload = nil) ⇒ Object
162 163 164 165 |
# File 'lib/oraclecloud/client.rb', line 162 def asset_put(asset_type, path, payload = nil) url = url_with_identity_domain(asset_type, path) http_put(url, payload) end |
#authenticate! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/oraclecloud/client.rb', line 107 def authenticate! path = '/authenticate/' response = RestClient::Request.execute(method: :post, url: full_url(path), headers: request_headers, payload: authenticate_payload.to_json, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else @cookie = (response.headers[:set_cookie]) end |
#authenticate_payload ⇒ Object
138 139 140 141 142 143 |
# File 'lib/oraclecloud/client.rb', line 138 def authenticate_payload { 'user' => username_with_domain, 'password' => @password } end |
#authenticated? ⇒ Boolean
121 122 123 |
# File 'lib/oraclecloud/client.rb', line 121 def authenticated? ! @cookie.nil? end |
#directory(asset_type, path) ⇒ Object
176 177 178 |
# File 'lib/oraclecloud/client.rb', line 176 def directory(asset_type, path) asset_get(:directory, asset_type, path) end |
#full_identity_domain ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/oraclecloud/client.rb', line 99 def full_identity_domain if private_cloud? @identity_domain else "Compute-#{@identity_domain}" end end |
#full_url(path) ⇒ Object
145 146 147 |
# File 'lib/oraclecloud/client.rb', line 145 def full_url(path) @api_url + path end |
#http_delete(path) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/oraclecloud/client.rb', line 223 def http_delete(path) authenticate! unless authenticated? response = RestClient::Request.execute(method: :delete, url: full_url(path), headers: request_headers, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else FFI_Yajl::Parser.parse(response) end |
#http_get(request_type, url) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/oraclecloud/client.rb', line 184 def http_get(request_type, url) authenticate! unless authenticated? response = RestClient::Request.execute(method: :get, url: full_url(url), headers: request_headers(type: request_type), verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, url) else FFI_Yajl::Parser.parse(response) end |
#http_post(path, payload) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/oraclecloud/client.rb', line 197 def http_post(path, payload) authenticate! unless authenticated? response = RestClient::Request.execute(method: :post, url: full_url(path), headers: request_headers, payload: payload, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else FFI_Yajl::Parser.parse(response) end |
#http_put(path, payload = nil) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/oraclecloud/client.rb', line 210 def http_put(path, payload = nil) authenticate! unless authenticated? response = RestClient::Request.execute(method: :put, url: full_url(path), headers: request_headers, payload: payload, verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else FFI_Yajl::Parser.parse(response) end |
#imagelists ⇒ Object
methods to other API objects
44 45 46 |
# File 'lib/oraclecloud/client.rb', line 44 def imagelists OracleCloud::ImageLists.new(self) end |
#instance_request(*args) ⇒ Object
48 49 50 |
# File 'lib/oraclecloud/client.rb', line 48 def instance_request(*args) OracleCloud::InstanceRequest.new(self, *args) end |
#instances ⇒ Object
52 53 54 |
# File 'lib/oraclecloud/client.rb', line 52 def instances OracleCloud::Instances.new(self) end |
#ip_associations ⇒ Object
56 57 58 |
# File 'lib/oraclecloud/client.rb', line 56 def ip_associations OracleCloud::IPAssociations.new(self) end |
#orchestrations ⇒ Object
60 61 62 |
# File 'lib/oraclecloud/client.rb', line 60 def orchestrations OracleCloud::Orchestrations.new(self) end |
#private_cloud? ⇒ Boolean
91 92 93 |
# File 'lib/oraclecloud/client.rb', line 91 def private_cloud? @private_cloud end |
#process_auth_cookies(cookies) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/oraclecloud/client.rb', line 149 def () = .find { |c| c.start_with?('nimbula=') } raise 'No nimbula auth cookie received in authentication request' if .nil? .gsub!(/ Path=.* Max-Age=.*$/, '') end |
#raise_http_exception(caught_exception, path) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/oraclecloud/client.rb', line 235 def raise_http_exception(caught_exception, path) raise unless caught_exception.respond_to?(:http_code) if caught_exception.http_code == 404 klass = OracleCloud::Exception::HTTPNotFound else klass = OracleCloud::Exception::HTTPError end begin error_body = FFI_Yajl::Parser.parse(caught_exception.response) rescue error_body = { 'message' => caught_exception.response } end exception = klass.new(code: caught_exception.http_code, body: caught_exception.response, klass: caught_exception.class, error: error_body['message'].to_s, path: path) = exception.error.empty? ? caught_exception. : exception.error raise exception, end |
#request_headers(opts = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/oraclecloud/client.rb', line 125 def request_headers(opts = {}) headers = { 'Content-Type' => 'application/oracle-compute-v3+json' } if opts[:type] == :directory headers['Accept'] = 'application/oracle-compute-v3+directory+json' else headers['Accept'] = 'application/oracle-compute-v3+json' end headers['Cookie'] = @cookie if @cookie headers end |
#shapes ⇒ Object
64 65 66 |
# File 'lib/oraclecloud/client.rb', line 64 def shapes OracleCloud::Shapes.new(self) end |
#single_item(asset_type, path) ⇒ Object
172 173 174 |
# File 'lib/oraclecloud/client.rb', line 172 def single_item(asset_type, path) asset_get(:single, asset_type, path) end |
#sshkeys ⇒ Object
68 69 70 |
# File 'lib/oraclecloud/client.rb', line 68 def sshkeys OracleCloud::SSHKeys.new(self) end |
#url_with_identity_domain(type, path = '') ⇒ Object
180 181 182 |
# File 'lib/oraclecloud/client.rb', line 180 def url_with_identity_domain(type, path = '') "/#{type}/#{full_identity_domain}/#{path}" end |
#username_with_domain ⇒ Object
95 96 97 |
# File 'lib/oraclecloud/client.rb', line 95 def username_with_domain "#{full_identity_domain}/#{@username}" end |
#valid_uri?(uri) ⇒ Boolean
84 85 86 87 88 89 |
# File 'lib/oraclecloud/client.rb', line 84 def valid_uri?(uri) uri = URI.parse(uri) uri.is_a?(URI::HTTP) rescue URI::InvalidURIError false end |
#validate_client_options! ⇒ Object
client methods
77 78 79 80 81 82 |
# File 'lib/oraclecloud/client.rb', line 77 def raise ArgumentError, 'Username, password and identity_domain are required' if @username.nil? || @password.nil? || @identity_domain.nil? raise ArgumentError, 'An API URL is required' if @api_url.nil? raise ArgumentError, "API URL #{@api_url} is not a valid URI." unless valid_uri?(@api_url) end |