Class: App42::Command::User

Inherits:
Object
  • Object
show all
Includes:
Base::Util, UserToken, Interactive
Defined in:
lib/app42/command/user.rb

Instance Method Summary collapse

Methods included from Base::Util

#app42_client_info, #ask_app_name, #camel_case_to_whitespace, #check_transaction_status, #check_transaction_status_of_setup, #escape_path, #get_flavour_for_upgrade_or_downgrade, #get_instance_config_for_upgrade_or_downgrade, #input, #ip_address_valid?, #is_app_exist?, #is_service_exist?, #is_setup_name_exist?, #json_parse, #message, #number_valid?, #numeric?, #numeric_including_zero?, #params, #parse_error_message, #print_new_line, #request_failed?, #resource_url, #show_wait_spinner, #signature, #status_call, #time_valid?, #util_base, #validate_app_and_service_name, #validate_database_name, #validate_git_url, #validate_setup_name, #validate_upload_backup_path, #validate_url, #validate_vm_config

Methods included from UserToken

#check_key_file?, #config_path, #ensure_config_dir, #ensure_key_file, #get_keys, #key_path, #local_app42_key, #remove_key_file

Constructor Details

#initialize(options = {}) ⇒ Object

dup argument to get a non-frozen string



17
18
19
# File 'lib/app42/command/user.rb', line 17

def initialize(options={} )
  @options = options.dup 
end

Instance Method Details

#addObject

Configure app42 credentials



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/app42/command/user.rb', line 57

def add
  update_key if App42::Command::Auth.logged_in?
  api_key, secret_key = collect_app42_credentials
  if is_api_key_valid? api_key, secret_key
    configure_app42 api_key, secret_key
    print Paint["Adding keys...", :yellow]
    puts Paint["done", :green]
  else
    puts Paint["#{Message::WRONG_KEY}", :red]
    remove_key_file
    print_new_line
    exit!  
  end
end

#clearObject

clear local credentials



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/app42/command/user.rb', line 37

def clear
  if App42::Command::Auth.logged_in? then
    begin
      ans = ask "Do you want to delete existing keys?", :default => true
      print_new_line
      if ans == true
        key_file = remove_key_file
        puts message "#{Message::KEYS_CLEARED}", false, 'green'
      else
        exit!
      end
    rescue
      message "#{Message::SOMETHING_WRONG}", false, 'red'
    end
  else
    puts message "#{Message::KEYS_NOT_FIND}", false, 'red'
  end
end

#collect_app42_credentialsObject

collect keys for keys:add Get api key Get secret key



100
101
102
103
104
105
106
107
# File 'lib/app42/command/user.rb', line 100

def collect_app42_credentials
  api_key = @options[:api] if @options[:api]
  secret_key = @options[:secret] if @options[:secret]
  message '=== Enter your App42PaaS keys ===', true, 'blue' if api_key.nil? && secret_key.nil?
  api_key = get_api_key if api_key.nil?
  secret_key = get_secret_key if secret_key.nil?
  return api_key, secret_key
end

#get_api_key(prompt = Paint["Enter API Key:", :bright]) ⇒ Object

collect api key from client



110
111
112
113
114
# File 'lib/app42/command/user.rb', line 110

def get_api_key(prompt = Paint["Enter API Key:", :bright])
  api_key = @options[:api_key] if @options[:api_key]
  api_key = ask(prompt) {|q| q.echo = true} if api_key.nil?
  return api_key.strip
end

#get_secret_key(prompt = Paint["Enter Secret Key:", :bright]) ⇒ Object

collect secret key from client



117
118
119
120
121
# File 'lib/app42/command/user.rb', line 117

def get_secret_key(prompt = Paint["Enter Secret Key:", :bright])
  secret_key = @options[:secret_key] if @options[:secret_key]
  secret_key = ask(prompt) {|q| q.echo = true} if secret_key.nil?
  return secret_key.strip
end

#is_api_key_valid?(api_key, secret_key) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# File 'lib/app42/command/user.rb', line 80

def is_api_key_valid? api_key, secret_key #:nodoc:
  key_validate_params =  {
      'apiKey'=> api_key,
      'version' => VERSION,
      'timeStamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
  }
  api_key_valid = validate_api_and_secret_key key_validate_params, 'info', 'authenticate', secret_key
  return api_key_valid['success']
end

#keysObject

list local api and secret key



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/app42/command/user.rb', line 22

def keys
  if check_key_file?
    puts message "#{Message::KEYS}", false, 'green'
    api_key, secret_key = get_keys 
    puts "API Key      = #{api_key}"
    puts "Secret Key   = #{secret_key}"
  else
    message "#{Message::KEYS_NOT_FIND}", true, 'red'
    # TODO, should be dynamic
    App42::Base::Help.addkeys
  end

end

#update_keyObject

update existing key



73
74
75
76
77
78
# File 'lib/app42/command/user.rb', line 73

def update_key
  puts message "#{Message::KEYS_EXIST}", false, 'red'
  ans = ask "\nDo you want to update existing keys?", :default => true
  print_new_line
  ans == true ? (return) : (exit!)
end

#validate_api_and_secret_key(query_params, resource, action, secret_key) ⇒ Object

validate api and secret key



91
92
93
94
95
# File 'lib/app42/command/user.rb', line 91

def validate_api_and_secret_key query_params, resource, action, secret_key
  url = resource_url(resource, action)
  sign = App42::Client::RestUtil.new.sign(secret_key, query_params )
  response = App42::Client::App42RestClient.new.get(sign, url, query_params)
end