class RHC::Rest::Client

Constants

CLIENT_API_VERSIONS

Keep the list of supported API versions here The list may not necessarily be sorted; we will select the last matching one supported by the server. See api_version_negotiated

Attributes

client_api_versions[R]
server_api_versions[R]

Public Class Methods

new(end_point, username, password, use_debug=false, preferred_api_versions = CLIENT_API_VERSIONS) click to toggle source
# File lib/rhc/rest/client.rb, line 19
def initialize(end_point, username, password, use_debug=false, preferred_api_versions = CLIENT_API_VERSIONS)
  @debug = use_debug
  @end_point = end_point
  @server_api_versions = []
  debug "Connecting to #{end_point}"

  credentials = nil
  userpass = "#{username}:#{password}"
  # :nocov: version dependent code
  if RUBY_VERSION.to_f == 1.8
    credentials = Base64.encode64(userpass).delete("\n")
  else
    credentials = Base64.strict_encode64(userpass)
  end
  # :nocov:
  @@headers["Authorization"] = "Basic #{credentials}"
  @@headers["User-Agent"] = RHC::Helpers.user_agent rescue nil
  RestClient.proxy = ENV['http_proxy']
  
  # API version negotiation
  begin
    debug "Client supports API versions #{preferred_api_versions.join(', ')}"
    @client_api_versions = preferred_api_versions
    default_request = new_request(:url => @end_point, :method => :get, :headers => @@headers)
    @server_api_versions, links = api_info(default_request)
    debug "Server supports API versions #{@server_api_versions.join(', ')}"
  
    if api_version_negotiated
      unless server_api_version_current?
        debug "Client API version #{api_version_negotiated} is not current. Refetching API"
        # need to re-fetch API
        @@headers["Accept"] = "application/json; version=#{api_version_negotiated}"
        req = new_request(:url => @end_point, :method => :get, :headers => @@headers)
        @server_api_versions, links = api_info req
      end
    else
      warn_about_api_versions
    end
  rescue Exception => e
    raise ResourceAccessException.new("Failed to access resource: #{e.message}")
  end

  super({:links => links}, use_debug)
end

Public Instance Methods

add_domain(id) click to toggle source
# File lib/rhc/rest/client.rb, line 64
def add_domain(id)
  debug "Adding domain #{id}"
  rest_method "ADD_DOMAIN", :id => id
end
add_key(name, key, content) click to toggle source
# File lib/rhc/rest/client.rb, line 89
def add_key(name, key, content)
  debug "Adding key #{key} for #{user.login}"
  user.add_key name, key, content
end
api_version_match?() click to toggle source

API version related methods

# File lib/rhc/rest/client.rb, line 158
def api_version_match?
  ! api_version_negotiated.nil?
end
api_version_negotiated() click to toggle source

return the API version that the server and this client can agree on

# File lib/rhc/rest/client.rb, line 163
def api_version_negotiated
  client_api_versions.reverse. # choose the last API version listed
    detect { |v| @server_api_versions.include? v }
end
cartridges() click to toggle source
# File lib/rhc/rest/client.rb, line 74
def cartridges
  debug "Getting all cartridges"
  rest_method "LIST_CARTRIDGES"
end
client_api_version_current?() click to toggle source
# File lib/rhc/rest/client.rb, line 168
def client_api_version_current?
  current_client_api_version == api_version_negotiated
end
close() click to toggle source
Alias for: logout
current_client_api_version() click to toggle source
# File lib/rhc/rest/client.rb, line 172
def current_client_api_version
  client_api_versions.last
end
debug?() click to toggle source
# File lib/rhc/rest/client.rb, line 188
def debug?
  @debug
end
delete_key(name) click to toggle source
# File lib/rhc/rest/client.rb, line 94
def delete_key(name)
  debug "Deleting key '#{name}'"
  key = find_key(name)
  key.destroy
end
domains() click to toggle source
# File lib/rhc/rest/client.rb, line 69
def domains
  debug "Getting all domains"
  rest_method "LIST_DOMAINS"
end
find_cartridges(name) click to toggle source

Find Cartridge by name or regex

# File lib/rhc/rest/client.rb, line 109
def find_cartridges(name)
  debug "Finding cartridge #{name}"
  if name.is_a?(Hash)
    regex = name[:regex]
    type = name[:type]
    name = name[:name]
  end

  filtered = Array.new
  cartridges.each do |cart|
    if regex
      filtered.push(cart) if cart.name.match(regex) and (type.nil? or cart.type == type)
    else
      filtered.push(cart) if (name.nil? or cart.name == name) and (type.nil? or cart.type == type)
    end
  end
  return filtered
end
find_domain(id) click to toggle source

Find Domain by namesapce

# File lib/rhc/rest/client.rb, line 101
def find_domain(id)
  debug "Finding domain #{id}"
  domains.each { |domain| return domain if domain.id == id }

  raise RHC::DomainNotFoundException.new("Domain #{id} does not exist")
end
find_key(name) click to toggle source

find Key by name

# File lib/rhc/rest/client.rb, line 129
def find_key(name)
  debug "Finding key #{name}"
  user.find_key(name) or raise RHC::KeyNotFoundException.new("Key #{name} does not exist")
end
logout() click to toggle source
# File lib/rhc/rest/client.rb, line 150
def logout
  #TODO logout
  debug "Logout/Close client"
end
Also aliased as: close
server_api_version_current?() click to toggle source
# File lib/rhc/rest/client.rb, line 176
def server_api_version_current?
  @server_api_versions && @server_api_versions.max == api_version_negotiated
end
sshkeys() click to toggle source
# File lib/rhc/rest/client.rb, line 84
def sshkeys
  debug "Finding all keys for #{user.login}"
  user.keys
end
user() click to toggle source
# File lib/rhc/rest/client.rb, line 79
def user
  debug "Getting user info"
  rest_method "GET_USER"
end
warn_about_api_versions() click to toggle source
# File lib/rhc/rest/client.rb, line 180
      def warn_about_api_versions
        if !api_version_match?
          warn "WARNING: API version mismatch. This client supports #{client_api_versions.join(', ')} but
server at #{URI.parse(@end_point).host} supports #{@server_api_versions.join(', ')}."
          warn "The client version may be outdated; please consider updating 'rhc'. We will continue, but you may encounter problems."
        end
      end