Class: Azuki::API

Inherits:
Object
  • Object
show all
Defined in:
lib/azuki/api.rb,
lib/azuki/api/apps.rb,
lib/azuki/api/keys.rb,
lib/azuki/api/logs.rb,
lib/azuki/api/mock.rb,
lib/azuki/api/user.rb,
lib/azuki/api/login.rb,
lib/azuki/api/addons.rb,
lib/azuki/api/errors.rb,
lib/azuki/api/stacks.rb,
lib/azuki/api/domains.rb,
lib/azuki/api/version.rb,
lib/azuki/api/features.rb,
lib/azuki/api/releases.rb,
lib/azuki/api/mock/apps.rb,
lib/azuki/api/mock/keys.rb,
lib/azuki/api/mock/logs.rb,
lib/azuki/api/mock/user.rb,
lib/azuki/api/processes.rb,
lib/azuki/api/mock/login.rb,
lib/azuki/api/attachments.rb,
lib/azuki/api/config_vars.rb,
lib/azuki/api/mock/addons.rb,
lib/azuki/api/mock/stacks.rb,
lib/azuki/api/mock/domains.rb,
lib/azuki/api/collaborators.rb,
lib/azuki/api/mock/features.rb,
lib/azuki/api/mock/releases.rb,
lib/azuki/api/ssl_endpoints.rb,
lib/azuki/api/vendor/okjson.rb,
lib/azuki/api/mock/processes.rb,
lib/azuki/api/mock/attachments.rb,
lib/azuki/api/mock/config_vars.rb,
lib/azuki/api/mock/collaborators.rb

Defined Under Namespace

Modules: Errors, Mock, OkJson

Constant Summary collapse

HEADERS =
{
  'Accept'                => 'application/json',
  'Accept-Encoding'       => 'gzip',
  #'Accept-Language'       => 'en-US, en;q=0.8',
  'User-Agent'            => "azuki-rb/#{Azuki::API::VERSION}",
  'X-Ruby-Version'        => RUBY_VERSION,
  'X-Ruby-Platform'       => RUBY_PLATFORM
}
OPTIONS =
{
  :headers  => {},
  :host     => 'api.azukiapp.com',
  :nonblock => false,
  :scheme   => 'https'
}
VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/azuki/api.rb', line 55

def initialize(options={})
  options = OPTIONS.merge(options)

  @api_key = options.delete(:api_key) || ENV['AZUKI_API_KEY']
  if !@api_key && options.has_key?(:username) && options.has_key?(:password)
    @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS))
    @api_key = self.(options[:username], options[:password]).body["api_key"]
  end

  user_pass = ":#{@api_key}"
  options[:headers] = HEADERS.merge({
    'Authorization' => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
  }).merge(options[:headers])

  @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
end

Instance Method Details

#delete_addon(app, addon) ⇒ Object

DELETE /apps/:app/addons/:addon



5
6
7
8
9
10
11
# File 'lib/azuki/api/addons.rb', line 5

def delete_addon(app, addon)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/addons/#{addon}"
  )
end

#delete_app(app) ⇒ Object

DELETE /apps/:app



5
6
7
8
9
10
11
# File 'lib/azuki/api/apps.rb', line 5

def delete_app(app)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}"
  )
end

#delete_collaborator(app, email) ⇒ Object

DELETE /apps/:app/collaborators/:email



5
6
7
8
9
10
11
# File 'lib/azuki/api/collaborators.rb', line 5

def delete_collaborator(app, email)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/collaborators/#{email}"
  )
end

#delete_config_var(app, key) ⇒ Object

DELETE /apps/:app/config_vars/:key



5
6
7
8
9
10
11
# File 'lib/azuki/api/config_vars.rb', line 5

def delete_config_var(app, key)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/config_vars/#{escape(key)}"
  )
end

#delete_domain(app, domain) ⇒ Object

DELETE /apps/:app/domains/:domain



5
6
7
8
9
10
11
# File 'lib/azuki/api/domains.rb', line 5

def delete_domain(app, domain)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/domains/#{escape(domain)}"
  )
end

#delete_domains(app) ⇒ Object

DELETE /apps/:app/domains



14
15
16
17
18
19
20
# File 'lib/azuki/api/domains.rb', line 14

def delete_domains(app)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/domains"
  )
end

#delete_feature(feature, app = nil) ⇒ Object

