class Deis::Client

Public Class Methods

new(deis_url, username, password) click to toggle source
# File lib/deis_client.rb, line 90
def initialize(deis_url, username, password)
  @http = Deis::ApiWrapper.new deis_url
  @headers = {'Content-Type' => 'application/json'}
  @auth = { username: username, password: password }
end

Public Instance Methods

add_domain(app_id, domain) click to toggle source
# File lib/deis_client.rb, line 182
def add_domain(app_id, domain)
  perform :add_domain, { app: app_id }, domain: domain
end
app(id) click to toggle source
# File lib/deis_client.rb, line 134
def app(id)
  perform :app, app: id
end
app_logs(id) click to toggle source
# File lib/deis_client.rb, line 138
def app_logs(id)
  perform :app_logs, app: id
end
app_run(id, command) click to toggle source
# File lib/deis_client.rb, line 142
def app_run(id, command)
  perform :app_run, { app: id }, command: command
end
apps() click to toggle source
# File lib/deis_client.rb, line 118
def apps
  perform :apps
end
builds(app_id) click to toggle source
# File lib/deis_client.rb, line 190
def builds(app_id)
  perform :builds, app: app_id
end
cert(domain) click to toggle source
# File lib/deis_client.rb, line 226
def cert(domain)
  perform :cert, { domain: domain }
end
certs() click to toggle source
# File lib/deis_client.rb, line 222
def certs
  perform :certs
end
change_password(old, new, opts = {}) click to toggle source
# File lib/deis_client.rb, line 107
def change_password(old, new, opts = {})
  body = {
    password: old,
    new_password: new
  }

  body[:username] = opts[:username] if opts[:username]

  perform :change_password, {}, body
end
config(app_id) click to toggle source
# File lib/deis_client.rb, line 170
def config(app_id)
  perform :config, app: app_id
end
containers(app_id, opts = {}) click to toggle source
# File lib/deis_client.rb, line 146
def containers(app_id, opts = {})
  if opts[:type]
    perform :containers_by_type, app: app_id, type: opts[:type]
  else
    perform :containers, app: app_id
  end
end
create_app(id = nil) click to toggle source
# File lib/deis_client.rb, line 122
def create_app(id = nil)
  if id
    perform :create_app, {}, id: id
  else
    perform :create_app
  end
end
create_build(app_id, image) click to toggle source
# File lib/deis_client.rb, line 194
def create_build(app_id, image)
  perform :create_build, { app: app_id }, image: image
end
create_cert(certificate, key, name = nil) click to toggle source
# File lib/deis_client.rb, line 210
def create_cert(certificate, key, name = nil)
  if name.nil?
    perform :create_cert, { }, { certificate: certificate, key: key }
  else
    perform :create_cert, { }, { certificate: certificate, key: key, common_name: name }
  end
end
delete_app(id) click to toggle source
# File lib/deis_client.rb, line 130
def delete_app(id)
  perform :delete_app, app: id
end
delete_cert(domain) click to toggle source
# File lib/deis_client.rb, line 218
def delete_cert(domain)
  perform :delete_cert, { domain: domain }
end
domains(app_id) click to toggle source
# File lib/deis_client.rb, line 178
def domains(app_id)
  perform :domains, app: app_id
end
login() click to toggle source
# File lib/deis_client.rb, line 96
def login
  verb, path = @@methods[:login]
  response = @http.public_send(verb, path, body: @auth)

  raise AuthorizationError.new unless response.code == 200

  @token = response['token']
  @headers['Authorization'] = "token #{@token}"
  response
end
release(app_id, release) click to toggle source
# File lib/deis_client.rb, line 202
def release(app_id, release)
  perform :releases, app: app_id, release: release
end
releases(app_id) click to toggle source
# File lib/deis_client.rb, line 198
def releases(app_id)
  perform :releases, app: app_id
end
remove_domain(app_id, domain) click to toggle source
# File lib/deis_client.rb, line 186
def remove_domain(app_id, domain)
  perform :remove_domain, { app: app_id, domain: domain }
end
restart_containers(app_id, opts = {}) click to toggle source
# File lib/deis_client.rb, line 154
def restart_containers(app_id, opts = {})
  if opts[:type]
    if opts[:number]
      perform :restart_container, opts.merge(app: app_id)
    else
      perform :restart_containers_by_type, app: app_id, type: opts[:type]
    end
  else
    perform :restart_containers, app: app_id
  end
end
rollback_release(app_id, release) click to toggle source
# File lib/deis_client.rb, line 206
def rollback_release(app_id, release)
  perform :rollback_release, { app: app_id }, release: release
end
scale(app_id, type_number_hash) click to toggle source
# File lib/deis_client.rb, line 166
def scale(app_id, type_number_hash)
  perform :scale, { app: app_id }, type_number_hash
end
set_config(app_id, values) click to toggle source
# File lib/deis_client.rb, line 174
def set_config(app_id, values)
  perform :set_config, { app: app_id }, values: values
end

Protected Instance Methods

handle(response) click to toggle source
# File lib/deis_client.rb, line 254
def handle(response)
  case response.code
  when 200...300
    response.parsed_response
  when 401
    raise AuthorizationError.new
  when 404
    raise NotFound.new
  when 400...500
    raise ClientError.new response.code, response.message, response: response
  when 500...600
    raise ServerError.new response.code, response.message, response: response
  else
    raise Error.new response.code, response.message, response: response
  end
end
interpolate_path(path, interpolations) click to toggle source
# File lib/deis_client.rb, line 271
def interpolate_path(path, interpolations)
  %r{/:(?<key>\w+)/?} =~ path
  return path unless key

  value = interpolations[key.to_sym]
  path[':' + key] = value

  # this catched only one occurance of an key, so call recursively until nothing is found anymore
  interpolate_path path, interpolations
end
perform(method_sym, interpolations = {}, body = {}, try_twice = true) click to toggle source
# File lib/deis_client.rb, line 232
def perform(method_sym, interpolations = {}, body = {}, try_twice = true)
  login unless @token

  verb, path = @@methods[method_sym]
  # Interpolate path modifies the path instead of only returning the new one.
  #   hence we duplicate it so the original is never modified.
  path = interpolate_path(path.dup, interpolations)

  options = {
    headers: @headers,
    body: body.to_json
  }

  begin
    handle @http.public_send(verb, path, options)
  rescue AuthorizationError => e
    raise e unless try_twice
    login
    handle @http.public_send(verb, path, options)
  end
end