Class: Morpheus::Cli::MonitoringSettings

Inherits:
Object
  • Object
show all
Includes:
AccountsHelper, CliCommand
Defined in:
lib/morpheus/cli/commands/monitoring_settings.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_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

#connect(opts) ⇒ Object



11
12
13
14
15
# File 'lib/morpheus/cli/commands/monitoring_settings.rb', line 11

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @monitoring_settings_interface = @api_client.monitoring_settings
  @options_interface = @api_client.options
end

#get(args) ⇒ Object



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

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_get_options(opts, options)
    opts.footer = "Get monitoring settings."
  end
  optparse.parse!(args)
  connect(options)
  verify_args!(args:args, optparse:optparse, count:0)
  params.merge!(parse_query_options(options))
  @monitoring_settings_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @monitoring_settings_interface.dry.get(options)
    return
  end
  json_response = @monitoring_settings_interface.get(options)
  render_response(json_response, options, 'monitoringSettings') do
    monitoring_settings = json_response['monitoringSettings']
    service_now_settings = monitoring_settings['serviceNow']
    print_h1 "Monitoring Settings"
    print cyan
    description_cols = {
      "Auto Create Checks" => lambda {|it| format_boolean(it['autoManageChecks']) },
      "Availability Time Frame" => lambda {|it| it['availabilityTimeFrame'] ? it['availabilityTimeFrame'].to_s + ' days' : '' },
      "Availability Precision" => lambda {|it| it['availabilityPrecision'] ? it['availabilityPrecision'].to_s : '' },
      "Default Check Interval" => lambda {|it| it['defaultCheckInterval'] ? it['defaultCheckInterval'].to_s + ' minutes' : '' },
    }
    print_description_list(description_cols, monitoring_settings, options)
    
      print_h2 "ServiceNow Settings", options.merge(:border_style => :thin)
      description_cols = {
        "Enabled" => lambda {|it| format_boolean(it['enabled']) },
        "Integration" => lambda {|it| it['integration'] ? it['integration']['name'] : '' },
        "New Incident Action" => lambda {|it| format_service_now_action(it['newIncidentAction']) },
        "Close Incident Action" => lambda {|it| format_service_now_action(it['closeIncidentAction']) },
        "Info Mapping" => lambda {|it| format_service_now_mapping(it['infoMapping']) },
        "Warning Mapping" => lambda {|it| format_service_now_mapping(it['warningMapping']) },
        "Critical Mapping" => lambda {|it| format_service_now_mapping(it['criticalMapping']) },
      }
      print_description_list(description_cols, service_now_settings)
    
    print reset, "\n"
  end
  return 0, nil
end

#handle(args) ⇒ Object



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

def handle(args)
  handle_subcommand(args)
end

#update(args) ⇒ Object



69
70
71
72
73
74
75
76
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
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
# File 'lib/morpheus/cli/commands/monitoring_settings.rb', line 69

