Module: NexClient::Commands::Users

Extended by:
Helpers
Defined in:
lib/nex_client/commands/users.rb

Constant Summary collapse

USERS_TITLE =
"Users".colorize(:blue)
USERS_HEADERS =
['handle','name','email','token','api-only','orgs'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS, Helpers::VARS_HEADERS, Helpers::VARS_TITLE

Class Method Summary collapse

Methods included from Helpers

display_events, display_logs, display_raw_vars, display_record_errors, display_vars, display_yaml_vars, error, events, format_cmd, hash_from_file, perform_ssh_cmd, success, vars_from_file, vars_from_plain_file, vars_from_yaml_file, with_cert_identity

Class Method Details

.create_api_user(args, opts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nex_client/commands/users.rb', line 30

def self.create_api_user(args,opts)
  o = NexClient::Organization.find(handle: args.first).first
  unless o
    error("Error! Could not find organization: #{args.first}")
    return false
  end

  # Create user
  u = o.create_api_user
  self.display_users(u)
end

.destroy(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/users.rb', line 42

def self.destroy(args,opts)
  handle = args.first
  e = NexClient::User.find(handle: handle).first

  # Display error
  unless e
    error("Error! Could not find user: #{name}")
    return false
  end

  # Display error
  unless e.api_only
    error("Error! Only api users can be destroyed")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the handle of this user to confirm: ")
  unless answer == e.handle
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed api user: #{handle}")
end

.display_users(list) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/nex_client/commands/users.rb', line 69

def self.display_users(list)
  table = Terminal::Table.new title: USERS_TITLE, headings: USERS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end

.format_orgs(record) ⇒ Object



91
92
93
94
# File 'lib/nex_client/commands/users.rb', line 91

def self.format_orgs(record)
  return "-" unless record.organizations.present?
  record.organizations.map(&:handle).uniq.join(',')
end

.format_record(record) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nex_client/commands/users.rb', line 79

def self.format_record(record)
  orgs = self.format_orgs(record)
  [
    record.handle,
    record.first_name || '-',
    record.email || '-',
    record.api_token || '*****',
    record.api_only,
    orgs
  ]
end

.list(args, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nex_client/commands/users.rb', line 10

def self.list(args,opts)
  filters = {}
  filters[:api_only] = opts.api if opts.api

  # Org filter
  org = opts.organization || opts.org
  filters[:'organization.handle'] = org if org.present?

  # Create table
  list = NexClient::User.includes(:organizations).where(filters).order('handle')
  self.display_users(list)

  # Loop through results
  while (list.pages.links||{})['next']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_users(list)
  end
end