Class: Morpheus::Cli::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/morpheus/cli/credentials.rb

Instance Method Summary collapse

Constructor Details

#initialize(appliance_name, appliance_url) ⇒ Credentials

Returns a new instance of Credentials.



8
9
10
11
# File 'lib/morpheus/cli/credentials.rb', line 8

def initialize(appliance_name, appliance_url)
	@appliance_url = appliance_url
	@appliance_name = appliance_name
end

Instance Method Details

#clear_saved_credentials ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/morpheus/cli/credentials.rb', line 66

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 } #Store
end

#credentials_file_path ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/morpheus/cli/credentials.rb', line 93

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



84
85
86
87
88
89
90
91
# File 'lib/morpheus/cli/credentials.rb', line 84

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



75
76
77
78
79
80
81
82
# File 'lib/morpheus/cli/credentials.rb', line 75

def load_saved_credentials()
	credential_map = load_credential_file
	if credential_map.nil?
		return nil
	else
		return credential_map[@appliance_name]
	end
end

#login ⇒ Object



57
58
59
60
# File 'lib/morpheus/cli/credentials.rb', line 57

def ()
	clear_saved_credentials
	request_credentials
end

#logout ⇒ Object



62
63
64
# File 'lib/morpheus/cli/credentials.rb', line 62

def logout()
	clear_saved_credentials
end

#request_credentials(opts = {}) ⇒ Object



13
14
15
16
17
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
# File 'lib/morpheus/cli/credentials.rb', line 13

def request_credentials(opts = {})
	username = nil
	password = nil
	creds = nil
	skip_save = false
	# We should return an access Key for Morpheus CLI Here
	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
		if username.nil? || username.empty?
			puts "Enter Username: "
			username = $stdin.gets.chomp!
		end
		if password.nil? || password.empty?
			puts "Enter Password: "
			password = STDIN.noecho(&:gets).chomp!
		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, password: password}},verify_ssl: false)
			json_response = JSON.parse(authorize_response.to_s)
			access_token = json_response['access_token']
			if access_token

				save_credentials(access_token) unless skip_save
				return access_token
			else
				puts "Credentials not verified."
				return nil
			end
		rescue => e
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
			return nil
		end
	else
		return creds
	end
end

#save_credentials(token) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/morpheus/cli/credentials.rb', line 102

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 } #Store
end