DELETE /features/:feature



5
6
7
8
9
10
11
12
# File 'lib/azuki/api/features.rb', line 5

def delete_feature(feature, app = nil)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#delete_key(key) ⇒ Object

DELETE /user/keys/:key



5
6
7
8
9
10
11
# File 'lib/azuki/api/keys.rb', line 5

def delete_key(key)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/user/keys/#{escape(key)}"
  )
end

#delete_keysObject

DELETE /user/keys



14
15
16
17
18
19
20
# File 'lib/azuki/api/keys.rb', line 14

def delete_keys
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/user/keys"
  )
end

#delete_ssl_endpoint(app, cname) ⇒ Object

DELETE /apps/:app/ssl-endpoint/:cname



5
6
7
8
9
10
11
# File 'lib/azuki/api/ssl_endpoints.rb', line 5

def delete_ssl_endpoint(app, cname)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{app}/ssl-endpoints/#{escape(cname)}"
  )
end

#get_addons(app = nil) ⇒ Object

GET /addons GET /apps/:app/addons



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/azuki/api/addons.rb', line 15

def get_addons(app=nil)
  path = if app
    "/apps/#{app}/addons"
  else
    "/addons"
  end
  request(
    :expects  => 200,
    :method   => :get,
    :path     => path
  )
end

#get_app(app) ⇒ Object

GET /apps/:app



23
24
25
26
27
28
29
# File 'lib/azuki/api/apps.rb', line 23

def get_app(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}"
  )
end

#get_app_maintenance(app) ⇒ Object

GET /apps/:app/server/maintenance



32
33
34
35
36
37
38
# File 'lib/azuki/api/apps.rb', line 32

def get_app_maintenance(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/server/maintenance"
  )
end

#get_appsObject

GET /apps



14
15
16
17
18
19
20
# File 'lib/azuki/api/apps.rb', line 14

def get_apps
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps"
  )
end

#get_attachments(app) ⇒ Object

GET /apps/:app/attachments



5
6
7
8
9
10
11
# File 'lib/azuki/api/attachments.rb', line 5

def get_attachments(app)
  request(
    :expects => 200,
    :method => :get,
    :path => "/apps/#{app}/attachments"
  )
end

#get_collaborators(app) ⇒ Object

GET /apps/:app/collaborators



14
15
16
17
18
19
20
# File 'lib/azuki/api/collaborators.rb', line 14

def get_collaborators(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/collaborators"
  )
end

#get_config_vars(app) ⇒ Object

GET /apps/:app/config_vars



14
15
16
17
18
19
20
# File 'lib/azuki/api/config_vars.rb', line 14

def get_config_vars(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/config_vars"
  )
end

#get_domains(app) ⇒ Object

GET /apps/:app/domains



23
24
25
26
27
28
29
# File 'lib/azuki/api/domains.rb', line 23

def get_domains(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/domains"
  )
end

#get_feature(feature, app = nil) ⇒ Object

GET /features/:feature



25
26
27
28
29
30
31
32
# File 'lib/azuki/api/features.rb', line 25

def get_feature(feature, app = nil)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#get_features(app = nil) ⇒ Object

GET /features



15
16
17
18
19
20
21
22
# File 'lib/azuki/api/features.rb', line 15

def get_features(app = nil)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/features",
    :query    => { 'app' => app }
  )
end

#get_keysObject

GET /user/keys



23
24
25
26
27
28
29
# File 'lib/azuki/api/keys.rb', line 23

def get_keys
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/user/keys"
  )
end

#get_logs(app, options = {}) ⇒ Object

GET /apps/:app/logs



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/azuki/api/logs.rb', line 5

def get_logs(app, options = {})
  options = {
    'logplex' => 'true'
  }.merge(options)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/logs",
    :query    => options
  )
end

#get_ps(app) ⇒ Object

GET /apps/:app/ps



5
6
7
8
9
10
11
# File 'lib/azuki/api/processes.rb', line 5

def get_ps(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/ps"
  )
end

#get_release(app, release) ⇒ Object

GET /apps/:app/releases/:release



14
15
16
17
18
19
20
# File 'lib/azuki/api/releases.rb', line 14

def get_release(app, release)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/releases/#{release}"
  )
end

#get_releases(app) ⇒ Object

GET /apps/:app/releases



5
6
7
8
9
10
11
# File 'lib/azuki/api/releases.rb', line 5

