Class: Morpheus::Cli::Library

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeLibrary

Returns a new instance of Library.



14
15
16
# File 'lib/morpheus/cli/library.rb', line 14

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#add(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/library.rb', line 120

def add(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = Morpheus::Cli::OptionTypes.prompt(add_instance_type_option_types, options[:options], @api_client, options[:params])
    instance_type_keys = ['name', 'description', 'category', 'visibility', 'environmentPrefix']
    instance_type_payload = params.select {|k,v| instance_type_keys.include?(k) }
    logo_file = nil
    if params['logo']
      filename = File.expand_path(params['logo'])
      if !File.exists?(filename)
        print_red_alert "File not found: #{filename}"
        exit 1
      end
      #instance_type_payload['logo'] = File.new(filename, 'rb')
      logo_file = File.new(filename, 'rb')
    end
    if params['hasAutoScale'] == 'on'
      instance_type_payload['hasAutoScale'] = true
    end
    if params['hasDeployment'] == 'on'
      instance_type_payload['hasDeployment'] = true
    end
    payload = {instanceType: instance_type_payload}
    if options[:dry_run]
      print_dry_run @custom_instance_types_interface.dry.create(payload)
      if logo_file
        print_dry_run @custom_instance_types_interface.dry.(":id", logo_file)
      end
      return
    end
    json_response = @custom_instance_types_interface.create(payload)

    if json_response['success']
      if logo_file
        begin
          @custom_instance_types_interface.(json_response['instanceType']['id'], logo_file)
        rescue RestClient::Exception => e
          print_red_alert "Failed to save logo!"
          print_rest_exception(e, options)
        end
      end
    end

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Added Instance Type #{instance_type_payload['name']}"
    unless options[:no_prompt]
      if ::Morpheus::Cli::OptionTypes::confirm("Add first version?", options)
        puts "\n"
        add_version(["code:#{json_response['code']}"])
      end
    end

    #list([])

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

#add_version(args) ⇒ Object



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
396
397
398
399
400
401
# File 'lib/morpheus/cli/library.rb', line 313

def add_version(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:options, :json])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    instance_type = find_custom_instance_type_by_name_or_code(args[0])
    exit 1 if instance_type.nil?

    #params = Morpheus::Cli::OptionTypes.prompt(add_version_option_types, options[:options], @api_client, options[:params])
    provision_types = @provision_types_interface.get({customSupported: true})['provisionTypes']
    if provision_types.empty?
      print_red_alert "No available provision types found!"
      exit 1
    end
    provision_type_options = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
    payload = {'containerType' => {}, 'instanceTypeLayout' => {}, 'instanceType' => {}}

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'instanceTypeLayout', 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true, 'description' => 'A name for this layout.'}], options[:options])
    payload['instanceTypeLayout']['name'] = v_prompt['instanceTypeLayout']['name']

    # shortName is only available for the first new version
    if !instance_type['versions'] || instance_type['versions'].size == 0
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'containerType', 'fieldName' => 'shortName', 'type' => 'text', 'fieldLabel' => 'Short Name', 'required' => true, 'description' => 'The short name is a lowercase name with no spaces used for display in your container list.'}], options[:options])
      payload['containerType']['shortName'] = v_prompt['containerType']['shortName']
    end

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'containerType', 'fieldName' => 'provisionTypeCode', 'type' => 'select', 'selectOptions' => provision_type_options, 'fieldLabel' => 'Technology', 'required' => true, 'description' => 'The type of container technology.'}], options[:options])
    payload['containerType']['provisionTypeCode'] = v_prompt['containerType']['provisionTypeCode']
    provision_type = provision_types.find {|it| it['code'] == payload['containerType']['provisionTypeCode'] }

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'instanceTypeLayout', 'fieldName' => 'instanceVersion', 'type' => 'text', 'fieldLabel' => 'Version', 'required' => true, 'description' => 'A Version Number eg. 0.0.1'}], options[:options])
    payload['instanceTypeLayout']['instanceVersion'] = v_prompt['instanceTypeLayout']['instanceVersion']

    custom_option_types = provision_type['customOptionTypes']

    if (!custom_option_types || custom_option_types.empty?)
      puts yellow,"Sorry, no options were found for #{provision_type['name']}.",reset
      exit 1
    end
    # prompt custom options for the selected provision type
    field_group_name = custom_option_types.first['fieldGroup'] || "#{provision_type['name']} Options"
    puts field_group_name
    puts "==============="
    v_prompt = Morpheus::Cli::OptionTypes.prompt(custom_option_types,options[:options],@api_client, {provisionTypCode: payload['provisionTypeCode']})

    if v_prompt['containerType']
      payload['containerType'].merge!(v_prompt['containerType'])
    end
    if v_prompt['containerType.config']
      payload['containerType']['config'] = v_prompt['containerType.config']
    end
    if v_prompt['instanceTypeLayout']
      payload['instanceTypeLayout'].merge!(v_prompt['instanceTypeLayout'])
    end
    # instanceType.backupType, which is not even persisted on the server?
    if v_prompt['instanceType']
      payload['instanceType'].merge!(v_prompt['instanceType'])
    end

    # puts "PAYLOAD:"
    # puts JSON.pretty_generate(payload)
    # puts "\nexiting early"
    # exit 0

    payload['exposedPorts'] = prompt_exposed_ports(options, @api_client)

    request_payload = payload
    json_response = @custom_instance_types_interface.create_version(instance_type['id'], request_payload)

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Added Instance Type Version #{instance_type['name']} - #{payload['instanceTypeLayout']['instanceVersion']}"
    #list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



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

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @custom_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).custom_instance_types
  @provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types
  @option_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_types
  @option_type_lists_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_type_lists
