xcal-parktronic 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +8 -8
  2. data/.travis.yml +7 -0
  3. data/README.md +73 -7
  4. data/lib/xcal/parktronic.rb +11 -0
  5. data/lib/xcal/parktronic/api_client.rb +8 -5
  6. data/lib/xcal/parktronic/generic_response.rb +18 -2
  7. data/lib/xcal/parktronic/routes.rb +9 -2
  8. data/lib/xcal/parktronic/routes/alarms.rb +115 -34
  9. data/lib/xcal/parktronic/routes/command_notifications.rb +56 -0
  10. data/lib/xcal/parktronic/routes/custom_queries.rb +80 -0
  11. data/lib/xcal/parktronic/routes/event_history_items.rb +36 -0
  12. data/lib/xcal/parktronic/routes/events.rb +67 -0
  13. data/lib/xcal/parktronic/routes/metrics.rb +120 -0
  14. data/lib/xcal/parktronic/routes/nested/alarm_actions.rb +84 -0
  15. data/lib/xcal/parktronic/routes/nested/device_errors.rb +35 -0
  16. data/lib/xcal/parktronic/routes/nested/events.rb +57 -0
  17. data/lib/xcal/parktronic/routes/nested/metric_values.rb +35 -0
  18. data/lib/xcal/parktronic/routes/outages.rb +34 -0
  19. data/lib/xcal/parktronic/routes/stack_changes.rb +53 -0
  20. data/lib/xcal/parktronic/version.rb +1 -1
  21. data/spec/lib/xcal/parktronic/api_client_spec.rb +12 -2
  22. data/spec/lib/xcal/parktronic/routes/alarms_route_spec.rb +16 -1
  23. data/spec/lib/xcal/parktronic/routes/command_notifications_routes_spec.rb +32 -0
  24. data/spec/lib/xcal/parktronic/routes/event_history_items_routes_spec.rb +28 -0
  25. data/spec/lib/xcal/parktronic/routes/events_route_spec.rb +33 -0
  26. data/spec/lib/xcal/parktronic/routes/metrics_routes_spec.rb +92 -49
  27. data/spec/lib/xcal/parktronic/routes/nested/alarm_actions_spec.rb +58 -0
  28. data/spec/lib/xcal/parktronic/routes/nested/events_spec.rb +24 -0
  29. data/spec/lib/xcal/parktronic/routes/outages_routes_spec.rb +25 -0
  30. data/spec/lib/xcal/parktronic/routes/stack_changes_routes_spec.rb +38 -0
  31. data/spec/support/api_mock.rb +147 -2
  32. data/spec/support/fixtures/responses/alarm_action.json +19 -0
  33. data/spec/support/fixtures/responses/alarm_actions.json +40 -0
  34. data/spec/support/fixtures/responses/alarm_events.json +180 -0
  35. data/spec/support/fixtures/responses/alarm_post.json +12 -0
  36. data/spec/support/fixtures/responses/alarm_tags.json +1 -0
  37. data/spec/support/fixtures/responses/command_notification.json +7 -0
  38. data/spec/support/fixtures/responses/command_notifications.json +45 -0
  39. data/spec/support/fixtures/responses/event.json +22 -0
  40. data/spec/support/fixtures/responses/event_tags.json +1 -0
  41. data/spec/support/fixtures/responses/events.json +42 -0
  42. data/spec/support/fixtures/responses/events_history_page_1.json +55 -0
  43. data/spec/support/fixtures/responses/events_history_page_2.json +55 -0
  44. data/spec/support/fixtures/responses/metric.json +8 -0
  45. data/spec/support/fixtures/responses/metric_device_errors.json +610 -0
  46. data/spec/support/fixtures/responses/metric_metric_values.json +586 -0
  47. data/spec/support/fixtures/responses/metric_post.json +12 -0
  48. data/spec/support/fixtures/responses/metrics_page_1.json +90 -0
  49. data/spec/support/fixtures/responses/metrics_page_2.json +90 -0
  50. data/spec/support/fixtures/responses/outages_page_1.json +55 -0
  51. data/spec/support/fixtures/responses/outages_page_2.json +55 -0
  52. data/spec/support/fixtures/responses/stack_changes_page_1.json +80 -0
  53. data/spec/support/fixtures/responses/stack_changes_page_2.json +80 -0
  54. data/spec/support/fixtures/responses/stack_changes_post.json +12 -0
  55. data/xcal-parktronic.gemspec +1 -1
  56. metadata +76 -2
