28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/morpheus/cli/login.rb', line 28
def login(args)
options = {}
username, password = nil, nil
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = usage
opts.on( '-u', '--username USERNAME', "Username" ) do |val|
username = val
end
opts.on( '-p', '--password PASSWORD', "Password" ) do |val|
password = val
end
opts.on( '-t', '--test', "Test credentials only, does not update stored credentials for the appliance." ) do
options[:test_only] = true
end
opts.on( '-T', '--token ACCESS_TOKEN', "Use an existing access token to login instead of authenticating with a username and password." ) do |val|
options[:remote_token] = val
end
build_common_options(opts, options, [:json, :dry_run, :remote, :quiet], [:remote_username, :remote_password, :remote_token])
opts. = "Login to a remote appliance with a username and password or an access token.\n" +
"[username] is required .\n" +
"[password] is required.\n" +
"Logging in with username and password will make an authentication api request to obtain an access token.\n" +
"The --token option can be used to login with an existing token instead of username and password.\n" +
"Using --token makes a whoami api request to validate the token.\n" +
"If successful, the access token will be saved with the active session for the remote appliance.\n" +
"This command will first logout any active session before attempting to login.\n" +
"The --test option can be used for testing credentials without updating your active session."
end
optparse.parse!(args)
if args.count > 2
print_error Morpheus::Terminal.angry_prompt
puts_error "#{command_name} list expects 0-2 arguments and received #{args.count}: #{args}\n#{optparse}"
return 1
end
username = args[0] if args[0]
password = args[1] if args[1]
if options[:remote]
appliance = Morpheus::Cli::Remote.appliances[options[:remote].to_sym]
if appliance
@appliance_name, @appliance_url = options[:remote].to_sym, appliance[:host]
else
@appliance_name, @appliance_url = nil, nil
end
if !@appliance_name
print_error red, "You have no appliance named '#{options[:remote]}' configured. See the `remote list` command.", reset, "\n"
return 1
end
elsif options[:remote_url]
@appliance_name, @appliance_url = nil, appliance[:remote_url]
else
@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
if !@appliance_name
print_error yellow, "Please specify a remote appliance with -r or see the command `remote use`", reset, "\n"
return 1
end
end
begin
if (options[:quiet] && (!options[:remote_token]) && !(username && password))
print_error yellow,"Please specify username and password, or token.", reset, "\n"
return 1
end
options[:username] = username if username
options[:password] = password if password
do_save = true
if options[:test_only] || options[:remote_url]
do_save = false
end
login_result = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).login(options, do_save)
if options[:dry_run]
return 0
end
wallet = login_result
::Morpheus::Cli::Remote.recalculate_variable_map()
if wallet && wallet['access_token']
if !options[:quiet]
if options[:test_only]
print green,"Success! Credentials verified for #{wallet['username']}.", reset, "\n"
else
print green,"Success! Logged in to #{@appliance_name} as #{wallet['username']}.", reset, "\n"
end
end
return 0 else
if !options[:quiet]
if options[:test_only]
else
end
end
return 1, "Login failed"
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|