def update(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage()
    opts.on('--auto-create-checks [on|off]', String, "Auto Create Checks") do |val|
      params['autoManageChecks'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--availability-time-frame DAYS", Integer, "Availability Time Frame. The number of days availability should be calculated for. Changes will not take effect until your checks have passed their check interval.") do |val|
      params['availabilityTimeFrame'] = val.to_i
    end
    opts.on("--availability-precision DIGITS", Integer, "Availability Precision. The number of decimal places availability should be displayed in. Can be anywhere between 0 and 5.") do |val|
      params['availabilityPrecision'] = val.to_i
    end
    opts.on("--default-check-interval MINUTES", Integer, "Default Check Interval. The default interval to use when creating new checks. Value is in minutes.") do |val|
      params['defaultCheckInterval'] = val.to_i
    end
    opts.on('--service-now-enabled [on|off]', String, "ServiceNow: Enabled (on) or disabled (off)") do |val|
      params['serviceNow'] ||= {}
      params['serviceNow']['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on('--service-now-integration ID', String, "ServiceNow: Integration ID or Name") do |val|
      params['serviceNow'] ||= {}
      params['serviceNow']['integration'] = val # {'id' => val.to_i}
    end
    opts.on("--service-now-new-incident-action create|none", String, "ServiceNow: New Incident Action") do |val|
      # allowed_values = 'create|none'.split('|') #get_service_now_actions().keys
      # if !allowed_values.include?(val)
      #   raise ::OptionParser::InvalidOption.new("New Incident Action value '#{val}' is invalid.\nThe allowed values are: #{allowed_values.join(', ')}")
      # end
      params['serviceNow'] ||= {}
      params['serviceNow']['newIncidentAction'] = val
    end
    opts.on("--service-now-close-incident-action close|activity|none", String, "ServiceNow: Close Incident Action") do |val|
      # allowed_values = 'close|activity|none'.split('|') #get_service_now_mappings().keys
      # if !allowed_values.include?(val)
      #   raise ::OptionParser::InvalidOption.new("Close Incident Action value '#{val}' is invalid.\nThe allowed values are: #{allowed_values.join(', ')}")
      # end
      params['serviceNow'] ||= {}
      params['serviceNow']['closeIncidentAction'] = val
    end
    opts.on("--service-now-info-mapping low|medium|high", String, "ServiceNow: Info Mapping") do |val|
      # allowed_values = 'low|medium|high'.split('|') # get_service_now_mappings().keys
      # if !allowed_values.include?(val)
      #   raise ::OptionParser::InvalidOption.new("Info Mapping value '#{val}' is invalid.\nThe allowed values are: #{allowed_values.join(', ')}")
      # end
      params['serviceNow'] ||= {}
      params['serviceNow']['infoMapping'] = val
    end
    opts.on("--service-now-warning-mapping low|medium|high", String, "ServiceNow: Warning Mapping") do |val|
      # allowed_values = 'low|medium|high'.split('|') # get_service_now_mappings().keys
      # if !allowed_values.include?(val)
      #   raise ::OptionParser::InvalidOption.new("Warning Info Mapping value '#{val}' is invalid.\nThe allowed values are: #{allowed_values.join(', ')}")
      # end
      params['serviceNow'] ||= {}
      params['serviceNow']['warningMapping'] = val
    end
    opts.on("--service-now-critical-mapping low|medium|high", String, "ServiceNow: Critical Mapping") do |val|
      # allowed_values = 'low|medium|high'.split('|') # get_service_now_mappings().keys
      # if !allowed_values.include?(val)
      #   raise ::OptionParser::InvalidOption.new("Critical Info Mapping value '#{val}' is invalid.\nThe allowed values are: #{allowed_values.join(', ')}")
      # end
      params['serviceNow'] ||= {}
      params['serviceNow']['criticalMapping'] = val
    end
    build_standard_update_options(opts, options)
    opts.footer = "Update monitoring settings."
  end
  optparse.parse!(args)
  connect(options)
  verify_args!(args:args, optparse:optparse, count:0)
  payload = parse_payload(options)
  if !payload
    payload = {}
    payload.deep_merge!({object_key => parse_passed_options(options)}) # inject options passed with -O foo=bar
    if params['serviceNow'] && params['serviceNow']['integration']
      integration = find_by_name_or_id(:integration, params['serviceNow']['integration'])
      if integration.nil?
        exit 1 #return 1, "Integration not found by '#{options[:servicenow_integration]}'"
      else
        if integration['integrationType']['code'] != 'serviceNow'
          raise_command_error "Integration '#{integration['id']}' must be a Service Now integration"
        end
        params['serviceNow'] ||= {}
        params['serviceNow']['integration'] = {'id' => integration['id'].to_i}
      end
    end
    payload.deep_merge!({object_key => params})
  end
  if payload[object_key].empty?
    raise_command_error "Specify at least one option to update.\n#{optparse}"
  end
  @monitoring_settings_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @monitoring_settings_interface.dry.update(payload)
    return
  end
  json_response = @monitoring_settings_interface.update(payload)
  exit_code, err = 0, nil
  render_response(json_response, options, object_key) do
    if json_response['success']
      print_green_success "Updated monitoring settings"
      get([] + (options[:remote] ? ["-r",options[:remote]] : []))
    else
      exit_code, err = 1, "Error updating monitoring settings: #{json_response['msg'] || json_response['errors']}"
      print_rest_errors(json_response)
    end
  end
  return exit_code, err
end