Overview

Namespaces

  • None
  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Exceptions
      • Log
      • Request
        • Response
    • Compute
    • Database
    • DNS
    • LoadBalancer
      • Resources
    • ObjectStore
      • Resource
    • Orchestration
    • Volume
  • PHP

Classes

  • AbstractResource
  • Account
  • Agent
  • AgentConnection
  • AgentHost
  • AgentHostInfo
  • AgentTarget
  • AgentToken
  • Alarm
  • Changelog
  • Check
  • CheckType
  • Entity
  • Metric
  • Notification
  • NotificationHistory
  • NotificationPlan
  • NotificationType
  • ReadonlyResource
  • View
  • Zone

Interfaces

  • ResourceInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: 
  3: namespace OpenCloud\CloudMonitoring\Resource;
  4: 
  5: use OpenCloud\Common\Exceptions;
  6: use OpenCloud\Common\Lang;
  7: use OpenCloud\Common\PersistentObject;
  8: use OpenCloud\CloudMonitoring\Exception;
  9: 
 10: /**
 11:  * Abstract AbstractResource class.
 12:  * 
 13:  * @abstract
 14:  * @extends PersistentObject
 15:  * @package phpOpenCloud
 16:  * @version 1.0
 17:  * @author  Jamie Hannaford <jamie@limetree.org>
 18:  */
 19: abstract class AbstractResource extends PersistentObject
 20: {
 21: 
 22:     /**
 23:      * Unique identifier
 24:      * 
 25:      * @var mixed
 26:      * @access public
 27:      */
 28:     public $id;
 29: 
 30:     /**
 31:      * Name
 32:      * 
 33:      * @var mixed
 34:      * @access public
 35:      */
 36:     public $name;
 37: 
 38:     /**
 39:      * __construct function.
 40:      * 
 41:      * @access public
 42:      * @param mixed $service
 43:      * @param mixed $info
 44:      * @return void
 45:      */
 46:     public function __construct($service, $info)
 47:     {
 48:         $this->setService($service);
 49:         parent::__construct($service, $info);
 50:     }
 51: 
 52:     /**
 53:      * Retrieve property from array/object.
 54:      * 
 55:      * @access public
 56:      * @param mixed $haystack
 57:      * @param mixed $needle
 58:      * @return void
 59:      */
 60:     public function getProperty($haystack, $needle)
 61:     {
 62:         if (is_object($haystack) && isset($haystack->$needle)) {
 63:             return $haystack->$needle;
 64:         }
 65: 
 66:         if (is_array($haystack) && isset($haystack[$needle])) {
 67:             return $haystack[$needle];
 68:         }
 69: 
 70:         return false;
 71:     }
 72:     
 73:     /**
 74:      * Url function.
 75:      * 
 76:      * @access public
 77:      * @param string $subresource (default: '')
 78:      * @return void
 79:      */
 80:     public function url($subresource = '', $query = array())
 81:     {
 82:         $url = $this->baseUrl();
 83: 
 84:         if ($subresource) {
 85:             $url .= "/$subresource";
 86:         }
 87: 
 88:         return $url . $this->MakeQueryString($query);
 89:     }
 90: 
 91:     /**
 92:      * Procedure for JSON create object.
 93:      * 
 94:      * @access protected
 95:      * @return void
 96:      */
 97:     protected function createJson()
 98:     {
 99:         $object = new \stdClass;
100: 
101:         foreach (static::$emptyObject as $key) {
102:             if (isset($this->$key)) {
103:                 $object->$key = $this->$key;
104:             }
105:         }
106: 
107:         foreach (static::$requiredKeys as $requiredKey) {
108:             if (!isset($object->$requiredKey)) {
109:                 throw new Exceptions\CreateError(sprintf(
110:                     "%s is required to create a new %s", $requiredKey, get_class()
111:                 ));
112:             }
113:         }
114: 
115:         return $object;
116:     }
117: 
118:     /**
119:      * Procedure for JSON update object.
120:      * 
121:      * @access protected
122:      * @return void
123:      */
124:     protected function updateJson($params = array())
125:     {
126:         foreach (static::$requiredKeys as $requiredKey) {
127:             if (!isset($this->$requiredKey)) {
128:                 throw new Exceptions\UpdateError(sprintf(
129:                     "%s is required to create a new %s", $requiredKey, get_class()
130:                 ));
131:             }
132:         }
133: 
134:         return $this;
135:     }
136: 
137:     /**
138:      * Retrieves a collection of resource objects.
139:      * 
140:      * @access public
141:      * @return void
142:      */
143:     public function listAll()
144:     {
145:         return $this->getService()->collection(get_class($this), $this->Url());
146:     }
147: 
148:     public function updateUrl()
149:     {
150:         return $this->url($this->id);
151:     }
152: 
153:     /**
154:      * Update object.
155:      * 
156:      * @access public
157:      * @param array $params (default: array())
158:      * @return void
159:      */
160:     public function update($params = array())
161:     {
162:         // set parameters
163:         foreach ($params as $key => $value) {
164:             $this->$key = $value;
165:         }
166: 
167:         // debug
168:         $this->getLogger()->info('{class}::update({name})', array(
169:             'class' => get_class($this), 
170:             'name'  => $this->name()
171:         ));
172: 
173:         // construct the JSON
174:         $obj = $this->updateJson($params);
175:         $json = json_encode($obj);
176: 
177:         $this->checkJsonError();
178: 
179:         $this->getLogger()->info('{class}::Update JSON [{json}]', array(
180:             'class' => get_class($this), 
181:             'json'  => $json
182:         ));
183: 
184:         // send the request
185:         $response = $this->Service()->Request(
186:             $this->updateUrl(), 'PUT', array(), $json
187:         );
188: 
189:         // @codeCoverageIgnoreStart
190:         if ($response->HttpStatus() > 204) {
191:             throw new Exceptions\UpdateError(sprintf(
192:                 Lang::translate('Error updating [%s] with [%s], status [%d] response [%s]'), 
193:                 get_class($this), 
194:                 $json, 
195:                 $response->HttpStatus(), 
196:                 $response->HttpBody()
197:             ));
198:         }
199:         // @codeCoverageIgnoreEnd
200: 
201:         return $response;
202:     }
203: 
204:     /**
205:      * Delete object.
206:      * 
207:      * @codeCoverageIgnore
208:      * @access public
209:      * @return void
210:      */
211:     public function delete()
212:     {
213:         $this->getLogger()->info('{class}::delete()', array('class' => get_class($this)));
214: 
215:         // send the request
216:         $response = $this->getService()->request($this->url($this->id), 'DELETE');
217: 
218:         // @codeCoverageIgnoreStart
219:         if ($response->HttpStatus() > 204) {
220:             throw new Exceptions\DeleteError(sprintf(
221:                 Lang::translate('Error deleting [%s] [%s], status [%d] response [%s]'), 
222:                 get_class(), 
223:                 $this->Name(), 
224:                 $response->HttpStatus(), 
225:                 $response->HttpBody()
226:             ));
227:         }
228:         // @codeCoverageIgnoreEnd
229: 
230:         return $response;
231:     }
232: 
233:     /**
234:      * Request function.
235:      * 
236:      * @access protected
237:      * @param mixed $url
238:      * @param string $method (default: 'GET')
239:      * @param array $headers (default: array())
240:      * @param mixed $body (default: null)
241:      * @return void
242:      */
243:     protected function request($url, $method = 'GET', array $headers = array(), $body = null)
244:     {
245:         $response = $this->getService()->request($url, $method, $headers, $body);
246:         return ($body = $response->HttpBody()) ? json_decode($body) : false;
247:     }
248: 
249:     /**
250:      * Test the validity of certain parameters for the resource.
251:      * 
252:      * @access public
253:      * @param array $params (default: array())
254:      * @param bool $debug (default: false)
255:      * @return void
256:      */
257:     public function test($params = array(), $debug = false)
258:     {
259:         if (!empty($params)) {
260:             $this->populate($params);
261:         }
262: 
263:         $json = json_encode($this->createJson());
264:         $this->checkJsonError();
265: 
266:         // send the request
267:         return $this->customAction($this->testUrl($debug), 'POST', $json);
268:     }
269: 
270:     /**
271:      * Test the validity of an existing resource.
272:      * 
273:      * @access public
274:      * @param bool $debug (default: false)
275:      * @return void
276:      */
277:     public function testExisting($debug = false)
278:     {
279:         $json = json_encode($this->updateJson());
280:         $this->checkJsonError();
281: 
282:         $url = $this->url($this->id . '/test' . ($debug ? '?debug=true' : ''));
283:         return $this->customAction($url, 'POST', $json);
284:     }
285: 
286:     public function refresh($id = null, $url = null)
287:     {
288:         if (!$url) {
289:             $url = $this->url($id);
290:         }
291:         
292:         parent::refresh($id, $url);
293:     }
294:    
295: }
PHP OpenCloud API API documentation generated by ApiGen 2.8.0