morpheus-cli 0.9.10 → 2.9.0
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 +4 -4
- data/bin/morpheus +48 -33
- data/lib/morpheus/api/api_client.rb +12 -0
- data/lib/morpheus/api/custom_instance_types.rb +55 -0
- data/lib/morpheus/api/custom_instance_types_interface.rb +133 -0
- data/lib/morpheus/api/dashboard_interface.rb +37 -0
- data/lib/morpheus/api/users_interface.rb +17 -0
- data/lib/morpheus/api/whoami_interface.rb +20 -0
- data/lib/morpheus/cli.rb +13 -1
- data/lib/morpheus/cli/app_templates.rb +1 -1
- data/lib/morpheus/cli/cli_command.rb +22 -1
- data/lib/morpheus/cli/cli_registry.rb +1 -1
- data/lib/morpheus/cli/credentials.rb +33 -11
- data/lib/morpheus/cli/dashboard_command.rb +74 -0
- data/lib/morpheus/cli/instance_types.rb +4 -2
- data/lib/morpheus/cli/key_pairs.rb +1 -1
- data/lib/morpheus/cli/library.rb +539 -0
- data/lib/morpheus/cli/login.rb +57 -0
- data/lib/morpheus/cli/logout.rb +61 -0
- data/lib/morpheus/cli/mixins/accounts_helper.rb +52 -10
- data/lib/morpheus/cli/mixins/print_helper.rb +23 -16
- data/lib/morpheus/cli/mixins/provisioning_helper.rb +1 -1
- data/lib/morpheus/cli/mixins/whoami_helper.rb +34 -0
- data/lib/morpheus/cli/option_types.rb +15 -1
- data/lib/morpheus/cli/recent_activity_command.rb +83 -0
- data/lib/morpheus/cli/remote.rb +71 -28
- data/lib/morpheus/cli/roles.rb +89 -24
- data/lib/morpheus/cli/shell.rb +24 -11
- data/lib/morpheus/cli/users.rb +166 -24
- data/lib/morpheus/cli/version.rb +1 -1
- data/lib/morpheus/cli/version_command.rb +45 -0
- data/lib/morpheus/cli/whoami.rb +139 -0
- data/lib/morpheus/logging.rb +78 -0
- metadata +15 -2
@@ -0,0 +1,57 @@
|
|
1
|
+
# require 'yaml'
|
2
|
+
require 'io/console'
|
3
|
+
require 'rest_client'
|
4
|
+
require 'optparse'
|
5
|
+
require 'morpheus/cli/cli_command'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class Morpheus::Cli::Login
|
9
|
+
include Morpheus::Cli::CliCommand
|
10
|
+
# include Morpheus::Cli::WhoamiHelper
|
11
|
+
# include Morpheus::Cli::AccountsHelper
|
12
|
+
|
13
|
+
def initialize()
|
14
|
+
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
|
15
|
+
end
|
16
|
+
|
17
|
+
def connect(opts)
|
18
|
+
#@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
|
19
|
+
#@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
|
20
|
+
end
|
21
|
+
|
22
|
+
def usage
|
23
|
+
"Usage: morpheus login"
|
24
|
+
end
|
25
|
+
|
26
|
+
def handle(args)
|
27
|
+
login(args)
|
28
|
+
end
|
29
|
+
|
30
|
+
# def login
|
31
|
+
# Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).login()
|
32
|
+
# end
|
33
|
+
|
34
|
+
def login(args)
|
35
|
+
options = {}
|
36
|
+
optparse = OptionParser.new do|opts|
|
37
|
+
opts.banner = usage
|
38
|
+
build_common_options(opts, options, [:json]) # todo: support :remote too perhaps
|
39
|
+
end
|
40
|
+
optparse.parse(args)
|
41
|
+
|
42
|
+
connect(options)
|
43
|
+
|
44
|
+
begin
|
45
|
+
|
46
|
+
Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).login(options)
|
47
|
+
|
48
|
+
rescue RestClient::Exception => e
|
49
|
+
print_rest_exception(e, options)
|
50
|
+
exit 1
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# require 'yaml'
|
2
|
+
require 'io/console'
|
3
|
+
require 'rest_client'
|
4
|
+
require 'optparse'
|
5
|
+
require 'morpheus/cli/cli_command'
|
6
|
+
require 'morpheus/cli/remote'
|
7
|
+
require 'morpheus/cli/credentials'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class Morpheus::Cli::Logout
|
11
|
+
include Morpheus::Cli::CliCommand
|
12
|
+
# include Morpheus::Cli::WhoamiHelper
|
13
|
+
# include Morpheus::Cli::AccountsHelper
|
14
|
+
|
15
|
+
def initialize()
|
16
|
+
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
|
17
|
+
end
|
18
|
+
|
19
|
+
def connect(opts)
|
20
|
+
#@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
|
21
|
+
#@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
|
22
|
+
end
|
23
|
+
|
24
|
+
def usage
|
25
|
+
"Usage: morpheus login"
|
26
|
+
end
|
27
|
+
|
28
|
+
def handle(args)
|
29
|
+
logout(args)
|
30
|
+
end
|
31
|
+
|
32
|
+
def logout(args)
|
33
|
+
options = {}
|
34
|
+
optparse = OptionParser.new do|opts|
|
35
|
+
opts.banner = usage
|
36
|
+
build_common_options(opts, options, []) # todo: support :remote too perhaps
|
37
|
+
end
|
38
|
+
optparse.parse(args)
|
39
|
+
|
40
|
+
connect(options)
|
41
|
+
|
42
|
+
begin
|
43
|
+
|
44
|
+
creds = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).load_saved_credentials()
|
45
|
+
if !creds
|
46
|
+
print yellow,"\nYou are not logged in to #{@appliance_name} - #{@appliance_url}.\n\n",reset
|
47
|
+
# exit 0
|
48
|
+
else
|
49
|
+
Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).logout()
|
50
|
+
end
|
51
|
+
|
52
|
+
rescue RestClient::Exception => e
|
53
|
+
print_rest_exception(e, options)
|
54
|
+
exit 1
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
end
|
@@ -7,7 +7,7 @@ require 'morpheus/cli/mixins/print_helper'
|
|
7
7
|
module Morpheus::Cli::AccountsHelper
|
8
8
|
|
9
9
|
def self.included(klass)
|
10
|
-
klass.include Morpheus::Cli::PrintHelper
|
10
|
+
klass.send :include, Morpheus::Cli::PrintHelper
|
11
11
|
end
|
12
12
|
|
13
13
|
def find_account_by_id(id)
|
@@ -16,7 +16,7 @@ module Morpheus::Cli::AccountsHelper
|
|
16
16
|
json_response = @accounts_interface.get(id.to_i)
|
17
17
|
return json_response['account']
|
18
18
|
rescue RestClient::Exception => e
|
19
|
-
if e.response.code == 404
|
19
|
+
if e.response && e.response.code == 404
|
20
20
|
print_red_alert "Account not found by id #{id}"
|
21
21
|
else
|
22
22
|
raise e
|
@@ -61,7 +61,7 @@ module Morpheus::Cli::AccountsHelper
|
|
61
61
|
json_response = @roles_interface.get(account_id, id.to_i)
|
62
62
|
return json_response['role']
|
63
63
|
rescue RestClient::Exception => e
|
64
|
-
if e.response.code == 404
|
64
|
+
if e.response && e.response.code == 404
|
65
65
|
print_red_alert "Role not found by id #{id}"
|
66
66
|
else
|
67
67
|
raise e
|
@@ -93,7 +93,7 @@ module Morpheus::Cli::AccountsHelper
|
|
93
93
|
json_response = @users_interface.get(account_id, id.to_i)
|
94
94
|
return json_response['user']
|
95
95
|
rescue RestClient::Exception => e
|
96
|
-
if e.response.code == 404
|
96
|
+
if e.response && e.response.code == 404
|
97
97
|
print_red_alert "User not found by id #{id}"
|
98
98
|
else
|
99
99
|
raise e
|
@@ -148,6 +148,25 @@ module Morpheus::Cli::AccountsHelper
|
|
148
148
|
print reset
|
149
149
|
end
|
150
150
|
|
151
|
+
def format_role_type(role)
|
152
|
+
str = ""
|
153
|
+
if role['roleType'] == "account"
|
154
|
+
str = "Account"
|
155
|
+
elsif role['roleType'] == "user"
|
156
|
+
str = "User"
|
157
|
+
else
|
158
|
+
if role['scope'] == 'Account'
|
159
|
+
str = "Legacy"
|
160
|
+
else
|
161
|
+
str = "Admin" # System Admin
|
162
|
+
end
|
163
|
+
end
|
164
|
+
# if role['scope'] && role['filterType'] != 'Account'
|
165
|
+
# str = "(System) #{str}"
|
166
|
+
# end
|
167
|
+
return str
|
168
|
+
end
|
169
|
+
|
151
170
|
def print_roles_table(roles, opts={})
|
152
171
|
table_color = opts[:color] || cyan
|
153
172
|
# tp roles, [
|
@@ -162,8 +181,10 @@ module Morpheus::Cli::AccountsHelper
|
|
162
181
|
id: role['id'],
|
163
182
|
name: role['authority'],
|
164
183
|
description: role['description'],
|
165
|
-
scope: role['scope'],
|
166
|
-
|
184
|
+
scope: role['scope'],
|
185
|
+
multitenant: role['multitenant'] ? 'Yes' : 'No',
|
186
|
+
type: format_role_type(role),
|
187
|
+
owner: role['owner'] ? role['owner']['name'] : "System",
|
167
188
|
dateCreated: format_local_dt(role['dateCreated'])
|
168
189
|
}
|
169
190
|
end
|
@@ -172,21 +193,42 @@ module Morpheus::Cli::AccountsHelper
|
|
172
193
|
:id,
|
173
194
|
:name,
|
174
195
|
:description,
|
175
|
-
:scope,
|
176
|
-
:
|
196
|
+
# opts[:is_master_account] ? :scope : nil,
|
197
|
+
opts[:is_master_account] ? :type : nil,
|
198
|
+
opts[:is_master_account] ? :multitenant : nil,
|
199
|
+
opts[:is_master_account] ? :owner : nil,
|
177
200
|
{:dateCreated => {:display_name => "Date Created"} }
|
178
|
-
]
|
201
|
+
].compact
|
179
202
|
print reset
|
180
203
|
end
|
181
204
|
|
182
205
|
def print_users_table(users, opts={})
|
183
206
|
table_color = opts[:color] || cyan
|
184
207
|
rows = users.collect do |user|
|
185
|
-
{id: user['id'], username: user['username'], first: user['firstName'], last: user['lastName'], email: user['email'], role: user
|
208
|
+
{id: user['id'], username: user['username'], first: user['firstName'], last: user['lastName'], email: user['email'], role: format_user_role_names(user), account: user['account'] ? user['account']['name'] : nil}
|
186
209
|
end
|
187
210
|
print table_color
|
188
211
|
tp rows, :id, :account, :first, :last, :username, :email, :role
|
189
212
|
print reset
|
190
213
|
end
|
191
214
|
|
215
|
+
def format_user_role_names(user)
|
216
|
+
role_names = ""
|
217
|
+
if user && user['roles']
|
218
|
+
roles = user['roles']
|
219
|
+
roles = roles.sort {|a,b| a['authority'].to_s.downcase <=> b['authority'].to_s.downcase }
|
220
|
+
role_names = roles.collect {|r| r['authority'] }.join(', ')
|
221
|
+
end
|
222
|
+
role_names
|
223
|
+
end
|
224
|
+
|
225
|
+
def get_access_string(val)
|
226
|
+
val ||= 'none'
|
227
|
+
if val == 'none'
|
228
|
+
"#{white}#{val.to_s.capitalize}#{cyan}"
|
229
|
+
else
|
230
|
+
"#{green}#{val.to_s.capitalize}#{cyan}"
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
192
234
|
end
|
@@ -4,7 +4,7 @@ require 'json'
|
|
4
4
|
module Morpheus::Cli::PrintHelper
|
5
5
|
|
6
6
|
def self.included(klass)
|
7
|
-
klass.include Term::ANSIColor
|
7
|
+
klass.send :include, Term::ANSIColor
|
8
8
|
end
|
9
9
|
|
10
10
|
def print_red_alert(msg)
|
@@ -44,24 +44,31 @@ module Morpheus::Cli::PrintHelper
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def print_rest_exception(e, options={})
|
47
|
-
if e.response
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
47
|
+
if e.response
|
48
|
+
if e.response.code == 400
|
49
|
+
response = JSON.parse(e.response.to_s)
|
50
|
+
print_errors(response, options)
|
51
|
+
else
|
52
|
+
print_red_alert "Error Communicating with the Appliance. (#{e.response.code}) #{e}"
|
53
|
+
if options[:json]
|
54
|
+
begin
|
55
|
+
response = JSON.parse(e.response.to_s)
|
56
|
+
print red, "\n"
|
57
|
+
print JSON.pretty_generate(response)
|
58
|
+
print reset, "\n\n"
|
59
|
+
rescue TypeError, JSON::ParserError => ex
|
60
|
+
#print_red_alert "Failed to parse JSON response: #{ex}"
|
61
|
+
ensure
|
62
|
+
print reset
|
63
|
+
end
|
62
64
|
end
|
63
65
|
end
|
66
|
+
else
|
67
|
+
print_red_alert "Error Communicating with the Appliance. #{e}"
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
71
|
+
def required_blue_prompt
|
72
|
+
"#{cyan}|#{reset}"
|
73
|
+
end
|
67
74
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'table_print'
|
2
|
+
require 'morpheus/cli/mixins/print_helper'
|
3
|
+
|
4
|
+
# Mixin for Morpheus::Cli command classes
|
5
|
+
# Provides common methods for fetching and printing accounts, roles, and users.
|
6
|
+
# The including class must establish @accounts_interface, @roles_interface, @users_interface
|
7
|
+
module Morpheus::Cli::WhoamiHelper
|
8
|
+
|
9
|
+
def self.included(klass)
|
10
|
+
klass.send :include, Morpheus::Cli::PrintHelper
|
11
|
+
end
|
12
|
+
|
13
|
+
def load_whoami()
|
14
|
+
whoami_interface = @whoami_interface || @api_client.whoami
|
15
|
+
whoami_response = whoami_interface.get()
|
16
|
+
# whoami_response = @whoami_interface.get()
|
17
|
+
@current_user = whoami_response["user"]
|
18
|
+
if @current_user.empty?
|
19
|
+
print_red_alert "Unauthenticated. Please login."
|
20
|
+
exit 1
|
21
|
+
end
|
22
|
+
@is_master_account = whoami_response["isMasterAccount"]
|
23
|
+
@user_permissions = whoami_response["permissions"]
|
24
|
+
|
25
|
+
if whoami_response["appliance"]
|
26
|
+
@appliance_build_verison = whoami_response["appliance"]["buildVersion"]
|
27
|
+
else
|
28
|
+
@appliance_build_verison = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
return whoami_response
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -287,7 +287,7 @@ module Morpheus
|
|
287
287
|
print "\n"
|
288
288
|
if input == '?'
|
289
289
|
help_prompt(option_type)
|
290
|
-
elsif !value.
|
290
|
+
elsif !value.empty? || option_type['required'] != true
|
291
291
|
value_found = true
|
292
292
|
end
|
293
293
|
end
|
@@ -311,6 +311,20 @@ module Morpheus
|
|
311
311
|
end
|
312
312
|
puts "\n\n"
|
313
313
|
end
|
314
|
+
|
315
|
+
def self.format_option_types_help(option_types)
|
316
|
+
if option_types.empty?
|
317
|
+
"Available Options:\nNone\n\n"
|
318
|
+
else
|
319
|
+
option_lines = option_types.collect {|it| " -O #{it['fieldName']}=\"value\"" }.join("\n")
|
320
|
+
"Available Options:\n#{option_lines}\n\n"
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
def self.display_option_types_help(option_types)
|
325
|
+
puts self.format_option_types_help(option_types)
|
326
|
+
end
|
327
|
+
|
314
328
|
end
|
315
329
|
end
|
316
330
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'morpheus/cli/cli_command'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class Morpheus::Cli::RecentActivityCommand
|
6
|
+
include Morpheus::Cli::CliCommand
|
7
|
+
|
8
|
+
cli_command_name :'recent-activity'
|
9
|
+
cli_command_hidden # remove once this is done
|
10
|
+
|
11
|
+
def initialize()
|
12
|
+
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
|
13
|
+
@active_groups = ::Morpheus::Cli::Groups.load_group_file
|
14
|
+
end
|
15
|
+
|
16
|
+
def connect(opts)
|
17
|
+
@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).load_saved_credentials()
|
18
|
+
if @access_token.empty?
|
19
|
+
print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
|
20
|
+
exit 1
|
21
|
+
end
|
22
|
+
@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
|
23
|
+
@dashboard_interface = @api_client.dashboard
|
24
|
+
end
|
25
|
+
|
26
|
+
def usage
|
27
|
+
"Usage: morpheus recent-activity"
|
28
|
+
end
|
29
|
+
|
30
|
+
def handle(args)
|
31
|
+
list(args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def list(args)
|
35
|
+
options = {}
|
36
|
+
optparse = OptionParser.new do|opts|
|
37
|
+
opts.banner = usage
|
38
|
+
opts.on(nil,'--start', "Start timestamp. Default is 30 days ago.") do |val|
|
39
|
+
options[:start_ts] = val
|
40
|
+
end
|
41
|
+
opts.on(nil,'--end', "End timestamp. Default is now.") do |val|
|
42
|
+
options[:end_ts] = val
|
43
|
+
end
|
44
|
+
build_common_options(opts, options, [:list, :json])
|
45
|
+
end
|
46
|
+
optparse.parse(args)
|
47
|
+
|
48
|
+
connect(options)
|
49
|
+
begin
|
50
|
+
|
51
|
+
params = {}
|
52
|
+
[:phrase, :offset, :max, :sort, :direction, :start, :end].each do |k|
|
53
|
+
params[k] = options[k] unless options[k].nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
json_response = @dashboard_interface.recent_activity(params)
|
57
|
+
|
58
|
+
if options[:json]
|
59
|
+
print JSON.pretty_generate(json_response)
|
60
|
+
print "\n"
|
61
|
+
else
|
62
|
+
|
63
|
+
|
64
|
+
# todo: impersonate command and show that info here
|
65
|
+
|
66
|
+
print "\n" ,cyan, bold, "Dashboard\n","==================", reset, "\n\n"
|
67
|
+
print cyan
|
68
|
+
|
69
|
+
print "\n"
|
70
|
+
puts "Coming soon.... see --json"
|
71
|
+
print "\n"
|
72
|
+
|
73
|
+
print reset,"\n"
|
74
|
+
|
75
|
+
end
|
76
|
+
rescue RestClient::Exception => e
|
77
|
+
print_rest_exception(e, options)
|
78
|
+
exit 1
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|