Class: Cute::G5K::G5KRest Private
- Inherits:
-
Object
- Object
- Cute::G5K::G5KRest
- 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
- #user ⇒ Object readonly private
- #user_agent ⇒ Object private
Instance Method Summary collapse
-
#delete_json(path) ⇒ Object
private
Deletes a resource on the server.
-
#follow_parent(obj) ⇒ Object
private
The parent link.
-
#get_json(path) ⇒ Hash
private
The HTTP response.
-
#initialize(uri, api_version, user, pass, on_error) ⇒ G5KRest
constructor
private
Initializes a REST connection.
-
#post_json(path, json) ⇒ Object
private
Creates a resource on the server.
-
#resource(path) ⇒ Object
private
Returns a resource object.
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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/cute/g5k_api.rb', line 208 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} 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
#user ⇒ Object (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 |
#user_agent ⇒ 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.
201 202 203 |
# File 'lib/cute/g5k_api.rb', line 201 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
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/cute/g5k_api.rb', line 284 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.
301 302 303 |
# File 'lib/cute/g5k_api.rb', line 301 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.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/cute/g5k_api.rb', line 241 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/cute/g5k_api.rb', line 262 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
231 232 233 234 |
# File 'lib/cute/g5k_api.rb', line 231 def resource(path) path = path[1..-1] if path.start_with?('/') return @api[path] end |