Class: Morpheus::Cli::Login

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #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



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

def initialize()
  
end

Instance Method Details

#connect(opts) ⇒ Object



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

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

#handle(args) ⇒ Object



24
25
26
# File 'lib/morpheus/cli/login.rb', line 24

def handle(args)
  (args)
end

#login(args) ⇒ Object



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

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( '-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 @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



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

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