Class: OsCtl::Cli::IdRange

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

Constant Summary collapse

FIELDS =
%i[pool name start_id last_id block_size block_count allocated free].freeze
TABLE_FIELDS =
%i[type block_index block_count owner first_id last_id id_count].freeze

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, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

Instance Method Details

#allocateObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/osctl/cli/id_range.rb', line 156

def allocate
  require_args!('id-range')

  osctld_fmt(:id_range_allocate, cmd_opts: {
    pool: gopts[:pool],
    name: args[0],
    block_count: opts['block-count'],
    block_index: opts['block-index'],
    owner: opts[:owner]
  })
end

#assetsObject



202
203
204
205
# File 'lib/osctl/cli/id_range.rb', line 202

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

#createObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/osctl/cli/id_range.rb', line 68

def create
  require_args!('id-range')

  osctld_fmt(:id_range_create, cmd_opts: {
    pool: gopts[:pool],
    name: args[0],
    start_id: opts['start-id'],
    block_size: opts['block-size'],
    block_count: opts['block-count']
  })
end

#deleteObject



80
81
82
83
84
85
86
87
# File 'lib/osctl/cli/id_range.rb', line 80

def delete
  require_args!('id-range')

  osctld_fmt(:id_range_delete, cmd_opts: {
    pool: gopts[:pool],
    name: args[0]
  })
end

#freeObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/osctl/cli/id_range.rb', line 168

def free
  require_args!('id-range')

  if !opts['block-index'] === !opts['owner']
    raise GLI::BadCommandLine, 'use --block-index or --owner'
  end

  osctld_fmt(:id_range_free, cmd_opts: {
    pool: gopts[:pool],
    name: args[0],
    block_index: opts['block-index'],
    owner: opts['owner']
  })
end

#listObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/osctl/cli/id_range.rb', line 12

def list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  cols = param_selector.parse_option(opts[:output])
  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  if sort
    sort.each do |v|
      cols << v unless cols.include?(v)
    end
  end

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    cols:,
    sort:
  }

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

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

  osctld_fmt(:id_range_list, cmd_opts:, fmt_opts:)
end

#set_attrObject



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

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

#showObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctl/cli/id_range.rb', line 45

def show
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id-range')

  cmd_opts = { name: args[0], pool: gopts[:pool] }
  fmt_opts = {
    layout: :rows,
    cols: param_selector.parse_option(opts[:output])
  }

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

  osctld_fmt(:id_range_show, cmd_opts:, fmt_opts:)
end

#table_listObject



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osctl/cli/id_range.rb', line 89

def table_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: TABLE_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id-range', optional: %w[type])

  types = %w[all allocated free]

  if args[1] && !types.include?(args[1])
    raise GLI::BadCommandLine, "type can be one of: #{types.join(', ')}"
  end

  cols = param_selector.parse_option(opts[:output])
  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  if sort
    sort.each do |v|
      cols << v unless cols.include?(v)
    end
  end

  cmd_opts = {
    pool: gopts[:pool],
    name: args[0],
    type: args[1] || 'all'
  }

  fmt_opts = {
    layout: :columns,
    cols:,
    sort:
  }

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

  osctld_fmt(:id_range_table_list, cmd_opts:, fmt_opts:)
end

#table_showObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/osctl/cli/id_range.rb', line 133

def table_show
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: TABLE_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id-range', 'block-index')

  cmd_opts = { name: args[0], pool: gopts[:pool], block_index: args[1].to_i }
  fmt_opts = {
    layout: :rows,
    cols: param_selector.parse_option(opts[:output])
  }

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

  osctld_fmt(:id_range_table_show, cmd_opts:, fmt_opts:)
end

#unset_attrObject



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

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