Class: Morpheus::Cli::IntegrationsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #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, #execute_api, #execute_api_payload, #execute_api_request, #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_each_payload, #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_options, #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, params, options) ⇒ Object



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
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
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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 101

def _get(id, params, options)
  integration = nil
  if id.to_s !~ /\A\d{1,}\Z/
    integration = find_integration_by_name_or_id(id)
    return 1, "integration not found for #{id}" if integration.nil?
    id = integration['id']
  end
  if options[:show_objects]
    params['objects'] = true
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.get(id, params)
    return
  end
  json_response = @integrations_interface.get(id, params)
  integration = json_response[integration_object_key]
  render_response(json_response, options, integration_object_key) do
    print_h1 "Integration Details", [], options
    print cyan
    show_columns = {
      "ID" => 'id',
      "Name" => 'name',
      "Type" => lambda {|it| format_integration_type(it) },
      "URL" => lambda {|it| it['url'] },
      "Host" => lambda {|it| it['host'] },
      "Port" => lambda {|it| it['port'] },
      "Username" => lambda {|it| it['username'] },
      "Password" => lambda {|it| it['password'] },
      "Token" => lambda {|it| it['token'] },
      "Service Key" => lambda {|it| it['serviceKey'] ? it['serviceKey']['name'] : nil },
      "Auth Key" => lambda {|it| it['authKey'] ? it['authKey']['name'] : nil },
      "Enabled" => lambda {|it| format_boolean(it['enabled']) },
      "Status Date" => lambda {|it| format_local_dt(it['statusDate']) },
      "Status" => lambda {|it| format_integration_status(it) },
      # "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      # "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
    }
    show_columns.delete("URL") if integration['url'].nil?
    show_columns.delete("Host") if integration['host'].nil?
    show_columns.delete("Port") if integration['port'].nil?
    show_columns.delete("Password") if integration['password'].nil?
    show_columns.delete("Token") if integration['token'].nil?
    show_columns.delete("Service Key") if integration['serviceKey'].nil?
    show_columns.delete("Auth Key") if integration['authKey'].nil?
    print_description_list(show_columns, integration, options)

    if options[:show_objects]
      # they are loaded above with ?objects=true
      integration_objects = integration['objects']
      if integration_objects.nil?
        objects_json_response = @integrations_interface.list_objects(integration['id'], {})
        integration_objects = objects_json_response[integration_object_list_key]
      end
      cloud_objects = integration_objects.select {|it| it['refType'] == "ComputeZone" }
      library_objects = integration_objects.select {|it| it['refType'] == "InstanceTypeLayout" || it['refType'] == "InstanceType" }
      blueprint_objects = integration_objects.select {|it| it['refType'] == "AppTemplate" }
      catalog_objects = integration_objects.select {|it| it['refType'] == "CatalogItemType" }
      if integration_objects.empty?
        print reset,"\n"
        print cyan,"No objects found.",reset,"\n"
      else
        # Exposed Clouds
        if !cloud_objects.empty?
          print_h2 "Exposed Clouds", [], options
          list_columns = {
            # "ID" => 'id',
            "Name" => 'name',
            # "Category" => 'category',
            # "Ref Type" => 'refType',
            # "Cloud ID" => 'refId',
            "Group" => lambda {|it| it['group']['name'] rescue nil },
          }.upcase_keys!
          print as_pretty_table(cloud_objects, list_columns, options)
        end

        # Exposed Libraries
        if !library_objects.empty?
          # print_h2 "Exposed Libraries", [], options
          print_h2 "Exposed Layouts", [], options
          list_columns = {
            # "ID" => 'id',
            "Name" => 'name',
            "Version" => lambda {|it| it['layout']['instanceVersion'] rescue nil },
            "Instance Type" => lambda {|it| it['layout']['instanceType']['name'] rescue nil },
            "Provision Type" => lambda {|it| it['layout']['provisionType']['name'] rescue nil },
          }.upcase_keys!
          print as_pretty_table(library_objects, list_columns, options)
        end

        # Exposed Blueprints
        if !blueprint_objects.empty?
          print_h2 "Exposed Blueprints", [], options
          list_columns = {
            # "ID" => 'id',
            "Name" => 'name',
            # "Type" => lambda {|it| it['blueprint']['type'] rescue nil },
            "Blueprint" => lambda {|it| it['blueprint']['name'] rescue nil },
            "Group" => lambda {|it| it['group']['name'] rescue nil },
          }.upcase_keys!
          print as_pretty_table(blueprint_objects, list_columns, options)
        end

        # Exposed Catalog Items
        if !catalog_objects.empty?
          print_h2 "Exposed Catalog Items", [], options
          list_columns = {
            # "ID" => 'id',
            "Name" => 'name',
          }.upcase_keys!
          print as_pretty_table(catalog_objects, list_columns, options)
        end
      end
    end
    print reset,"\n"
  end
  return 0, nil
