qtc-sdk 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +18 -18
  3. data/Changelog.md +14 -6
  4. data/Gemfile +4 -4
  5. data/LICENSE.txt +22 -22
  6. data/README.md +44 -44
  7. data/Rakefile +1 -1
  8. data/bin/qtc-cli +13 -13
  9. data/lib/qtc/cli/commands.rb +15 -15
  10. data/lib/qtc/cli/common.rb +146 -91
  11. data/lib/qtc/cli/eds/base.rb +27 -27
  12. data/lib/qtc/cli/eds/commands.rb +20 -20
  13. data/lib/qtc/cli/eds/instances.rb +30 -30
  14. data/lib/qtc/cli/mar/apps.rb +116 -125
  15. data/lib/qtc/cli/mar/base.rb +60 -60
  16. data/lib/qtc/cli/mar/commands.rb +221 -199
  17. data/lib/qtc/cli/mar/debug.rb +87 -86
  18. data/lib/qtc/cli/mar/domains.rb +35 -38
  19. data/lib/qtc/cli/mar/env.rb +38 -40
  20. data/lib/qtc/cli/mar/repository.rb +24 -26
  21. data/lib/qtc/cli/mar/ssl_certificates.rb +40 -42
  22. data/lib/qtc/cli/mar/stack.rb +29 -0
  23. data/lib/qtc/cli/mdb/base.rb +47 -43
  24. data/lib/qtc/cli/mdb/commands.rb +43 -31
  25. data/lib/qtc/cli/mdb/instances.rb +79 -60
  26. data/lib/qtc/cli/platform/clouds.rb +33 -15
  27. data/lib/qtc/cli/platform/commands.rb +132 -65
  28. data/lib/qtc/cli/platform/datacenters.rb +23 -21
  29. data/lib/qtc/cli/platform/ssh_keys.rb +41 -41
  30. data/lib/qtc/cli/platform/user.rb +25 -25
  31. data/lib/qtc/cli/platform/vpn.rb +93 -0
  32. data/lib/qtc/client.rb +170 -170
  33. data/lib/qtc/eds/client.rb +116 -116
  34. data/lib/qtc/eds/collection.rb +124 -124
  35. data/lib/qtc/eds/user_collection.rb +13 -13
  36. data/lib/qtc/eds/usergroup_collection.rb +41 -41
  37. data/lib/qtc/errors.rb +13 -11
  38. data/lib/qtc/version.rb +3 -3
  39. data/lib/qtc-sdk.rb +1 -1
  40. data/qtc-sdk.gemspec +28 -28
  41. data/spec/unit/qtc/client_spec.rb +147 -147
  42. metadata +20 -19
  43. data/lib/qtc/mws/client.rb +0 -89
