Class: Morpheus::Cli::LogSettingsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from AccountsHelper

#account_column_definitions, #account_users_interface, #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

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_payload, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#add_syslog_rule(args) ⇒ Object



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

def add_syslog_rule(args)
  options = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[name] [rule]")
    build_common_options(opts, options, [:json, :payload, :dry_run, :quiet, :remote])
    opts.footer = "Add syslog rule.\n" +
        "[name] is required. If syslog already exists, the specified rule will be updated\n" +
        "[rule] is required"
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 2
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = parse_payload(options)

    if !payload
      payload = {'syslogRule' => {'name' => args[0], 'rule' => args[1]}}
    end

    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.add_syslog_rule(payload)
      return
    end
    json_response = @log_settings_interface.add_syslog_rule(payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Syslog rule added"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error adding syslog rule: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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

#connect(opts) ⇒ Object



16
17
18
19
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 16

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @log_settings_interface = @api_client.log_settings
end

#disable_integration(args) ⇒ Object



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

def disable_integration(args)
  print_error yellow,"[DEPRECATED] The command `#{command_name} disable-integration` is deprecated.",reset,"\n"
  options = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Disabled specifed integration.\n" +
        "[name] is required. Currently supports splunk and logrhythm integrations."
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = {'integration' => {'enabled' => false}}

    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.update_integration(args[0], payload)
      return
    end
    json_response = @log_settings_interface.update_integration(args[0], payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Integration removed"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error disabling integration: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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

#enable_integration(args) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 189

def enable_integration(args)
  print_error yellow,"[DEPRECATED] The command `#{command_name} enable-integration` is deprecated.",reset,"\n"
  options = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[name] [host] [port]")
    build_common_options(opts, options, [:json, :payload, :dry_run, :quiet, :remote])
    opts.footer = "Enables specifed integration.\n" +
        "[name] is required. Currently supports splunk and logrhythm integrations.\n" +
        "[host] is required. Host of the integration.\n" +
        "[port] is required. Port of the integration."
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 3
    raise_command_error "wrong number of arguments, expected 3 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  if !args[2].to_i
    raise_command_error "port argument must be a number"
  end

  begin
    payload = parse_payload(options)

    if !payload
      payload = {'integration' => {'enabled' => true, 'host' => args[1], 'port' => args[2]}}
    end

    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.update_integration(args[0], payload)
      return
    end
    json_response = @log_settings_interface.update_integration(args[0], payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Integration added"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error enabling integration: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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

#get(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 25

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get log settings."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  
  begin
    @log_settings_interface.setopts(options)

    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.get()
      return
    end
    json_response = @log_settings_interface.get()
    if options[:json]
      puts as_json(json_response, options, "logSettings")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "logSettings")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['logSettings']], options)
      return 0
    end

    log_settings = json_response['logSettings']

    print_h1 "Log Settings"
    print cyan
    description_cols = {
      "Logs Enabled" => lambda {|it| format_boolean(it['enabled']) },
      "Availability Time Frame" => lambda {|it| it['retentionDays'] }
    }
    print_description_list(description_cols, log_settings)

    # Syslog Forwarding Rules
    if !log_settings['syslogRules'].empty?
      print_h2 "Syslog Forwarding Rules"
      print cyan
      print as_pretty_table(log_settings['syslogRules'], [:id, :name, :rule])
    end

    # Integrations
    if !log_settings['integrations'].empty?
      print_h2 "Integrations"
      print cyan
      print as_pretty_table(log_settings['integrations'], [:name, :enabled, :host, :port])
    end
    print reset, "\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



21
22
23
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 21

def handle(args)
  handle_subcommand(args)
end

#remove_integration(args) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 290

def remove_integration(args)
  print_error yellow,"[DEPRECATED] The command `#{command_name } remove-integration` is deprecated.",reset,"\n"
  options = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Deletes specifed integration.\n" +
        "[name] is required. Currently supports splunk and logrhythm integrations."
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.destroy_integration(args[0])
      return
    end
    json_response = @log_settings_interface.destroy_integration(args[0])

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Integration removed"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error removing integration: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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

#remove_syslog_rule(args) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/morpheus/cli/commands/log_settings_command.rb', line 384

def remove_syslog_rule(args)
  options = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[syslog-rule]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Delete a syslog rule.\n" +
        "[syslog-rule] is required. This is the name or id of an syslog rule."
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    syslog_rule = find_syslog_rule_by_name_or_id(args[0])

    if syslog_rule.nil?
      print_red_alert "Syslog rule not found for: #{args[0]}"
      return 1
    end

    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.destroy_syslog_rule(syslog_rule['id'])
      return
    end
    json_response = @log_settings_interface.destroy_syslog_rule(syslog_rule['id'])

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Syslog rule removed"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error removing syslog rule: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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

#update(args) ⇒ Object



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

def update(args)
  options = {}
  params = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage()
    opts.on("--enabled [on|off]", ['on','off'], "Logs enabled") do |val|
      params['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("-R", "--retention NUMBER", Integer, "Availability time frame in days") do |val|
      params['retentionDays'] = val.to_i
    end
    opts.on("-s", "--syslog JSON", String, "Syslog rules JSON") do |val|
      begin
        syslog_rules = JSON.parse(val.to_s)
        options[:syslogRules] = syslog_rules.kind_of?(Array) ? syslog_rules : [syslog_rules]
      rescue JSON::ParserError => e
        print_red_alert "Unable to parse syslog rules JSON"
        exit 1
      end
    end
    opts.on('--syslog-list LIST', Array, "Syslog rules list in form of name value pairs: name1=rule1,name2=rule2") do |val|
      options[:syslogRules] = val.collect { |nv|
        parts = nv.split('=')
        {'name' => parts[0].strip, 'rule' => (parts.count > 1 ? parts[1].strip : '')}
      }
    end
    opts.on( '-i', '--integrations JSON', "Integrations") do |val|
      begin
        ints = JSON.parse(val.to_s)
        options[:integrations] = ints.kind_of?(Array) ? ints : [ints]
      rescue JSON::ParserError => e
        print_red_alert "Unable to parse integrations JSON"
        exit 1
      end
    end
    build_common_options(opts, options, [:json, :payload, :dry_run, :quiet, :remote])
    opts.footer = "Update your log settings."
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = parse_payload(options)

    if !payload
      payload = {'logSettings' => params}

      if !options[:syslogRules].nil?
        if options[:syslogRules].reject { |rule| rule['name'] }.count > 0
          print_red_alert "Invalid forwarding rule(s), name is required"
          return 1
        end
        payload['logSettings']['syslogRules'] = options[:syslogRules]
      end

      if !options[:integrations].nil?
        if options[:integrations].reject { |rule| rule['name'] }.count > 0
          print_red_alert "Invalid integration: name is required"
          return 1
        end
        payload['logSettings']['integrations'] = options[:integrations]
      end
    end

    if payload['logSettings'].empty?
      print_green_success "Nothing to update"
      exit 1
    end

    @log_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @log_settings_interface.dry.update(payload)
      return
    end
    json_response = @log_settings_interface.update(payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Updated log settings"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error updating log settings: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

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