end

#_get_inventory(integration, id, params, options) ⇒ Object



1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 1014

def _get_inventory(integration, id, params, options)
  integration_object = nil
  if id.to_s !~ /\A\d{1,}\Z/
    integration_inventory = find_integration_inventory_by_name_or_id(integration['id'], id)
    return 1, "integration inventory not found for #{id}" if integration_inventory.nil?
    id = integration_inventory['id']
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.get_inventory(integration['id'], id, params)
    return
  end
  json_response = @integrations_interface.get_inventory(integration['id'], id, params)
  integration_inventory = json_response[integration_inventory_object_key]
  render_response(json_response, options, integration_inventory_object_key) do
    print_h1 "Integration Inventory Details", [], options
    print cyan
    show_columns = {
      "ID" => 'id',
      "Name" => 'name',
      "Description" => 'description',
      "Tenant Default" => lambda {|it| (format_list(it['tenants'].collect {|t| t['name'] }) rescue "") },
    }
    print_description_list(show_columns, integration_inventory, options)
    print reset,"\n"
  end
  return 0, nil
end

#_get_object(integration, id, params, options) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
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
698
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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 624

def _get_object(integration, id, params, options)
  integration_object = nil
  if id.to_s !~ /\A\d{1,}\Z/
    integration_object = find_integration_object_by_name_or_id(integration['id'], id)
    return 1, "integration object not found for #{id}" if integration_object.nil?
    id = integration_object['id']
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.get_object(integration['id'], id, params)
    return
  end
  json_response = @integrations_interface.get_object(integration['id'], id, params)
  integration_object = json_response[integration_object_object_key]
  config = integration_object['config']
  # export just the config as json (default) or yaml
  if options[:show_config]
    unless options[:json] || options[:yaml] || options[:csv]
      options[:json] = :true
    end
    return render_with_format(config, options)
  end
  render_response(json_response, options, integration_object_object_key) do
    print_h1 "Integration Object Details", [], options
    print cyan
    if integration_object['type'] == 'cloud'
      show_columns = {
        "Integration" => lambda {|it| integration['name'] },
        "Object ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        # "Cloud" => lambda {|it| it['cloud']['name'] rescue nil },
        # "Ref Type" => 'refType',
        "Ref ID" => 'refId',
        # "Reference" => lambda {|it| ("#{it['refType']}: #{it['refId']}" rescue nil) },
        "Group" => lambda {|it| it['group']['name'] rescue nil },
      }
      print_description_list(show_columns, integration_object, options)
      print reset,"\n"
    elsif integration_object['type'] == 'layout'
      show_columns = {
        "Integration" => lambda {|it| integration['name'] },
        "Object ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        # "Layout" => lambda {|it| it['layout']['name'] rescue nil },
        # "Ref Type" => 'refType',
        "Ref ID" => 'refId',
        # "Reference" => lambda {|it| ("#{it['refType']}: #{it['refId']}" rescue nil) },
        "Provision Type" => lambda {|it| it['layout']['provisionType']['name'] rescue nil },
        "Instance Type" => lambda {|it| it['layout']['instanceType']['name'] rescue nil },
        "Version" => lambda {|it| it['layout']['instanceVersion'] rescue nil },
      }
      print_description_list(show_columns, integration_object, options)
      print reset,"\n"
    elsif integration_object['type'] == 'blueprint'
      show_columns = {
        "Integration" => lambda {|it| integration['name'] },
        "Object ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        # "Ref Type" => 'refType',
        "Ref ID" => 'refId',
        # "Reference" => lambda {|it| ("#{it['refType']}: #{it['refId']}" rescue nil) },
        # "Blueprint Type" => lambda {|it| it['blueprint']['type'] rescue nil },
        "Blueprint" => lambda {|it| it['blueprint']['name'] rescue nil },
        "Group" => lambda {|it| it['group']['name'] rescue nil },
        "Default Cloud" => lambda {|it| it['defaultCloud']['name'] rescue nil },
        "Environment" => lambda {|it| it['environment'] rescue nil },
      }
      print_description_list(show_columns, integration_object, options)
      # print reset,"\n"
      # print_h2 "App Spec"
      print_h2 "Config"
      if config
        # config_string = integration_object['config'] || ""
        config_string = config.is_a?(Hash) ? JSON.pretty_generate(config) : config.to_s
        #print reset,config_string,"\n",reset
        config_lines = config_string.split("\n")
        config_line_count = config_lines.size
        max_lines = 10
        if config_lines.size > max_lines
          config_string = config_lines.first(max_lines).join("\n")
          config_string << "\n\n"
          config_string << "#{dark}(#{(config_line_count - max_lines)} more lines were not shown, use -c to show the config)#{reset}"
          #config_string << "\n"
        end
        # strip --- yaml header
        if config_string[0..3] == "---\n"
          config_string = config_string[4..-1]
        end
        print reset,config_string.chomp("\n"),"\n",reset
      else
        print reset,"(blank)","\n",reset
      end
      print reset,"\n"
    elsif integration_object['type'] == 'catalog'
      show_columns = {
        "Integration" => lambda {|it| integration['name'] },
        "Object ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        "Catalog Item" => lambda {|it| it['catalogItemType']['name'] rescue nil },
        # "Ref Type" => 'refType',
        # "Ref ID" => 'refId',
        # "Reference" => lambda {|it| ("#{it['refType']}: #{it['refId']}" rescue nil) },
      }
      print_description_list(show_columns, integration_object, options)
      print reset,"\n"
    else
      # Unknown type?
      show_columns = {
        "Integration" => lambda {|it| integration['name'] },
        "Object ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        "Ref Type" => 'refType',
        "Ref ID" => 'refId',
      }
      print_description_list(show_columns, integration_object, options)
      print reset,"\n"
    end
  end
  return 0, nil
