Class: CF::UAA::CommonCli

Inherits:
Topic
  • Object
show all
Defined in:
lib/uaa/cli/common.rb

Direct Known Subclasses

ClientCli, CurlCli, GroupCli, InfoCli, MiscCli, TokenCli, UserCli

Instance Method Summary collapse

Methods inherited from Topic

#add_command, #ask, #ask_pwd, commands, define_option, desc, #gripe, #help_col_start, #initialize, #opt_help, #opt_strs, option_defs, #opts, #pp, #print_tree, #say, #say_cmd_helper, #say_command_help, #say_commands, #say_definition, #say_help, #terminal_columns, topic

Constructor Details

This class inherits a constructor from CF::UAA::Topic

Instance Method Details

#askd(prompt, defary) ⇒ Object



48
49
50
51
52
# File 'lib/uaa/cli/common.rb', line 48

def askd(prompt, defary)
  return ask(prompt) unless defary
  result = ask("#{prompt} [#{Util.strlist(defary)}]")
  result.nil? || result.empty? ? defary : result
end

#auth_headerObject



25
26
27
28
29
30
# File 'lib/uaa/cli/common.rb', line 25

def auth_header
  unless (ttype = Config.value(:token_type)) && (token = Config.value(:access_token))
    raise "Need an access token to complete this command. Please login."
  end
  "#{ttype} #{token}"
end

#clientid(id = opts[:client]) ⇒ Object



35
# File 'lib/uaa/cli/common.rb', line 35

def clientid(id = opts[:client]); id || ask("Client ID") end

#clientname(name = opts[:name]) ⇒ Object



37
# File 'lib/uaa/cli/common.rb', line 37

def clientname(name = opts[:name]); name end

#clientsecret(secret = opts[:secret]) ⇒ Object



36
# File 'lib/uaa/cli/common.rb', line 36

def clientsecret(secret = opts[:secret]); secret || ask_pwd("Client secret") end

#complain(e) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/uaa/cli/common.rb', line 54

def complain(e)
  case e
  when TargetError then gripe "\n#{e.message}:\n#{Util.json_pretty(e.info)}"
  when Exception
    gripe "\n#{e.class}: #{e.message}\n\n"
    gripe e.backtrace if trace?
  when String then gripe e
  else gripe "unknown type of gripe: #{e.class}, #{e}"
  end
end

#debug?Boolean

Returns:

  • (Boolean)


23
# File 'lib/uaa/cli/common.rb', line 23

def debug?; opts[:debug] end

#handle_requestObject



65
66
67
68
69
# File 'lib/uaa/cli/common.rb', line 65

def handle_request
  yield
rescue Exception => e
  complain e
end

#passcode(passcode = opts[:passcode]) ⇒ Object



34
# File 'lib/uaa/cli/common.rb', line 34

def passcode(passcode = opts[:passcode]); passcode || ask("Passcode (from #{Config.target}/passcode)") end

#scim_common_list(type, filter) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/uaa/cli/common.rb', line 88

def scim_common_list(type, filter)
  pp scim_request { |sr|
    query = { attributes: opts[:attrs], filter: filter }
    info = nil
    if type == :user
      info = sr.query(type, query.merge!(startIndex: opts[:start], count: opts[:count]))
    else
      info = opts[:start] || opts[:count] ?
             sr.query(type, query.merge!(startIndex: opts[:start], count: opts[:count])):
             sr.all_pages(type, query)
    end

    nattr = sr.name_attr(type).downcase
    info.is_a?(Array) && info.length > 0 && info[0][nattr] ?
        info.each_with_object({}) { |v, h| h[v.delete(nattr)] = v } : info
  }
end

#scim_get_helper(attrs, query, scim, type) ⇒ Object

Raises:

  • (BadResponse)


135
136
137
138
139
140
141
142
# File 'lib/uaa/cli/common.rb', line 135

def scim_get_helper(attrs, query, scim, type)
  info = scim.all_pages(type, query)
  raise BadResponse unless info.is_a?(Array) && info.length < 2
  raise NotFound if info.length == 0
  info = info[0]
  # when getting whole object, handle case of UAA < 1.3 which did not return meta attr from query
  attrs || !info["id"] || info["meta"] ? info : scim.get(type, info["id"])
end

#scim_get_object(scim, type, name, attrs = nil) ⇒ Object



130
131
132
133
# File 'lib/uaa/cli/common.rb', line 130

def scim_get_object(scim, type, name, attrs = nil)
  query = { attributes: attrs, filter: "#{scim.name_attr(type)} eq \"#{name}\""}
  scim_get_helper(attrs, query, scim, type)
end

#scim_get_user_object(scim, type, name, origin, attrs = nil) ⇒ Object

Raises:

  • (BadResponse)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/uaa/cli/common.rb', line 106

def scim_get_user_object(scim, type, name, origin, attrs = nil)
  origin_filter = origin ? " and origin eq \"#{origin}\"" : ''
  query = { attributes: attrs, filter: "#{scim.name_attr(type)} eq \"#{name}\"#{origin_filter}"}
  info = scim.all_pages(type, query)
  raise BadResponse unless info.is_a?(Array)
  raise NotFound if info.length == 0
  if info.length >= 2
    info.each_with_index do |i, idx|
       puts "#{idx+1}: #{i['username']} from #{i['origin']}"
    end

    choice = @highline.ask("Select user:  ").to_i
    if choice > info.length || choice <= 0
      raise ArgumentError 'bad input, klugscheisser'
    end
    info = info[choice - 1]
  else
    info = info[0]
  end

  # when getting whole object, handle case of UAA < 1.3 which did not return meta attr from query
  attrs || !info["id"] || info["meta"] ? info : scim.get(type, info["id"])
end

#scim_requestObject



71
72
73
74
75
76
77
78
# File 'lib/uaa/cli/common.rb', line 71

def scim_request
  yield Scim.new(Config.target, auth_header, {
    skip_ssl_validation: Config.target_value(:skip_ssl_validation),
    ssl_ca_file: Config.target_value(:ca_cert),
    zone: opts[:zone] })
rescue Exception => e
  complain e
end

#trace?Boolean

Returns:

  • (Boolean)


22
# File 'lib/uaa/cli/common.rb', line 22

def trace?; opts[:trace] end

#update_target_info(info = nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/uaa/cli/common.rb', line 80

def update_target_info(info = nil)
  return if !info && Config.target_value(:prompts)
  info ||= @cli_class.uaa_info_client.server
  Config.target_opts(prompts: info['prompts'])
  Config.target_opts(token_endpoint: info['token_endpoint']) if info['token_endpoint']
  info
end

#username(name) ⇒ Object



32
# File 'lib/uaa/cli/common.rb', line 32

def username(name); name || ask("User name") end

#userpwd(pwd = opts[:password]) ⇒ Object



33
# File 'lib/uaa/cli/common.rb', line 33

def userpwd(pwd = opts[:password]); pwd || ask_pwd("Password") end

#verified_pwd(prompt, pwd = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/uaa/cli/common.rb', line 39

def verified_pwd(prompt, pwd = nil)
  while pwd.nil?
    pwd_a = ask_pwd prompt
    pwd_b = ask_pwd "Verify #{prompt.downcase}"
    pwd = pwd_a if pwd_a == pwd_b
  end
  pwd
end