Class: Morpheus::Cli::IntegrationsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from AccountsHelper

#account_column_definitions, #accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #find_all_user_ids, #find_role_by_id, #find_role_by_name, #find_role_by_name_or_id, #find_user_by_id, #find_user_by_username, #find_user_by_username_or_id, #find_user_group_by_id, #find_user_group_by_name, #find_user_group_by_name_or_id, #format_access_string, #format_role_type, #format_user_role_names, #format_user_status, #get_access_color, #get_access_string, included, #list_account_column_definitions, #list_user_column_definitions, #list_user_group_column_definitions, #role_column_definitions, #roles_interface, #subtenant_role_column_definitions, #user_column_definitions, #user_group_column_definitions, #user_groups_interface, #users_interface

Methods included from CliCommand

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

Instance Method Details

#_get(id, params, options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/morpheus/cli/integrations_command.rb', line 97

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
  @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)
    print reset,"\n"
  end
  return 0, nil
end

#_get_type(id, params, options) ⇒ Object



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

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



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
219
220
221
222
223
224
# File 'lib/morpheus/cli/integrations_command.rb', line 145

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])
      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

#connect(opts) ⇒ Object



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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/integrations_command.rb', line 76

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[integration]")
    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_type(args) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/morpheus/cli/integrations_command.rb', line 396

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



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

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/morpheus/cli/integrations_command.rb', line 23

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_types(args) ⇒ Object



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

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



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

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



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

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

#update(args) ⇒ Object



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

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