      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;
     19:
     20: use OpenCloud\Common\Service\CatalogService;
     21:
     22: /**
     23:  * Cloud Monitoring service.
     24:  *
     25:  * @package OpenCloud\CloudMonitoring
     26:  * @link    http://docs.rackspace.com/cm/api/v1.0/cm-devguide/content/index.html
     27:  */
     28: class Service extends CatalogService
     29: {
     30:     const DEFAULT_TYPE = 'rax:monitor';
     31:     const DEFAULT_NAME = 'cloudMonitoring';
     32:
     33:     protected $regionless = true;
     34:
     35:     /**
     36:      * @var array CloudMonitoring resources.
     37:      */
     38:     protected $resources = array(
     39:         'Agent',
     40:         'AgentConnection',
     41:         'AgentHost',
     42:         'AgentHostInfo',
     43:         'AgentTarget',
     44:         'AgentToken',
     45:         'Alarm',
     46:         'Changelog',
     47:         'Check',
     48:         'CheckType',
     49:         'Entity',
     50:         'Metric',
     51:         'Notification',
     52:         'NotificationHistory',
     53:         'NotificationPlan',
     54:         'NotificationType',
     55:         'View',
     56:         'Zone'
     57:     );
     58:
     59:     /**
     60:      * Get an agent.
     61:      *
     62:      * @param string|null $id
     63:      * @return \OpenCloud\CloudMonitoring\Resource\Agent
     64:      */
     65:     public function getAgent($id = null)
     66:     {
     67:         return $this->resource('Agent', $id);
     68:     }
     69:
     70:     public function getAgents()
     71:     {
     72:         return $this->resourceList('Agent');
     73:     }
     74:
     75:     public function getAgentHost($id = null)
     76:     {
     77:         return $this->resource('AgentHost', $id);
     78:     }
     79:
     80:     public function getAgentTargets()
     81:     {
     82:         return $this->resourceList('AgentTarget');
     83:     }
     84:
     85:     public function getAgentToken($id = null)
     86:     {
     87:         return $this->resource('AgentToken', $id);
     88:     }
     89:
     90:     public function getAgentTokens()
     91:     {
     92:         return $this->resourceList('AgentToken');
     93:     }
     94:
     95:     /**
     96:      * Return a collection of Entities.
     97:      *
     98:      * @return \OpenCloud\Common\Collection
     99:      */
    100:     public function getEntities()
    101:     {
    102:         return $this->resourceList('Entity');
    103:     }
    104:
    105:     public function createEntity(array $params)
    106:     {
    107:         return $this->getEntity()->create($params);
    108:     }
    109:
    110:     /**
    111:      * Get either an empty object, or a populated one that exists on the API.
    112:      *
    113:      * @param null $id
    114:      * @return \OpenCloud\CloudMonitoring\Resource\Entity
    115:      */
    116:     public function getEntity($id = null)
    117:     {
    118:         return $this->resource('Entity', $id);
    119:     }
    120:
    121:     /**
    122:      * Get a collection of possible check types.
    123:      *
    124:      * @return \OpenCloud\Common\Collection
    125:      */
    126:     public function getCheckTypes()
    127:     {
    128:         return $this->resourceList('CheckType');
    129:     }
    130:
    131:     /**
    132:      * Get a particular check type.
    133:      *
    134:      * @param null $id
    135:      * @return \OpenCloud\CloudMonitoring\Resource\CheckType
    136:      */
    137:     public function getCheckType($id = null)
    138:     {
    139:         return $this->resource('CheckType', $id);
    140:     }
    141:
    142:     /**
    143:      * Create a new notification.
    144:      *
    145:      * @param array $params
    146:      * @return
    147:      */
    148:     public function createNotification(array $params)
    149:     {
    150:         return $this->getNotification($params)->create();
    151:     }
    152:
    153:     /**
    154:      * Test the parameters of a notification before creating it.
    155:      *
    156:      * @param array $params
    157:      * @return mixed
    158:      */
    159:     public function testNotification(array $params)
    160:     {
    161:         return $this->getNotification()->testParams($params);
    162:     }
    163:
    164:     /**
    165:      * Get a particular notification.
    166:      *
    167:      * @param null $id
    168:      * @return \OpenCloud\CloudMonitoring\Resource\Notification
    169:      */
    170:     public function getNotification($id = null)
    171:     {
    172:         return $this->resource('Notification', $id);
    173:     }
    174:
    175:     /**
    176:      * Get a collection of Notifications.
    177:      *
    178:      * @return \OpenCloud\Common\Collection
    179:      */
    180:     public function getNotifications()
    181:     {
    182:         return $this->resourceList('Notification');
    183:     }
    184:
    185:     /**
    186:      * Create a new notification plan.
    187:      *
    188:      * @param array $params
    189:      * @return mixed
    190:      */
    191:     public function createNotificationPlan(array $params)
    192:     {
    193:         return $this->getNotificationPlan()->create($params);
    194:     }
    195:
    196:     /**
    197:      * Get a particular notification plan.
    198:      *
    199:      * @param null $id
    200:      * @return \OpenCloud\CloudMonitoring\Resource\NotificationPlan
    201:      */
    202:     public function getNotificationPlan($id = null)
    203:     {
    204:         return $this->resource('NotificationPlan', $id);
    205:     }
    206:
    207:     /**
    208:      * Get a collection of notification plans.
    209:      *
    210:      * @return \OpenCloud\Common\Collection
    211:      */
    212:     public function getNotificationPlans()
    213:     {
    214:         return $this->resourceList('NotificationPlan');
    215:     }
    216:
    217:     /**
    218:      * Get a particular notification type.
    219:      *
    220:      * @param null $id
    221:      * @return \OpenCloud\CloudMonitoring\Resource\NotificationType
    222:      */
    223:     public function getNotificationType($id = null)
    224:     {
    225:         return $this->resource('NotificationType', $id);
    226:     }
    227:
    228:     /**
    229:      * Get a collection of notification types.
    230:      *
    231:      * @return \OpenCloud\Common\Collection
    232:      */
    233:     public function getNotificationTypes()
    234:     {
    235:         return $this->resourceList('NotificationType');
    236:     }
    237:
    238:     /**
    239:      * Get a collection of monitoring zones.
    240:      *
    241:      * @return \OpenCloud\Common\Collection
    242:      */
    243:     public function getMonitoringZones()
    244:     {
    245:         return $this->resourceList('Zone');
    246:     }
    247:
    248:     /**
    249:      * Get a particular monitoring zone.
    250:      *
    251:      * @param null $id
    252:      * @return \OpenCloud\CloudMonitoring\Resource\Zone
    253:      */
    254:     public function getMonitoringZone($id = null)
    255:     {
    256:         return $this->resource('Zone', $id);
    257:     }
    258:
    259:     /**
    260:      * Get a changelog - either a general one or one catered for a particular entity.
    261:      *
    262:      * @param string|null $data
    263:      * @return object|false
    264:      */
    265:     public function getChangelog($data = null)
    266:     {
    267:         // Cater for Collections
    268:         if (is_object($data)) {
    269:             return $this->resource('Changelog', $data);
    270:         }
    271:
    272:         $url = $this->resource('Changelog')->getUrl();
    273:
    274:         if ($data) {
    275:             $url->setQuery(array('entityId' => (string) $data));
    276:         }
    277:
    278:         return $this->resourceList('Changelog', $url);
    279:     }
    280:
    281:     /**
    282:      * @return object|false
    283:      */
    284:     public function getViews()
    285:     {
    286:         return $this->resourceList('View');
    287:     }
    288: }
    289: