Class: Morpheus::Cli::MonitoringContactsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from MonitoringHelper

#available_severities, #check_type_for_id, #check_type_for_name, #check_type_for_name_or_id, #find_alert_by_id, #find_alert_by_name, #find_alert_by_name_or_id, #find_check_by_id, #find_check_by_name, #find_check_by_name_or_id, #find_check_group_by_id, #find_check_group_by_name, #find_check_group_by_name_or_id, #find_contact_by_id, #find_contact_by_name, #find_contact_by_name_or_id, #find_incident_by_id, #find_monitoring_app_by_id, #find_monitoring_app_by_name, #find_monitoring_app_by_name_or_id, #format_health_status, #format_monitoring_check_last_metric, #format_monitoring_check_status, #format_monitoring_check_type, #format_monitoring_incident_status, #format_monitoring_issue_attachment_type, #format_monitoring_issue_status, #format_recipient_method, #format_severity, #get_available_check_types, included, #monitoring_interface, #parse_recipient_method, #print_check_group_history_table, #print_check_groups_table, #print_check_history_table, #print_check_notifications_table, #print_checks_table, #print_incident_history_table, #print_incident_issues_table, #print_incident_notifications_table, #print_incidents_table, #print_monitor_app_history_table, #print_monitoring_apps_table, #prompt_for_check_groups, #prompt_for_checks, #prompt_for_monitor_apps, #prompt_for_recipients

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

Constructor Details

#initializeMonitoringContactsCommand

Returns a new instance of MonitoringContactsCommand.



11
12
13
# File 'lib/morpheus/cli/commands/monitoring_contacts_command.rb', line 11

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

Instance Method Details

#_get(id, options) ⇒ Object



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

def _get(id, options)

  begin
    contact = find_contact_by_name_or_id(id)
    @monitoring_contacts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @monitoring_contacts_interface.dry.get(contact['id'])
      return
    end
    # save a request
    # json_response = @monitoring_contacts_interface.get(contact['id'])
    # contact = json_response['contact']
    json_response = {'contact' => contact}
    
    if options[:json]
      puts as_json(json_response, options, "contact")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "contact")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['contact']], options)
      return 0
    end

    print_h1 "Contact Details"
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => 'name',
      "Email" => 'emailAddress',
      "Mobile" => 'smsAddress',
      "Slack Hook" => 'slackHook'
    }
    description_cols.delete("Slack Hook") if contact['slackHook'].to_s.empty?
    puts as_description_list(contact, description_cols)
   
    ## Notifications
    # show notify events here...

    print reset
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#_remove(id, options) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/morpheus/cli/commands/monitoring_contacts_command.rb', line 302

