module RHC::Rest::ApiMethods

These are methods that belong to the API object but are callable from the client for convenience.

Public Instance Methods

add_authorization(options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 317
def add_authorization(options={})
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('ADD_AUTHORIZATION', options, options)
end
add_domain(id, payload={}) click to toggle source
# File lib/rhc/rest/client.rb, line 17
def add_domain(id, payload={})
  debug "Adding domain #{id} with options #{payload.inspect}"
  @domains = nil
  payload.delete_if{ |k,v| k.nil? or v.nil? }
  api.rest_method "ADD_DOMAIN", {:id => id}.merge(payload)
end
add_key(name, key, content) click to toggle source
# File lib/rhc/rest/client.rb, line 269
def add_key(name, key, content)
  debug "Adding key #{key} for #{user.login}"
  user.add_key name, key, content
end
add_team(name, payload={}) click to toggle source
# File lib/rhc/rest/client.rb, line 76
def add_team(name, payload={})
  debug "Adding team #{name} with options #{payload.inspect}"
  @teams = nil
  payload.delete_if{ |k,v| k.nil? or v.nil? }
  if api.supports? 'ADD_TEAM'
    api.rest_method "ADD_TEAM", {:name => name}.merge(payload)
  else
    raise RHC::TeamsNotSupportedException
  end
end
allows_region_selection?() click to toggle source

allows_region_selection? determines if the broker will allow a client to specify the region for an app @returns true if allowed; nil otherwise

# File lib/rhc/rest/client.rb, line 293
def allows_region_selection?
  supported_regions = regions rescue []
  supported_regions.any? { |region| region.allow_selection == true }
end
applications(options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 38
def applications(options={})
  debug "Getting applications"
  if link = api.link_href(:LIST_APPLICATIONS)
    api.rest_method :LIST_APPLICATIONS, options
  else
    self.domains.map{ |d| d.applications(options) }.flatten
  end
end
authorization_scope_list() click to toggle source
# File lib/rhc/rest/client.rb, line 332
def authorization_scope_list
  raise AuthorizationsNotSupported unless supports_sessions?
  link = api.links['ADD_AUTHORIZATION']
  scope = link['optional_params'].find{ |h| h['name'] == 'scope' }
  scope['description'].scan(/(?!\n)\*(.*?)\n(.*?)(?:\n|\Z)/m).inject([]) do |h, (a, b)|
    h << [a.strip, b.strip] if a.present? && b.present?
    h
  end
end
authorizations() click to toggle source
# File lib/rhc/rest/client.rb, line 298
def authorizations
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method 'LIST_AUTHORIZATIONS'
end
cartridges() click to toggle source
# File lib/rhc/rest/client.rb, line 57
def cartridges
  debug "Getting all cartridges"
  @cartridges ||= api.rest_method("LIST_CARTRIDGES", nil, :lazy_auth => true)
end
delete_authorization(token) click to toggle source
# File lib/rhc/rest/client.rb, line 327
def delete_authorization(token)
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('SHOW_AUTHORIZATION', nil, {:method => :delete, :params => {':id' => token}})
end
delete_authorizations() click to toggle source
# File lib/rhc/rest/client.rb, line 322
def delete_authorizations
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('LIST_AUTHORIZATIONS', nil, {:method => :delete})
end
delete_key(name) click to toggle source
# File lib/rhc/rest/client.rb, line 274
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 24
def domains
  debug "Getting all domains"
  @domains ||= api.rest_method "LIST_DOMAINS"
end
find_application(domain, application, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 156
def find_application(domain, application, options={})
  precheck_domain_id(domain)
  precheck_application_id(application)
  request(:url => link_show_application_by_domain_name(domain, application), :method => "GET", :payload => options)
end
find_application_aliases(domain, application, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 174
def find_application_aliases(domain, application, options={})
  precheck_domain_id(domain)
  precheck_application_id(application)
  request(:url => link_show_application_by_domain_name(domain, application, "aliases"), :method => "GET", :payload => options)
end
find_application_by_id(id, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 180
def find_application_by_id(id, options={})
  precheck_application_id(id)
  if api.supports? :show_application
    request(:url => link_show_application_by_id(id), :method => "GET", :payload => options)
  else
    applications.find{ |a| a.id == id }
  end or raise ApplicationNotFoundException.new("Application with id #{id} not found")
end
find_application_by_id_gear_groups(id, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 189
def find_application_by_id_gear_groups(id, options={})
  precheck_application_id(id)
  if api.supports? :show_application
    request(:url => link_show_application_by_id(id, 'gear_groups'), :method => "GET", :payload => options)
  else
    applications.find{ |a| return a.gear_groups if a.id == id }
  end or raise ApplicationNotFoundException.new("Application with id #{id} not found")
end
find_application_gear_groups(domain, application, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 162
def find_application_gear_groups(domain, application, options={})
  precheck_domain_id(domain)
  precheck_application_id(application)
  request(:url => link_show_application_by_domain_name(domain, application, "gear_groups"), :method => "GET", :payload => options)
end
find_application_gear_groups_endpoints(domain, application, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 168
def find_application_gear_groups_endpoints(domain, application, options={})
  precheck_domain_id(domain)
  precheck_application_id(application)
  request(:url => link_show_application_by_domain_name(domain, application, "gear_groups"), :method => "GET", :payload => options.merge(:include => 'endpoints'))
end
find_cartridges(name) click to toggle source

Find Cartridge by name or regex

# File lib/rhc/rest/client.rb, line 239
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 namespace

# File lib/rhc/rest/client.rb, line 147
def find_domain(id)
  debug "Finding domain #{id}"
  if link = api.link_href(:SHOW_DOMAIN, ':name' => id)
    request(:url => link, :method => "GET")
  else
    domains.find{ |d| d.name.downcase == id.downcase }
  end or raise DomainNotFoundException.new("Domain #{id} not found")
end
find_key(name) click to toggle source

find Key by name

# File lib/rhc/rest/client.rb, line 259
def find_key(name)
  debug "Finding key #{name}"
  user.find_key(name) or raise RHC::KeyNotFoundException.new("Key #{name} does not exist")
end
find_team(name, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 119
def find_team(name, options={})
  matching_teams = if options[:global]
    search_teams(name, true).select { |t| t.name == name }
  elsif options[:owned]
    owned_teams.select { |t| t.name == name }
  else
    teams.select{ |t| t.name == name }
  end

  if matching_teams.blank?
    raise TeamNotFoundException.new("Team with name #{name} not found")
  elsif matching_teams.length > 1
    raise TeamNotFoundException.new("Multiple teams with name #{name} found. Use --team-id to select the team by id.")
  else
    matching_teams.first
  end
end
find_team_by_id(id, options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 137
def find_team_by_id(id, options={})
  precheck_team_id(id)
  if api.supports? :show_team
    request(:url => link_show_team_by_id(id), :method => "GET", :payload => options)
  else
    teams.find{ |t| t.id == id }
  end or raise TeamNotFoundException.new("Team with id #{id} not found")
end
new_session(options={}) click to toggle source

Returns nil if creating sessions is not supported, raises on error, otherwise returns an Authorization object.

# File lib/rhc/rest/client.rb, line 307
def new_session(options={})
  if supports_sessions?
    api.rest_method('ADD_AUTHORIZATION', {
      :scope => 'session',
      :note => "RHC/#{RHC::VERSION::STRING} (from #{Socket.gethostname rescue 'unknown'} on #{RUBY_PLATFORM})",
      :reuse => true
    }, options)
  end
end
owned_applications(options={}) click to toggle source
# File lib/rhc/rest/client.rb, line 47
def owned_applications(options={})
  debug "Getting owned applications"
  if link = api.link_href(:LIST_APPLICATIONS_BY_OWNER)
    @owned_applications ||= api.rest_method 'LIST_APPLICATIONS_BY_OWNER', :owner => '@self'
  else
    owned_domains_names = owned_domains.map{|d| d.name}
    @owned_applications ||= applications(options).select{|app| owned_domains_names.include?(app.domain)}
  end
end
owned_domains() click to toggle source
# File lib/rhc/rest/client.rb, line 29
def owned_domains
  debug "Getting owned domains"
  if link = api.link_href(:LIST_DOMAINS_BY_OWNER)
    @owned_domains ||= api.rest_method 'LIST_DOMAINS_BY_OWNER', :owner => '@self'
  else
    domains
  end
end
owned_teams(opts={}) click to toggle source
# File lib/rhc/rest/client.rb, line 96
def owned_teams(opts={})
  debug "Getting owned teams"
  if link = api.link_href(:LIST_TEAMS_BY_OWNER)
    @owned_teams ||= api.rest_method("LIST_TEAMS_BY_OWNER", opts.merge({:owner => '@self'}))
  else
    raise RHC::TeamsNotSupportedException
  end
end
precheck_application_id(application) click to toggle source
# File lib/rhc/rest/client.rb, line 204
def precheck_application_id(application)
  raise ApplicationNotFoundException.new("Application not specified") if application.blank?
  raise ApplicationNotFoundException.new("Application #{application} not found") if ['.','..'].include?(application)
end
precheck_domain_id(domain) click to toggle source

Catch domain ids which we can't make API calls for

# File lib/rhc/rest/client.rb, line 199
def precheck_domain_id(domain)
  raise DomainNotFoundException.new("Domain not specified") if domain.blank?
  raise DomainNotFoundException.new("Domain #{domain} not found") if ['.','..'].include?(domain)
end
precheck_team_id(team) click to toggle source
# File lib/rhc/rest/client.rb, line 209
def precheck_team_id(team)
  raise TeamNotFoundException.new("Team not specified") if team.blank?
  raise TeamNotFoundException.new("Team #{team} not found") if ['.','..'].include?(team)
end
regions() click to toggle source
# File lib/rhc/rest/client.rb, line 62
def regions
  debug "Getting all regions and zones available"
  if supports_regions_and_zones?
    @regions ||= api.rest_method("LIST_REGIONS")
  else
    raise RHC::RegionsAndZonesNotSupportedException
  end
end
reset() click to toggle source
# File lib/rhc/rest/client.rb, line 342
def reset
  (instance_variables - [
    :@end_point, :@debug, :@preferred_api_versions, :@auth, :@options, :@headers,
    :@last_options, :@httpclient, :@self_signed, :@current_api_version, :@api
  ]).each{ |sym| instance_variable_set(sym, nil) }
  self
end
search_owned_teams(search) click to toggle source
# File lib/rhc/rest/client.rb, line 114
def search_owned_teams(search)
  debug "Searching owned teams"
  owned_teams.select{|team| team.name.downcase =~ /#{Regexp.escape(search)}/i}
end
search_teams(search, global=false) click to toggle source
# File lib/rhc/rest/client.rb, line 105
def search_teams(search, global=false)
  debug "Searching teams"
  if link = api.link_href(:SEARCH_TEAMS)
    api.rest_method "SEARCH_TEAMS", :search => search, :global => global
  else
    raise RHC::TeamsNotSupportedException
  end
end
sshkeys() click to toggle source
# File lib/rhc/rest/client.rb, line 264
def sshkeys
  debug "Finding all keys for #{user.login}"
  user.keys
end
supports_regions_and_zones?() click to toggle source
# File lib/rhc/rest/client.rb, line 284
def supports_regions_and_zones?
  api.supports? :LIST_REGIONS
end
supports_sessions?() click to toggle source
# File lib/rhc/rest/client.rb, line 280
def supports_sessions?
  api.supports? 'ADD_AUTHORIZATION'
end
teams(opts={}) click to toggle source
# File lib/rhc/rest/client.rb, line 87
def teams(opts={})
  debug "Getting teams you are a member of"
  if link = api.link_href(:LIST_TEAMS)
    @teams ||= api.rest_method("LIST_TEAMS", opts)
  else
    raise RHC::TeamsNotSupportedException
  end
end
user() click to toggle source
# File lib/rhc/rest/client.rb, line 71
def user
  debug "Getting user info"
  @user ||= api.rest_method "GET_USER"
end