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.



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

  validate_client_options!
end

Instance Attribute Details

#identity_domainObject (readonly)

Returns the value of attribute identity_domain.



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

def identity_domain
  @identity_domain
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

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

#authenticate_payloadObject



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

Returns:

  • (Boolean)


111
112
113
# File 'lib/oraclecloud/client.rb', line 111

def authenticated?
  ! @cookie.nil?
end

#compute_identity_domainObject



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

#imagelistsObject

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

#instancesObject



50
51
52
# File 'lib/oraclecloud/client.rb', line 50

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

#ip_associationsObject



54
55
56
# File 'lib/oraclecloud/client.rb', line 54

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

#orchestrationsObject



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 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)


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)

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

#shapesObject



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

#sshkeysObject



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_domainObject



89
90
91
# File 'lib/oraclecloud/client.rb', line 89

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

#valid_uri?(uri) ⇒ Boolean

Returns:

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

Raises:

  • (ArgumentError)


75
76
77
78
79
80
# File 'lib/oraclecloud/client.rb', line 75

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