qtc-sdk 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +18 -18
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/Changelog.md +28 -23
  6. data/Gemfile +4 -4
  7. data/LICENSE.txt +22 -22
  8. data/README.md +42 -44
  9. data/Rakefile +1 -1
  10. data/bin/qtc-cli +13 -13
  11. data/lib/qtc-sdk.rb +1 -1
  12. data/lib/qtc/cli/commands.rb +15 -15
  13. data/lib/qtc/cli/common.rb +146 -146
  14. data/lib/qtc/cli/eds/base.rb +27 -27
  15. data/lib/qtc/cli/eds/commands.rb +20 -20
  16. data/lib/qtc/cli/eds/instances.rb +30 -30
  17. data/lib/qtc/cli/mar/apps.rb +116 -116
  18. data/lib/qtc/cli/mar/base.rb +60 -60
  19. data/lib/qtc/cli/mar/commands.rb +268 -266
  20. data/lib/qtc/cli/mar/debug.rb +94 -88
  21. data/lib/qtc/cli/mar/domains.rb +35 -35
  22. data/lib/qtc/cli/mar/env.rb +38 -38
  23. data/lib/qtc/cli/mar/repository.rb +24 -24
  24. data/lib/qtc/cli/mar/slugs.rb +80 -80
  25. data/lib/qtc/cli/mar/ssl_certificates.rb +40 -40
  26. data/lib/qtc/cli/mar/stack.rb +29 -29
  27. data/lib/qtc/cli/mdb/base.rb +47 -47
  28. data/lib/qtc/cli/mdb/commands.rb +43 -43
  29. data/lib/qtc/cli/mdb/instances.rb +79 -79
  30. data/lib/qtc/cli/platform/clouds.rb +33 -33
  31. data/lib/qtc/cli/platform/commands.rb +133 -132
  32. data/lib/qtc/cli/platform/datacenters.rb +23 -23
  33. data/lib/qtc/cli/platform/ssh_keys.rb +41 -41
  34. data/lib/qtc/cli/platform/user.rb +29 -25
  35. data/lib/qtc/cli/platform/vpn.rb +93 -93
  36. data/lib/qtc/client.rb +170 -170
  37. data/lib/qtc/eds/client.rb +116 -116
  38. data/lib/qtc/eds/collection.rb +124 -124
  39. data/lib/qtc/eds/user_collection.rb +13 -13
  40. data/lib/qtc/eds/usergroup_collection.rb +41 -41
  41. data/lib/qtc/errors.rb +13 -13
  42. data/lib/qtc/version.rb +3 -3
  43. data/qtc-sdk.gemspec +28 -28
  44. data/spec/spec_helper.rb +17 -0
  45. data/spec/unit/qtc/client_spec.rb +147 -147
  46. metadata +5 -3
