deis_client 1.5.0.dev2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/deis_client.rb +167 -0
  3. metadata +62 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 834087c2eeaf7a82e30d1a2ab722fe51ec1fe420
4
+ data.tar.gz: 784f3a5eaedd823b30dce5eb3741dd10ae613bf3
5
+ SHA512:
6
+ metadata.gz: f45f4f0e9ef6101d2d8117b6119af683b56d4a9606f840ea2dc5826b6ea48316f188ad3103905a8693671734d8fde83ace80d88ca70b6d15922bbc303e7407d4
7
+ data.tar.gz: 323f996e030cd5b39e22cb5d0db51add75576c8790daa434559be9a704ede0eef28ae5781d2ce1cd3ca5d9f9a2e0846d7c6417fe0a9b28d89a51eac2925e4348
@@ -0,0 +1,167 @@
1
+ require 'httparty'
2
+
3
+ module Deis
4
+ class ApiWrapper
5
+ include HTTParty
6
+ format :json
7
+ headers 'Accept' => 'application/json'
8
+
9
+ API_PATH = '/v1'
10
+
11
+ def initialize(deis_url)
12
+ @base_uri = deis_url + API_PATH
13
+ # self.class.base_uri (deis_url + API_PATH)
14
+ end
15
+
16
+ def get(path, options)
17
+ self.class.get(@base_uri + path, options)
18
+ end
19
+
20
+ def post(path, options)
21
+ self.class.post(@base_uri + path, options)
22
+ end
23
+
24
+ def delete(path, options)
25
+ self.class.delete(@base_uri + path, options)
26
+ end
27
+ end
28
+
29
+ class Client
30
+ @@methods = {
31
+ # method => HTTP-verb, path
32
+ login: [:post, '/auth/login/'],
33
+ apps: [:get, '/apps/'],
34
+ create_app: [:post, '/apps/'],
35
+ delete_app: [:delete, '/apps/:app/'],
36
+ app: [:get, '/apps/:app/'],
37
+ app_logs: [:get, '/apps/:app/logs/'],
38
+ app_run: [:post, '/apps/:app/run/'],
39
+ containers: [:get, '/apps/:app/containers/'],
40
+ config: [:get, '/apps/:app/config'],
41
+ domains: [:get, '/apps/:app/domains'],
42
+ builds: [:get, '/apps/:app/builds'],
43
+ create_build: [:post, '/apps/:app/builds'],
44
+ releases: [:get, '/apps/:app/releases'],
45
+ release: [:get, '/apps/:app/releases/:release'],
46
+ rollback_release: [:post, '/apps/:app/releases/rollback']
47
+ }
48
+
49
+ def initialize(deis_url, username, password)
50
+ @http = Deis::ApiWrapper.new deis_url
51
+ @headers = {}
52
+ @auth = {username: username, password: password}
53
+ end
54
+
55
+ def login
56
+ response = @http.post('/auth/login/', {body: @auth})
57
+
58
+ throw Exception unless response.code == 200
59
+
60
+ @token = response['token']
61
+ @headers['Authorization'] = "token #{@token}"
62
+ response
63
+ end
64
+
65
+ def apps
66
+ perform :apps
67
+ end
68
+
69
+ def create_app(id=nil)
70
+ if id
71
+ perform :create_app, {app: id}
72
+ else
73
+ perform :create_app
74
+ end
75
+ end
76
+
77
+ def delete_app(id)
78
+ perform :delete_app, {app: id}
79
+ end
80
+
81
+ def app(id)
82
+ perform :app, {app: id}
83
+ end
84
+
85
+ def app_logs(id)
86
+ perform :app_logs, {app: id}
87
+ end
88
+
89
+ def app_run(id, command)
90
+ perform :app_run, {app: id, command: command}
91
+ end
92
+
93
+ def containers(app_id)
94
+ perform :containers, {app: app_id}
95
+ end
96
+
97
+ def config(app_id)
98
+ perform :config, {app: app_id}
99
+ end
100
+
101
+ def domains(app_id)
102
+ perform :domains, {app: app_id}
103
+ end
104
+
105
+ def builds(app_id)
106
+ perform :builds, {app: app_id}
107
+ end
108
+
109
+ def create_build(app_id, image)
110
+ perform :create_build, {app: app_id, image: image}
111
+ end
112
+
113
+ def releases(app_id)
114
+ perform :releases, {app: app_id}
115
+ end
116
+
117
+ def release(app_id, release)
118
+ perform :releases, {app: app_id, release: release}
119
+ end
120
+
121
+ def rollback_release(app_id, release)
122
+ perform :rollback_release, {app: app_id, release: release}
123
+ end
124
+
125
+ protected
126
+
127
+ # TODO: use own, meaningful exceptions expecially in this method
128
+ def perform(method_sym, body={}, try_twice=true)
129
+ login unless @token
130
+
131
+ verb, path = @@methods[method_sym]
132
+ path = interpolate_path(path, body)
133
+
134
+ options = {
135
+ headers: @headers,
136
+ body: body
137
+ }
138
+ handle @http.public_send(verb, path, options), try_twice
139
+ end
140
+
141
+ def handle(response, try_twice)
142
+ case response.code
143
+ when 200...300
144
+ response.parsed_response
145
+ when 401 # authentification required
146
+ raise Exception unless try_twice
147
+ login
148
+ perform method_sym, options, false
149
+ when 404
150
+ nil # or better an exception?
151
+ else
152
+ raise Exception
153
+ end
154
+ end
155
+
156
+ def interpolate_path(path, body)
157
+ /\/:(?<key>\w+)\/?/ =~ path
158
+ return path unless key
159
+
160
+ value = body[key.to_sym]
161
+ path[':' + key] = value
162
+
163
+ # this catched only one occurance of an key, so call recursively until nothing is found anymore
164
+ interpolate_path path, body
165
+ end
166
+ end
167
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deis_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.0.dev2
5
+ platform: ruby
6
+ authors:
7
+ - Franz Liedke
8
+ - Cornelius Bock
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.13'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.13'
28
+ description: |-
29
+ (Deis)[http://deis.io/] is an open source application platform for public and private clouds.
30
+ This gem makes it easy to communicate with a Deis controller programmatically
31
+ via the (Deis controller api)[http://docs.deis.io/en/latest/reference/api-v1.5/]
32
+ email:
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/deis_client.rb
38
+ homepage: https://github.com/franzliedke/deis-client
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">"
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.1
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.4.5
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Client library to communicate with a (Deis)[http://deis.io/] Controller
62
+ test_files: []