Class: Morpheus::Cli::ProvisioningLicensesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/provisioning_licenses_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#add(args) ⇒ Object



143
144
145
146
147
148
149
150
151
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 143

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    opts.on( '-t', '--type TYPE', "License Type Code eg. win" ) do |val|
      options[:options]['licenseType'] ||= val
    end
    opts.add_hidden_option('--licenseType')
    build_option_type_options(opts, options, add_license_option_types)
    build_standard_add_options(opts, options)
    opts.footer = "Create license."
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  if args[0]
    options[:options]['name'] ||= args[0]
  end
  connect(options)
  begin
    # construct payload
    passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'license' => passed_options}) unless passed_options.empty?
    else
      payload = {
        'license' => {
        }
      }
      # allow arbitrary -O options
      payload.deep_merge!({'license' => passed_options}) unless passed_options.empty?
      v_prompt = Morpheus::Cli::OptionTypes.prompt(add_license_option_types, options[:options], @api_client)
      params.deep_merge!(v_prompt)
      payload.deep_merge!({'license' => params}) unless params.empty?
    end

    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_licenses_interface.dry.create(payload)
      return
    end
    json_response = @provisioning_licenses_interface.create(payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      display_name = json_response['license']  ? json_response['license']['name'] : ''
      print_green_success "License #{display_name} added"
      get([json_response['license']['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 8

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @provisioning_licenses_interface = @api_client.provisioning_licenses
  @provisioning_license_types_interface = @api_client.provisioning_license_types
  @virtual_images_interface = @api_client.virtual_images
  @options_interface = @api_client.options
end

#get(args) ⇒ Object



78
79
80
81
82
83
84
85
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 78

def get(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[license]")
    build_standard_get_options(opts, options)
    opts.footer = "Get details about a license.\n[license] is required. License ID or name"
  end
  optparse.parse!(args)

  if args.count < 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(', ')}\n#{optparse}"
  end

  connect(options)
  
  begin
    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @provisioning_licenses_interface.dry.get(args[0], params)
      else
        print_dry_run @provisioning_licenses_interface.dry.list({name: args[0].to_s})
      end
      return 0
    end
    license = find_license_by_name_or_id(args[0])
    return 1 if license.nil?
    # skip reload if already fetched via get(id)
    json_response = {'license' => license}
    if args[0].to_s != license['id'].to_s
      json_response = @provisioning_licenses_interface.get(license['id'], params)
      license = json_response['license']
    end
    render_result = render_with_format(json_response, options, 'license')
    return 0 if render_result

    
    print_h1 "License Details"
    print cyan
    columns = [
      {"ID" => lambda {|license| license['id'] } },
      {"Name" => lambda {|license| license['name'] } },
      {"License Type" => lambda {|license| license['licenseType']['name'] rescue license['licenseType'] } },
      {"License Key" => lambda {|license| license['licenseKey'] } },
      {"Org Name" => lambda {|license| license['orgName'] } },
      {"Full Name" => lambda {|license| license['fullName'] } },
      {"Version" => lambda {|license| license['licenseVersion'] } },
      {"Description" => lambda {|license| license['description'] } },
      {"Copies" => lambda {|license| 
        "#{license['reservationCount']}/#{license['copies']}"
      } },
      {"Description" => lambda {|license| license['description'] } },
      {"Virtual Images" => lambda {|it| it['virtualImages'] ? it['virtualImages'].collect {|v| v['name']}.join(', ') : '' } },
      {"Tenants" => lambda {|it| it['tenants'] ? it['tenants'].collect {|acnt| acnt['name']}.join(', ') : '' } },
    ]
    print_description_list(columns, license, options)
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



16
17
18
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 16

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
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
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 20

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_list_options(opts, options)
    opts.footer = "List licenses."
  end
  optparse.parse!(args)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_licenses_interface.dry.list(params)
      return 0
    end
    json_response = @provisioning_licenses_interface.list(params)
    render_result = render_with_format(json_response, options, 'licenses')
    return 0 if render_result
    licenses = json_response['licenses']

    title = "Morpheus Licenses"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if licenses.empty?
      print cyan,"No licenses found.",reset,"\n"
    else
      columns = [
        {"ID" => lambda {|license| license['id'] } },
        {"NAME" => lambda {|license| license['name'] } },
        {"LICENSE TYPE" => lambda {|license| license['licenseType']['name'] rescue license['licenseType'] } },
        {"VERSION" => lambda {|license| license['licenseVersion'] } },
        {"COPIES" => lambda {|license| 
          "#{license['reservationCount']}/#{license['copies']}"
        } },
        {"VIRTUAL IMAGES" => lambda {|it| it['virtualImages'] ? it['virtualImages'].collect {|v| v['name']}.join(', ') : '' } },
        {"TENANTS" => lambda {|it| it['tenants'] ? it['tenants'].collect {|acnt| acnt['name']}.join(', ') : '' } },
      ]
      if options[:include_fields]
        columns = options[:include_fields]
      end
      print as_pretty_table(licenses, columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
    
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#list_types(args) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 371

def list_types(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_list_options(opts, options)
    opts.footer = "List license types."
  end
  optparse.parse!(args)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    @provisioning_license_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_license_types_interface.dry.list(params)
      return 0
    end
    json_response = @provisioning_license_types_interface.list(params)
    render_result = render_with_format(json_response, options, 'licenseTypes')
    return 0 if render_result
    license_types = json_response['licenseTypes']

    title = "Morpheus License Types"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if license_types.empty?
      print cyan,"No license types found.",reset,"\n"
    else
      columns = [
        {"ID" => lambda {|it| it['id'] } },
        {"NAME" => lambda {|it| it['name'] } },
        {"CODE" => lambda {|it| it['code'] } },
      ]
      if options[:include_fields]
        columns = options[:include_fields]
      end
      print as_pretty_table(license_types, columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
    
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 276

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_standard_remove_options(opts, options)
    opts.footer = "Delete license.\n[license] is required. License ID or name"
  end
  optparse.parse!(args)

  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end

  connect(options)
  begin
    license = find_license_by_name_or_id(args[0])
    return 1 if license.nil?

    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the license #{license['name']}?")
      return 9, "aborted command"
    end
    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_licenses_interface.dry.destroy(license['id'])
      return
    end
    json_response = @provisioning_licenses_interface.destroy(license['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "License #{license['name']} removed"
      # list([] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#reservations(args) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 317

def reservations(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_standard_list_options(opts, options)
    opts.footer = "List reservations for a license.\n[license] is required. License ID or name"
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    license = find_license_by_name_or_id(args[0])
    return 1 if license.nil?
    params.merge!(parse_list_options(options))
    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_licenses_interface.dry.reservations(license['id'], params)
      return 0
    end
    json_response = @provisioning_licenses_interface.reservations(license['id'], params)
    render_result = render_with_format(json_response, options, 'reservations')
    return 0 if render_result
    reservations = json_response['reservations']
    
    title = "License Reservations: [#{license['id']}] #{license['name']}"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if reservations.empty?
      print cyan,"No reservations found.",reset,"\n"
    else
      columns = [
        #{"ID" => lambda {|it| it['id'] } },
        {"RESOURCE ID" => lambda {|it| it['resourceId'] } },
        {"RESOURCE TYPE" => lambda {|it| it['resourceType'] } },
      ]
      if options[:include_fields]
        columns = options[:include_fields]
      end
      print as_pretty_table(reservations, columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
    
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/morpheus/cli/provisioning_licenses_command.rb', line 204

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[license] [options]")
    build_option_type_options(opts, options, update_license_option_types)
    build_standard_update_options(opts, options)
    opts.footer = "Update license.\n[license] is required. License ID or name"
  end
  optparse.parse!(args)

  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end

  connect(options)
  begin

    license = find_license_by_name_or_id(args[0])
    return 1 if license.nil?

    # construct payload
    passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'license' => passed_options}) unless passed_options.empty?
    else
      payload = {
        'license' => {
        }
      }
      # allow arbitrary -O options
      # virtual_images = passed_options.delete('virtualImages')
      # tenants = passed_options.delete('tenants')
      payload.deep_merge!({'license' => passed_options}) unless passed_options.empty?
      # prompt for options
      #params = Morpheus::Cli::OptionTypes.prompt(update_license_option_types, options[:options], @api_client, options[:params])
      v_prompt = Morpheus::Cli::OptionTypes.prompt(update_license_option_types, options[:options].merge(:no_prompt => true), @api_client)
      params.deep_merge!(v_prompt)
      
      # if !virtual_images.empty?
      #   params['virtualImages'] = virtual_images # split(",") and lookup ?
      # end
      
      payload.deep_merge!({'license' => params}) unless params.empty?

      if payload.empty? || payload['license'].empty?
        raise_command_error "Specify at least one option to update.\n#{optparse}"
      end
    end
    @provisioning_licenses_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_licenses_interface.dry.update(license['id'], payload)
      return
    end
    json_response = @provisioning_licenses_interface.update(license['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      display_name = json_response['license'] ? json_response['license']['name'] : ''
      print_green_success "License #{display_name} updated"
      get([json_response['license']['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end