@@ -0,0 +1,80 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+
5
+ module CustomQueries
6
+
7
+ # Fetches *active* custom queries
8
+ #
9
+ # ==== Examples
10
+ # api.get_custom_queries
11
+ # api.custom_queries
12
+ def get_custom_queries
13
+ response = http.get("/#{api_version}/custom_queries?#{URI.encode_www_form(:access_token => access_token)}")
14
+
15
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
16
+ if response.code == '200'
17
+ generic_response.custom_queries.map { |cq| Xcal::Parktronic::GenericResponse.new(cq.custom_query, self) }
18
+ else
19
+ generic_response
20
+ end
21
+
22
+ # TODO Add caching
23
+ end
24
+ alias :custom_queries :get_custom_queries
25
+
26
+
27
+ # Posts new Custom Query
28
+ #
29
+ # ==== Parameters
30
+ # * +params+ hash of Custom Query data.
31
+ #
32
+ # ==== Examples
33
+ # api.post_custom_query(body: 'test query', bucket_interval: 30, database_name: 'database')
34
+ def post_custom_query(params)
35
+ request = Net::HTTP::Post.new("/#{api_version}/custom_queries", 'Content-Type' => 'application/json')
36
+ request.body = { access_token: access_token, custom_query: params }.to_json
37
+ response = http.start { |net| net.request(request) }
38
+
39
+ Xcal::Parktronic::GenericResponse.new(response.body)
40
+ end
41
+
42
+
43
+ # Update Custom Query with specific ID
44
+ #
45
+ # ==== Parameters
46
+ # * +id+ alarm ID
47
+ # * +params+ custom query params
48
+ #
49
+ # ==== Examples
50
+ # api.update_custom_query(2, body: 'new body')
51
+ # api.update_custom_query(5, active: false, bucket_interval: 5, source: 'xbo')
52
+ def update_custom_query(id, params)
53
+ request = Net::HTTP::Patch.new("/#{api_version}/custom_queries/#{id}", 'Content-Type' => 'application/json')
54
+ request.body = { access_token: access_token, custom_query: params }.to_json
55
+
56
+ response = http.start { |net| net.request(request) }
57
+ Xcal::Parktronic::GenericResponse.new(response.body)
58
+ end
59
+
60
+
61
+ # Find last Custom Query by reference (by widget id and type)
62
+ #
63
+ # ==== Parameters
64
+ # * +referencable_id+ referencable ID (widget id)
65
+ # * +referencable_type+ referencable_type (widget type)
66
+ #
67
+ # ==== Examples
68
+ # api.find_custom_query(3, 'ChartSeries')
69
+ def find_custom_query(referencable_id, referencable_type)
70
+ response = http.get("/#{api_version}/custom_queries/find/#{referencable_id}/#{referencable_type}?#{URI.encode_www_form(:access_token => access_token)}")
71
+
72
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
73
+ response.code == '200' ? Xcal::Parktronic::GenericResponse.new(generic_response.custom_query, self) : generic_response
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,36 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+
5
+ module EventHistoryItems
6
+
7
+ # Fetches event history items by *host* and *service*
8
+ #
9
+ # ==== Parameters
10
+ # * +host_impacted+ event host
11
+ # * +service_impacted+ event service
12
+ # * +page+ page number, defaults to 1
13
+ # * +per_page+ per page value, defaults to 100
14
+ #
15
+ # ==== Examples
16
+ # api.get_paged_event_history_items
17
+ # api.events_history(host_impacted: 'google.com', service_impacted: 'http')
18
+ def get_paged_event_history_items(args = {})
19
+ args.merge!(access_token: access_token)
20
+ response = http.get("/#{api_version}/events_history?#{URI.encode_www_form(args)}")
21
+
22
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
23
+ if response.code == '200' && generic_response.has_key?(:event_history_items)
24
+ generic_response.event_history_items.map { |history| Xcal::Parktronic::GenericResponse.new(history.event_history_item, self) }
25
+ else
26
+ generic_response
27
+ end
28
+
29
+ # TODO Add caching
30
+ end
31
+ alias :events_history :get_paged_event_history_items
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,67 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+
5
+ module Events
6
+
7
+ # Fetches event with specific ID
8
+ #
9
+ # ==== Parameters
10
+ # * +id+ event ID
11
+ #
12
+ # ==== Examples
13
+ # api.get_event(1)
14
+ # api.event(1)
15
+ def get_event(id)
16
+ response = http.get("/#{api_version}/events/#{id}?access_token=#{access_token}")
17
+
18
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
19
+ response.code == '200' ? Xcal::Parktronic::GenericResponse.new(generic_response.event, self) : generic_response
20
+
21
+ # TODO Add caching
22
+ end
23
+ alias :event :get_event
24
+
25
+
26
+ # Update event with specific ID
27
+ #
28
+ # ==== Parameters
29
+ # * +id+ event ID
30
+ # * +params+ alarm params
31
+ #
32
+ # ==== Examples
33
+ # api.update_event(2, subject: 'new subject')
34
+ # api.update_event(5, subject: 'new subject', description: 'new description')
35
+ def update_event(id, params)
36
+ begin
37
+ request = Net::HTTP::Patch.new("/#{api_version}/events/#{id}", 'Content-Type' => 'application/json')
38
+ rescue # ruby 1.9.2
39
+ request = Net::HTTP::Put.new("/#{api_version}/events/#{id}", 'Content-Type' => 'application/json')
40
+ end
41
+ request.body = { access_token: access_token, event: params }.to_json
42
+
43
+ response = http.start { |net| net.request(request) }
44
+ Xcal::Parktronic::GenericResponse.new(response.body)
45
+ end
46
+
47
+ # Find all Event Tags
48
+ #
49
+ # ==== Parameters
50
+ # * +id+ event ID
51
+ #
52
+ # ==== Examples
53
+ # api.get_event_tags(2)
54
+ # api.event_tags(2)
55
+ def get_event_tags(id)
56
+ response = http.get("/#{api_version}/events/#{id}/tags?#{URI.encode_www_form(access_token: access_token)}")
57
+
58
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, self)
59
+ response.code == '200' && generic_response.has_key?(:tags) ? generic_response.tags.map(&:tag) : generic_response
60
+ end
61
+ alias :event_tags :get_event_tags
62
+
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,120 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+
5
+ module Metrics
6
+
7
+ # Fetches latest metrics
8
+ #
9
+ # ==== Parameters
10
+ # * +page+ page number, defaults to 1
11
+ # * +per_page+ per page value, defaults to 100
12
+ #
13
+ # ==== Examples
14
+ # api.get_paged_metrics
15
+ # api.metrics
16
+ # api.metrics(page: 2, per_page: 5)
17
+ def get_paged_metrics(args = {})
18
+ args.merge!(:access_token => access_token)
19
+ response = http.get("/#{api_version}/metrics?#{URI.encode_www_form(args)}")
20
+
21
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
22
+ if response.code == '200'
23
+ generic_response.metrics.map { |metric| Xcal::Parktronic::GenericResponse.new(metric.metric, self) }
24
+ else
25
+ generic_response
26
+ end
27
+
28
+ # TODO Add caching
29
+ end
30
+ alias :metrics :get_paged_metrics
31
+
32
+
33
+ # Fetches metric with specific ID
34
+ #
35
+ # ==== Parameters
36
+ # * +id+ metric ID
37
+ #
38
+ # ==== Examples
39
+ # api.get_metric(2)
40
+ def get_metric(id)
41
+ response = http.get("/#{api_version}/metrics/#{id}?access_token=#{access_token}")
42
+
43
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body)
44
+ response.code == '200' ? Xcal::Parktronic::GenericResponse.new(generic_response.metric, self) : generic_response
45
+
46
+ # TODO Add caching
47
+ end
48
+ alias :metric :get_metric
49
+
50
+ # Posts new metric
51
+ #
52
+ # Alarm argument should be a hash of Alarm + Events data.
53
+ #
54
+ # Example of metric data:
55
+ #
56
+ # +metric+ :
57
+ # {
58
+ # :name => 'metric name',
59
+ # :originating_system => 'Source System',
60
+ # :impact_level => 'low',
61
+ # :tag_list => %w(taga tagb)
62
+ # }
63
+ #
64
+ # Example of events array:
65
+ # +events+ :
66
+ # [
67
+ # {
68
+ # :subject => 'EventSubj',
69
+ # :description => 'EventDesc',
70
+ # :host_impacted => 'host',
71
+ # :initiated_at => '2013-11-22T01:00:24Z',
72
+ # :service_impacted => 'EventSvc',
73
+ # :incident_status => 'CRITICAL'
74
+ # }
75
+ # ]
76
+ #
77
+
78
+ # Posts new metric
79
+ #
80
+ # ==== Parameters
81
+ # * +metric+ hash of Metric data.
82
+ # * +metric_values+ array of Metric Values.
83
+ # * +device_errors+ array of Device Errors.
84
+ #
85
+ # ==== Examples
86
+ # metric_params = { name: 'metric name', description: 'metric description', created_at: Time.now }
87
+ # metric_value_1 = { value: 2, timestamp: (Time.now - 240)}
88
+ # metric_value_2 = { value: 4, timestamp: (Time.now - 120)}
89
+ # device_error_1 = { timestamp: (Time.now() - 120), value: 5}
90
+ # api.post_metric( metric: metric_params, metric_values: [metric_value_1, metric_value_2], device_error: [device_error_1] )
91
+ # TODO check this method!
92
+ def post_metric(args = {})
93
+ request = Net::HTTP::Post.new("/#{api_version}/metrics", 'Content-Type' => 'application/json')
94
+ request.body = { access_token: access_token, metric: args[:metric], metric_values: args[:metric_values], device_errors: [args[:device_errors]] }.to_json
95
+ response = http.start { |net| net.request(request) }
96
+
97
+ Xcal::Parktronic::GenericResponse.new(response.body)
98
+ end
99
+
100
+ # Update metric with specific ID
101
+ #
102
+ # ==== Parameters
103
+ # * +id+ metric ID
104
+ # * +params+ metric params
105
+ #
106
+ # ==== Examples
107
+ # api.update_metric(2, description: 'new description')
108
+ def update_metric(id, params)
109
+ request = Net::HTTP::Patch.new("/#{api_version}/metrics/#{id}", 'Content-Type' => 'application/json')
110
+ request.body = { access_token: access_token, metric: params }.to_json
111
+
112
+ response = http.start { |net| net.request(request) }
113
+ Xcal::Parktronic::GenericResponse.new(response.body)
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,84 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+ module Nested
5
+
6
+ module AlarmActions
7
+
8
+ # Fetches alarm_actions for the specific alarm
9
+ # Executed as a method chain from the GenericResponse object
10
+ #
11
+ # Call example:
12
+ # alarm(3).get_alarm_actions
13
+ def get_alarm_actions
14
+ response = client.http.get("/#{client.api_version}/alarms/#{id}/alarm_actions?#{URI.encode_www_form(access_token: client.access_token)}")
15
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, client)
16
+
17
+ if response.code == '200' && generic_response.has_key?(:alarm_actions)
18
+ generic_response.alarm_actions.map(&:alarm_action)
19
+ else
20
+ generic_response
21
+ end
22
+ end
23
+
24
+ # Fetches alarm action with specific ID
25
+ #
26
+ # Accepted attributes:
27
+ # +id+ alarm_action ID
28
+ def get_alarm_action(alarm_action_id)
29
+ response = client.http.get("/#{client.api_version}/alarms/#{id}/alarm_actions/#{alarm_action_id}?access_token=#{client.access_token}")
30
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, client)
31
+
32
+ if response.code == '200' && generic_response.has_key?(:alarm_action)
33
+ generic_response.alarm_action
34
+ else
35
+ #TODO think, maybe return false or nil or 'Record not found?'
36
+ generic_response
37
+ end
38
+
39
+ # TODO Add caching
40
+ end
41
+
42
+ # Posts new alarm
43
+ #
44
+ # +params+ hash of AlarmAction data.
45
+ def post_alarm_action(params)
46
+ request = Net::HTTP::Post.new("/#{client.api_version}/alarms/#{id}/alarm_actions", 'Content-Type' => 'application/json')
47
+ request.body = { access_token: client.access_token, alarm_action: params }.to_json
48
+ response = client.http.start { |net| net.request(request) }
49
+
50
+ Xcal::Parktronic::GenericResponse.new(response.body)
51
+ end
52
+
53
+ # Update Alarm Action
54
+ #
55
+ # Accepted attributes:
56
+ # +alarm_action_id+ alarm action id
57
+ # +params+ alarm params
58
+ def update_alarm_action(alarm_action_id, params)
59
+ begin
60
+ request = Net::HTTP::Patch.new("/#{client.api_version}/alarms/#{id}/alarm_actions/#{alarm_action_id}", 'Content-Type' => 'application/json')
61
+ rescue # ruby 1.9.2
62
+ request = Net::HTTP::Put.new("/#{client.api_version}/alarms/#{id}/alarm_actions/#{alarm_action_id}", 'Content-Type' => 'application/json')
63
+ end
64
+ request.body = { access_token: client.access_token, alarm_action: params }.to_json
65
+
66
+ response = client.http.start { |net| net.request(request) }
67
+ Xcal::Parktronic::GenericResponse.new(response.body)
68
+ end
69
+
70
+ # Insert the Alarm Action at the given position
71
+ def set_position(alarm_action_id, position)
72
+ request = Net::HTTP::Post.new("/#{client.api_version}/alarms/#{id}/alarm_actions/#{alarm_action_id}/insert_at", 'Content-Type' => 'application/json')
73
+ request.body = { access_token: client.access_token, position: position }.to_json
74
+
75
+ response = client.http.start { |net| net.request(request) }
76
+ Xcal::Parktronic::GenericResponse.new(response.body)
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,35 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+ module Nested
5
+
6
+ module DeviceErrors
7
+
8
+ # Fetches device_errors for the specific metric
9
+ # Executed as a method chain from the GenericResponse object
10
+ #
11
+ # Call example:
12
+ # metric(id).get_device_errors(page: 2, per_page: 4)
13
+ #
14
+ # Accepted attributes:
15
+ # +page+ page number, defaults to 1
16
+ # +per_page+ per page value, defaults to 100
17
+ def get_paged_device_errors(args = {})
18
+ args.merge!(:access_token => client.access_token)
19
+ response = client.http.get("/#{client.api_version}/metrics/#{id}/device_errors?#{URI.encode_www_form(args)}")
20
+
21
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, client)
22
+ if response.code == '200' && generic_response.has_key?(:device_errors)
23
+ generic_response.device_errors.map(&:device_error)
24
+ else
25
+ generic_response
26
+ end
27
+ end
28
+ alias :get_device_errors :get_paged_device_errors
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
1
+ module Xcal
2
+ module Parktronic
3
+ module Routes
4
+ module Nested
5
+
6
+ module Events
7
+
8
+ # Fetches events for the specific alarm
9
+ # Executed as a method chain from the GenericResponse object
10
+ #
11
+ # Call example:
12
+ # alarm(id).get_events(page: 2, per_page: 4)
13
+ #
14
+ # Accepted attributes:
15
+ # +page+ page number, defaults to 1
16
+ # +per_page+ per page value, defaults to 100
17
+ def get_paged_events(args = {})
18
+ args.merge!(:access_token => client.access_token)
19
+ response = client.http.get("/#{client.api_version}/alarms/#{id}/events?#{URI.encode_www_form(args)}")
20
+
21
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, client)
22
+ response.code == '200' && generic_response.has_key?(:events) ? generic_response.events.map(&:event) : generic_response
23
+ end
24
+ alias :get_events :get_paged_events
25
+
26
+ # Fetches all events for the specific alarm
27
+ # Executed as a method chain from the GenericResponse object
28
+ #
29
+ # Call example:
30
+ # alarm(id).get_all_events
31
+ def get_all_events
32
+ response = client.http.get("/#{client.api_version}/alarms/#{id}/events?#{URI.encode_www_form(access_token: client.access_token)}")
33
+
34
+ generic_response = Xcal::Parktronic::GenericResponse.new(response.body, client)
35
+ response.code == '200' && generic_response.has_key?(:events) ? generic_response.events.map(&:event) : generic_response
36
+ end
37
+
38
+ # Create event for the specific alarm
39
+ # Executed as a method chain from the GenericResponse object
40
+ #
41
+ # Call example:
42
+ # alarms.last.post_event(subject: 'example', host_impacted: 'test')
43
+ # alarm(id).post_event(subject: 'example', host_impacted: 'test')
44
+ def post_event(params)
45
+ request = Net::HTTP::Post.new("/#{client.api_version}/alarms/#{id}/events", 'Content-Type' => 'application/json')
46
+ request.body = { access_token: client.access_token, event: params }.to_json
47
+ response = client.http.start { |net| net.request(request) }
48
+
49
+ Xcal::Parktronic::GenericResponse.new(response.body)
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end