Class: Morpheus::Cli::MonitorContactsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from MonitoringHelper

#check_type_for_id, #check_type_for_name, #check_type_for_name_or_id, #find_app_by_id, #find_app_by_name, #find_app_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, #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_severity, #get_available_check_types, included, #monitoring_interface, #print_check_history_table, #print_check_notifications_table, #print_checks_table, #print_incident_history_table, #print_incident_notifications_table, #print_incidents_table

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeMonitorContactsCommand

Returns a new instance of MonitorContactsCommand.



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

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

Instance Method Details

#_get(id, options) ⇒ Object



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

def _get(id, options)

  begin
    contact = find_contact_by_name_or_id(id)
    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.get(contact['id'])
      return
    end
    json_response = @monitoring_interface.contacts.get(contact['id'])
    contact = json_response['contact']
    
    if options[:json]
      if options[:include_fields]
        json_response = {"contact" => filter_data(json_response["contact"], options[:include_fields]) }
      end
      puts as_json(json_response, options)
      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["Slack Hook"] = 'slackHook' if !contact['slackHook'].empty?
    puts as_description_list(contact, description_cols)
   
    ## Notifications
    # show notify events here...

    print reset,"\n"

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

#_remove(id, options) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 289

def _remove(id, options)

  begin
    contact = find_contact_by_name_or_id(id)
    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.destroy(contact['id'])
      return
    end
    json_response = @monitoring_interface.contacts.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

#_reopen(id, options) ⇒ Object



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

def _reopen(id, options)

  begin
    contact = find_contact_by_name_or_id(id)
    already_open = contact['status'] == 'open'
    if already_open
      print bold,yellow,"contact #{contact['id']} is already open",reset,"\n"
      return false
    end
    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.reopen(contact['id'])
      return
    end
    json_response = @monitoring_interface.contacts.reopen(contact['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      print_green_success json_response["msg"] || "contact #{contact['id']} is now open"
      # _get(contact['id'] {})
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add(args) ⇒ Object



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

def add(args)
  options = {}
  params = {}
  optparse = 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, [:json, :dry_run, :quiet])
  end
  optparse.parse!(args)
  connect(options)

  begin

    if !params["name"]
      print_red_alert "Name is required"
      puts optparse
      exit 1
    end

    payload = {
      'contact' => {}
    }
    payload['contact'].merge!(params)

    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.create(payload)
      return
    end

    json_response = @monitoring_interface.contacts.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'], {})
    end

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

#connect(opts) ⇒ Object



23
24
25
26
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 23

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @monitoring_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).monitoring
end

#get(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 84

def get(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[id list]")
    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])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  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



28
29
30
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 28

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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

def list(args)
  options = {}
  params = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :csv, :fields, :json, :dry_run])
  end
  optparse.parse!(args)
  connect(options)
  begin
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    # JD: lastUpdated 500ing, contacts don't have that property ? =o  Fix it!

    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.list(params)
      return
    end

    json_response = @monitoring_interface.contacts.list(params)
    if options[:json]
      if options[:include_fields]
        json_response = {"contacts" => filter_data(json_response["contacts"], options[:include_fields]) }
      end
      puts as_json(json_response, options)
      return 0
    end
    if options[:csv]
      puts records_as_csv(json_response['contacts'], options)
      return 0
    end
    contacts = json_response['contacts']
    title = "Morpheus Monitoring Contacts"
    subtitles = []
    if params[:phrase]
      subtitles << "Search: #{params[:phrase]}".strip
    end
    print_h1 title, subtitles
    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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 268

def remove(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[id list]")
    build_common_options(opts, options, [:auto_confirm, :quiet, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  id_list = parse_id_list(args)
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete contact #{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

#reopen(args) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 309

def reopen(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[id list]")
    build_common_options(opts, options, [:auto_confirm, :quiet, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  id_list = parse_id_list(args)
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to reopen #{id_list.size == 1 ? 'contact' : 'contacts'} #{anded_list(id_list)}?", options)
    exit 1
  end
  return run_command_for_each_arg(id_list) do |arg|
    _reopen(arg, options)
  end
end

#update(args) ⇒ Object



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/morpheus/cli/monitoring_contacts_command.rb', line 208

def update(args)
  options = {}
  params = {}
  optparse = 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, [:json, :dry_run, :quiet])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    contact = find_contact_by_name_or_id(args[0])

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

    payload = {
      'contact' => {id: contact["id"]}
    }
    payload['contact'].merge!(params)

    if options[:dry_run]
      print_dry_run @monitoring_interface.contacts.dry.update(contact["id"], payload)
      return
    end

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

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