end

#get(args) ⇒ Object



77
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
# File 'lib/morpheus/cli/library.rb', line 77

def get(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end

  connect(options)
  begin
    if options[:dry_run]
      if args[0] =~ /code:/
        print_dry_run @custom_instance_types_interface.dry.list({code: args[0]})
      else
        print_dry_run @custom_instance_types_interface.dry.list({name: args[0]})
      end
      return
    end
    instance_type = find_custom_instance_type_by_name_or_code(args[0])
    exit 1 if instance_type.nil?

    if options[:json]
      print JSON.pretty_generate({instanceType: instance_type}), "\n"
      return
    end
    print_h1 "Custom Instance Type Details"
    versions = instance_type['versions'].join(', ')
    print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
    instance_type['instanceTypeLayouts'].each do |layout|
      print green, "     - #{layout['name']}\n",reset
    end
    print reset,"\n"

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

#handle(args) ⇒ Object



26
27
28
# File 'lib/morpheus/cli/library.rb', line 26

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/library.rb', line 31

def list(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :dry_run, :json])
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end

    if options[:dry_run]
      print_dry_run @custom_instance_types_interface.dry.list(params)
      return
    end

    json_response = @custom_instance_types_interface.list(params)

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    instance_types = json_response['instanceTypes']
    print_h1 "Morpheus Custom Instance Types"
    if instance_types.empty?
      puts yellow,"No instance types currently configured.",reset
    else
      instance_types.each do |instance_type|
        versions = instance_type['versions'].join(', ')
        print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
        instance_type['instanceTypeLayouts'].each do |layout|
          print green, "     - #{layout['name']}\n",reset
        end
      end
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_lists_add(args) ⇒ Object



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/morpheus/cli/library.rb', line 780

def option_lists_add(args)
  # JD: this is annoying because our option_types (for prompting and help)
  # are the same type of object being managed here.., options options options
  options = {}
  my_option_types = nil
  list_type = nil
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[type] [options]")
    opts.on( '-t', '--type TYPE', "Option List Type. (rest, manual)" ) do |val|
      list_type = val
      # options[:options] ||= {}
      # options[:options]['type'] = val
    end
    build_option_type_options(opts, options, new_option_type_list_option_types())
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  
  if !list_type
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Type', 'type' => 'select', 'selectOptions' => get_available_option_list_types, 'defaultValue' => 'rest', 'required' => true}], options[:options], @api_client, {})
    list_type = v_prompt['type']
  end

  connect(options)
  begin
    params = Morpheus::Cli::OptionTypes.prompt(new_option_type_list_option_types(list_type), options[:options], @api_client, options[:params])
    if params.key?('required')
      params['required'] = ['on','true'].include?(params['required'].to_s)
    end
    params['type'] = list_type
    list_payload = params
    payload = {'optionTypeList' => list_payload}
    if options[:dry_run]
      print_dry_run @option_type_lists_interface.dry.create(payload)
      return
    end
    json_response = @option_type_lists_interface.create(payload)
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end
    print_green_success "Added Option List #{list_payload['name']}"
    #option_lists_list([])
    option_type_list = json_response['optionTypeList']
    if option_type_list
      option_lists_get([option_type_list['id']])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_lists_get(args) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/morpheus/cli/library.rb', line 699

