Class: Morpheus::Cli::Credentials

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

Constant Summary collapse

@@saved_credentials_map =
nil

Instance Method Summary collapse

Methods included from PrintHelper

#generate_usage_bar, included, #print_available_options, #print_dry_run, #print_errors, #print_green_success, #print_red_alert, #print_rest_exception, #print_rest_exception_request_and_response, #print_rest_request, #print_rest_response, #print_results_pagination, #print_stats_usage, #print_yellow_warning, #required_blue_prompt

Constructor Details

#initialize(appliance_name, appliance_url) ⇒ Credentials

Returns a new instance of Credentials.



16
17
18
19
# File 'lib/morpheus/cli/credentials.rb', line 16

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

Instance Method Details

#clear_saved_credentials(appliance_name) ⇒ Object



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

def clear_saved_credentials(appliance_name)
  @@saved_credentials_map = load_credentials_file || {}
  @@saved_credentials_map.delete(appliance_name)
  print "#{dark} #=> updating credentials file #{credentials_file_path}#{reset}\n"  if Morpheus::Logging.debug?
  File.open(credentials_file_path, 'w') {|f| f.write @@saved_credentials_map.to_yaml } #Store
end

#credentials_file_pathObject



135
136
137
# File 'lib/morpheus/cli/credentials.rb', line 135

def credentials_file_path
  File.join(Morpheus::Cli.home_directory, "credentials")
end

#load_credentials_fileObject



125
126
127
128
129
130
131
132
133
# File 'lib/morpheus/cli/credentials.rb', line 125

def load_credentials_file
  fn = credentials_file_path
  if File.exist? fn
    print "#{dark} #=> loading credentials file #{fn}#{reset}\n" if Morpheus::Logging.debug?
    return YAML.load_file(fn)
  else
    return nil
  end
end

#load_saved_credentials(reload = false) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/morpheus/cli/credentials.rb', line 109

def load_saved_credentials(reload=false)
  if saved_credentials_map && !reload
    return saved_credentials_map
  end
  @@saved_credentials_map = load_credentials_file || {}
  return @@saved_credentials_map[@appliance_name]
end

#login(opts = {}) ⇒ Object



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

def (opts = {})
  clear_saved_credentials(@appliance_name)
  request_credentials(opts)
end

#logoutObject



98
99
100
# File 'lib/morpheus/cli/credentials.rb', line 98

def logout()
  clear_saved_credentials(@appliance_name)
end

#request_credentials(opts = {}) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/morpheus/cli/credentials.rb', line 21

def request_credentials(opts = {})
  #puts "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
    return creds
  end
  unless opts[:quiet] || opts[:no_prompt]
    # if username.empty? || password.empty?
      print "Enter Morpheus Credentials for #{display_appliance(@appliance_name, @appliance_url)}\n",reset
    # end
    if username.empty?
      print "Username: #{required_blue_prompt} "
      username = $stdin.gets.chomp!
    else
      print "Username: #{required_blue_prompt} #{username}\n"
    end
    if password.empty?
      print "Password: #{required_blue_prompt} "
      password = STDIN.noecho(&:gets).chomp!
      print "\n"
    else
      print "Password: #{required_blue_prompt} \n"
    end
  end
  if username.empty? || password.empty?
    print_red_alert "Username and password are required to login."
    return nil
  end
  begin
    auth_interface = Morpheus::AuthInterface.new(@appliance_url)
    json_response = auth_interface.(username, password)
    if opts[:json]
      print JSON.pretty_generate(json_response)
      print reset, "\n"
    end
    access_token = json_response['access_token']
    if !access_token.empty?
      unless skip_save
        save_credentials(@appliance_name, access_token)
      end
      return access_token
    else
      print_red_alert "Credentials not verified."
      return nil
    end
  rescue ::RestClient::Exception => e
    #raise 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"
      end
    else
      print_rest_exception(e, opts)
    end
  end

end

#save_credentials(app_name, token) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/morpheus/cli/credentials.rb', line 139

def save_credentials(app_name, token)
  # credential_map = saved_credentials_map
  # reloading file is better for now, otherwise you can lose credentials with multiple shells.
  credential_map = load_credentials_file || {}
  if credential_map.nil?
    credential_map = {}
  end
  credential_map[app_name] = token
  begin
    fn = credentials_file_path
    print "#{dark} #=> adding credentials to #{fn}#{reset}\n" if Morpheus::Logging.debug?
    File.open(fn, 'w') {|f| f.write credential_map.to_yaml } #Store
    FileUtils.chmod(0600, fn)
  rescue => e
    puts "failed to save #{fn}. #{e}"  if Morpheus::Logging.debug?
  end
end

#saved_credentials_mapObject

Provides the current credential information, simply :appliance_name => “access_token”



118
119
120
121
122
123
# File 'lib/morpheus/cli/credentials.rb', line 118

def saved_credentials_map
  if !defined?(@@saved_credentials_map)
    @@saved_credentials_map = load_credentials_file
  end
  return @@saved_credentials_map ? @@saved_credentials_map[@appliance_name.to_sym] : nil
end