Class: OsCtl::Cli::User

Inherits:
Command show all
Includes:
Assets, Attributes
Defined in:
lib/osctl/cli/user.rb

Constant Summary collapse

FIELDS =
%i(
  pool
  name
  username
  groupname
  ugid
  dataset
  homedir
  registered
)
FILTERS =
%i(pool registered)
DEFAULT_FIELDS =
%i(
  pool
  name
  registered
)
IDMAP_FIELDS =
%i(type ns_id host_id count)

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods included from Attributes

#do_set_attr, #do_unset_attr

Methods included from Assets

#print_assets

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#assetsObject



147
148
149
150
# File 'lib/osctl/cli/user.rb', line 147

def assets
  require_args!('name')
  print_assets(:user_assets, name: args[0], pool: gopts[:pool])
end

#createObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/osctl/cli/user.rb', line 86

def create
  require_args!('name')

  if opts['map'].any? && (opts['map-uid'].any? || opts['map-gid'].any?)
    raise GLI::BadCommandLine, 'use either --map, or --map-uid and --map-gid'

  elsif (opts['map'] + opts['map-uid']).empty?
    raise GLI::BadCommandLine, 'provide at least one UID mapping with --map '+
      'or --map-uid'

  elsif (opts['map'] + opts['map-gid']).empty?
    raise GLI::BadCommandLine, 'provide at least one GID mapping with --map '+
      'or --map-gid'
  end

  if opts['map'].any?
    uid_map = gid_map = opts['map']

  else
    uid_map = opts['map-uid']
    gid_map = opts['map-gid']
  end

  osctld_fmt(:user_create, {
    name: args[0],
    pool: opts[:pool] || gopts[:pool],
    ugid: opts[:ugid],
    uid_map: uid_map,
    gid_map: gid_map,
  })
end

#deleteObject



118
119
120
121
# File 'lib/osctl/cli/user.rb', line 118

def delete
  require_args!('name')
  osctld_fmt(:user_delete, name: args[0], pool: gopts[:pool])
end

#idmap_lsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/osctl/cli/user.rb', line 152

def idmap_ls
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  require_args!('name')

  cmd_opts = {name: args[0], uid: true, gid: true}
  fmt_opts = {layout: :columns}

  case args[1]
  when 'uid'
    cmd_opts[:gid] = false
  when 'gid'
    cmd_opts[:uid] = false
  when nil, 'both'
    # pass
  else
    raise GLI::BadCommandLine, "expected uid|gid|both, got '#{args[1]}'"
  end

  fmt_opts[:header] = false if opts['hide-header']

  osctld_fmt(
    :user_idmap_list,
    cmd_opts,
    opts[:output] ? opts[:output].split(',').map(&:to_sym) : IDMAP_FIELDS,
    fmt_opts
  )
end

#listObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/osctl/cli/user.rb', line 30

def list
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  cmd_opts = {}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
  }

  cmd_opts[:names] = args if args.count > 0

  if opts[:registered]
    cmd_opts[:registered] = true
  elsif opts[:unregistered]
    cmd_opts[:registered] = false
  end

  FILTERS.each do |v|
    [gopts, opts].each do |options|
      next unless options[v]
      cmd_opts[v] = options[v].split(',')
    end
  end

  fmt_opts[:header] = false if opts['hide-header']

  osctld_fmt(
    :user_list,
    cmd_opts,
    opts[:output] ? opts[:output].split(',').map(&:to_sym) : DEFAULT_FIELDS,
    fmt_opts
  )
end

#registerObject



123
124
125
126
127
128
129
130
131
# File 'lib/osctl/cli/user.rb', line 123

def register
  require_args!('name')

  if args[0] == 'all'
    osctld_fmt(:user_register, all: true)
  else
    osctld_fmt(:user_register, name: args[0], pool: gopts[:pool])
  end
end

#set_attrObject



184
185
186
187
188
189
190
191
192
# File 'lib/osctl/cli/user.rb', line 184

def set_attr
  require_args!('name', 'attribute', 'value')
  do_set_attr(
    :user_set,
    {name: args[0], pool: gopts[:pool]},
    args[1],
    args[2],
  )
end

#showObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctl/cli/user.rb', line 67

def show
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  require_args!('name')

  fmt_opts = {layout: :rows}
  fmt_opts[:header] = false if opts['hide-header']

  osctld_fmt(
    :user_show,
    {name: args[0], pool: gopts[:pool]},
    opts[:output] ? opts[:output].split(',').map(&:to_sym) : nil,
    fmt_opts
  )
end

#subugidsObject



143
144
145
# File 'lib/osctl/cli/user.rb', line 143

def subugids
  osctld_fmt(:user_subugids)
end

#unregisterObject



133
134
135
136
137
138
139
140
141
# File 'lib/osctl/cli/user.rb', line 133

def unregister
  require_args!('name')

  if args[0] == 'all'
    osctld_fmt(:user_unregister, all: true)
  else
    osctld_fmt(:user_unregister, name: args[0], pool: gopts[:pool])
  end
end

#unset_attrObject



194
195
196
197
198
199
200
201
# File 'lib/osctl/cli/user.rb', line 194

def unset_attr
  require_args!('name', 'attribute')
  do_unset_attr(
    :user_unset,
    {name: args[0], pool: gopts[:pool]},
    args[1],
  )
end