Class: Morpheus::Cli::Credentials
- Inherits:
-
Object
- Object
- Morpheus::Cli::Credentials
show all
- Includes:
- PrintHelper
- Defined in:
- lib/morpheus/cli/credentials.rb
Instance Method Summary
collapse
included, #print_errors, #print_green_success, #print_red_alert, #print_rest_exception, #required_blue_prompt
Constructor Details
#initialize(appliance_name, appliance_url) ⇒ Credentials
Returns a new instance of Credentials.
13
14
15
16
|
# File 'lib/morpheus/cli/credentials.rb', line 13
def initialize(appliance_name, appliance_url)
@appliance_url = appliance_url
@appliance_name = appliance_name
end
|
Instance Method Details
#clear_saved_credentials ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/morpheus/cli/credentials.rb', line 88
def clear_saved_credentials()
credential_map = load_credential_file
if credential_map.nil?
credential_map = {}
end
credential_map.delete(@appliance_name)
File.open(credentials_file_path, 'w') {|f| f.write credential_map.to_yaml } end
|
#credentials_file_path ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/morpheus/cli/credentials.rb', line 115
def credentials_file_path
home_dir = Dir.home
morpheus_dir = File.join(home_dir,".morpheus")
if !Dir.exist?(morpheus_dir)
Dir.mkdir(morpheus_dir)
end
return File.join(morpheus_dir,"credentials")
end
|
#load_credential_file ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/morpheus/cli/credentials.rb', line 106
def load_credential_file
creds_file = credentials_file_path
if File.exist? creds_file
return YAML.load_file(creds_file)
else
return nil
end
end
|
#load_saved_credentials ⇒ Object
97
98
99
100
101
102
103
104
|
# File 'lib/morpheus/cli/credentials.rb', line 97
def load_saved_credentials()
credential_map = load_credential_file
if credential_map.nil?
return nil
else
return credential_map[@appliance_name]
end
end
|
#login(opts = {}) ⇒ Object
79
80
81
82
|
# File 'lib/morpheus/cli/credentials.rb', line 79
def login(opts = {})
clear_saved_credentials
request_credentials(opts)
end
|
#logout ⇒ Object
84
85
86
|
# File 'lib/morpheus/cli/credentials.rb', line 84
def logout()
clear_saved_credentials
end
|
#request_credentials(opts = {}) ⇒ Object
18
19
20
21
22
23
24
25
26
27
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
|
# File 'lib/morpheus/cli/credentials.rb', line 18
def request_credentials(opts = {})
username = nil
password = nil
creds = nil
skip_save = false
if !opts[:remote_username].nil?
username = opts[:remote_username]
password = opts[:remote_password]
skip_save = opts[:remote_url] ? true : false
else
creds = load_saved_credentials
end
if !creds
print "\nEnter Morpheus Credentials for #{@appliance_name} - #{@appliance_url}\n\n",reset
if username.nil? || username.empty?
print "Username: #{required_blue_prompt} "
username = $stdin.gets.chomp!
end
if password.nil? || password.empty?
print "Password: #{required_blue_prompt} "
password = STDIN.noecho(&:gets).chomp!
print "\n"
end
oauth_url = File.join(@appliance_url, "/oauth/token")
begin
authorize_response = Morpheus::RestClient.execute(method: :post, url: oauth_url, headers:{ params: {grant_type: 'password', scope:'write', client_id: 'morph-cli', username: username}}, payload: {password: password},verify_ssl: false, timeout: 10)
json_response = JSON.parse(authorize_response.to_s)
access_token = json_response['access_token']
if !access_token.empty?
save_credentials(access_token) unless skip_save
return access_token
else
print_red_alert "Credentials not verified."
return nil
end
rescue ::RestClient::Exception => e
if (e.response && e.response.code == 400)
print_red_alert "Credentials not verified."
if opts[:json]
json_response = JSON.parse(e.response.to_s)
print JSON.pretty_generate(json_response)
print reset, "\n\n"
end
else
print_rest_exception(e, opts)
end
exit 1
rescue => e
print_red_alert "Error Communicating with the Appliance. #{e}"
exit 1
end
else
return creds
end
end
|
#save_credentials(token) ⇒ Object
124
125
126
127
128
129
130
131
|
# File 'lib/morpheus/cli/credentials.rb', line 124
def save_credentials(token)
credential_map = load_credential_file
if credential_map.nil?
credential_map = {}
end
credential_map[@appliance_name] = token
File.open(credentials_file_path, 'w') {|f| f.write credential_map.to_yaml } end
|