Module: RHC::Rest::ApiMethods

Included in:
Client
Defined in:
lib/rhc/rest/client.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#add_authorization(options = {}) ⇒ Object



176
177
178
179
# File 'lib/rhc/rest/client.rb', line 176

def add_authorization(options={})
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('ADD_AUTHORIZATION', options, options)
end

#add_domain(id, payload = {}) ⇒ Object



17
18
19
20
21
22
# 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) ⇒ Object



142
143
144
145
# File 'lib/rhc/rest/client.rb', line 142

def add_key(name, key, content)
  debug "Adding key #{key} for #{user.}"
  user.add_key name, key, content
end

#applications(options = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rhc/rest/client.rb', line 38

def applications(options={})
  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_listObject



191
192
193
194
195
196
197
198
199
# File 'lib/rhc/rest/client.rb', line 191

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

#authorizationsObject



157
158
159
160
# File 'lib/rhc/rest/client.rb', line 157

def authorizations
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method 'LIST_AUTHORIZATIONS'
end

#cartridgesObject



46
47
48
49
# File 'lib/rhc/rest/client.rb', line 46

def cartridges
  debug "Getting all cartridges"
  @cartridges ||= api.rest_method("LIST_CARTRIDGES", nil, :lazy_auth => true)
end

#delete_authorization(token) ⇒ Object



186
187
188
189
# File 'lib/rhc/rest/client.rb', line 186

def delete_authorization(token)
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('SHOW_AUTHORIZATION', nil, {:method => :delete, :params => {':id' => token}})
end

#delete_authorizationsObject



181
182
183
184
# File 'lib/rhc/rest/client.rb', line 181

def delete_authorizations
  raise AuthorizationsNotSupported unless supports_sessions?
  api.rest_method('LIST_AUTHORIZATIONS', nil, {:method => :delete})
end

#delete_key(name) ⇒ Object



147
148
149
150
151
# File 'lib/rhc/rest/client.rb', line 147

def delete_key(name)
  debug "Deleting key '#{name}'"
  key = find_key(name)
  key.destroy
end

#domainsObject



24
25
26
27
# 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 = {}) ⇒ Object



66
67
68
# File 'lib/rhc/rest/client.rb', line 66

def find_application(domain, application, options={})
  request(:url => link_show_application_by_domain_name(domain, application), :method => "GET", :payload => options)
end

#find_application_aliases(domain, application, options = {}) ⇒ Object



74
75
76
# File 'lib/rhc/rest/client.rb', line 74

def find_application_aliases(domain, application, options={})
  request(:url => link_show_application_by_domain_name(domain, application, "aliases"), :method => "GET", :payload => options)
end

#find_application_by_id(id, options = {}) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rhc/rest/client.rb', line 78

def find_application_by_id(id, options={})
  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 = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/rhc/rest/client.rb', line 86

def find_application_by_id_gear_groups(id, options={})
  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 = {}) ⇒ Object



70
71
72
# File 'lib/rhc/rest/client.rb', line 70

def find_application_gear_groups(domain, application, options={})
  request(:url => link_show_application_by_domain_name(domain, application, "gear_groups"), :method => "GET", :payload => options)
end

#find_cartridges(name) ⇒ Object

Find Cartridge by name or regex



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rhc/rest/client.rb', line 112

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) ⇒ Object

Find Domain by namesapce



57
58
59
60
61
62
63
64
# File 'lib/rhc/rest/client.rb', line 57

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) ⇒ Object

find Key by name



132
133
134
135
# File 'lib/rhc/rest/client.rb', line 132

def find_key(name)
  debug "Finding key #{name}"
  user.find_key(name) or raise RHC::KeyNotFoundException.new("Key #{name} does not exist")
end


94
95
96
97
98
99
100
101
# File 'lib/rhc/rest/client.rb', line 94

def link_show_application_by_domain_name(domain, application, *args)
  [
    api.links['LIST_DOMAINS']['href'],
    domain,
    "applications",
    application,
  ].concat(args).map{ |s| URI.escape(s) }.join("/")
end


103
104
105
# File 'lib/rhc/rest/client.rb', line 103

def link_show_application_by_id(id, *args)
  api.link_href(:SHOW_APPLICATION, {':id' => id}, *args)
end


107
108
109
# File 'lib/rhc/rest/client.rb', line 107

def link_show_domain_by_name(domain, *args)
  api.link_href(:SHOW_DOMAIN, ':id' => domain)
end

#new_session(options = {}) ⇒ Object

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



166
167
168
169
170
171
172
173
174
# File 'lib/rhc/rest/client.rb', line 166

def new_session(options={})
  if supports_sessions?
    api.rest_method('ADD_AUTHORIZATION', {
      :scope => 'session',
      :note => "PBOX/#{RHC::VERSION::STRING} (from #{Socket.gethostname rescue 'unknown'} on #{RUBY_PLATFORM})",
      :reuse => true
    }, options)
  end
end

#owned_domainsObject



29
30
31
32
33
34
35
36
# 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

#resetObject



201
202
203
204
205
206
207
# File 'lib/rhc/rest/client.rb', line 201

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

#sshkeysObject



137
138
139
140
# File 'lib/rhc/rest/client.rb', line 137

def sshkeys
  debug "Finding all keys for #{user.}"
  user.keys
end

#supports_sessions?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/rhc/rest/client.rb', line 153

def supports_sessions?
  api.supports? 'ADD_AUTHORIZATION'
end

#userObject



51
52
53
54
# File 'lib/rhc/rest/client.rb', line 51

def user
  debug "Getting user info"
  @user ||= api.rest_method "GET_USER"
end