def _remove(id, options)

  begin
    contact = find_contact_by_name_or_id(id)
    @monitoring_contacts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @monitoring_contacts_interface.dry.destroy(contact['id'])
      return
    end
    json_response = @monitoring_contacts_interface.destroy(contact['id'])
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success json_response["msg"] || "Contact (#{contact['id']}) #{contact['name']} deleted"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add(args) ⇒ Object



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

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    opts.on("--name STRING", String, "Contact name") do |val|
      params['name'] = val
    end
    opts.on("--email STRING", String, "Contact email address") do |val|
      params['emailAddress'] = val == 'null' ? nil : val
    end
    opts.on("--mobile STRING", String, "Contact sms addresss") do |val|
      params['smsAddress'] = val == 'null' ? nil : val
    end
    opts.on("--slackHook STRING", String, "Contact slack hook") do |val|
      params['slackHook'] = val == 'null' ? nil : val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Create a monitoring contact." + "\n" +
                  "[name] is required. This is the name of the new contact."
  end
  optparse.parse!(args)
  connect(options)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  if args[0]
    params['name'] = args[0]
  end
  begin
    params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
    if params['name'].nil?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true, 'description' => 'The name of this contact.'}], options[:options])
      params['name'] = v_prompt['name']
    end
    if params['emailAddress'].nil?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'emailAddress', 'type' => 'text', 'fieldLabel' => 'Email', 'required' => false, 'description' => 'Contact email address.'}], options[:options])
      params['emailAddress'] = v_prompt['emailAddress'] unless v_prompt['emailAddress'].to_s.empty?
    end
    if params['smsAddress'].nil?
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'smsAddress', 'type' => 'text', 'fieldLabel' => 'Mobile', 'required' => false, 'description' => 'Contact sms address, or phone number.'}], options[:options])
      params['smsAddress'] = v_prompt['smsAddress'] unless v_prompt['smsAddress'].to_s.empty?
    end
    # if params['slackHook'].nil?
    #   v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'slackHook', 'type' => 'text', 'fieldLabel' => 'Slack Hook', 'required' => false, 'description' => 'Contact slack hook.'}], options[:options])
    #   params['slackHook'] = v_prompt['slackHook'] unless v_prompt['slackHook'].to_s.empty?
    # end
    payload = {
      'contact' => {}
    }
    payload['contact'].merge!(params)
    @monitoring_contacts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @monitoring_contacts_interface.dry.create(payload)
      return
    end

    json_response = @monitoring_contacts_interface.create(payload)
    contact = json_response['contact']
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Created contact (#{contact['id']}) #{contact['name']}"
      #_get(contact['id'], options)
    end

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

#connect(opts) ⇒ Object



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

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @monitoring_interface = @api_client.monitoring
  @monitoring_contacts_interface = @api_client.monitoring.contacts
end

#get(args) ⇒ Object



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

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[contact]")
    opts.on(nil,'--history', "Display History") do |val|
      options[:show_history] = true
    end
    opts.on(nil,'--notifications', "Display Notifications") do |val|
      options[:show_notifications] = true
    end
    build_common_options(opts, options, [:json, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a monitoring contact." + "\n" +
                  "[contact] is required. This is the name or ID of the contact. Supports 1-N [contact] arguments."
  end
  optparse.parse!(args)
  if args.count < 1
    raise_command_error "wrong number of arguments, expected 1-N and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  end
end

#handle(args) ⇒ Object



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

def handle(args)
  handle_subcommand(args)
end

#list(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
# File 'lib/morpheus/cli/commands/monitoring_contacts_command.rb', line 25

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :csv, :yaml, :fields, :json, :dry_run, :remote])
    opts.footer = "List monitoring contacts."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    # JD: lastUpdated 500ing, contacts don't have that property ? =o  Fix it!
    @monitoring_contacts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @monitoring_contacts_interface.dry.list(params)
      return
    end

    json_response = @monitoring_contacts_interface.list(params)
    if options[:json]
      puts as_json(json_response, options, "contacts")
      return 0
    elsif options[:yaml]
      puts as_json(json_response, options, "contacts")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['contacts'], options)
      return 0
    end
    contacts = json_response['contacts']
    title = "Morpheus Monitoring Contacts"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles, options
    if contacts.empty?
      print cyan,"No contacts found.",reset,"\n"
    else
      print_contacts_table(contacts, options)
      print_results_pagination(json_response, {:label => "contact", :n_label => "contacts"})
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/morpheus/cli/commands/monitoring_contacts_command.rb', line 280

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[contact]")
    build_common_options(opts, options, [:auto_confirm, :quiet, :json, :dry_run, :remote])
    opts.footer = "Delete a monitoring contact." + "\n" +
                  "[contact] is required. This is the name or ID of the contact. Supports 1-N [contact] arguments."
  end
  optparse.parse!(args)
  if args.count < 1
    raise_command_error "wrong number of arguments, expected 1-N and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  id_list = parse_id_list(args)
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete #{id_list.size == 1 ? 'contact' : 'contacts'} #{anded_list(id_list)}?", options)
    exit 1
  end
  return run_command_for_each_arg(id_list) do |arg|
    _remove(arg, options)
  end
end

#update(args) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/morpheus/cli/commands/monitoring_contacts_command.rb', line 218

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[contact]")
    opts.on("--name STRING", String, "Contact name") do |val|
      params['name'] = val
    end
    opts.on("--email STRING", String, "Contact email address") do |val|
      params['emailAddress'] = val == 'null' ? nil : val
    end
    opts.on("--mobile STRING", String, "Contact sms addresss") do |val|
      params['smsAddress'] = val == 'null' ? nil : val
    end
    opts.on("--slackHook STRING", String, "Contact slack hook") do |val|
      params['slackHook'] = val == 'null' ? nil : val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Update a monitoring contact." + "\n" +
                  "[contact] is required. This is the name or ID of the contact."
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)

  begin
    contact = find_contact_by_name_or_id(args[0])

    if params.empty?
      print_red_alert "Specify at least one option to update"
      puts optparse
      exit 1
    end

    payload = {
      'contact' => {id: contact["id"]}
    }
    payload['contact'].merge!(params)
    @monitoring_contacts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @monitoring_contacts_interface.dry.update(contact["id"], payload)
      return
    end

    json_response = @monitoring_contacts_interface.update(contact["id"], payload)
    contact = json_response['contact']
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Updated contact (#{contact['id']}) #{contact['name']}"
      _get(contact['id'], options)
    end

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