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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cute/g5k_api.rb', line 206

def initialize(uri,api_version,user,pass,on_error)
  @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

  machine =`uname -ov`.chop
  @user_agent = "ruby-cute/#{VERSION} (#{machine}) Ruby #{RUBY_VERSION}"
  @api = RestClient::Resource.new(@endpoint, :timeout => 30,: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.



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

def user
  @user
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



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/cute/g5k_api.rb', line 282

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



299
300
301
# File 'lib/cute/g5k_api.rb', line 299

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/cute/g5k_api.rb', line 239

def get_json(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 G5KJSON.parse(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.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cute/g5k_api.rb', line 260

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



229
230
231
232
# File 'lib/cute/g5k_api.rb', line 229

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