      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\CloudMonitoring\Resource;
     19:
     20: use OpenCloud\CloudMonitoring\Exception;
     21: use OpenCloud\Common\Http\Message\Formatter;
     22:
     23: /**
     24:  * Alarm class.
     25:  */
     26: class Alarm extends AbstractResource
     27: {
     28:     /**
     29:      * @var string
     30:      */
     31:     private $id;
     32:
     33:     /**
     34:      * @var string The ID of the check to alert on.
     35:      */
     36:     private $check_id;
     37:
     38:     /**
     39:      * @var string The id of the notification plan to execute when the state changes.
     40:      */
     41:     private $notification_plan_id;
     42:
     43:     /**
     44:      * @var string The alarm DSL for describing alerting conditions and their output states.
     45:      */
     46:     private $criteria;
     47:
     48:     /**
     49:      * @var bool Disable processing and alerts on this alarm.
     50:      */
     51:     private $disabled;
     52:
     53:     /**
     54:      * @var string A friendly label for an alarm.
     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: