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.

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cute/g5k_api.rb', line 200

def initialize(uri,api_version,user,pass,on_error)
  @user = user
  @pass = pass
  @api_version = api_version.nil? ? "sid" : 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)
  @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.



194
195
196
# File 'lib/cute/g5k_api.rb', line 194

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



257
258
259
260
261
262
263
# File 'lib/cute/g5k_api.rb', line 257

def delete_json(path)
  begin
    return resource(path).delete()
  rescue RestClient::InternalServerError => e
    raise RequestFailed.new("Service internal error", 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



266
267
268
# File 'lib/cute/g5k_api.rb', line 266

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



228
229
230
231
232
233
234
235
236
237
# File 'lib/cute/g5k_api.rb', line 228

def get_json(path)

  begin
    r = resource(path).get(:content_type => "application/json",
                           :user_agent => @user_agent)
  rescue => e
    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.



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

def post_json(path, json)

  begin
    r = resource(path).post(json.to_json,
                            :content_type => "application/json",
                            :accept => "application/json",
                            :user_agent => @user_agent)
  rescue => e
    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



221
222
223
224
# File 'lib/cute/g5k_api.rb', line 221

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