end

#_get_type(id, params, options) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 495

def _get_type(id, params, options)
  integration_type = nil
  if id.to_s !~ /\A\d{1,}\Z/
    integration_type = find_integration_type_by_name_or_code(id)
    return 1, "integration type not found for name or code '#{id}'" if integration_type.nil?
    id = integration_type['id']
  end
  # /api/integration-types does not return optionTypes by default, use ?optionTypes=true
  @integration_types_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integration_types_interface.dry.get(id, params)
    return
  end
  json_response = @integration_types_interface.get(id, params)
  integration_type = json_response[integration_type_object_key]
  render_response(json_response, options, integration_type_object_key) do
    print_h1 "Integration Type Details", [], options
    print cyan
    show_columns = integration_type_column_definitions
    print_description_list(show_columns, integration_type)

    if integration_type['optionTypes'] && integration_type['optionTypes'].size > 0
      print_h2 "Option Types"
      opt_columns = [
        # {"ID" => lambda {|it| it['id'] } },
        {"FIELD NAME" => lambda {|it| (it['fieldContext'] && it['fieldContext'] != 'integration') ? [it['fieldContext'], it['fieldName']].join('.') : it['fieldName']  } },
        {"FIELD LABEL" => lambda {|it| it['fieldLabel'] } },
        {"TYPE" => lambda {|it| it['type'] } },
        {"DEFAULT" => lambda {|it| it['defaultValue'] } },
        {"REQUIRED" => lambda {|it| format_boolean it['required'] } },
        # {"DESCRIPTION" => lambda {|it| it['description'] }, # do it!
      ]
      print as_pretty_table(integration_type['optionTypes'], opt_columns)
    else
      # print cyan,"No option types found for this integration type.","\n",reset
    end

    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 220

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] -t CODE [options]")
    # opts.on('-t', '--type CODE', "Integration Type code, see `#{command_name} list-types` for available type codes") do |val|
    #   options[:options]['type'] = val
    # end
    build_option_type_options(opts, options, add_integration_option_types)
    build_option_type_options(opts, options, add_integration_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new integration.
[name] is required. This is the name of the new integration
Configuration options vary by integration type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:1)
  options[:options]['name'] = args[0] if args[0]
  connect(options)
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
    # Type prompt first
    #params['type'] = Morpheus::Cli::OptionTypes.no_prompt([{'fieldName' => 'type', 'fieldLabel' => 'Type', 'type' => 'select', 'selectOptions' => [{'name' => 'Instance', 'value' => 'instance'}, {'name' => 'Blueprint', 'value' => 'blueprint'}, {'name' => 'Workflow', 'value' => 'workflow'}], 'defaultValue' => 'instance', 'required' => true}], options[:options], @api_client, options[:params])['type']
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_integration_option_types(), options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_integration_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)

    # lookup type by name or code to validate it exists and to prompt for its optionTypes
    # set integration.type=code because the api expects it that way.
    if params['type'].to_s.empty?
      raise_command_error "missing required option: --type TYPE", args, optparse
    end
    integration_type = find_integration_type_by_name_or_code_id(params['type'])
    if integration_type.nil?
      return 1, "integration type not found for #{params['type']}"
    end
    params['type'] = integration_type['code']
    config_option_types = integration_type['optionTypes']
    if config_option_types.nil?
      config_option_types = @integration_types_interface.option_types(integration_type['id'])['optionTypes']
    end
    if config_option_types.nil?
      print yellow,"No option types found for integration type: #{integration_type['name']} (#{integration_type['code']})", reset, "\n"
    end
    if config_option_types && config_option_types.size > 0
      # optionTypes do not need fieldContext: 'integration'
      config_option_types.each do |opt|
        if opt['fieldContext'] == 'integration' || opt['fieldContext'] == 'domain'
          opt['fieldContext'] = nil
        end
      end
      config_prompt = Morpheus::Cli::OptionTypes.prompt(config_option_types, options[:options], @api_client, options[:params], false, true)
      config_prompt.deep_compact!
      params.deep_merge!(config_prompt)
    end
    # convert checkbox "on" and "off" to true and false
    params.booleanize!
    payload[integration_object_key].deep_merge!(params)
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.create(payload)
    return 0, nil
  end
  json_response = @integrations_interface.create(payload)
  integration = json_response[integration_object_key]
  render_response(json_response, options, integration_object_key) do
    print_green_success "Added integration #{integration['name']}"
    return _get(integration["id"], {}, options)
  end
  return 0, nil
