Class: OracleCloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/oraclecloud/client.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

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

  validate_client_options!
end

Instance Attribute Details

#identity_domainObject (readonly)

Returns the value of attribute identity_domain.



25
26
27
# File 'lib/oraclecloud/client.rb', line 25

def identity_domain
  @identity_domain
end

#passwordObject (readonly)

Returns the value of attribute password.



25
26
27
# File 'lib/oraclecloud/client.rb', line 25

def password
  @password
end

#usernameObject (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 = process_auth_cookies(response.headers[:set_cookie])
end

#authenticate_payloadObject



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

Returns:

  • (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_domainObject



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

#imagelistsObject

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

#instancesObject



52
53
54
# File 'lib/oraclecloud/client.rb', line 52

def instances
  OracleCloud::Instances.new(self)
end

#ip_associationsObject



56
57
58
# File 'lib/oraclecloud/client.rb', line 56

def ip_associations
  OracleCloud::IPAssociations.new(self)
end

#orchestrationsObject



60
61
62
# File 'lib/oraclecloud/client.rb', line 60

def orchestrations
  OracleCloud::Orchestrations.new(self)
end

#private_cloud?Boolean

Returns:

  • (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 process_auth_cookies(cookies)
  cookie = cookies.find { |c| c.start_with?('nimbula=') }
  raise 'No nimbula auth cookie received in authentication request' if cookie.nil?

  cookie.gsub!(/ Path=.* Max-Age=.*$/, '')
  cookie
end

#raise_http_exception(caught_exception, path) ⇒ Object

Raises:

  • (exception)


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)

  message = exception.error.empty? ? caught_exception.message : exception.error
  raise exception, message
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

#shapesObject



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

#sshkeysObject



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_domainObject



95
96
97
# File 'lib/oraclecloud/client.rb', line 95

def username_with_domain
  "#{full_identity_domain}/#{@username}"
end

#valid_uri?(uri) ⇒ Boolean

Returns:

  • (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

Raises:

  • (ArgumentError)


77
78
79
80
81
82
# File 'lib/oraclecloud/client.rb', line 77

def validate_client_options!
  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