Class: OsCtl::Cli::KernelKeyring

Inherits:
Object
  • Object
show all
Includes:
Lib::Utils::Humanize
Defined in:
lib/osctl/cli/kernel_keyring.rb

Constant Summary collapse

PREFIX =
'keyring.'.freeze
PARAMS =
%w[nkeys nikeys qnkeys qnbytes].freeze

Instance Method Summary collapse

Instance Method Details

#add_param_values(data, params, precise) ⇒ Object (protected)



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/osctl/cli/kernel_keyring.rb', line 33

def add_param_values(data, params, precise)
  uid_map = OsCtl::Lib::IdMap.from_hash_list(data[:uid_map])
  key_users = keyring.for_id_map(uid_map)

  params.each do |param|
    # Key users are tracked per UID, so one container/mapping can have many
    # key users, we sum them all.
    sum = key_users.inject(0) { |acc, ku| acc + ku.send(param) }

    data[name_to_cli(param).to_sym] = param_value(param, sum, precise)
  end
end

#add_values(data, cols, precise: false) ⇒ Object Also known as: add_container_values, add_user_values



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osctl/cli/kernel_keyring.rb', line 15

def add_values(data, cols, precise: false)
  params = selected_params(cols)
  return if params.empty?

  if data.is_a?(::Hash)
    add_param_values(data, params, precise)
  elsif data.is_a?(::Array)
    data.each do |ct|
      add_param_values(ct, params, precise)
    end
  end
end

#keyringObject (protected)



59
60
61
# File 'lib/osctl/cli/kernel_keyring.rb', line 59

def keyring
  @keyring ||= OsCtl::Lib::KernelKeyring.new
end

#list_param_namesObject



11
12
13
# File 'lib/osctl/cli/kernel_keyring.rb', line 11

def list_param_names
  PARAMS.map { |v| name_to_cli(v) }
end

#name_to_cli(v) ⇒ Object (protected)



63
64
65
# File 'lib/osctl/cli/kernel_keyring.rb', line 63

def name_to_cli(v)
  "#{PREFIX}#{v}"
end

#name_to_internal(v) ⇒ Object (protected)



67
68
69
# File 'lib/osctl/cli/kernel_keyring.rb', line 67

def name_to_internal(v)
  v[PREFIX.length..]
end

#param_value(param, v, precise) ⇒ Object (protected)



71
72
73
74
75
76
77
78
79
# File 'lib/osctl/cli/kernel_keyring.rb', line 71

def param_value(param, v, precise)
  case param
  when 'nkeys', 'nikeys', 'qnkeys'
    OsCtl::Lib::Cli::Presentable.new(v, formatted: precise ? nil : humanize_number(v))

  when 'qnbytes'
    OsCtl::Lib::Cli::Presentable.new(v, formatted: precise ? nil : humanize_data(v))
  end
end

#selected_params(cols) ⇒ Object (protected)



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/osctl/cli/kernel_keyring.rb', line 46

def selected_params(cols)
  ret = []

  cols.each do |c|
    next unless c.start_with?(PREFIX)

    name = name_to_internal(c)
    ret << name if PARAMS.include?(name)
  end

  ret
end