Class: Morpheus::Cli::Login

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/login.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeLogin

include Morpheus::Cli::WhoamiHelper include Morpheus::Cli::AccountsHelper



7
8
9
# File 'lib/morpheus/cli/commands/login.rb', line 7

def initialize()
  
end

Instance Method Details

#connect(opts) ⇒ Object



11
12
13
# File 'lib/morpheus/cli/commands/login.rb', line 11

def connect(opts)
  # @api_client = establish_remote_appliance_connection({:skip_verify_access_token => true, :skip_login => true}.merge(opts))
end

#handle(args) ⇒ Object



19
20
21
# File 'lib/morpheus/cli/commands/login.rb', line 19

def handle(args)
  (args)
end

#login(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/commands/login.rb', line 23

def (args)
  options = {}
  username, password = nil, nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = usage
    opts.on( '-u', '--username USERNAME', "Username. Sub-tenant users must format their username with a prefix like {subdomain}\\{username}" ) do |val|
      username = val
    end
    opts.on( '-p', '--password PASSWORD', "Password" ) do |val|
      password = val
    end
    opts.on( '--password-file FILE', String, "Password File, read a file containing the password." ) do |val|
      password_file = File.expand_path(val)
      if !File.exist?(password_file) || !File.file?(password_file) # check readable too
        raise ::OptionParser::InvalidOption.new("File not found: #{password_file}")
      end
      password = File.read(password_file) #.to_s.split("\n").first.strip
    end
    opts.on( '-t', '--test', "Test credentials only, does not update stored credentials for the appliance." ) do
      options[:test_only] = true
    end
    opts.on( '--client-id CLIENT', "Used to test authentication with a client_id other than #{Morpheus::APIClient::CLIENT_ID}. Currently behaves like --test, credentials are not stored." ) do |val|
      options[:client_id] = val.to_s
      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.footer = <<-EOT
Login to a remote appliance with a username and password or using an access token.
Logging in with username and password will make an authentication api request to obtain an access token.
[username] is required, this is the username of the Morpheus User
[password] is required, this is the password of the Morpheus User
Sub-tenant users will need to pass their tenant subdomain prefix. ie. {subdomain}\\{username}
By default, the subdomain is the tenant account ID. Example: 2\\neo
The --token option can be used to login with a valid access token instead of username and password.
The specified token will be verified by making a whoami api request
If successful, the access token will be saved with the active session for the remote appliance.
This command will first logout any active session before attempting authentication.
The --test option can be used to test credentials without updating the stored credentials for the appliance, neither logging you in or out.
EOT
                  
  end
  optparse.parse!(args)
  verify_args!(args:args, max:2, optparse:optparse)
  username = args[0] if args[0]
  password = args[1] if args[1]
  
  # connect(options)
  @api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
  
  if options[:test_only]
    puts "Testing credentials, your current session will not be modified."
  elsif @remote_appliance[:authenticated]
    puts "You will be automatically logged out of your current session as '#{@remote_appliance[:username]}'"
  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
    #old_wallet = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).load_saved_credentials()
    
     = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).(options, do_save)
    if options[:dry_run]
      return 0
    end
    wallet = 

    # needed here too?
    ::Morpheus::Cli::Remote.recalculate_variable_map()

    # should happen here, not in Credentials.login()
    # if options[:json]
    #   puts as_json(login_result)
    #   return (login_result && login_result['access_token']) ? 0 : 1
    # end

    if wallet && wallet['access_token']
      # Login Success!
      if !options[:quiet]
        if options[:test_only]
          print green,"Success! Test Credentials verified for #{wallet['username']}", reset, "\n"
        else
          # clear whoami cache, it will be lazily load_saved_credentials
          ::Morpheus::Cli::Whoami.clear_whoami(@appliance_name, wallet['username'])
          print green,"Success! Logged in as #{wallet['username']}", reset, "\n"
        end
      end
      return 0 # ,  nil
    else
      # Login Failed
      # so login() already prints 'Bad Credentials' (deprecate class Credentials plz)
      # tell them if they're logged out now.
      if !options[:quiet]
        if options[:test_only]
          # you are fine, nothing has changed
        else
          # if old_wallet && old_wallet['access_token']
          #   #print reset,"You are no longer logged in. Goodbye #{old_wallet['username']}!", reset, "\n"
          #    # todo: prompt to recover wallet ?
          # end
        end
      end
      return 1, "Login failed"
    end

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end

end

#usageObject



15
16
17
# File 'lib/morpheus/cli/commands/login.rb', line 15

def usage
  "Usage: morpheus login [username] [password]"
end