class CF::UAA::CommonCli

Public Instance Methods

askd(prompt, defary) click to toggle source
# 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_header() click to toggle source
# 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]) click to toggle source
# File lib/uaa/cli/common.rb, line 35
def clientid(id = opts[:client]); id || ask("Client ID") end
clientname(name = opts[:name]) click to toggle source
# File lib/uaa/cli/common.rb, line 37
def clientname(name = opts[:name]); name end
clientsecret(secret = opts[:secret]) click to toggle source
# File lib/uaa/cli/common.rb, line 36
def clientsecret(secret = opts[:secret]); secret || ask_pwd("Client secret") end
complain(e) click to toggle source
# 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?() click to toggle source
# File lib/uaa/cli/common.rb, line 23
def debug?; opts[:debug] end
handle_request() { || ... } click to toggle source
# File lib/uaa/cli/common.rb, line 65
def handle_request
  yield
rescue Exception => e
  complain e
end
passcode(passcode = opts[:passcode]) click to toggle source
# 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) click to toggle source
# 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 = if type == :user
      sr.query(type, query.merge!(startIndex: opts[:start], count: opts[:count]))
    else
      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) click to toggle source
# File lib/uaa/cli/common.rb, line 136
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) click to toggle source
# File lib/uaa/cli/common.rb, line 131
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) click to toggle source
# File lib/uaa/cli/common.rb, line 105
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
  chosen_info = if info.length >= 2
    say 'Select an origin:'
    info.each_with_index do |i, idx|
      say "#{idx + 1}. #{i['origin']}"
    end

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


  # when getting whole object, handle case of UAA < 1.3 which did not return meta attr from query
  attrs || !chosen_info["id"] || chosen_info["meta"] ? chosen_info : scim.get(type, chosen_info["id"])
end
scim_request() { |scim(target, auth_header, { skip_ssl_validation: target_value(:skip_ssl_validation), ssl_ca_file: target_value(:ca_cert), zone: opts })| ... } click to toggle source
# 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?() click to toggle source
# File lib/uaa/cli/common.rb, line 22
def trace?; opts[:trace] end
update_target_info(info = nil) click to toggle source
# 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) click to toggle source
# File lib/uaa/cli/common.rb, line 32
def username(name); name || ask("User name") end
userpwd(pwd = opts[:password]) click to toggle source
# File lib/uaa/cli/common.rb, line 33
def userpwd(pwd = opts[:password]); pwd || ask_pwd("Password") end
verified_pwd(prompt, pwd = nil) click to toggle source
# 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