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
- #compute_identity_domain ⇒ Object
- #directory(asset_type, path) ⇒ 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
- #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.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/oraclecloud/client.rb', line 26 def initialize(opts) @api_url = opts[:api_url] @identity_domain = opts[:identity_domain] @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.
24 25 26 |
# File 'lib/oraclecloud/client.rb', line 24 def identity_domain @identity_domain end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
24 25 26 |
# File 'lib/oraclecloud/client.rb', line 24 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
24 25 26 |
# File 'lib/oraclecloud/client.rb', line 24 def username @username end |
Instance Method Details
#asset_delete(asset_type, path) ⇒ Object
157 158 159 160 |
# File 'lib/oraclecloud/client.rb', line 157 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
147 148 149 150 |
# File 'lib/oraclecloud/client.rb', line 147 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
152 153 154 155 |
# File 'lib/oraclecloud/client.rb', line 152 def asset_put(asset_type, path, payload = nil) url = url_with_identity_domain(asset_type, path) http_put(url, payload) end |
#authenticate! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/oraclecloud/client.rb', line 97 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
128 129 130 131 132 133 |
# File 'lib/oraclecloud/client.rb', line 128 def authenticate_payload { 'user' => username_with_domain, 'password' => @password } end |
#authenticated? ⇒ Boolean
111 112 113 |
# File 'lib/oraclecloud/client.rb', line 111 def authenticated? ! @cookie.nil? end |
#compute_identity_domain ⇒ Object
93 94 95 |
# File 'lib/oraclecloud/client.rb', line 93 def compute_identity_domain "Compute-#{@identity_domain}" end |
#directory(asset_type, path) ⇒ Object
166 167 168 |
# File 'lib/oraclecloud/client.rb', line 166 def directory(asset_type, path) asset_get(:directory, asset_type, path) end |
#full_url(path) ⇒ Object
135 136 137 |
# File 'lib/oraclecloud/client.rb', line 135 def full_url(path) @api_url + path end |
#http_delete(path) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/oraclecloud/client.rb', line 213 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
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/oraclecloud/client.rb', line 174 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
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/oraclecloud/client.rb', line 187 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
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/oraclecloud/client.rb', line 200 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
42 43 44 |
# File 'lib/oraclecloud/client.rb', line 42 def imagelists OracleCloud::ImageLists.new(self) end |
#instance_request(*args) ⇒ Object
46 47 48 |
# File 'lib/oraclecloud/client.rb', line 46 def instance_request(*args) OracleCloud::InstanceRequest.new(self, *args) end |
#instances ⇒ Object
50 51 52 |
# File 'lib/oraclecloud/client.rb', line 50 def instances OracleCloud::Instances.new(self) end |
#ip_associations ⇒ Object
54 55 56 |
# File 'lib/oraclecloud/client.rb', line 54 def ip_associations OracleCloud::IPAssociations.new(self) end |
#orchestrations ⇒ Object
58 59 60 |
# File 'lib/oraclecloud/client.rb', line 58 def orchestrations OracleCloud::Orchestrations.new(self) end |
#process_auth_cookies(cookies) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/oraclecloud/client.rb', line 139 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
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/oraclecloud/client.rb', line 225 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
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/oraclecloud/client.rb', line 115 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
62 63 64 |
# File 'lib/oraclecloud/client.rb', line 62 def shapes OracleCloud::Shapes.new(self) end |
#single_item(asset_type, path) ⇒ Object
162 163 164 |
# File 'lib/oraclecloud/client.rb', line 162 def single_item(asset_type, path) asset_get(:single, asset_type, path) end |
#sshkeys ⇒ Object
66 67 68 |
# File 'lib/oraclecloud/client.rb', line 66 def sshkeys OracleCloud::SSHKeys.new(self) end |
#url_with_identity_domain(type, path = '') ⇒ Object
170 171 172 |
# File 'lib/oraclecloud/client.rb', line 170 def url_with_identity_domain(type, path = '') "/#{type}/#{compute_identity_domain}/#{path}" end |
#username_with_domain ⇒ Object
89 90 91 |
# File 'lib/oraclecloud/client.rb', line 89 def username_with_domain "#{compute_identity_domain}/#{@username}" end |
#valid_uri?(uri) ⇒ Boolean
82 83 84 85 86 87 |
# File 'lib/oraclecloud/client.rb', line 82 def valid_uri?(uri) uri = URI.parse(uri) uri.is_a?(URI::HTTP) rescue URI::InvalidURIError false end |
#validate_client_options! ⇒ Object
client methods
75 76 77 78 79 80 |
# File 'lib/oraclecloud/client.rb', line 75 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 |