Class: Cute::G5K::G5KRest Private

Inherits:
Object
  • Object
show all
Defined in:
lib/cute/g5k_api.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages the low level operations for communicating with the REST API.

Constant Summary collapse

RETRY_503_MAX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

120
RETRY_503_SLEEP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, api_version, user, pass, on_error, timeout) ⇒ G5KRest

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a REST connection

Parameters:

  • uri (String)

    resource identifier which normally is the URL of the Rest API

  • user (String)

    user if authentication is needed

  • pass (String)

    password if authentication is needed

  • on_error (Symbol)

    option to deactivate the RequestFailed exceptions

  • timeout (Integer)

    timeout for Rest request



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cute/g5k_api.rb', line 210

def initialize(uri,api_version,user,pass,on_error,timeout)
  @user = user
  @pass = pass
  @api_version = api_version.nil? ? "stable" : api_version
  if (user.nil? or pass.nil?)
    @endpoint = uri # Inside Grid'5000
  else
    user_escaped = CGI.escape(user)
    pass_escaped = CGI.escape(pass)
    @endpoint = "https://#{user_escaped}:#{pass_escaped}@#{uri.split("https://")[1]}"
  end

  @user_agent = "ruby-cute/#{VERSION} Ruby/#{RUBY_VERSION}"
  @api = RestClient::Resource.new(@endpoint, :timeout => timeout,:verify_ssl => false)
  # some versions of restclient do not verify by default SSL certificates , :verify_ssl => true)
  # SSL verify is disabled due to Grid'5000 API certificate problem
  @on_error = on_error
  test_connection
end

Instance Attribute Details

#userObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



201
202
203
# File 'lib/cute/g5k_api.rb', line 201

def user
  @user
end

#user_agentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



202
203
204
# File 'lib/cute/g5k_api.rb', line 202

def user_agent
  @user_agent
end

Instance Method Details

#delete_json(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes a resource on the server

Parameters:

  • path (String)

    this complements the URI to address to a specific resource



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/cute/g5k_api.rb', line 291

def delete_json(path)
  retries = 0
  begin
    return resource(path).delete()
  rescue  => e
    if e.kind_of?(RestClient::Exception) and e.http_code == 503 and retries < RETRY_503_MAX
      # the G5K REST API sometimes fail with error 503. In that case we should just wait and retry
      puts("G5KRest: DELETE #{path} failed with error 503, retrying after #{RETRY_503_SLEEP} seconds")
      retries += 1
      sleep RETRY_503_SLEEP
      retry
    end
    handle_exception(e)
  end
end

#follow_parent(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the parent link.

Returns:

  • the parent link



308
309
310
# File 'lib/cute/g5k_api.rb', line 308

def follow_parent(obj)
  get_json(obj.rel_parent)
end

#get_json(path) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the HTTP response.

Parameters:

  • path (String)

    this complements the URI to address to a specific resource

Returns:

  • (Hash)

    the HTTP response



242
243
244
# File 'lib/cute/g5k_api.rb', line 242

def get_json(path)
  return G5KJSON.parse(get_raw(path))
end

#get_raw(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the HTTP response.

Parameters:

  • path (String)

    this complements the URI to address to a specific resource

Returns:

  • (String)

    the HTTP response



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cute/g5k_api.rb', line 248

def get_raw(path)
  retries = 0
  begin
    r = resource(path).get(:content_type => "application/json",
                           :user_agent => @user_agent)
  rescue => e
    if e.kind_of?(RestClient::Exception) and e.http_code == 503 and retries < RETRY_503_MAX
      # the G5K REST API sometimes fail with error 503. In that case we should just wait and retry
      puts("G5KRest: GET #{path} failed with error 503, retrying after #{RETRY_503_SLEEP} seconds")
      retries += 1
      sleep RETRY_503_SLEEP
      retry
    end
    handle_exception(e)
  end
  return r
end

#post_json(path, json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a resource on the server

Parameters:

  • path (String)

    this complements the URI to address to a specific resource

  • json (Hash)

    contains the characteristics of the resources to be created.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/cute/g5k_api.rb', line 269

def post_json(path, json)
  retries = 0
  begin
    r = resource(path).post(json.to_json,
                            :content_type => "application/json",
                            :accept => "application/json",
                            :user_agent => @user_agent)
  rescue => e
    if e.kind_of?(RestClient::Exception) and e.http_code == 503 and retries < RETRY_503_MAX
      # the G5K REST API sometimes fail with error 503. In that case we should just wait and retry
      puts("G5KRest: POST #{path} failed with error 503, retrying after #{RETRY_503_SLEEP} seconds")
      retries += 1
      sleep RETRY_503_SLEEP
      retry
    end
    handle_exception(e)
  end
  return G5KJSON.parse(r)
end

#resource(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a resource object

Parameters:

  • path (String)

    this complements the URI to address to a specific resource



232
233
234
235
# File 'lib/cute/g5k_api.rb', line 232

def resource(path)
  path = path[1..-1] if path.start_with?('/')
  return @api[path]
end