def option_lists_get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run])
    opts.footer = "This outputs details about a particular Option List."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end

  connect(options)
  begin
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @option_type_lists_interface.dry.get(args[0].to_i)
      else
        print_dry_run @option_type_lists_interface.dry.list({name: args[0]})
      end
      return
    end
    option_type_list = find_option_type_list_by_name_or_id(args[0])
    exit 1 if option_type_list.nil?

    if options[:json]
      print JSON.pretty_generate({optionTypeList: option_type_list}), "\n"
      return
    end

    print_h1 "Option List Details"
    print cyan
    if option_type_list['type'] == 'manual'
      print_description_list({
        "ID" => 'id',
        "Name" => 'name',
        "Description" => 'description',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
      }, option_type_list)
      print_h2 "Initial Dataset"
      print bright_black,"  #{option_type_list['initialDataset']}","\n",reset
    else
      print_description_list({
        "ID" => 'id',
        "Name" => 'name',
        "Description" => 'description',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        "Source URL" => 'sourceUrl',
        "Ignore SSL Errors" => lambda {|it| format_boolean it['ignoreSSLErrors'] },
        "Source Method" => lambda {|it| it['sourceMethod'].to_s.upcase },
      }, option_type_list)
      if !option_type_list['initialDataset'].empty?
        print_h2 "Initial Dataset"
        print bright_black,"  #{option_type_list['initialDataset']}","\n",reset
      end
      if !option_type_list['translationScript'].empty?
        print_h2 "Translation Script"
        print bright_black,"  #{option_type_list['translationScript']}","\n",reset
      end
    end

    print_h2 "List Items"
    if option_type_list['listItems']
      # puts "\tNAME\tVALUE"
      # option_type_list['listItems'].each do |list_item|
      #   puts "\t#{list_item['name']}\t#{list_item['value']}"
      # end
      print cyan
      tp option_type_list['listItems'], ['name', 'value']
    else
      puts "No data"
    end
    print reset,"\n"

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

#option_lists_list(args) ⇒ Object

Option Type List actions



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/morpheus/cli/library.rb', line 640

def option_lists_list(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :dry_run, :json])
    opts.footer = "This outputs a list of custom Option List records."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end

    if options[:dry_run]
      print_dry_run @option_type_lists_interface.dry.list(params)
      return
    end

    json_response = @option_type_lists_interface.list(params)

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    option_type_lists = json_response['optionTypeLists']
    print_h1 "Morpheus Option Lists"
    if option_type_lists.empty?
      print cyan,"No option lists found.",reset,"\n"
    else
      rows = option_type_lists.collect do |option_type_list|
        {
          id: option_type_list['id'],
          name: option_type_list['name'],
          description: option_type_list['description'],
          type: option_type_list['type'],
          size: option_type_list['listItems'] ? option_type_list['listItems'].size : ''
        }
      end
      print cyan
      tp rows, [
        :id,
        :name,
        :description,
        :type,
        :size
      ]
      print reset
      print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_lists_remove(args) ⇒ Object



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/morpheus/cli/library.rb', line 880

