1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\CloudMonitoring\Resource;
19
20 use OpenCloud\CloudMonitoring\Exception;
21 use OpenCloud\Common\Http\Message\Formatter;
22
23 24 25
26 class Alarm extends AbstractResource
27 {
28 29 30
31 private $id;
32
33 34 35
36 private $check_id;
37
38 39 40
41 private $notification_plan_id;
42
43 44 45
46 private $criteria;
47
48 49 50
51 private $disabled;
52
53 54 55
56 private $label;
57
58 protected static $json_name = false;
59 protected static $json_collection_name = 'values';
60 protected static $url_resource = 'alarms';
61
62 protected static $requiredKeys = array(
63 'check_id',
64 'notification_plan_id'
65 );
66
67 protected static $emptyObject = array(
68 'check_id',
69 'notification_plan_id',
70 'criteria',
71 'disabled',
72 'label',
73 'metadata'
74 );
75
76 public function test($params = array(), $debug = false)
77 {
78 if (!isset($params['criteria'])) {
79 throw new Exception\AlarmException(
80 'Please specify a "criteria" value'
81 );
82 }
83
84 if (!isset($params['check_data']) || !is_array($params['check_data'])) {
85 throw new Exception\AlarmException(
86 'Please specify a "check_data" array'
87 );
88 }
89
90 $url = $this->getParent()->url('test-alarm');
91 $body = json_encode((object) $params);
92
93 $response = $this->getService()
94 ->getClient()
95 ->post($url, self::getJsonHeader(), $body)
96 ->send();
97
98 return Formatter::decode($response);
99 }
100
101 public function getHistoryUrl()
102 {
103 return $this->getUrl()->addPath(NotificationHistory::resourceName());
104 }
105
106 public function getRecordedChecks()
107 {
108 $response = $this->getService()
109 ->getClient()
110 ->get($this->getHistoryUrl())
111 ->send();
112
113 $body = Formatter::decode($response);
114
115 return (isset($body->check_ids)) ? $body->check_ids : false;
116 }
117
118 public function getNotificationHistoryForCheck($checkId)
119 {
120 $url = $this->getHistoryUrl()->addPath($checkId);
121
122 return $this->getService()->resourceList('NotificationHistory', $url, $this);
123 }
124
125 public function getNotificationHistoryItem($checkId, $uuid)
126 {
127 $resource = $this->getService()->resource('NotificationHistory', null, $this);
128
129 $url = clone $resource->getUrl();
130 $url->addPath($checkId)->addPath($uuid);
131 $resource->refresh(null, $url);
132
133 return $resource;
134 }
135
136 public function notificationHistory($info)
137 {
138 return $this->getService()->resource('NotificationHistory', $info, $this);
139 }
140
141 public function isDisabled()
142 {
143 return $this->getDisabled() === true;
144 }
145 }
146