def get_releases(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/releases"
  )
end

#get_ssl_endpoint(app, cname) ⇒ Object

GET /apps/:app/ssl-endpoint/:cname



14
15
16
17
18
19
20
# File 'lib/azuki/api/ssl_endpoints.rb', line 14

def get_ssl_endpoint(app, cname)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/ssl-endpoints/#{escape(cname)}"
  )
end

#get_ssl_endpoints(app) ⇒ Object

GET /apps/:app/ssl-endpoints



23
24
25
26
27
28
29
# File 'lib/azuki/api/ssl_endpoints.rb', line 23

def get_ssl_endpoints(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/ssl-endpoints"
  )
end

#get_stack(app) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/azuki/api/stacks.rb', line 4

def get_stack(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{app}/stack"
  )
end

#get_userObject

GET /user



5
6
7
8
9
10
11
# File 'lib/azuki/api/user.rb', line 5

def get_user
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/user"
  )
end

#post_addon(app, addon, config = {}) ⇒ Object

POST /apps/:app/addons/:addon



29
30
31
32
33
34
35
36
# File 'lib/azuki/api/addons.rb', line 29

def post_addon(app, addon, config = {})
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/addons/#{addon}",
    :query    => addon_params(config)
  )
end

#post_app(params = {}) ⇒ Object

POST /apps



41
42
43
44
45
46
47
48
# File 'lib/azuki/api/apps.rb', line 41

def post_app(params={})
  request(
    :expects  => 202,
    :method   => :post,
    :path     => '/apps',
    :query    => app_params(params)
  )
end

#post_app_maintenance(app, maintenance_mode) ⇒ Object

POST /apps/:app/server/maintenance



51
52
53
54
55
56
57
58
# File 'lib/azuki/api/apps.rb', line 51

def post_app_maintenance(app, maintenance_mode)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/server/maintenance",
    :query    => {'maintenance_mode' => maintenance_mode}
  )
end

#post_collaborator(app, email) ⇒ Object

POST /apps/:app/collaborators



23
24
25
26
27
28
29
30
# File 'lib/azuki/api/collaborators.rb', line 23

def post_collaborator(app, email)
  request(
    :expects  => [200, 201],
    :method   => :post,
    :path     => "/apps/#{app}/collaborators",
    :query    => {'collaborator[email]' => email}
  )
end

#post_domain(app, domain) ⇒ Object

POST /apps/:app/domains



32
33
34
35
36
37
38
39
# File 'lib/azuki/api/domains.rb', line 32

def post_domain(app, domain)
  request(
    :expects  => 201,
    :method   => :post,
    :path     => "/apps/#{app}/domains",
    :query    => {'domain_name[domain]' => domain}
  )
end

#post_feature(feature, app = nil) ⇒ Object

POST /features/:feature



35
36
37
38
39
40
41
42
# File 'lib/azuki/api/features.rb', line 35

def post_feature(feature, app = nil)
  request(
    :expects  => [200, 201],
    :method   => :post,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#post_key(key) ⇒ Object

POST /user/keys



32
33
34
35
36
37
38
39
# File 'lib/azuki/api/keys.rb', line 32

def post_key(key)
  request(
    :body     => key,
    :expects  => 200,
    :method   => :post,
    :path     => "/user/keys"
  )
end

#post_login(username, password) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/azuki/api/login.rb', line 4

def (username, password)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => '/login',
    :query    => { 'username' => username, 'password' => password }
  )
end

#post_ps(app, command, options = {}) ⇒ Object

POST /apps/:app/ps



14
15
16
17
18
19
20
21
22
# File 'lib/azuki/api/processes.rb', line 14

def post_ps(app, command, options={})
  options = { 'command' => command }.merge(options)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ps",
    :query    => ps_options(options)
  )
end

#post_ps_restart(app, options = {}) ⇒ Object

POST /apps/:app/ps/restart



25
26
27
28
29
30
31
32
# File 'lib/azuki/api/processes.rb', line 25

def post_ps_restart(app, options={})
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ps/restart",
    :query    => options
  )
end

#post_ps_scale(app, type, quantity) ⇒ Object

POST /apps/:app/ps/scale



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/azuki/api/processes.rb', line 35

def post_ps_scale(app, type, quantity)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ps/scale",
    :query    => {
      'type'  => type,
      'qty'   => quantity
    }
  )
