dawn-api 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f67aba9063e02175a8f39faef656f1046d01a474
4
- data.tar.gz: 1b4424a7e02702a2acf8625162e30f233f58c6ea
3
+ metadata.gz: fe30248ae573868bf991429cda95c8dfceec4bf7
4
+ data.tar.gz: 161aa1267f3bbc2d5e4a08b79db80157a411033f
5
5
  SHA512:
6
- metadata.gz: c680999789ae45609b11fc9e311ae2a9a09437b9d182ab1d0beb5c30f0b9111e06466d364e3a85047bd6d16b52d6fed140d9abc7790fff925680442016e3e779
7
- data.tar.gz: 569ba8aab02dbd02e00952dcca4b32d5d6f251222d3ae8047ad448c0f748fe882bb3e781ccac24f7ed2d5441ea6ab496203683855b42db4fd3b75d4c6bef1e77
6
+ metadata.gz: bf6133ff0aa98e30f1d1f5b795b411e44aced402bcb524cdb836c01eb8978003b91f67c7038e1e4074761c1b2a1e673f2a5736d861c2b59ec80fb77b04c75309
7
+ data.tar.gz: 465a63b82801693130763868a26707871adbc88985e953c705d3bf9f54c0f9793cd488122b1c9d06bcc91913bf7215d45802ec47da26b75c6f4aef4852f0f4c3
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ Overview
2
+ ========
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Dawn-API
2
+ ========
3
+ [link](https://github.com/dawn/dawn-api)
4
+
5
+ [![Dependency Status](https://gemnasium.com/dawn/dawn-api.svg)](https://gemnasium.com/dawn/dawn-api)
6
+ [![Gem Version](https://badge.fury.io/rb/dawn-api.svg)](http://badge.fury.io/rb/dawn-api)
7
+ [![Code Climate](https://codeclimate.com/github/dawn/dawn-api.png)](https://codeclimate.com/github/dawn/dawn-api)
8
+
9
+ API for [Dawn](https://github.com/dawn/dawn)
10
+
11
+ ## Installation
12
+ ```shell
13
+ gem install dawn-api
14
+ ```
15
+
16
+ ## Building
17
+ ```shell
18
+ gem pack dawn-api.gem
19
+ ```
20
+
21
+ ## Documentation:
22
+ [Rubydoc](http://rubydoc.info/gems/dawn-api)
23
+
24
+ [API Reference](http://dawn.github.io/docs/)
25
+
26
+ ## Influential ENV variable
27
+ | VAR | API default | Description |
28
+ | ------------- | ------------- | ---------------------------------------------------------- |
29
+ | DAWN_HOST | dawn.dev | Main hostname |
30
+ | DAWN_SCHEME | http | |
31
+ | DAWN_API_HOST | api.dawn.dev | Dawn API hostname, if not given, api.$DAWN_HOST is used |
32
+ | DAWN_GIT_HOST | dawn.dev | Target for git push dawn, if not given, $DAWN_HOST is used |
33
+ | DAWN_LOG_HOST | dawn.dev:8001 | Dawn logs hostname, if not given, $DAWN_HOST:8001 is used |
34
+
35
+ ## What is this?
36
+ This is the ruby API interface for [Dawn](https://github.com/dawn/dawn),
37
+ - It is not a standalone gem.
38
+ - It requires a dawn server.
39
+ - It is not a CLI, look at [Dawn-CLI](https://github.com/dawn/dawn-cli) instead
40
+
41
+ ## Usage
42
+ One day I'll write this properly, however for now:
43
+
44
+ ```ruby
45
+ Dawn.authenticate
46
+ my_app = Dawn::App.create
47
+ my_app.update(name: "DawnIsAwesome")
48
+ my_app.gears.restart
49
+ ```
@@ -1,4 +1,5 @@
1
1
  require 'dawn/api/version'
2
+ require 'base64'
2
3
  require 'excon'
3
4
  require 'netrc'
4
5
 
@@ -15,21 +16,29 @@ module Dawn
15
16
 
16
17
  OPTIONS = {
17
18
  headers: {},
18
- host: 'api.dawn.dev',
19
19
  nonblock: false,
20
- scheme: 'http'
21
20
  }
22
21
 
23
22
  class AuthenticationError < RuntimeError
24
23
  end
25
24
 
25
+ class << self
26
+ attr_accessor :last_response_body
27
+ end
28
+
26
29
  def self.request(options)
27
30
  Dawn.authenticate unless @connection
28
- @connection.request(options)
31
+ expects = options.delete(:expects)
32
+ response = @connection.request(options)
33
+ self.last_response_body = response.body
34
+ if expects && response.status != expects
35
+ raise Excon::Errors.status_error(options.merge(expects: expects), response)
36
+ end
37
+ response
29
38
  end
30
39
 
31
40
  def self.authenticate(options={})
32
- options = OPTIONS.merge options
41
+ options = OPTIONS.merge(host: dawn_api_host, scheme: dawn_scheme).merge(options)
33
42
  options[:headers] = options[:headers].merge(HEADERS)
34
43
  @api_key = options.delete(:api_key) || ENV['DAWN_API_KEY']
35
44
 
@@ -59,7 +68,9 @@ module Dawn
59
68
  end
60
69
  end
61
70
  end
62
- @headers = HEADERS.merge 'Authorization' => "Token token=\"#{@api_key}\""
71
+ @headers = HEADERS.merge(
72
+ 'Authorization' => "Basic #{::Base64.strict_encode64("#{username}:#{@api_key}")}"
73
+ )
63
74
  @connection = Excon.new "#{options[:scheme]}://#{options[:host]}", headers: @headers
64
75
  end
65
76
 
@@ -3,21 +3,42 @@ require 'json'
3
3
 
4
4
  module Dawn
5
5
  module BaseApi
6
- module Extension
6
+ module RequestExtension
7
7
  def request(options)
8
- Dawn.request options
8
+ options[:expects] = 200 unless options.key?(:expects)
9
+ JSON.load Dawn.request(options).body
9
10
  end
10
11
 
11
- def json_request(options)
12
- JSON.load request(options).body
12
+ [:get, :post, :put, :patch, :delete].each do |key|
13
+ define_method(key) { |options| request options.merge(method: key) }
13
14
  end
14
15
  end
15
16
 
16
- include Extension
17
+ module ClassExtension
18
+ include RequestExtension
19
+
20
+ def id_param(options)
21
+ options.delete(:id)
22
+ end
23
+
24
+ def data_key(key_name, key_path=key_name)
25
+ route = key_path.to_s.split("/")
26
+ route_dp = route.dup
27
+ last_key = route_dp.pop
28
+
29
+ define_method(key_name) do
30
+ route.inject(@data) { |d, key| d[key] }
31
+ end
32
+ define_method(key_name.to_s+"=") do |v|
33
+ route_dp.inject(@data) { |d, key| d[key] }[last_key] = v
34
+ end
35
+ end
36
+ end
17
37
 
18
38
  def self.included(mod)
19
- mod.extend Extension
39
+ mod.extend ClassExtension
20
40
  end
21
41
 
42
+ include RequestExtension
22
43
  end
23
44
  end
@@ -1,11 +1,23 @@
1
1
  module Dawn
2
2
 
3
+ def self.dawn_scheme
4
+ ENV["DAWN_SCHEME"] || "http"
5
+ end
6
+
7
+ def self.dawn_host
8
+ ENV["DAWN_HOST"] || "dawn.dev"
9
+ end
10
+
11
+ def self.dawn_api_host
12
+ ENV["DAWN_API_HOST"] || "api.#{dawn_host}"
13
+ end
14
+
3
15
  def self.git_host
4
- "dawn.dev"
16
+ ENV["DAWN_GIT_HOST"] || dawn_host
5
17
  end
6
18
 
7
19
  def self.log_host
8
- "dawn.dev:8001"
20
+ ENV["DAWN_LOG_HOST"] || "#{dawn_host}:8001"
9
21
  end
10
22
 
11
23
  end
@@ -0,0 +1,43 @@
1
+ require 'dawn/api/base_api'
2
+
3
+ module Dawn
4
+ class Account
5
+
6
+ include BaseApi
7
+
8
+ def initialize(hsh)
9
+ @data = hsh
10
+ end
11
+
12
+ data_key :id
13
+ data_key :created_at
14
+ data_key :updated_at
15
+ data_key :username
16
+ data_key :email
17
+ data_key :api_key
18
+
19
+ def refresh(options={})
20
+ @data = get(
21
+ path: "/account",
22
+ query: options
23
+ )["account"]
24
+ end
25
+
26
+ def update(options={})
27
+ options.fetch(:account)
28
+
29
+ @data = patch(
30
+ path: "/account",
31
+ body: options.to_json
32
+ )["account"]
33
+ end
34
+
35
+ def self.current(options={})
36
+ new get(
37
+ path: "/account",
38
+ query: options
39
+ )["account"]
40
+ end
41
+
42
+ end
43
+ end
@@ -1,11 +1,12 @@
1
1
  require 'dawn/api/base_api'
2
- require 'dawn/api/domain'
2
+ require 'dawn/api/models/domain'
3
3
 
4
4
  module Dawn
5
5
  class App
6
6
  class Domains
7
7
 
8
8
  include BaseApi
9
+ include Enumerable
9
10
 
10
11
  attr_reader :app
11
12
 
@@ -13,25 +14,27 @@ module Dawn
13
14
  @app = app
14
15
  end
15
16
 
16
- def create(options={})
17
- Domain.new(json_request(
18
- expects: 200,
19
- method: :post,
17
+ def each(&block)
18
+ all.each(&block)
19
+ end
20
+
21
+ def create(options)
22
+ options.fetch(:domain)
23
+
24
+ Domain.new(post(
20
25
  path: "/apps/#{app.id}/domains",
21
26
  body: options.to_json
22
27
  )["domain"]).tap { |d| d.app = @app }
23
28
  end
24
29
 
25
30
  def all(options={})
26
- json_request(
27
- expects: 200,
28
- method: :get,
31
+ get(
29
32
  path: "/apps/#{app.id}/domains",
30
33
  query: options
31
34
  ).map { |hsh| Domain.new(hsh["domain"]).tap { |d| d.app = @app } }
32
35
  end
33
36
 
34
- def find(options={})
37
+ def find(options)
35
38
  Domain.find(options).tap { |d| d.app = @app }
36
39
  end
37
40
 
@@ -1,11 +1,12 @@
1
1
  require 'dawn/api/base_api'
2
- require 'dawn/api/drain'
2
+ require 'dawn/api/models/drain'
3
3
 
4
4
  module Dawn
5
5
  class App
6
6
  class Drains
7
7
 
8
8
  include BaseApi
9
+ include Enumerable
9
10
 
10
11
  attr_reader :app
11
12
 
@@ -13,19 +14,21 @@ module Dawn
13
14
  @app = app
14
15
  end
15
16
 
17
+ def each(&block)
18
+ all.each(&block)
19
+ end
20
+
16
21
  def create(options={})
17
- Drain.new(json_request(
18
- expects: 200,
19
- method: :post,
22
+ options.fetch(:drain)
23
+
24
+ Drain.new(post(
20
25
  path: "/apps/#{app.id}/drains",
21
26
  body: options.to_json
22
27
  )["drain"]).tap { |d| d.app = @app }
23
28
  end
24
29
 
25
30
  def all(options={})
26
- json_request(
27
- expects: 200,
28
- method: :get,
31
+ get(
29
32
  path: "/apps/#{app.id}/drains",
30
33
  query: options
31
34
  ).map { |hsh| Drain.new(hsh["drain"]).tap { |d| d.app = @app } }
@@ -15,20 +15,16 @@ module Dawn
15
15
  end
16
16
 
17
17
  def refresh(options={})
18
- replace json_request(
19
- expects: 200,
20
- method: :get,
18
+ replace get(
21
19
  path: "/apps/#{app.id}/env",
22
20
  query: options
23
21
  )["env"]
24
22
  end
25
23
 
26
24
  def save(options={})
27
- replace json_request(
28
- expects: 200,
29
- method: :post,
25
+ replace post(
30
26
  path: "/apps/#{app.id}/env",
31
- body: { env: merge(options) }.to_json
27
+ body: { app: { env: merge(options) } }.to_json
32
28
  )["env"]
33
29
  end
34
30
 
@@ -1,11 +1,12 @@
1
1
  require 'dawn/api/base_api'
2
- require 'dawn/api/gear'
2
+ require 'dawn/api/models/gear'
3
3
 
4
4
  module Dawn
5
5
  class App
6
6
  class Gears
7
7
 
8
8
  include BaseApi
9
+ include Enumerable
9
10
 
10
11
  attr_reader :app
11
12
 
@@ -13,19 +14,21 @@ module Dawn
13
14
  @app = app
14
15
  end
15
16
 
17
+ def each(&block)
18
+ all.each(&block)
19
+ end
20
+
16
21
  def create(options={})
17
- Gear.new(json_request(
18
- expects: 200,
19
- method: :post,
22
+ options.fetch(:gear)
23
+
24
+ Gear.new(post(
20
25
  path: "/apps/#{app.id}/gears",
21
26
  body: options.to_json
22
27
  )["gear"]).tap { |d| d.app = @app }
23
28
  end
24
29
 
25
30
  def all(options={})
26
- json_request(
27
- expects: 200,
28
- method: :get,
31
+ get(
29
32
  path: "/apps/#{app.id}/gears",
30
33
  query: options
31
34
  ).map { |hsh| Gear.new(hsh["gear"]).tap { |d| d.app = @app } }
@@ -36,9 +39,7 @@ module Dawn
36
39
  end
37
40
 
38
41
  def restart(options={})
39
- request(
40
- expects: 200,
41
- method: :post,
42
+ post(
42
43
  path: "/apps/#{app.id}/gears/restart",
43
44
  body: options.to_json
44
45
  )
@@ -0,0 +1,43 @@
1
+ require 'dawn/api/base_api'
2
+ require 'dawn/api/models/release'
3
+
4
+ module Dawn
5
+ class App
6
+ class Releases
7
+
8
+ include BaseApi
9
+ include Enumerable
10
+
11
+ attr_reader :app
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def each(&block)
18
+ all.each(&block)
19
+ end
20
+
21
+ def create(options={})
22
+ options.fetch(:release)
23
+
24
+ Release.new(post(
25
+ path: "/apps/#{app.id}/releases",
26
+ body: options.to_json
27
+ )["release"]).tap { |d| d.app = @app }
28
+ end
29
+
30
+ def all(options={})
31
+ get(
32
+ path: "/apps/#{app.id}/releases",
33
+ query: options
34
+ ).map { |hsh| Release.new(hsh["release"]).tap { |d| d.app = @app } }
35
+ end
36
+
37
+ def find(options={})
38
+ Release.find(options).tap { |d| d.app = @app }
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,172 @@
1
+ require 'dawn/api/hosts'
2
+ require 'dawn/api/base_api'
3
+ require 'dawn/api/models/app/env'
4
+ require 'dawn/api/models/app/gears'
5
+ require 'dawn/api/models/app/drains'
6
+ require 'dawn/api/models/app/domains'
7
+ require 'dawn/api/models/app/releases'
8
+
9
+ module Dawn
10
+ class App
11
+
12
+ include BaseApi
13
+
14
+ attr_reader :data
15
+ attr_reader :env
16
+
17
+ def initialize(hsh)
18
+ @data = hsh
19
+ @env = Env.new(self, @data.delete("env"))
20
+ end
21
+
22
+ data_key :id
23
+ data_key :created_at
24
+ data_key :updated_at
25
+ data_key :name
26
+ data_key :formation
27
+ data_key :git
28
+
29
+ def gears
30
+ @gears ||= Gears.new self
31
+ end
32
+
33
+ def drains
34
+ @drains ||= Drains.new self
35
+ end
36
+
37
+ def domains
38
+ @domains ||= Domains.new self
39
+ end
40
+
41
+ def releases
42
+ @releases ||= Releases.new self
43
+ end
44
+
45
+ def refresh(options={})
46
+ @data = get(
47
+ path: "/apps/#{id}",
48
+ query: options
49
+ )["app"]
50
+ end
51
+
52
+ def update(options={})
53
+ options.fetch(:app)
54
+
55
+ @data = patch(
56
+ path: "/apps/#{id}",
57
+ body: options.to_json
58
+ )["app"]
59
+ end
60
+
61
+ def destroy(options={})
62
+ self.class.destroy(options.merge(id: id))
63
+ end
64
+
65
+ def run(options={})
66
+ self.class.run(options.merge(id: id))
67
+ end
68
+
69
+ def logs(options={})
70
+ self.class.logs(options.merge(id: id))
71
+ end
72
+
73
+ def scale(options={})
74
+ self.class.scale(options.merge(id: id))
75
+ end
76
+
77
+ def restart(options={})
78
+ self.class.restart(options.merge(id: id))
79
+ end
80
+
81
+ def self.id_param(options)
82
+ options.delete(:id) ||
83
+ options.delete(:name) ||
84
+ raise
85
+ end
86
+
87
+ def self.create(options)
88
+ options.fetch(:app)
89
+
90
+ new post(
91
+ path: "/apps",
92
+ body: options.to_json
93
+ )["app"]
94
+ end
95
+
96
+ def self.all(options={})
97
+ get(
98
+ path: "/apps",
99
+ query: options
100
+ ).map { |d| new d["app"] }
101
+ end
102
+
103
+ def self.find(options)
104
+ id = id_param(options)
105
+
106
+ new get(
107
+ path: "/apps/#{id}",
108
+ query: options
109
+ )["app"]
110
+ end
111
+
112
+ def self.update(options)
113
+ id = id_param(options)
114
+ options.fetch(:app)
115
+
116
+ new patch(
117
+ path: "/apps/#{id}",
118
+ body: options.to_json
119
+ )["app"]
120
+ end
121
+
122
+ def self.destroy(options={})
123
+ id = id_param(options)
124
+
125
+ delete(
126
+ path: "/apps/#{id}",
127
+ query: options
128
+ )
129
+ end
130
+
131
+ def self.restart(options={})
132
+ id = id_param(options)
133
+
134
+ post(
135
+ path: "/apps/#{id}/gears/restart",
136
+ body: options.to_json
137
+ )
138
+ end
139
+
140
+ def self.logs(options={})
141
+ id = id_param(options)
142
+
143
+ url = get(
144
+ path: "/apps/#{id}/logs",
145
+ query: options
146
+ )["logs"]
147
+
148
+ "http://#{Dawn.log_host}#{url}"
149
+ end
150
+
151
+ def self.scale(options={})
152
+ id = id_param(options)
153
+ options.fetch(:app).fetch(:formation)
154
+
155
+ post(
156
+ path: "/apps/#{id}/scale",
157
+ body: options.to_json
158
+ )
159
+ end
160
+
161
+ def self.run(options={})
162
+ id = id_param(options)
163
+ options.fetch(:command)
164
+
165
+ post(
166
+ path: "/apps/#{id}/run",
167
+ body: options.to_json
168
+ )
169
+ end
170
+
171
+ end
172
+ end
@@ -0,0 +1,64 @@
1
+ require 'dawn/api/base_api'
2
+
3
+ module Dawn
4
+ class Domain
5
+
6
+ include BaseApi
7
+
8
+ attr_reader :data
9
+ attr_writer :app
10
+
11
+ def initialize(data)
12
+ @app = nil
13
+ @data = data
14
+ end
15
+
16
+ data_key :id
17
+ data_key :created_at
18
+ data_key :updated_at
19
+ data_key :url
20
+ data_key :app_id, "app/id"
21
+
22
+ def app
23
+ @app ||= App.find(id: app_id)
24
+ end
25
+
26
+ def refresh
27
+ @data = get(
28
+ path: "/domains/#{id}",
29
+ query: options
30
+ )["domain"]
31
+ end
32
+
33
+ def destroy(options={})
34
+ self.class.destroy(options.merge(id: id))
35
+ end
36
+
37
+ def self.id_param(options)
38
+ id = options.delete(:id) ||
39
+ options.delete(:name) ||
40
+ options.delete(:uri) ||
41
+ options.delete(:url) ||
42
+ raise
43
+ end
44
+
45
+ def self.find(options)
46
+ id = id_param(options)
47
+
48
+ new get(
49
+ path: "/domains/#{id}",
50
+ query: options
51
+ )["domain"]
52
+ end
53
+
54
+ def self.destroy(options)
55
+ id = id_param(options)
56
+
57
+ delete(
58
+ path: "/domains/#{id}",
59
+ query: options
60
+ )
61
+ end
62
+
63
+ end
64
+ end