class Ropenstack::Rest

Public Instance Methods

build_headers(token) click to toggle source

Builds headers for requests which send JSON data, if a keystone token is supplied

it adds the X-Auth-Token field with the keystone token. Returns a hash which represents the http headers in a format accepted by Net::HTTP.

# File lib/ropenstack/rest.rb, line 57
def build_headers(token)
  headers = {'Content-Type' =>'application/json'}
  unless token.nil? 
    headers['X-Auth-Token'] = token
  end
  return headers
end
build_http(uri, timeout) click to toggle source

Build a HTTP object having been given a timeout and a URI object Returns Net::HTTP object.

# File lib/ropenstack/rest.rb, line 17
def build_http(uri, timeout)
  http = Net::HTTP.new(uri.host, uri.port)
  if(timeout > 0) 
          http.open_timeout = timeout
          http.read_timeout = timeout
  end
  return http
end
delete_request(uri, token = nil, manage_errors = true) click to toggle source

Wrapper function for delete requests, just provide a uri and it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the #do_request method.

# File lib/ropenstack/rest.rb, line 102
def delete_request(uri, token = nil, manage_errors = true)
  request = Net::HTTP::Delete.new(uri.request_uri, initheader = build_headers(token))
  return do_request(uri, request, manage_errors)
end
do_request(uri, request, manage_errors = true, timeout = 10) click to toggle source

The function which you call to perform a http request using the request object given in the parameters. By default manage errors is true, so all responses are passed through the error manager which converts the into Ropenstack errors.

# File lib/ropenstack/rest.rb, line 71
def do_request(uri, request, manage_errors = true, timeout = 10)
  begin 
    http = build_http(uri, timeout)
    if(manage_errors)
      return error_manager(uri, http.request(request))
    else
      http.request(request)
      return { "Success" => true }
    end
  rescue Timeout::Error
    raise Ropenstack::TimeoutError, "It took longer than #{timeout} to connect to #{uri.to_s}"      
  end       
end
error_manager(uri, response) click to toggle source

All responses from openstack where any errors need to be caught are passed through this function. Unless a successful response is passed it will throw a Ropenstack error. If successful returns a hash of response body, unless response body is nil then it returns an empty hash.

# File lib/ropenstack/rest.rb, line 33
def error_manager(uri, response)
  case response
  when Net::HTTPSuccess then
    # This covers cases where the response may not validate as JSON.
    unless response.body.nil? || response.body == 'null'
      return JSON.parse(response.body)
    end 
    return {}
  when Net::HTTPBadRequest
    raise Ropenstack::MalformedRequestError, response.body
  when Net::HTTPNotFound
    raise Ropenstack::NotFoundError, "URI: #{uri} \n" + response.body       
  when Net::HTTPUnauthorized
    raise Ropenstack::UnauthorisedError, response.body
  else
    raise Ropenstack::RopenstackError, response.body
  end
end
get_request(uri, token = nil, manage_errors = true) click to toggle source

Wrapper function for a get request, just provide a uri and it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the #do_request method.

# File lib/ropenstack/rest.rb, line 91
def get_request(uri, token = nil, manage_errors = true)
  request = Net::HTTP::Get.new(uri.request_uri, initheader = build_headers(token))
  return do_request(uri, request, manage_errors)
end
post_request(uri, body, token = nil, manage_errors = true) click to toggle source

Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data. For authenticated transactions a token can be provided.

# File lib/ropenstack/rest.rb, line 126
def post_request(uri, body, token = nil, manage_errors = true)
  request = Net::HTTP::Post.new(uri.request_uri, initheader = build_headers(token))
  request.body = body.to_json
  return do_request(uri, request, manage_errors)    
end
put_request(uri, body, token = nil, manage_errors = true) click to toggle source

Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the #do_request method

# File lib/ropenstack/rest.rb, line 114
def put_request(uri, body, token = nil, manage_errors = true)
  request = Net::HTTP::Put.new(uri.request_uri, initheader = build_headers(token))
  request.body = body.to_json
  return do_request(uri, request, manage_errors)    
end