end

#post_ps_stop(app, options) ⇒ Object

POST /apps/:app/ps/stop



48
49
50
51
52
53
54
55
# File 'lib/azuki/api/processes.rb', line 48

def post_ps_stop(app, options)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ps/stop",
    :query    => options
  )
end

#post_release(app, release = nil) ⇒ Object

POST /apps/:app/releases/:release



23
24
25
26
27
28
29
30
# File 'lib/azuki/api/releases.rb', line 23

def post_release(app, release=nil)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/releases",
    :query    => {'rollback' => release}
  )
end

#post_ssl_endpoint(app, pem, key) ⇒ Object

POST /apps/:app/ssl-endpoints



32
33
34
35
36
37
38
39
# File 'lib/azuki/api/ssl_endpoints.rb', line 32

def post_ssl_endpoint(app, pem, key)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ssl-endpoints",
    :query     => { 'key' => key, 'pem' => pem }
  )
end

#post_ssl_endpoint_rollback(app, cname) ⇒ Object

POST /apps/:app/ssl-endpoints/:cname/rollback



42
43
44
45
46
47
48
# File 'lib/azuki/api/ssl_endpoints.rb', line 42

def post_ssl_endpoint_rollback(app, cname)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{app}/ssl-endpoints/#{escape(cname)}/rollback"
  )
end

#put_addon(app, addon, config = {}) ⇒ Object

PUT /apps/:app/addons/:addon



39
40
41
42
43
44
45
46
# File 'lib/azuki/api/addons.rb', line 39

def put_addon(app, addon, config = {})
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/addons/#{addon}",
    :query    => addon_params(config)
  )
end

#put_app(app, params) ⇒ Object

PUT /apps/:app



61
62
63
64
65
66
67
68
# File 'lib/azuki/api/apps.rb', line 61

def put_app(app, params)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}",
    :query    => app_params(params)
  )
end

#put_config_vars(app, vars) ⇒ Object

PUT /apps/:app/config_vars



23
24
25
26
27
28
29
30
# File 'lib/azuki/api/config_vars.rb', line 23

def put_config_vars(app, vars)
  request(
    :body     => Azuki::API::OkJson.encode(vars),
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/config_vars"
  )
end

#put_dynos(app, dynos) ⇒ Object

PUT /apps/:app/dynos



58
59
60
61
62
63
64
65
# File 'lib/azuki/api/processes.rb', line 58

def put_dynos(app, dynos)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/dynos",
    :query    => {'dynos' => dynos}
  )
end

#put_ssl_endpoint(app, cname, pem, key) ⇒ Object

PUT /apps/:app/ssl-endpoints/:cname



51
52
53
54
55
56
57
58
# File 'lib/azuki/api/ssl_endpoints.rb', line 51

def put_ssl_endpoint(app, cname, pem, key)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/ssl-endpoints/#{escape(cname)}",
    :query     => { 'key' => key, 'pem' => pem }
  )
end

#put_stack(app, stack) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/azuki/api/stacks.rb', line 12

def put_stack(app, stack)
  request(
    :body     => stack,
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/stack"
  )
end

#put_workers(app, workers) ⇒ Object

PUT /apps/:app/workers



68
69
70
71
72
73
74
75
# File 'lib/azuki/api/processes.rb', line 68

def put_workers(app, workers)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{app}/workers",
    :query    => {'workers' => workers}
  )
end

#request(params, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/azuki/api.rb', line 72

def request(params, &block)
  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status
      when 401 then Azuki::API::Errors::Unauthorized
      when 402 then Azuki::API::Errors::VerificationRequired
      when 403 then Azuki::API::Errors::Forbidden
      when 404
        if error.request[:path].match /\/apps\/\/.*/
          Azuki::API::Errors::NilApp
        else
          Azuki::API::Errors::NotFound
        end
      when 408 then Azuki::API::Errors::Timeout
      when 422 then Azuki::API::Errors::RequestFailed
      when 423 then Azuki::API::Errors::Locked
      when /50./ then Azuki::API::Errors::RequestFailed
      else Azuki::API::Errors::ErrorWithResponse
    end

    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    raise(reerror)
  end

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    begin
      response.body = Azuki::API::OkJson.decode(response.body)
    rescue
      # leave non-JSON body as is
    end
  end

  # reset (non-persistent) connection
  @connection.reset

  response
end