@@ -1,15 +1,33 @@
1
- require 'qtc/client'
2
- require_relative '../common'
3
-
4
- module Qtc::Cli::Platform
5
- class Clouds
6
- include Qtc::Cli::Common
7
-
8
- def list
9
- accounts = platform_client.get('/user/accounts')
10
- accounts['results'].each do |account|
11
- print color("~ #{account['name']} (#{account['id']})", :bold)
12
- end
13
- end
14
- end
15
- end
1
+ require 'qtc/client'
2
+ require_relative '../common'
3
+
4
+ module Qtc::Cli::Platform
5
+ class Clouds
6
+ include Qtc::Cli::Common
7
+
8
+ def list
9
+ accounts = platform_client.get('/user/accounts')
10
+ template = '%-40.40s %-40.40s'
11
+ puts template % ['ID', 'NAME']
12
+ accounts['results'].each do |account|
13
+ name = account['name']
14
+ name = "* #{name}" if account['id'] == inifile['platform']['current_cloud']
15
+ puts template % [account['id'], name]
16
+ end
17
+ end
18
+
19
+ def use(id)
20
+ account = platform_client.get("/accounts/#{id}")
21
+ puts "Using cloud: #{account['name']} (#{id})"
22
+ inifile['platform']['current_cloud'] = id
23
+ inifile['platform']['current_dc'] = account['datacenter']['id']
24
+ inifile.save(filename: ini_filename)
25
+ end
26
+
27
+ def create(name, opts)
28
+ datacenter = opts.datacenter || 'eu-1'
29
+ data = {name: name, datacenter: datacenter, vpc: opts.vpc}
30
+ platform_client.post('/accounts', data)
31
+ end
32
+ end
33
+ end
@@ -1,65 +1,132 @@
1
- module Qtc::Cli::Platform; end;
2
-
3
- require_relative 'datacenters'
4
- require_relative 'clouds'
5
- require_relative 'user'
6
- require_relative 'ssh_keys'
7
-
8
- command 'datacenters' do |c|
9
- c.syntax = 'qtc-cli datacenters'
10
- c.description = 'List all datacenters'
11
- c.action do |args, options|
12
- Qtc::Cli::Platform::Datacenters.new.list
13
- end
14
- end
15
-
16
- command 'clouds' do |c|
17
- c.syntax = 'qtc-cli clouds'
18
- c.description = 'List all clouds'
19
- c.action do |args, options|
20
- Qtc::Cli::Platform::Clouds.new.list
21
- end
22
- end
23
-
24
- command 'login' do |c|
25
- c.syntax = 'qtc-cli login'
26
- c.description = 'Login to Qt Cloud Services'
27
- c.action do |args, options|
28
- Qtc::Cli::Platform::User.new.login
29
- end
30
- end
31
-
32
- command 'logout' do |c|
33
- c.syntax = 'qtc-cli logout'
34
- c.description = 'Logout from Qt Cloud Services'
35
- c.action do |args, options|
36
- Qtc::Cli::Platform::User.new.logout
37
- end
38
- end
39
-
40
- command 'ssh-keys' do |c|
41
- c.syntax = 'qtc-cli ssh-keys'
42
- c.description = 'List ssh keys'
43
- c.action do |args, options|
44
- Qtc::Cli::Platform::SshKeys.new.list
45
- end
46
- end
47
-
48
- command 'ssh-keys:add' do |c|
49
- c.syntax = 'qtc-cli ssh-keys:add'
50
- c.description = 'Add ssh key'
51
- c.option '--name NAME', String, 'SSH key name'
52
- c.option '--key KEY', String, 'Path to SSH public key file'
53
- c.action do |args, options|
54
- Qtc::Cli::Platform::SshKeys.new.create(options)
55
- end
56
- end
57
-
58
- command 'ssh-keys:remove' do |c|
59
- c.syntax = 'qtc-cli ssh-keys:remove'
60
- c.description = 'Add ssh key'
61
- c.option '--name NAME', String, 'SSH key name'
62
- c.action do |args, options|
63
- Qtc::Cli::Platform::SshKeys.new.destroy(options)
64
- end
65
- end
1
+ module Qtc::Cli::Platform; end;
2
+
3
+ require_relative 'datacenters'
4
+ require_relative 'clouds'
5
+ require_relative 'vpn'
6
+ require_relative 'user'
7
+ require_relative 'ssh_keys'
8
+
9
+ command 'datacenters' do |c|
10
+ c.syntax = 'qtc-cli datacenters'
11
+ c.description = 'List all datacenters'
12
+ c.action do |args, options|
13
+ Qtc::Cli::Platform::Datacenters.new.list
14
+ end
15
+ end
16
+
17
+ command 'clouds:list' do |c|
18
+ c.syntax = 'qtc-cli clouds:list'
19
+ c.description = 'List all clouds'
20
+ c.action do |args, options|
21
+ Qtc::Cli::Platform::Clouds.new.list
22
+ end
23
+ end
24
+
25
+ command 'clouds:use' do |c|
26
+ c.syntax = 'qtc-cli clouds:use <id>'
27
+ c.description = 'Set default cloud to use'
28
+ c.action do |args, options|
29
+ Qtc::Cli::Platform::Clouds.new.use(args[0])
30
+ end
31
+ end
32
+
33
+ command 'vpn:create' do |c|
34
+ c.syntax = 'qtc-cli vpn:create'
35
+ c.description = 'Create vpn connection'
36
+ c.action do |args, options|
37
+ Qtc::Cli::Platform::Vpn.new.create
38
+ end
39
+ end
40
+
41
+ command 'vpn:show' do |c|
42
+ c.syntax = 'qtc-cli vpn:show'
43
+ c.description = 'Show vpn connection'
44
+ c.action do |args, options|
45
+ Qtc::Cli::Platform::Vpn.new.show
46
+ end
47
+ end
48
+
49
+ command 'vpn:start' do |c|
50
+ c.syntax = 'qtc-cli vpn:start'
51
+ c.description = 'Start vpn server'
52
+ c.action do |args, options|
53
+ Qtc::Cli::Platform::Vpn.new.start
54
+ end
55
+ end
56
+
57
+ command 'vpn:stop' do |c|
58
+ c.syntax = 'qtc-cli vpn:stop'
59
+ c.description = 'Stop vpn server'
60
+ c.action do |args, options|
61
+ Qtc::Cli::Platform::Vpn.new.stop
62
+ end
63
+ end
64
+
65
+ command 'vpn:remove' do |c|
66
+ c.syntax = 'qtc-cli vpn:remove'
67
+ c.description = 'Remove vpn connection'
68
+ c.action do |args, options|
69
+ Qtc::Cli::Platform::Vpn.new.destroy
70
+ end
71
+ end
72
+
73
+ command 'vpn:config' do |c|
74
+ c.syntax = 'qtc-cli vpn:config'
75
+ c.description = 'Show vpn configuration'
76
+ c.action do |args, options|
77
+ Qtc::Cli::Platform::Vpn.new.config
78
+ end
79
+ end
80
+
81
+ command 'clouds:create' do |c|
82
+ c.syntax = 'qtc-cli clouds:create name'
83
+ c.description = 'Create a new cloud'
84
+ c.option '--datacenter STRING', String, 'Specify datacenter for this cloud (default: eu-1)'
85
+ c.option '--vpc', 'Enable virtual private cloud'
86
+ c.action do |args, options|
87
+ Qtc::Cli::Platform::Clouds.new.create(args.join(' '), options)
88
+ end
89
+ end
90
+
91
+ command 'login' do |c|
92
+ c.syntax = 'qtc-cli login'
93
+ c.description = 'Login to Qt Cloud Services'
94
+ c.action do |args, options|
95
+ Qtc::Cli::Platform::User.new.login
96
+ end
97
+ end
98
+
99
+ command 'logout' do |c|
100
+ c.syntax = 'qtc-cli logout'
101
+ c.description = 'Logout from Qt Cloud Services'
102
+ c.action do |args, options|
103
+ Qtc::Cli::Platform::User.new.logout
104
+ end
105
+ end
106
+
107
+ command 'ssh-keys' do |c|
108
+ c.syntax = 'qtc-cli ssh-keys'
109
+ c.description = 'List ssh keys'
110
+ c.action do |args, options|
111
+ Qtc::Cli::Platform::SshKeys.new.list
112
+ end
113
+ end
114
+
115
+ command 'ssh-keys:add' do |c|
116
+ c.syntax = 'qtc-cli ssh-keys:add'
117
+ c.description = 'Add ssh key'
118
+ c.option '--name NAME', String, 'SSH key name'
119
+ c.option '--key KEY', String, 'Path to SSH public key file'
120
+ c.action do |args, options|
121
+ Qtc::Cli::Platform::SshKeys.new.create(options)
122
+ end
123
+ end
124
+
125
+ command 'ssh-keys:remove' do |c|
126
+ c.syntax = 'qtc-cli ssh-keys:remove'
127
+ c.description = 'Add ssh key'
128
+ c.option '--name NAME', String, 'SSH key name'
129
+ c.action do |args, options|
130
+ Qtc::Cli::Platform::SshKeys.new.destroy(options)
131
+ end
132
+ end
@@ -1,21 +1,23 @@
1
- require 'qtc/client'
2
- require_relative '../common'
3
-
4
- module Qtc::Cli::Platform
5
- class Datacenters
6
- include Qtc::Cli::Common
7
-
8
- def list
9
- datacenters = platform_client.get('/datacenters')
10
- inifile['datacenters'] = {}
11
-
12
- datacenters['results'].each do |datacenter|
13
- datacenter['services'].each do |service|
14
- inifile['datacenters']["#{service['id']}-#{datacenter['id']}"] = service['url']
15
- end
16
- print color("~ #{datacenter['id']} (#{datacenter['description']})", :bold)
17
- end
18
- inifile.save(filename: ini_filename)
19
- end
20
- end
21
- end
1
+ require 'qtc/client'
2
+ require_relative '../common'
3
+
4
+ module Qtc::Cli::Platform
5
+ class Datacenters
6
+ include Qtc::Cli::Common
7
+
8
+ def list
9
+ datacenters = platform_client.get('/datacenters')
10
+ inifile['datacenters'] = {}
11
+
12
+ template = '%-20.20s %-40.40s'
13
+ puts template % ['NAME', 'DESCRIPTION']
14
+ datacenters['results'].each do |datacenter|
15
+ datacenter['services'].each do |service|
16
+ inifile['datacenters']["#{service['id']}-#{datacenter['id']}"] = service['url']
17
+ end
18
+ puts template % [datacenter['id'], datacenter['description']]
19
+ end
20
+ inifile.save(filename: ini_filename)
21
+ end
22
+ end
23
+ end
@@ -1,41 +1,41 @@
1
- require 'qtc/client'
2
- require_relative '../common'
3
-
4
- module Qtc::Cli::Platform
5
- class SshKeys
6
- include Qtc::Cli::Common
7
-
8
- def list
9
- response = platform_client.get('/user/ssh_keys')
10
- response['results'].each do |ssh_key|
11
- say "~ #{ssh_key['name']}"
12
- end
13
- end
14
-
15
- def create(options)
16
- raise ArgumentError.new('--name is required') if options.name.nil?
17
- raise ArgumentError.new('--key is required') if options.key.nil?
18
- unless File.exists?(options.key)
19
- raise ArgumentError.new("#{options.key} does not exist")
20
- end
21
-
22
- data = {
23
- name: options.name,
24
- key: File.read(options.key)
25
- }
26
-
27
- platform_client.post('/user/ssh_keys', data)
28
- end
29
-
30
- def destroy(options)
31
- raise ArgumentError.new('--name is required') if options.name.nil?
32
- response = platform_client.get('/user/ssh_keys')
33
- matches = response['results'].select{|r| r['name'] == options.name}
34
- if matches.size == 0
35
- raise ArgumentError.new("SSH key with name #{options.name} does not exist")
36
- else
37
- platform_client.delete("/user/ssh_keys/#{matches[0]['id']}")
38
- end
39
- end
40
- end
41
- end
1
+ require 'qtc/client'
2
+ require_relative '../common'
3
+
4
+ module Qtc::Cli::Platform
5
+ class SshKeys
6
+ include Qtc::Cli::Common
7
+
8
+ def list
9
+ response = platform_client.get('/user/ssh_keys')
10
+ response['results'].each do |ssh_key|
11
+ say "~ #{ssh_key['name']}"
12
+ end
13
+ end
14
+
15
+ def create(options)
16
+ raise ArgumentError.new('--name is required') if options.name.nil?
17
+ raise ArgumentError.new('--key is required') if options.key.nil?
18
+ unless File.exists?(options.key)
19
+ raise ArgumentError.new("#{options.key} does not exist")
20
+ end
21
+
22
+ data = {
23
+ name: options.name,
24
+ key: File.read(options.key)
25
+ }
26
+
27
+ platform_client.post('/user/ssh_keys', data)
28
+ end
29
+
30
+ def destroy(options)
31
+ raise ArgumentError.new('--name is required') if options.name.nil?
32
+ response = platform_client.get('/user/ssh_keys')
33
+ matches = response['results'].select{|r| r['name'] == options.name}
34
+ if matches.size == 0
35
+ raise ArgumentError.new("SSH key with name #{options.name} does not exist")
36
+ else
37
+ platform_client.delete("/user/ssh_keys/#{matches[0]['id']}")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,25 +1,25 @@
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
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
@@ -0,0 +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