def option_lists_remove(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    option_type_list = find_option_type_list_by_name_or_id(args[0])
    exit 1 if option_type_list.nil?

    unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the option type #{option_type_list['name']}?", options)
      exit
    end
    if options[:dry_run]
      print_dry_run @option_type_lists_interface.dry.destroy(option_type_list['id'])
      return
    end
    json_response = @option_type_lists_interface.destroy(option_type_list['id'])

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Removed Option List #{option_type_list['name']}"
    #option_lists_list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_lists_update(args) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# File 'lib/morpheus/cli/library.rb', line 833

def option_lists_update(args)
  # JD: this is annoying because our option_types (for prompting and help)
  # are the same type of object being managed here.., options options options
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, update_option_type_list_option_types())
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  connect(options)
  begin
    option_type_list = find_option_type_list_by_name_or_id(args[0])
    exit 1 if option_type_list.nil?

    list_type = option_type_list['type']
    prompt_options = update_option_type_list_option_types(list_type)
    #params = options[:options] || {}
    params = Morpheus::Cli::OptionTypes.no_prompt(prompt_options, options[:options], @api_client, options[:params])
    if params.empty?
      print_red_alert "Specify atleast one option to update"
      puts optparse
      exit 1
    end
    if params.key?('required')
      params['required'] = ['on','true'].include?(params['required'].to_s)
    end
    list_payload = params
    payload = {optionTypeList: list_payload}
    if options[:dry_run]
      print_dry_run @option_type_lists_interface.dry.update(option_type_list['id'], payload)
      return
    end
    json_response = @option_type_lists_interface.update(option_type_list['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end
    print_green_success "Updated Option List #{list_payload['name']}"
    #option_lists_list([])
    option_lists_get([option_type_list['id']])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_types_add(args) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/morpheus/cli/library.rb', line 518

def option_types_add(args)
  # JD: this is annoying because our option_types (for prompting and help)
  # are the same type of object being managed here.., options options options
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[options]")
    build_option_type_options(opts, options, new_option_type_option_types)
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = Morpheus::Cli::OptionTypes.prompt(new_option_type_option_types, options[:options], @api_client, options[:params])
    if params.key?('required')
      params['required'] = ['on','true'].include?(params['required'].to_s)
    end
    option_type_payload = params
    payload = {optionType: option_type_payload}
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.create(payload)
      return
    end
    json_response = @option_types_interface.create(payload)
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end
    print_green_success "Added Option Type #{option_type_payload['name']}"
    #option_types_list([])
    option_types_get([option_type['id']])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_types_get(args) ⇒ Object



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
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/morpheus/cli/library.rb', line 468

def option_types_get(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end

  connect(options)
  begin
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @option_types_interface.dry.get(args[0].to_i)
      else
        print_dry_run @option_types_interface.dry.list({name: args[0]})
      end
      return
    end
    option_type = find_option_type_by_name_or_id(args[0])
    exit 1 if option_type.nil?

    if options[:json]
      print JSON.pretty_generate({optionType: option_type}), "\n"
      return
    end

    print_h1 "Option Type Details"
    print cyan
    print_description_list({
      "ID" => 'id',
      "Name" => 'name',
      "Description" => 'description',
      "Field Name" => 'fieldName',
      "Type" => lambda {|it| it['type'].to_s.capitalize },
      "Label" => 'fieldLabel',
      "Placeholder" => 'placeHolder',
      "Default Value" => 'defaultValue'
    }, option_type)
    print reset,"\n"

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

#option_types_list(args) ⇒ Object

Option Type actions



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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/morpheus/cli/library.rb', line 406

def option_types_list(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :dry_run, :json])
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end

    if options[:dry_run]
      print_dry_run @option_types_interface.dry.list(params)
      return
    end

    json_response = @option_types_interface.list(params)

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    option_types = json_response['optionTypes']
    print_h1 "Morpheus Option Types"
    if option_types.empty?
      print cyan,"No option types found.",reset,"\n"
    else
      rows = option_types.collect do |option_type|
        {
          id: option_type['id'],
          name: option_type['name'],
          type: option_type['type'],
          fieldLabel: option_type['fieldLabel'],
          fieldName: option_type['fieldName'],
          default: option_type['defaultValue'],
          required: option_type['required'] ? 'yes' : 'no'
        }
      end
      print cyan
      tp rows, [
        :id,
        :name,
        :type,
        {:fieldLabel => {:display_name => "Field Label"} },
        {:fieldName => {:display_name => "Field Name"} },
        :default,
        :required
      ]
      print reset
      print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_types_remove(args) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/morpheus/cli/library.rb', line 599

def option_types_remove(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    option_type = find_option_type_by_name_or_id(args[0])
    exit 1 if option_type.nil?

    unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the option type #{option_type['name']}?", options)
      exit
    end
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.destroy(option_type['id'])
      return
    end
    json_response = @option_types_interface.destroy(option_type['id'])

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Removed Option Type #{option_type['name']}"
    #list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#option_types_update(args) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/morpheus/cli/library.rb', line 554

def option_types_update(args)
  # JD: this is annoying because our option_types (for prompting and help)
  # are the same type of object being managed here.., options options options
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, update_option_type_option_types)
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  connect(options)
  begin
    option_type = find_option_type_by_name_or_id(args[0])
    exit 1 if option_type.nil?

    #params = options[:options] || {}
    params = Morpheus::Cli::OptionTypes.no_prompt(update_option_type_option_types, options[:options], @api_client, options[:params])
    if params.empty?
      print_red_alert "Specify atleast one option to update"
      puts optparse
      exit 1
    end
    if params.key?('required')
      params['required'] = ['on','true'].include?(params['required'].to_s)
    end
    option_type_payload = params
    payload = {optionType: option_type_payload}
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.update(option_type['id'], payload)
      return
    end
    json_response = @option_types_interface.update(option_type['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end
    print_green_success "Updated Option Type #{option_type_payload['name']}"
    #option_types_list([])
    option_types_get([option_type['id']])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



274
275
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
# File 'lib/morpheus/cli/library.rb', line 274

def remove(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    instance_type = find_custom_instance_type_by_name_or_code(args[0])
    exit 1 if instance_type.nil?

    unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the instance type #{instance_type['name']}?", options)
      exit
    end
    if options[:dry_run]
      print_dry_run @custom_instance_types_interface.dry.destroy(instance_type['id'])
      return
    end
    json_response = @custom_instance_types_interface.destroy(instance_type['id'])

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Removed Instance Type #{instance_type['name']}"
    #list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/library.rb', line 190

def update(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    instance_type = find_custom_instance_type_by_name_or_code(args[0])
    exit 1 if instance_type.nil?
    # option_types = update_instance_type_option_types(instance_type)
    # params = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options], @api_client, options[:params])
    params = options[:options] || {}
    instance_type_keys = ['name', 'description', 'category', 'visibility', 'environmentPrefix']
    instance_type_payload = params.select {|k,v| instance_type_keys.include?(k) }
    logo_file = nil
    if params['logo']
      filename = File.expand_path(params['logo'])
      if !File.exists?(filename)
        print_red_alert "File not found: #{filename}"
        exit 1
      end
      #instance_type_payload['logo'] = File.new(filename, 'rb')
      logo_file = File.new(filename, 'rb')
    end
    if params['hasAutoScale'] == 'on'
      instance_type_payload['hasAutoScale'] = true
    elsif params['hasAutoScale'] == 'off'
      instance_type_payload['hasAutoScale'] = false
    end
    if params['hasDeployment'] == 'on'
      instance_type_payload['hasDeployment'] = true
    elsif params['hasDeployment'] == 'off'
      instance_type_payload['hasDeployment'] = false
    end
    if instance_type_payload.empty? && logo_file.nil?
      puts optparse
      option_lines = update_instance_type_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
      puts "\nAvailable Options:\n#{option_lines}\n\n"
      exit 1
    end
    if instance_type_payload.empty?
      # just updating logo (separate request)
      instance_type_payload['name'] = instance_type['name']
    end
    payload = {instanceType: instance_type_payload}
    if options[:dry_run]
      print_dry_run @custom_instance_types_interface.dry.update(instance_type['id'], payload)
      if logo_file
        print_dry_run @custom_instance_types_interface.dry.(":id", logo_file)
      end
      return
    end
    json_response = @custom_instance_types_interface.update(instance_type['id'], payload)

    if json_response['success']
      if logo_file
        begin
          @custom_instance_types_interface.(json_response['instanceType']['id'], logo_file)
        rescue RestClient::Exception => e
          print_red_alert "Failed to save logo!"
          print_rest_exception(e, options)
        end
      end
    end

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Updated Instance Type #{instance_type_payload['name']}"
    #list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end