Class: Morpheus::Cli::LibraryContainerTemplatesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, LibraryHelper
Defined in:
lib/morpheus/cli/commands/library_container_templates_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from LibraryHelper

#api_client, #find_container_type_by_id, #find_container_type_by_name, #find_container_type_by_name_or_id, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_option_type_by_id, #find_option_type_by_name, #find_option_type_by_name_or_id, #find_option_type_list_by_id, #find_option_type_list_by_name, #find_option_type_list_by_name_or_id, #find_spec_template_by_id, #find_spec_template_by_name, #find_spec_template_by_name_or_id, #find_spec_template_type_by_id, #find_spec_template_type_by_name_or_code, #find_spec_template_type_by_name_or_code_id, #format_container_type_technology, #format_instance_type_technology, #get_all_spec_template_types, included, #load_balance_protocols_dropdown, #print_container_types_table, #print_instance_types_table, #print_resource_specs_table, #prompt_exposed_ports, #prompt_for_container_types, #prompt_for_option_types, #prompt_for_spec_templates

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_payload, #build_standard_add_options, #build_standard_api_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, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_parameter_as_resource_id!, #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

#_get(id, options) ⇒ Object



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 105

def _get(id, options)

  begin
    container_template = find_container_template_by_name_or_id(id)
    if container_template.nil?
      return 1
    end
    @container_templates_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_templates_interface.dry.get(container_template['id'])
      return
    end
    json_response = @container_templates_interface.get(container_template['id'])
    container_template = json_response['containerTemplate']
    instances = json_response['instances'] || []
    servers = json_response['servers'] || []
    if options[:json]
      puts as_json(json_response, options, "containerTemplate")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "containerTemplate")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['containerTemplate']], options)
      return 0
    end

    print_h1 "File Template Details"
    print cyan
    description_cols = {
      "ID" => lambda {|it| it['id'] },
      "Name" => lambda {|it| it['name'] },
      "Labels" => lambda {|it| format_list(it['labels']) rescue '' },
      "File Name" => lambda {|it| it['fileName'] },
      "File Path" => lambda {|it| it['filePath'] },
      "Phase" => lambda {|it| it['templatePhase'] },
      "File Owner" => lambda {|it| it['fileOwner'] },
      "Setting Name" => lambda {|it| it['settingName'] },
      "Setting Category" => lambda {|it| it['settingCategory'] },
      "Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
      # "Enabled" => lambda {|it| format_boolean it['enabled'] },
      "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }
    }
    print_description_list(description_cols, container_template)

    print_h2 "Template"

    puts container_template['template']

    

    print reset,"\n"

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#add(args) ⇒ Object



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
203
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
275
276
277
278
279
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 165

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on('--name VALUE', String, "Name") do |val|
      params['name'] = val
    end
    opts.on('-l', '--labels [LIST]', String, "Labels") do |val|
      params['labels'] = parse_labels(val)
    end
    opts.on('--file-name VALUE', String, "File Name") do |val|
      params['fileName'] = val
    end
    opts.on('--fileName VALUE', String, "File Name") do |val|
      params['fileName'] = val
    end
    opts.add_hidden_option('--fileName')
    opts.on('--file-path VALUE', String, "File Path") do |val|
      params['filePath'] = val
    end
    opts.on('--filePath VALUE', String, "File Path") do |val|
      params['filePath'] = val
    end
    opts.add_hidden_option('--filePath')
    opts.on('--phase [preProvision|provision|postProvision]', String, "Template Phase. Default is 'provision'") do |val|
      params['templatePhase'] = val
    end
    opts.on('--template TEXT', String, "Contents of the template.") do |val|
      params['template'] = val
    end
    opts.on('--file FILE', "File containing the template. This can be used instead of --template" ) do |filename|
      full_filename = File.expand_path(filename)
      if File.exist?(full_filename)
        params['template'] = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        exit 1
      end
      # use the filename as the name by default.
      if !params['name']
        params['name'] = File.basename(full_filename)
      end
      if !params['fileName']
        params['fileName'] = File.basename(full_filename)
      end
      # if !params['filePath']
      #   params['filePath'] = File.dirname(full_filename)
      # end
    end
    opts.on('--file-owner VALUE', String, "File Owner") do |val|
      params['fileOwner'] = val
    end
    opts.on('--fileOwner VALUE', String, "File Owner") do |val|
      params['fileOwner'] = val
    end
    opts.add_hidden_option('--fileOwner')
    opts.on('--setting-name VALUE', String, "Setting Name") do |val|
      params['settingName'] = val
    end
    opts.on('--settingName VALUE', String, "Setting Name") do |val|
      params['settingName'] = val
    end
    opts.add_hidden_option('--settingName')
    opts.on('--setting-category VALUE', String, "Setting Category") do |val|
      params['settingCategory'] = val
    end
    opts.on('--settingCategory VALUE', String, "Setting Category") do |val|
      params['settingCategory'] = val
    end
    opts.add_hidden_option('--settingCategory')
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote, :quiet])
    opts.footer = "Create a new file template." + "\n" +
                  "[name] is required and can be passed as --name instead."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, max:1)
  # support [name] as first argument
  if args[0]
    params['name'] = args[0]
  end
  connect(options)
  begin
    payload = nil
    arbitrary_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    create_payload = {}
    create_payload.deep_merge!(params)
    create_payload.deep_merge!(arbitrary_options)
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'containerTemplate' => create_payload}) unless create_payload.empty?
    else
      prompt_result = Morpheus::Cli::OptionTypes.prompt(add_container_template_option_types, params.deep_merge(options[:options] || {}), @api_client)
      create_payload.deep_merge!(prompt_result)
      payload = {'containerTemplate' => create_payload}
    end
    @container_templates_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_templates_interface.dry.create(payload)
      return
    end
    json_response = @container_templates_interface.create(payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      container_template = json_response['containerTemplate']
      print_green_success "Added file template #{container_template['name']}"
      _get(container_template['id'], {})
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#connect(opts) ⇒ Object



11
12
13
14
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 11

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @container_templates_interface = @api_client.library_container_templates
end

#get(args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 87

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return 1
  end
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  end
end

#handle(args) ⇒ Object



16
17
18
# File 'lib/morpheus/cli/commands/library_container_templates_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
77
78
79
80
81
82
83
84
85
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 20

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on('-l', '--labels LABEL', String, "Filter by labels, can match any of the values") do |val|
      add_query_parameter(params, 'labels', parse_labels(val))
    end
    opts.on('--all-labels LABEL', String, "Filter by labels, must match all of the values") do |val|
      add_query_parameter(params, 'allLabels', parse_labels(val))
    end
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :dry_run, :remote])
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  connect(options)
  begin
    [:phrase, :offset, :max, :sort, :direction, :lastUpdated].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    @container_templates_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_templates_interface.dry.list(params)
      return
    end

    json_response = @container_templates_interface.list(params)
    if options[:json]
      puts as_json(json_response, options, "containerTemplates")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['containerTemplates'], options)
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "containerTemplates")
      return 0
    end
    container_templates = json_response['containerTemplates']
    title = "Morpheus Library - File Templates"
    subtitles = []
    # if group
    #   subtitles << "Group: #{group['name']}".strip
    # end
    # if cloud
    #   subtitles << "Cloud: #{cloud['name']}".strip
    # end
    if params[:phrase]
      subtitles << "Search: #{params[:phrase]}".strip
    end
    print_h1 title, subtitles
    if container_templates.empty?
      print cyan,"No container file templates found.",reset,"\n"
    else
      print_container_templates_table(container_templates, options)
      print_results_pagination(json_response, {:label => "container file template", :n_label => "container file templates"})
      # print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove(args) ⇒ Object



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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 397

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :auto_confirm])
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)

  begin
    container_template = find_container_template_by_name_or_id(args[0])
    if container_template.nil?
      return 1
    end

    unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete file template '#{container_template['name']}'?", options)
      return false
    end

    # payload = {
    #   'containerTemplate' => {id: container_template["id"]}
    # }
    # payload['containerTemplate'].merge!(container_template)
    payload = params
    @container_templates_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_templates_interface.dry.destroy(container_template["id"])
      return
    end

    json_response = @container_templates_interface.destroy(container_template["id"])
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Deleted file template #{container_template['name']}"
    end
    return 0, nil
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#update(args) ⇒ Object



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
316
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
370
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
# File 'lib/morpheus/cli/commands/library_container_templates_command.rb', line 282

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on('--name VALUE', String, "Name") do |val|
      params['name'] = val
    end
    opts.on('-l', '--labels [LIST]', String, "Labels") do |val|
      params['labels'] = parse_labels(val)
    end
    opts.on('--file-name VALUE', String, "File Name") do |val|
      params['fileName'] = val
    end
    opts.on('--fileName VALUE', String, "File Name") do |val|
      params['fileName'] = val
    end
    opts.add_hidden_option('--fileName')
    opts.on('--file-path VALUE', String, "File Path") do |val|
      params['filePath'] = val
    end
    opts.on('--filePath VALUE', String, "File Path") do |val|
      params['filePath'] = val
    end
    opts.add_hidden_option('--filePath')
    opts.on('--phase [preProvision|provision|postProvision]', String, "Template Phase. Default is 'provision'") do |val|
      params['templatePhase'] = val
    end
    opts.on('--template TEXT', String, "Contents of the template.") do |val|
      params['template'] = val
    end
    opts.on('--file FILE', "File containing the template. This can be used instead of --template" ) do |filename|
      full_filename = File.expand_path(filename)
      if File.exist?(full_filename)
        params['template'] = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        exit 1
      end
    end
    opts.on('--file-owner VALUE', String, "File Owner") do |val|
      params['fileOwner'] = val
    end
    opts.on('--fileOwner VALUE', String, "File Owner") do |val|
      params['fileOwner'] = val
    end
    opts.add_hidden_option('--fileOwner')
    opts.on('--setting-name VALUE', String, "Setting Name") do |val|
      params['settingName'] = val
    end
    opts.on('--settingName VALUE', String, "Setting Name") do |val|
      params['settingName'] = val
    end
    opts.add_hidden_option('--settingName')
    opts.on('--setting-category VALUE', String, "Setting Category") do |val|
      params['settingCategory'] = val
    end
    opts.on('--settingCategory VALUE', String, "Setting Category") do |val|
      params['settingCategory'] = val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote, :quiet])
    opts.footer = "Update a file template." + "\n" +
                  "[name] is required. This is the name or id of a file template."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  begin
    container_template = find_container_template_by_name_or_id(args[0])
    if container_template.nil?
      return 1
    end

    payload = nil
    arbitrary_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    update_payload = {}
    update_payload.deep_merge!(params)
    update_payload.deep_merge!(arbitrary_options)
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'containerTemplate' => update_payload}) unless update_payload.empty?
    else
      prompt_result = Morpheus::Cli::OptionTypes.no_prompt(update_container_template_option_types, params.deep_merge(options[:options] || {}), @api_client)
      update_payload.deep_merge!(prompt_result)
      payload = {'containerTemplate' => update_payload}
    end

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      # merge -O options into normally parsed options
      params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
      payload = {'containerTemplate' => params}
    end
    @container_templates_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_templates_interface.dry.update(container_template["id"], payload)
      return
    end
    json_response = @container_templates_interface.update(container_template["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Updated file template #{container_template['name']}"
      _get(container_template['id'], {})
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end