Module: Morpheus::Cli::LibraryHelper

Overview

Provides common methods for library management commands

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 7

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#api_clientObject



11
12
13
14
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 11

def api_client
  raise "#{self.class} has not defined @api_client" if @api_client.nil?
  @api_client
end

#find_container_type_by_id(layout_id, id) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 137

def find_container_type_by_id(layout_id, id)
  begin
    json_response = @library_container_types_interface.get(layout_id, id.to_i)
    return json_response['containerType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Instance Type not found by id #{id}"
    else
      raise e
    end
  end
end

#find_container_type_by_name(layout_id, name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 150

def find_container_type_by_name(layout_id, name)
  container_types = @library_container_types_interface.list(layout_id, {name: name.to_s})['containerTypes']
  if container_types.empty?
    print_red_alert "Node Type not found by name #{name}"
    return nil
  elsif container_types.size > 1
    print_red_alert "#{container_types.size} node types found by name #{name}"
    print_container_types_table(container_types, {color: red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return container_types[0]
  end
end

#find_container_type_by_name_or_id(layout_id, val) ⇒ Object

Container Types (Node Types)



129
130
131
132
133
134
135
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 129

def find_container_type_by_name_or_id(layout_id, val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_container_type_by_id(layout_id, val)
  else
    return find_container_type_by_name(layout_id, val)
  end
end

#find_instance_type_by_id(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 26

def find_instance_type_by_id(id)
  begin
    json_response = @library_instance_types_interface.get(id.to_i)
    return json_response['instanceType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Instance Type not found by id #{id}"
    else
      raise e
    end
  end
end

#find_instance_type_by_name(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 39

def find_instance_type_by_name(name)
  json_response = @library_instance_types_interface.list({name: name.to_s})
  instance_types = json_response['instanceTypes']
  if instance_types.empty?
    print_red_alert "Instance Type not found by name #{name}"
    return nil
  elsif instance_types.size > 1
    print_red_alert "#{instance_types.size} instance types found by name #{name}"
    print_instance_types_table(instance_types, {color: red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return instance_types[0]
  end
end

#find_instance_type_by_name_or_id(val) ⇒ Object

Instance Types



18
19
20
21
22
23
24
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 18

def find_instance_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_instance_type_by_id(val)
  else
    return find_instance_type_by_name(val)
  end
end

#find_option_type_by_id(id) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 257

def find_option_type_by_id(id)
  begin
    json_response = @option_types_interface.get(id.to_i)
    return json_response['optionType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Option Type not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_option_type_by_name(name) ⇒ Object



271
272
273
274
275
276
277
278
279
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 271

def find_option_type_by_name(name)
  json_results = @option_types_interface.list({name: name.to_s})
  if json_results['optionTypes'].empty?
    print_red_alert "Option Type not found by name #{name}"
    exit 1
  end
  option_type = json_results['optionTypes'][0]
  return option_type
end

#find_option_type_by_name_or_id(val) ⇒ Object

Option Types



249
250
251
252
253
254
255
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 249

def find_option_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_option_type_by_id(val)
  else
    return find_option_type_by_name(val)
  end
end

#find_option_type_list_by_id(id) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 515

def find_option_type_list_by_id(id)
  begin
    json_response = @option_type_lists_interface.get(id.to_i)
    return json_response['optionTypeList']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Option List not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_option_type_list_by_name(name) ⇒ Object



529
530
531
532
533
534
535
536
537
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 529

def find_option_type_list_by_name(name)
  json_results = @option_type_lists_interface.list({name: name.to_s})
  if json_results['optionTypeLists'].empty?
    print_red_alert "Option List not found by name #{name}"
    exit 1
  end
  option_type_list = json_results['optionTypeLists'][0]
  return option_type_list
end

#find_option_type_list_by_name_or_id(val) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 507

def find_option_type_list_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_option_type_list_by_id(val)
  else
    return find_option_type_list_by_name(val)
  end
end

#find_spec_template_by_id(id) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 348

def find_spec_template_by_id(id)
  begin
    json_response = @spec_templates_interface.get(id.to_i)
    return json_response['specTemplate']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Spec Template not found by id #{id}"
    else
      raise e
    end
  end
end

#find_spec_template_by_name(name) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 361

def find_spec_template_by_name(name)
  resource_specs = @spec_templates_interface.list({name: name.to_s})['specTemplates']
  if resource_specs.empty?
    print_red_alert "Spec Template not found by name #{name}"
    return nil
  elsif resource_specs.size > 1
    print_red_alert "#{resource_specs.size} spec templates found by name #{name}"
    print_resource_specs_table(resource_specs, {color: red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return resource_specs[0]
  end
end

#find_spec_template_by_name_or_id(val) ⇒ Object

Spec Template helper methods



340
341
342
343
344
345
346
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 340

def find_spec_template_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_spec_template_by_id(val)
  else
    return find_spec_template_by_name(val)
  end
end

#find_spec_template_type_by_id(id) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 389

def find_spec_template_type_by_id(id)
  begin
    json_response = @spec_template_types_interface.get(id.to_i)
    return json_response['networkType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Network Type not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_spec_template_type_by_name_or_code(name) ⇒ Object

def find_spec_template_type_by_name(name)

json_response = @spec_template_types_interface.list({name: name.to_s})
spec_template_types = json_response['networkTypes']
if spec_template_types.empty?
  print_red_alert "Network Type not found by name #{name}"
  return spec_template_types
elsif spec_template_types.size > 1
  print_red_alert "#{spec_template_types.size} network types found by name #{name}"
  rows = spec_template_types.collect do |it|
    {id: it['id'], name: it['name']}
  end
  puts as_pretty_table(rows, [:id, :name], {color:red})
  return nil
else
  return spec_template_types[0]
end

end



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 421

def find_spec_template_type_by_name_or_code(name)
  spec_template_types = get_all_spec_template_types().select { |it| name && it['code'] == name || it['name'] == name }
  if spec_template_types.empty?
    print_red_alert "Spec Template Type not found by code #{name}"
    return nil
  elsif spec_template_types.size > 1
    print_red_alert "#{spec_template_types.size} spec template types found by code #{name}"
    rows = spec_template_types.collect do |it|
      {id: it['id'], code: it['code'], name: it['name']}
    end
    print "\n"
    puts as_pretty_table(rows, [:id, :code, :name], {color:red})
    return nil
  else
    return spec_template_types[0]
  end
end

#find_spec_template_type_by_name_or_code_id(val) ⇒ Object



439
440
441
442
443
444
445
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 439

def find_spec_template_type_by_name_or_code_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_subnet_by_id(val)
  else
    return find_spec_template_type_by_name_or_code(val)
  end
end

#format_container_type_technology(container_type) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 183

def format_container_type_technology(container_type)
  if container_type
    container_type['provisionType'] ? container_type['provisionType']['name'] : ''
  else
    ""
  end
end

#format_instance_type_technology(instance_type) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 73

def format_instance_type_technology(instance_type)
  if instance_type
    instance_type['provisionTypeCode'].to_s.capitalize
  else
    ""
  end
end

#get_all_spec_template_typesObject



447
448
449
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 447

def get_all_spec_template_types
  @all_spec_template_types ||= @spec_template_types_interface.list({max: 1000})['specTemplateTypes'] || []
end

#load_balance_protocols_dropdownObject



81
82
83
84
85
86
87
88
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 81

def load_balance_protocols_dropdown
  [
    {'name' => 'None', 'value' => ''},
    {'name' => 'HTTP', 'value' => 'HTTP'},
    {'name' => 'HTTPS', 'value' => 'HTTPS'},
    {'name' => 'TCP', 'value' => 'TCP'}
  ]
end


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 166

def print_container_types_table(container_types, opts={})
  columns = [
    {"ID" => lambda {|it| it['id'] } },
    {"TECHNOLOGY" => lambda {|it| format_container_type_technology(it) } },
    {"NAME" => lambda {|it| it['name'] } },
    {"SHORT NAME" => lambda {|it| it['shortName'] } },
    {"VERSION" => lambda {|it| it['containerVersion'] } },
    {"LABELS" => lambda {|it| format_list(it['labels'], '', 3) } },
    {"CATEGORY" => lambda {|it| it['category'] } },
    {"OWNER" => lambda {|it| it['account'] ? it['account']['name'] : '' } }
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(container_types, columns, opts)
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 56

def print_instance_types_table(instance_types, opts={})
  columns = [
    {"ID" => lambda {|instance_type| instance_type['id'] } },
    {"NAME" => lambda {|instance_type| instance_type['name'] } },
    {"CODE" => lambda {|instance_type| instance_type['code'] } },
    {"TECHNOLOGY" => lambda {|instance_type| format_instance_type_technology(instance_type) } },
    {"LABELS" => lambda {|instance_type| format_list(instance_type['labels'], '', 3) } },
    {"CATEGORY" => lambda {|instance_type| instance_type['category'].to_s.capitalize } },
    {"FEATURED" => lambda {|instance_type| format_boolean instance_type['featured'] } },
    {"OWNER" => lambda {|instance_type| instance_type['account'] ? instance_type['account']['name'] : '' } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(instance_types, columns, opts)
end


377
378
379
380
381
382
383
384
385
386
387
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 377

def print_resource_specs_table(resource_specs, opts={})
  columns = [
    {"ID" => lambda {|resource_spec| resource_spec['id'] } },
    {"NAME" => lambda {|resource_spec| resource_spec['name'] } },
    #{"OWNER" => lambda {|resource_spec| resource_spec['account'] ? resource_spec['account']['name'] : '' } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(resource_specs, columns, opts)
end

#prompt_exposed_ports(options = {}, api_client = nil, api_params = {}) ⇒ Object

Prompts user for exposed ports array returns array of port objects



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
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 92

def prompt_exposed_ports(options={}, api_client=nil, api_params={})
  #puts "Configure ports:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))

  ports = []
  port_index = 0
  has_another_port = options[:options] && options[:options]["exposedPort#{port_index}"]
  add_another_port = has_another_port || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add an exposed port?"))
  while add_another_port do
    field_context = "exposedPort#{port_index}"

    port = {}
    #port['name'] ||= "Port #{port_index}"
    port_label = port_index == 0 ? "Port" : "Port [#{port_index+1}]"
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "#{port_label} Name", 'required' => false, 'description' => 'Choose a name for this port.', 'defaultValue' => port['name']}], options[:options])
    port['name'] = v_prompt[field_context]['name']

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'port', 'type' => 'number', 'fieldLabel' => "#{port_label} Number", 'required' => true, 'description' => 'A port number. eg. 8001', 'defaultValue' => (port['port'] ? port['port'].to_i : nil)}], options[:options])
    port['port'] = v_prompt[field_context]['port']

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'loadBalanceProtocol', 'type' => 'select', 'fieldLabel' => "#{port_label} LB", 'selectOptions' => load_balance_protocols_dropdown, 'required' => false, 'skipSingleOption' => true, 'description' => 'Choose a load balance protocol.', 'defaultValue' => port['loadBalanceProtocol']}], options[:options])
    port['loadBalanceProtocol'] = v_prompt[field_context]['loadBalanceProtocol']

    ports << port
    port_index += 1
    has_another_port = options[:options] && options[:options]["exposedPort#{port_index}"]
    add_another_port = has_another_port || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another exposed port?"))

  end


  return ports
end

#prompt_for_container_types(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



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
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 191

def prompt_for_container_types(params, options={}, api_client=nil, api_params={})
  # container_types
  container_type_list = nil
  container_type_ids = nil
  still_prompting = true
  if params['containerTypes'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'containerTypes', 'type' => 'text', 'fieldLabel' => 'Node Types', 'required' => false, 'description' => 'Node Types (Container Types) to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['containerTypes'].to_s.empty?
        container_type_list = v_prompt['containerTypes'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      container_type_ids = []
      bad_ids = []
      if container_type_list && container_type_list.size > 0
        container_type_list.each do |it|
          found_container_type = nil
          begin
            found_container_type = find_container_type_by_name_or_id(nil, it)
          rescue SystemExit => cmdexit
          end
          if found_container_type
            container_type_ids << found_container_type['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    container_type_list = params['containerTypes']
    still_prompting = false
    container_type_ids = []
    bad_ids = []
    if container_type_list && container_type_list.size > 0
      container_type_list.each do |it|
        found_container_type = nil
        begin
          found_container_type = find_container_type_by_name_or_id(nil, it)
        rescue SystemExit => cmdexit
        end
        if found_container_type
          container_type_ids << found_container_type['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Node Types not found: #{bad_ids}"}
    end
  end
  return {success:true, data: container_type_ids}
end

#prompt_for_option_types(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 281

def prompt_for_option_types(params, options={}, api_client=nil, api_params={})
  # option_types
  option_type_list = nil
  option_type_ids = nil
  still_prompting = true
  if params['optionTypes'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'optionTypes', 'type' => 'text', 'fieldLabel' => 'Option Types', 'required' => false, 'description' => 'Option Types to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['optionTypes'].to_s.empty?
        option_type_list = v_prompt['optionTypes'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      option_type_ids = []
      bad_ids = []
      if option_type_list && option_type_list.size > 0
        option_type_list.each do |it|
          found_option_type = nil
          begin
            found_option_type = find_option_type_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_option_type
            option_type_ids << found_option_type['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    option_type_list = params['optionTypes']
    still_prompting = false
    option_type_ids = []
    bad_ids = []
    if option_type_list && option_type_list.size > 0
      option_type_list.each do |it|
        found_option_type = nil
        begin
          found_option_type = find_option_type_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_option_type
          option_type_ids << found_option_type['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Option Types not found: #{bad_ids}"}
    end
  end
  return {success:true, data: option_type_ids}
end

#prompt_for_spec_templates(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 451

def prompt_for_spec_templates(params, options={}, api_client=nil, api_params={})
  # spec_templates
  spec_template_list = nil
  spec_template_ids = nil
  still_prompting = true
  if params['specTemplates'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'specTemplates', 'type' => 'text', 'fieldLabel' => 'Spec Templates', 'required' => false, 'description' => 'Spec Templates to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['specTemplates'].to_s.empty?
        spec_template_list = v_prompt['specTemplates'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      spec_template_ids = []
      bad_ids = []
      if spec_template_list && spec_template_list.size > 0
        spec_template_list.each do |it|
          found_spec_template = nil
          begin
            found_spec_template = find_spec_template_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_spec_template
            spec_template_ids << found_spec_template['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    spec_template_list = params['specTemplates']
    still_prompting = false
    spec_template_ids = []
    bad_ids = []
    if spec_template_list && spec_template_list.size > 0
      spec_template_list.each do |it|
        found_spec_template = nil
        begin
          found_spec_template = find_spec_template_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_spec_template
          spec_template_ids << found_spec_template['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Spec Templates not found: #{bad_ids}"}
    end
  end
  return {success:true, data: spec_template_ids}
end