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



287
288
289
290
# File 'lib/rhc/rest/client.rb', line 287

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



253
254
255
256
# File 'lib/rhc/rest/client.rb', line 253

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

#add_team(name, payload = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/rhc/rest/client.rb', line 67

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

#applications(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# 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_listObject



302
303
304
305
306
307
308
309
310
# File 'lib/rhc/rest/client.rb', line 302

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



268
269
270
271
# File 'lib/rhc/rest/client.rb', line 268

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

#cartridgesObject



57
58
59
60
# 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) ⇒ Object



297
298
299
300
# File 'lib/rhc/rest/client.rb', line 297

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

#delete_authorizationsObject



292
293
294
295
# File 'lib/rhc/rest/client.rb', line 292

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

#delete_key(name) ⇒ Object



258
259
260
261
262
# File 'lib/rhc/rest/client.rb', line 258

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



149
150
151
152
153
# File 'lib/rhc/rest/client.rb', line 149

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 = {}) ⇒ Object



161
162
163
164
165
# File 'lib/rhc/rest/client.rb', line 161

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 = {}) ⇒ Object



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

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 = {}) ⇒ Object



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

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 = {}) ⇒ Object



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

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_cartridges(name) ⇒ Object

Find Cartridge by name or regex



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rhc/rest/client.rb', line 223

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 namespace



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

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



243
244
245
246
# File 'lib/rhc/rest/client.rb', line 243

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 = {}) ⇒ Object



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

def find_team(name, options={})
  precheck_team_id(name)

  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 = {}) ⇒ Object



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

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


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

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


210
211
212
# File 'lib/rhc/rest/client.rb', line 210

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


214
215
216
# File 'lib/rhc/rest/client.rb', line 214

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


218
219
220
# File 'lib/rhc/rest/client.rb', line 218

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

#new_session(options = {}) ⇒ Object

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



277
278
279
280
281
282
283
284
285
# File 'lib/rhc/rest/client.rb', line 277

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

#owned_applications(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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_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

#owned_teams(opts = {}) ⇒ Object



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

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



191
192
193
194
# File 'lib/rhc/rest/client.rb', line 191

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

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



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

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



196
197
198
199
# File 'lib/rhc/rest/client.rb', line 196

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

#resetObject



312
313
314
315
316
317
318
# File 'lib/rhc/rest/client.rb', line 312

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



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

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



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

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

#sshkeysObject



248
249
250
251
# File 'lib/rhc/rest/client.rb', line 248

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

#supports_sessions?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/rhc/rest/client.rb', line 264

def supports_sessions?
  api.supports? 'ADD_AUTHORIZATION'
end

#teams(opts = {}) ⇒ Object



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

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

#userObject



62
63
64
65
# File 'lib/rhc/rest/client.rb', line 62

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