end

#add_object(args) ⇒ Object



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
779
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 750

def add_object(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [name] -t CODE [options]")
    # opts.on('-t', '--type CODE', "Integration ObjectType code, see `#{command_name} list-types` for available type codes") do |val|
    #   options[:options]['type'] = val
    # end
    build_option_type_options(opts, options, add_integration_object_option_types)
    opts.on('--config-file FILE', String, "Config from a local JSON or YAML file") do |val|
      options[:config_file] = val.to_s
      file_content = nil
      full_filename = File.expand_path(options[:config_file])
      if File.exist?(full_filename)
        file_content = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        return 1
      end
      parse_result = parse_json_or_yaml(file_content)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:error]}"
        #raise_command_error "Failed to parse config as valid YAML or JSON."
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    # build_option_type_options(opts, options, add_integration_object_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new integration object.
[integration] is required. This is the name or id of an integration.
[name] is required. This is the name of the new integration
Configuration options vary by integration type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1, max:2)
  options[:options]['name'] = args[1] if args[1]
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({integration_object_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({integration_object_object_key => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_integration_object_option_types(), options[:options], @api_client, options[:params])
    v_prompt.deep_compact!
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_integration_object_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    params.booleanize!

    # convert config string to a map
    # config = params['config']
    # if config && config.is_a?(String)
    #   parse_result = parse_json_or_yaml(config)
    #   config_map = parse_result[:data]
    #   if config_map.nil?
    #     # todo: bubble up JSON.parse error message
    #     raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:error]}"
    #     #raise_command_error "Failed to parse config as valid YAML or JSON."
    #   else
    #     params['config'] = config_map
    #   end
    # end
    # if params['config']
    #   config_map = params.delete('config')
    #   params['config'] = as_json(config_map, {:pretty_json => true})
    # end
    # if options[:interactive_config]
    #   print_h2 "App Config"
    #   config_map = prompt_app_config(options)
    #   params['config'] = config_map
    # end

    payload[integration_object_object_key].deep_merge!(params)
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.create_object(integration['id'], payload)
    return 0, nil
  end
  json_response = @integrations_interface.create_object(integration['id'], payload)
  integration_object = json_response[integration_object_object_key]
  render_response(json_response, options, integration_object_object_key) do
    print_green_success "Added integration_object #{integration_object['name']}"
    return _get_object(integration, integration_object["id"], {}, options)
  end
  return 0, nil
end

#connect(opts) ⇒ Object



14
15
16
17
18
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 14

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @integrations_interface = @api_client.integrations
  @integration_types_interface = @api_client.integration_types
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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 77

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration]")
    opts.on('--objects', 'Display exposed objects for the integration.') do
      options[:show_objects] = true
    end
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific integration.
[integration] is required. This is the name or id of an integration.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#get_inventory(args) ⇒ Object



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 989

def get_inventory(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [inventory]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific integration inventory item.
[integration] is required. This is the name or id of an integration.
[inventory] is required. This is the name or id of an integration inventory item.
Only certain types of integrations support this operation, such as Ansible Tower.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args[1..-1])
  return run_command_for_each_arg(id_list) do |arg|
    _get_inventory(integration, arg, params, options)
  end
end

#get_object(args) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 594

def get_object(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [object]")
    opts.on( '-c', '--config', "Display config only, for blueprint objects" ) do
      options[:show_config] = true
    end
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific integration object.
[integration] is required. This is the name or id of an integration.
[object] is required. This is the name or id of an integration object.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args[1..-1])
  return run_command_for_each_arg(id_list) do |arg|
    _get_object(integration, arg, params, options)
  end
end

#get_type(args) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 471

def get_type(args)
  params = {'optionTypes' => true}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[type]")
    build_standard_get_options(opts, options)
    opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is true.") do |val|
      params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    end
    opts.footer = <<-EOT
Get details about a specific integration type.
[type] is required. This is the name or id of an integration type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get_type(arg, params, options)
  end
end

#handle(args) ⇒ Object



20
21
22
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 20

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 24

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    opts.on('-t', '--type CODE', "Filter by type code(s), see `list-types` for available type codes") do |val|
      params['type'] = val
    end
    opts.on('--url URL', String, "Filter by url") do |val|
      params['url'] = val
    end
    build_standard_list_options(opts, options)
    opts.footer = "List integrations."
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  connect(options)
  params.merge!(parse_list_options(options))
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.list(params)
    return
  end
  json_response = @integrations_interface.list(params)
  render_response(json_response, options, integration_list_key) do
    integrations = json_response[integration_list_key]
    print_h1 "Morpheus Integrations", parse_list_subtitles(options), options
    if integrations.empty?
      print cyan,"No integrations found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "Name" => 'name',
        "Type" => lambda {|it| format_integration_type(it) },
        "URL" => lambda {|it| it['url'] },
        "Username" => lambda {|it| it['username'] },
        "Enabled" => lambda {|it| format_boolean(it['enabled']) },
        "Status Date" => lambda {|it| format_local_dt(it['statusDate']) },
        "Status" => lambda {|it| format_integration_status(it) },
        # "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
        # "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
      }.upcase_keys!
      print as_pretty_table(integrations, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_inventory(args) ⇒ Object



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 940

def list_inventory(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [search]")
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
List integration inventory.
[integration] is required. This is the name or id of an integration.
Only certain types of integrations support this operation, such as Ansible Tower.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)

  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?

  if args.count > 1
    options[:phrase] = args[1..-1].join(" ")
  end
  params.merge!(parse_list_options(options))
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.list_inventory(integration['id'], params)
    return 0, nil
  end
  json_response = @integrations_interface.list_inventory(integration['id'], params)
  render_response(json_response, options, integration_inventory_list_key) do
    integration_inventory = json_response[integration_inventory_list_key]
    print_h1 "Integration Inventory [#{integration['name']}]", parse_list_subtitles(options), options
    if integration_inventory.empty?
      print cyan,"No inventory found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "Name" => 'name',
        "Description" => 'description',
        "Tenant Default" => lambda {|it| (format_list(it['tenants'].collect {|t| t['name'] }) rescue "") },
      }
      print as_pretty_table(integration_inventory, list_columns.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_objects(args) ⇒ Object

Integration Objects



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 539

def list_objects(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [search]")
    opts.on('-t', '--type CODE', "Filter by types: cloud, layout, blueprint, catalog") do |val|
      params['type'] = [params['type'], val].compact.flatten.collect {|it| it.to_s.strip.split(",") }.flatten.collect {|it| it.to_s.strip }
    end
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
List integration objects.
[integration] is required. This is the name or id of an integration.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)

  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?

  if args.count > 1
    options[:phrase] = args[1..-1].join(" ")
  end
  params.merge!(parse_list_options(options))
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.list_objects(integration['id'], params)
    return 0, nil
  end
  json_response = @integrations_interface.list_objects(integration['id'], params)
  render_response(json_response, options, integration_list_key) do
    integration_objects = json_response[integration_object_list_key]
    print_h1 "Integration Objects [#{integration['name']}]", parse_list_subtitles(options), options
    if integration_objects.empty?
      print cyan,"No objects found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "Name" => 'name',
        # "Category" => 'category',
        # "Ref Type" => 'refType',
        # "Ref ID" => 'refId',
        # "Type" => lambda {|it| it['type'] },
        "Type" => lambda {|it| it['type'].to_s.capitalize },
        "Ref ID" => 'refId',
      }.upcase_keys!
      print as_pretty_table(integration_objects, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_types(args) ⇒ Object



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
467
468
469
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 432

def list_types(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is false.") do |val|
      params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    end
    build_standard_list_options(opts, options)
    opts.footer = "List integration types."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @integration_types_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integration_types_interface.dry.list(params)
    return
  end
  json_response = @integration_types_interface.list(params)
  render_response(json_response, options, integration_type_list_key) do
    integration_types = json_response[integration_type_list_key]
    print_h1 "Morpheus Integration Types", parse_list_subtitles(options), options
    if integration_types.empty?
      print cyan,"No integration types found.",reset,"\n"
    else
      list_columns = integration_type_column_definitions.upcase_keys!
      print as_pretty_table(integration_types, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#refresh(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 358

def refresh(args)
  options = {}
  query_params = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [options]")
    build_option_type_options(opts, options, update_integration_option_types)
    build_option_type_options(opts, options, update_integration_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Refresh an integration.
[integration] is required. This is the name or id of an integration.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  query_params.merge!(parse_query_options(options))
  integration = find_integration_by_name_or_id(args[0])
  return 1 if integration.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.refresh(integration['id'], query_params, payload)
    return
  end
  json_response = @integrations_interface.refresh(integration['id'], query_params, payload)
  integration = json_response[integration_object_key]
  render_response(json_response, options, integration_object_key) do
    print_green_success "Refreshed integration #{integration['name']}"
    return _get(integration["id"], {}, options)
  end
  return 0, nil
end

#remove(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 400

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete an integration.
[integration] is required. This is the name or id of an integration.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1 if integration.nil?
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.destroy(integration['id'], params)
    return
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the integration #{integration['name']}?")
    return 9, "aborted command"
  end
  json_response = @integrations_interface.destroy(integration['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed integration #{integration['name']}"
  end
  return 0, nil
end

#remove_object(args) ⇒ Object

payload.deep_merge!(=> params)

    if payload[integration_object_object_key].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.update_object(integration['id'], integration_object['id'], payload)
    return
  end
  json_response = @integrations_interface.update_object(integration['id'], integration_object['id'], payload)
  integration_object = json_response[integration_object_object_key]
  render_response(json_response, options, integration_object_object_key) do
    print_green_success "Updated integration object #{integration_object['name']}"
    return _get_object(integration, integration_object["id"], {}, options)
  end
  return 0, nil
end


905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 905

def remove_object(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete an integration object.
[integration] is required. This is the name or id of an integration.
[object] is required. This is the name or id of an integration object.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?
  integration_object = find_integration_object_by_name_or_id(integration['id'], args[1])
  return 1, "integration object not found for #{args[1]}" if integration_object.nil?
  params.merge!(parse_query_options(options))
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.destroy_object(integration['id'], integration_object['id'], params)
    return
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the integration object #{integration_object['name']}?")
    return 9, "aborted command"
  end
  json_response = @integrations_interface.destroy_object(integration['id'], integration_object['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed integration object #{integration_object['name']}"
  end
  return 0, nil
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 301

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [options]")
    build_option_type_options(opts, options, update_integration_option_types)
    build_option_type_options(opts, options, update_integration_advanced_option_types)
    opts.on(nil, '--no-refresh', "Skip refresh on update.") do
      params['refresh'] = 'false'
    end
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update an integration.
[integration] is required. This is the name or id of an integration.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1 if integration.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({integration_object_key => parse_passed_options(options)})
    # do not prompt on update
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_integration_option_types, options[:options], @api_client, options[:params])
    v_prompt.deep_compact!
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_integration_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    # convert checkbox "on" and "off" to true and false
    params.booleanize!
    # massage association params a bit
    
    payload.deep_merge!({integration_object_key => params})
    if payload[integration_object_key].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.update(integration['id'], payload)
    return
  end
  json_response = @integrations_interface.update(integration['id'], payload)
  integration = json_response[integration_object_key]
  render_response(json_response, options, integration_object_key) do
    print_green_success "Updated integration #{integration['name']}"
    return _get(integration["id"], {}, options)
  end
  return 0, nil
end

#update_inventory(args) ⇒ Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/morpheus/cli/commands/integrations_command.rb', line 1043

def update_inventory(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration] [inventory] [options]")
    opts.on('--tenants [LIST]', String, "Tenant Default, comma separated list of account IDs") do |val|
      options[:tenants] = parse_array(val)
    end
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update an integration inventory item.
[integration] is required. This is the name or id of an integration.
[inventory] is required. This is the name or id of an integration inventory item.
Only certain types of integrations support this operation, such as Ansible Tower.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)
  integration = find_integration_by_name_or_id(args[0])
  return 1, "integration not found for #{args[0]}" if integration.nil?
  integration_inventory = find_integration_inventory_by_name_or_id(integration['id'], args[1])
  return 1, "integration inventory not found for #{args[1]}" if integration_inventory.nil?
  # construct payload
  object_key = integration_inventory_object_key
  payload = parse_payload(options, object_key)
  if payload.nil?
    if options[:tenants]
      #params['tenants'] = options[:tenants]
      params['tenants'] = options[:tenants].collect do |val|
        if val.to_s =~ /\A\d{1,}\Z/
          val.to_i
        else
          # todo: use /api/options/allTenants to avoid permission errors here..
          record = find_by_name_or_id(:account, val)
          if record.nil?
            exit 1 #return 1, "Tenant not found by '#{val}'"
          else 
            record['id']
          end
        end
      end
    end
    payload.deep_merge!({object_key => params})
    if payload.empty? || payload[object_key].empty?
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  # make request
  @integrations_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @integrations_interface.dry.update_inventory(integration['id'], integration_inventory['id'], payload)
    return
  end
  json_response = @integrations_interface.update_inventory(integration['id'], integration_inventory['id'], payload)
  integration_inventory = json_response[object_key]
  render_response(json_response, options, object_key) do
    print_green_success "Updated integration inventory #{integration_inventory['name']}"
    # return _get_inventory(integration, integration_inventory["id"], {}, options)
  end
  return 0, nil
end