Class: HammerCLIForeman::BasicCredentials

Inherits:
ApipieBindings::AbstractCredentials
  • Object
show all
Defined in:
lib/hammer_cli_foreman/credentials.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BasicCredentials

Can keep username and passwords credentials and prompt for them when necessary

Examples:

use container with prompt

c = HammerCLIForeman::BasicCredentials.new()
c.username
> [Foreman] Username: admin
=> "admin"

use container with preset value

c = HammerCLIForeman::BasicCredentials.new(:username => 'admin')
c.username
=> "admin"

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :username (String)

    when nil, user is prompted when the attribute is accessed

  • :password (String)

    when nil, user is prompted when the attribute is accessed



18
19
20
21
# File 'lib/hammer_cli_foreman/credentials.rb', line 18

def initialize(params={})
  @username = params[:username]
  @password = params[:password]
end

Instance Method Details

#clearObject



41
42
43
44
45
# File 'lib/hammer_cli_foreman/credentials.rb', line 41

def clear
  super
  @username = nil
  @password = nil
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/hammer_cli_foreman/credentials.rb', line 37

def empty?
  !@username && !@password
end

#passwordString

Get password. Prompt for it when not set. Password characters are replaced with asterisks on the screen.

Returns:

  • (String)


32
33
34
35
# File 'lib/hammer_cli_foreman/credentials.rb', line 32

def password
  @password ||= ask_user(_("[Foreman] Password for %s: ") % username, true) if HammerCLI.interactive?
  @password
end

#to_paramsHash

Convert credentials to hash usable for merging to RestClient configuration.

Returns:

  • (Hash)


49
50
51
52
53
54
# File 'lib/hammer_cli_foreman/credentials.rb', line 49

def to_params
  {
    :user => username,
    :password => password
  }
end

#usernameString

Get username. Prompt for it when not set

Returns:

  • (String)


25
26
27
28
# File 'lib/hammer_cli_foreman/credentials.rb', line 25

def username
  @username ||= ask_user(_("[Foreman] Username: ")) if HammerCLI.interactive?
  @username
end