@@ -1,25 +1,29 @@
1
- require 'qtc/client'
2
- require_relative '../common'
3
-
4
- module Qtc::Cli::Platform
5
- class User
6
- include Qtc::Cli::Common
7
-
8
- def login
9
- pass = password("Personal Access Token (copy from https://console.qtcloudservices.com/#/user/profile):")
10
- inifile['platform']['token'] = pass
11
-
12
- response = platform_client(pass).get('/user/accounts', {}) rescue nil
13
- if response
14
- inifile.save(filename: ini_filename)
15
- else
16
- print color('Invalid Personal Access Token', :red)
17
- end
18
- end
19
-
20
- def logout
21
- inifile['platform'].delete('token')
22
- inifile.save(filename: ini_filename)
23
- end
24
- end
25
- end
1
+ require 'qtc/client'
2
+ require_relative '../common'
3
+
4
+ module Qtc::Cli::Platform
5
+ class User
6
+ include Qtc::Cli::Common
7
+
8
+ def login(opts)
9
+ if opts.token
10
+ inifile['platform']['token'] = opts.token
11
+ else
12
+ pass = password("Personal Access Token (copy from https://console.qtcloudservices.com/#/user/profile):")
13
+ inifile['platform']['token'] = pass
14
+ end
15
+
16
+ response = platform_client(pass).get('/user/accounts', {}) rescue nil
17
+ if response
18
+ inifile.save(filename: ini_filename)
19
+ else
20
+ print color('Invalid Personal Access Token', :red)
21
+ end
22
+ end
23
+
24
+ def logout
25
+ inifile['platform'].delete('token')
26
+ inifile.save(filename: ini_filename)
27
+ end
28
+ end
29
+ end
@@ -1,93 +1,93 @@
1
- require 'base64'
2
-
3
- module Qtc
4
- module Cli
5
- class Platform::Vpn
6
- include Qtc::Cli::Common
7
-
8
- def create
9
- self.datacenter_id = vpn_datacenter_id
10
- client.post('/vpn_containers', {name: 'default vpn'}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
11
- end
12
-
13
- def show
14
- result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
15
- vpn = result['results'][0]
16
- if vpn
17
- puts "id: #{vpn['id']}"
18
- puts "name: #{vpn['name']}"
19
- puts "state: #{vpn['state']}"
20
- else
21
- puts 'vpn not found, you can create vpn service with: qtc-cli vpn:create'
22
- end
23
- end
24
-
25
- def start
26
- result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
27
- vpn = result['results'][0]
28
- if vpn
29
- client.post("/vpn_containers/#{vpn['id']}/start", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
30
- else
31
- puts 'vpn not found, you can create vpn service with: qtc-cli vpn:create'
32
- end
33
- end
34
-
35
- def stop
36
- result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
37
- vpn = result['results'][0]
38
- if vpn
39
- client.post("/vpn_containers/#{vpn['id']}/stop", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
40
- else
41
- puts 'vpn not found, you can create vpn service with: qtc-cli mdb vpn:create'
42
- end
43
- end
44
-
45
- def destroy
46
- result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
47
- vpn = result['results'][0]
48
- if vpn
49
- client.delete("/vpn_containers/#{vpn['id']}", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
50
- end
51
- end
52
-
53
- def config
54
- all = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
55
- if all['results'][0]
56
- vpn = all['results'][0]
57
- if vpn['state'] != 'running'
58
- puts 'Cannot get config because vpn is not running'
59
- exit 1
60
- end
61
-
62
- vpn = client.get("/vpn_containers/#{vpn['id']}", {}, {'Authorization' => "Bearer #{current_cloud_token}"})
63
- if vpn['vpn_config']
64
- puts Base64.decode64(vpn['vpn_config'])
65
- end
66
- end
67
- end
68
-
69
- def vpn_datacenter_id
70
- "mdb-#{current_cloud_dc}"
71
- end
72
-
73
- ##
74
- # @return [Qtc::Client]
75
- def client
76
- if @client.nil?
77
- @client = Qtc::Client.new(base_url)
78
- end
79
-
80
- @client
81
- end
82
-
83
- def base_url
84
- datacenters = inifile['datacenters'] || {}
85
- if !self.vpn_datacenter_id.nil? && datacenters.has_key?(self.vpn_datacenter_id)
86
- "#{datacenters[self.vpn_datacenter_id]}/v1"
87
- else
88
- raise ArgumentError.new('Unknown datacenter. Please run qtc-cli datacenters to get latest list of your datacenters')
89
- end
90
- end
91
- end
92
- end
93
- end
1
+ require 'base64'
2
+
3
+ module Qtc
4
+ module Cli
5
+ class Platform::Vpn
6
+ include Qtc::Cli::Common
7
+
8
+ def create
9
+ self.datacenter_id = vpn_datacenter_id
10
+ client.post('/vpn_containers', {name: 'default vpn'}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
11
+ end
12
+
13
+ def show
14
+ result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
15
+ vpn = result['results'][0]
16
+ if vpn
17
+ puts "id: #{vpn['id']}"
18
+ puts "name: #{vpn['name']}"
19
+ puts "state: #{vpn['state']}"
20
+ else
21
+ puts 'vpn not found, you can create vpn service with: qtc-cli vpn:create'
22
+ end
23
+ end
24
+
25
+ def start
26
+ result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
27
+ vpn = result['results'][0]
28
+ if vpn
29
+ client.post("/vpn_containers/#{vpn['id']}/start", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
30
+ else
31
+ puts 'vpn not found, you can create vpn service with: qtc-cli vpn:create'
32
+ end
33
+ end
34
+
35
+ def stop
36
+ result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
37
+ vpn = result['results'][0]
38
+ if vpn
39
+ client.post("/vpn_containers/#{vpn['id']}/stop", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
40
+ else
41
+ puts 'vpn not found, you can create vpn service with: qtc-cli mdb vpn:create'
42
+ end
43
+ end
44
+
45
+ def destroy
46
+ result = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
47
+ vpn = result['results'][0]
48
+ if vpn
49
+ client.delete("/vpn_containers/#{vpn['id']}", {}, {}, {'Authorization' => "Bearer #{current_cloud_token}"})
50
+ end
51
+ end
52
+
53
+ def config
54
+ all = client.get('/vpn_containers', {}, {'Authorization' => "Bearer #{current_cloud_token}"})
55
+ if all['results'][0]
56
+ vpn = all['results'][0]
57
+ if vpn['state'] != 'running'
58
+ puts 'Cannot get config because vpn is not running'
59
+ exit 1
60
+ end
61
+
62
+ vpn = client.get("/vpn_containers/#{vpn['id']}", {}, {'Authorization' => "Bearer #{current_cloud_token}"})
63
+ if vpn['vpn_config']
64
+ puts Base64.decode64(vpn['vpn_config'])
65
+ end
66
+ end
67
+ end
68
+
69
+ def vpn_datacenter_id
70
+ "mdb-#{current_cloud_dc}"
71
+ end
72
+
73
+ ##
74
+ # @return [Qtc::Client]
75
+ def client
76
+ if @client.nil?
77
+ @client = Qtc::Client.new(base_url)
78
+ end
79
+
80
+ @client
81
+ end
82
+
83
+ def base_url
84
+ datacenters = inifile['datacenters'] || {}
85
+ if !self.vpn_datacenter_id.nil? && datacenters.has_key?(self.vpn_datacenter_id)
86
+ "#{datacenters[self.vpn_datacenter_id]}/v1"
87
+ else
88
+ raise ArgumentError.new('Unknown datacenter. Please run qtc-cli datacenters to get latest list of your datacenters')
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
data/lib/qtc/client.rb CHANGED
@@ -1,170 +1,170 @@
1
- require 'json'
2
- require 'httpclient'
3
- require_relative 'errors'
4
-
5
- module Qtc
6
- class Client
7
-
8
- attr_accessor :default_headers
9
- attr_reader :http_client
10
-
11
- # Initialize api client
12
- #
13
- # @param [String] api_url
14
- # @param [Hash] default_headers
15
- def initialize(api_url, default_headers = {})
16
- @http_client = HTTPClient.new
17
- @default_headers = {'Accept' => 'application/json', 'Content-Type' => 'application/json'}.merge(default_headers)
18
- @api_url = api_url
19
- end
20
-
21
- # Get request
22
- #
23
- # @param [String] path
24
- # @param [Hash,NilClass] params
25
- # @return [Hash]
26
- def get(path, params = nil, headers = {})
27
- response = http_client.get(request_uri(path), params, request_headers(headers))
28
- if response.status == 200
29
- parse_response(response)
30
- else
31
- handle_error_response(response)
32
- end
33
- end
34
-
35
- # Post request
36
- #
37
- # @param [String] path
38
- # @param [Object] obj
39
- # @param [Hash] params
40
- # @param [Hash] headers
41
- # @return [Hash]
42
- def post(path, obj, params = {}, headers = {})
43
- request_headers = request_headers(headers)
44
- request_options = {
45
- header: request_headers,
46
- body: encode_body(obj, request_headers['Content-Type']),
47
- query: params
48
- }
49
- response = http_client.post(request_uri(path), request_options)
50
- if [200, 201].include?(response.status)
51
- parse_response(response)
52
- else
53
- handle_error_response(response)
54
- end
55
- end
56
-
57
- # Put request
58
- #
59
- # @param [String] path
60
- # @param [Object] obj
61
- # @param [Hash] params
62
- # @param [Hash] headers
63
- # @return [Hash]
64
- def put(path, obj, params = {}, headers = {})
65
- request_headers = request_headers(headers)
66
- request_options = {
67
- header: request_headers,
68
- body: encode_body(obj, request_headers['Content-Type']),
69
- query: params
70
- }
71
-
72
- response = http_client.put(request_uri(path), request_options)
73
- if [200, 201].include?(response.status)
74
- parse_response(response)
75
- else
76
- handle_error_response(response)
77
- end
78
- end
79
-
80
- # Delete request
81
- #
82
- # @param [String] path
83
- # @param [Hash,String] body
84
- # @param [Hash] params
85
- # @param [Hash] headers
86
- # @return [Hash]
87
- def delete(path, body = nil, params = {}, headers = {})
88
- request_headers = request_headers(headers)
89
- request_options = {
90
- header: request_headers,
91
- body: encode_body(body, request_headers['Content-Type']),
92
- query: params
93
- }
94
- response = http_client.delete(request_uri(path), request_options)
95
- if response.status == 200
96
- parse_response(response)
97
- else
98
- handle_error_response(response)
99
- end
100
- end
101
-
102
- private
103
-
104
- ##
105
- # Get full request uri
106
- #
107
- # @param [String] path
108
- # @return [String]
109
- def request_uri(path)
110
- "#{@api_url}#{path}"
111
- end
112
-
113
- ##
114
- # Get request headers
115
- #
116
- # @param [Hash] headers
117
- # @return [Hash]
118
- def request_headers(headers = {})
119
- @default_headers.merge(headers)
120
- end
121
-
122
- ##
123
- # Encode body based on content type
124
- #
125
- # @param [Object] body
126
- # @param [String] content_type
127
- def encode_body(body, content_type)
128
- if content_type == 'application/json'
129
- dump_json(body)
130
- else
131
- body
132
- end
133
- end
134
-
135
- ##
136
- # Parse response
137
- #
138
- # @param [HTTP::Message]
139
- # @return [Object]
140
- def parse_response(response)
141
- if response.headers['Content-Type'].include?('application/json')
142
- parse_json(response.body)
143
- else
144
- response.body
145
- end
146
- end
147
-
148
- ##
149
- # Parse json
150
- #
151
- # @param [String] json
152
- # @return [Hash,Object,NilClass]
153
- def parse_json(json)
154
- JSON.parse(json) rescue nil
155
- end
156
-
157
- ##
158
- # Dump json
159
- #
160
- # @param [Object] obj
161
- # @return [String]
162
- def dump_json(obj)
163
- JSON.dump(obj)
164
- end
165
-
166
- def handle_error_response(response)
167
- raise Qtc::Errors::StandardError.new(response.status, response.body)
168
- end
169
- end
170
- end
1
+ require 'json'
2
+ require 'httpclient'
3
+ require_relative 'errors'
4
+
5
+ module Qtc
6
+ class Client
7
+
8
+ attr_accessor :default_headers
9
+ attr_reader :http_client
10
+
11
+ # Initialize api client
12
+ #
13
+ # @param [String] api_url
14
+ # @param [Hash] default_headers
15
+ def initialize(api_url, default_headers = {})
16
+ @http_client = HTTPClient.new
17
+ @default_headers = {'Accept' => 'application/json', 'Content-Type' => 'application/json'}.merge(default_headers)
18
+ @api_url = api_url
19
+ end
20
+
21
+ # Get request
22
+ #
23
+ # @param [String] path
24
+ # @param [Hash,NilClass] params
25
+ # @return [Hash]
26
+ def get(path, params = nil, headers = {})
27
+ response = http_client.get(request_uri(path), params, request_headers(headers))
28
+ if response.status == 200
29
+ parse_response(response)
30
+ else
31
+ handle_error_response(response)
32
+ end
33
+ end
34
+
35
+ # Post request
36
+ #
37
+ # @param [String] path
38
+ # @param [Object] obj
39
+ # @param [Hash] params
40
+ # @param [Hash] headers
41
+ # @return [Hash]
42
+ def post(path, obj, params = {}, headers = {})
43
+ request_headers = request_headers(headers)
44
+ request_options = {
45
+ header: request_headers,
46
+ body: encode_body(obj, request_headers['Content-Type']),
47
+ query: params
48
+ }
49
+ response = http_client.post(request_uri(path), request_options)
50
+ if [200, 201].include?(response.status)
51
+ parse_response(response)
52
+ else
53
+ handle_error_response(response)
54
+ end
55
+ end
56
+
57
+ # Put request
58
+ #
59
+ # @param [String] path
60
+ # @param [Object] obj
61
+ # @param [Hash] params
62
+ # @param [Hash] headers
63
+ # @return [Hash]
64
+ def put(path, obj, params = {}, headers = {})
65
+ request_headers = request_headers(headers)
66
+ request_options = {
67
+ header: request_headers,
68
+ body: encode_body(obj, request_headers['Content-Type']),
69
+ query: params
70
+ }
71
+
72
+ response = http_client.put(request_uri(path), request_options)
73
+ if [200, 201].include?(response.status)
74
+ parse_response(response)
75
+ else
76
+ handle_error_response(response)
77
+ end
78
+ end
79
+
80
+ # Delete request
81
+ #
82
+ # @param [String] path
83
+ # @param [Hash,String] body
84
+ # @param [Hash] params
85
+ # @param [Hash] headers
86
+ # @return [Hash]
87
+ def delete(path, body = nil, params = {}, headers = {})
88
+ request_headers = request_headers(headers)
89
+ request_options = {
90
+ header: request_headers,
91
+ body: encode_body(body, request_headers['Content-Type']),
92
+ query: params
93
+ }
94
+ response = http_client.delete(request_uri(path), request_options)
95
+ if response.status == 200
96
+ parse_response(response)
97
+ else
98
+ handle_error_response(response)
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ ##
105
+ # Get full request uri
106
+ #
107
+ # @param [String] path
108
+ # @return [String]
109
+ def request_uri(path)
110
+ "#{@api_url}#{path}"
111
+ end
112
+
113
+ ##
114
+ # Get request headers
115
+ #
116
+ # @param [Hash] headers
117
+ # @return [Hash]
118
+ def request_headers(headers = {})
119
+ @default_headers.merge(headers)
120
+ end
121
+
122
+ ##
123
+ # Encode body based on content type
124
+ #
125
+ # @param [Object] body
126
+ # @param [String] content_type
127
+ def encode_body(body, content_type)
128
+ if content_type == 'application/json'
129
+ dump_json(body)
130
+ else
131
+ body
132
+ end
133
+ end
134
+
135
+ ##
136
+ # Parse response
137
+ #
138
+ # @param [HTTP::Message]
139
+ # @return [Object]
140
+ def parse_response(response)
141
+ if response.headers['Content-Type'].include?('application/json')
142
+ parse_json(response.body)
143
+ else
144
+ response.body
145
+ end
146
+ end
147
+
148
+ ##
149
+ # Parse json
150
+ #
151
+ # @param [String] json
152
+ # @return [Hash,Object,NilClass]
153
+ def parse_json(json)
154
+ JSON.parse(json) rescue nil
155
+ end
156
+
157
+ ##
158
+ # Dump json
159
+ #
160
+ # @param [Object] obj
161
+ # @return [String]
162
+ def dump_json(obj)
163
+ JSON.dump(obj)
164
+ end
165
+
166
+ def handle_error_response(response)
167
+ raise Qtc::Errors::StandardError.new(response.status, response.body)
168
+ end
169
+ end
170
+ end