PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/OpenCloud/lbresources.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 551 lines | 311 code | 29 blank | 211 comment | 3 complexity | a67148e416920eeda6aff14c720ed8ed MD5 | raw file
  1. <?php
  2. /**
  3. * Defines a a number of classes that are child resources of LoadBalancer
  4. *
  5. * @copyright 2012-2013 Rackspace Hosting, Inc.
  6. * See COPYING for licensing information
  7. *
  8. * @package phpOpenCloud
  9. * @version 1.0
  10. * @author Glen Campbell <glen.campbell@rackspace.com>
  11. */
  12. namespace OpenCloud\LoadBalancerService;
  13. require_once(__DIR__.'/persistentobject.php');
  14. /**
  15. * SubResource is an abstract class that handles subresources of a
  16. * LoadBalancer object; for example, the
  17. * `/loadbalancers/{id}/errorpage`. Since most of the subresources are
  18. * handled in a similar manner, this consolidates the functions.
  19. *
  20. * There are really four pieces of data that define a subresource:
  21. * * `$url_resource` - the actual name of the url component
  22. * * `$json_name` - the name of the JSON object holding the data
  23. * * `$json_collection_name` - if the collection is not simply
  24. * `$json_name . 's'`, this defines the collectio name.
  25. * * `$json_collection_element` - if the object in a collection is not
  26. * anonymous, this defines the name of the element holding the object.
  27. * Of these, only the `$json_name` and `$url_resource` are required.
  28. */
  29. abstract class SubResource extends \OpenCloud\PersistentObject {
  30. private
  31. $parent; // holds the parent load balancer
  32. /**
  33. * constructs the SubResource's object
  34. *
  35. * @param mixed $obj the parent object
  36. * @param mixed $value the ID or array of values for the object
  37. */
  38. public function __construct($obj, $value=NULL) {
  39. $this->parent = $obj;
  40. parent::__construct($obj->Service(), $value);
  41. /**
  42. * Note that, since these sub-resources do not have an ID, we must
  43. * fake out the `Refresh` method.
  44. */
  45. if (isset($this->id))
  46. $this->Refresh();
  47. else
  48. $this->Refresh('<no-id>');
  49. }
  50. /**
  51. * returns the URL of the SubResource
  52. *
  53. * @api
  54. * @param string $subresource the subresource of the parent
  55. * @param array $qstr an array of key/value pairs to be converted to
  56. * query string parameters for the subresource
  57. * @return string
  58. */
  59. public function Url($subresource=NULL, $qstr=array()) {
  60. return $this->Parent()->Url($this->ResourceName());
  61. }
  62. /**
  63. * returns the JSON document's object for creating the subresource
  64. *
  65. * The value `$_create_keys` should be an array of names of data items
  66. * that can be used in the creation of the object.
  67. *
  68. * @return \stdClass;
  69. */
  70. protected function CreateJson() {
  71. $obj = new \stdClass();
  72. $top = $this->JsonName();
  73. if ($top) {
  74. $obj->$top = new \stdClass();
  75. foreach($this->_create_keys as $item)
  76. $obj->$top->$item = $this->$item;
  77. }
  78. else {
  79. foreach($this->_create_keys as $item)
  80. $obj->$item = $this->$item;
  81. }
  82. return $obj;
  83. }
  84. /**
  85. * returns the JSON for the update (same as create)
  86. *
  87. * For these subresources, the update JSON is the same as the Create JSON
  88. * @return \stdClass
  89. */
  90. protected function UpdateJson($params = array()) {
  91. return $this->CreateJson();
  92. }
  93. /**
  94. * returns the Parent object (usually a LoadBalancer, but sometimes another
  95. * SubResource)
  96. *
  97. * @return mixed
  98. */
  99. public function Parent() {
  100. return $this->parent;
  101. }
  102. /**
  103. * returns a (default) name of the object
  104. *
  105. * The name is constructed by the object class and the object's ID.
  106. *
  107. * @api
  108. * @return string
  109. */
  110. public function Name() {
  111. return sprintf(_('%s-%s'),
  112. get_class($this), $this->Parent()->Id());
  113. }
  114. } // end SubResource
  115. /**
  116. * This defines a read-only SubResource - one that cannot be created, updated,
  117. * or deleted. Many subresources are like this, and this simplifies their
  118. * class definitions.
  119. */
  120. abstract class ReadonlySubResource extends SubResource {
  121. /**
  122. * no Create
  123. */
  124. public function Create($params=array()) { $this->NoCreate(); }
  125. /**
  126. * no Update
  127. */
  128. public function Update($params=array()) { $this->NoUpdate(); }
  129. /**
  130. * no Delete
  131. */
  132. public function Delete() { $this->NoDelete(); }
  133. } // end class ReadonlySubResource
  134. /**
  135. * The /loadbalancer/{id}/errorpage manages the error page for the load
  136. * balancer.
  137. */
  138. class ErrorPage extends SubResource {
  139. public
  140. $content;
  141. protected static
  142. $json_name = 'errorpage',
  143. $url_resource = 'errorpage';
  144. protected
  145. $_create_keys = array('content');
  146. /**
  147. * creates a new error page
  148. *
  149. * This calls the Update() method, since it requires a PUT to create
  150. * a new error page. A POST request is not supported, since the URL
  151. * resource is already defined.
  152. *
  153. * @param array $parm array of parameters
  154. */
  155. public function Create($parm=array()) { $this->Update($parm); }
  156. } // end ErrorPage
  157. /**
  158. * Stats returns statistics about the load balancer
  159. */
  160. class Stats extends ReadonlySubResource {
  161. public
  162. $connectTimeOut,
  163. $connectError,
  164. $connectFailure,
  165. $dataTimedOut,
  166. $keepAliveTimedOut,
  167. $maxConn;
  168. protected static
  169. $json_name = FALSE,
  170. $url_resource = 'stats';
  171. }
  172. /**
  173. * information on a single node in the load balancer
  174. *
  175. * This extends `PersistentObject` because it has an ID, unlike most other
  176. * sub-resources.
  177. */
  178. class Node extends \OpenCloud\PersistentObject {
  179. public
  180. $id,
  181. $address,
  182. $port,
  183. $condition,
  184. $status,
  185. $weight,
  186. $type;
  187. protected static
  188. $json_name = FALSE,
  189. $json_collection_name = 'nodes',
  190. $url_resource = 'nodes';
  191. private
  192. $_create_keys = array(
  193. 'address',
  194. 'port',
  195. 'condition',
  196. 'type',
  197. 'weight'
  198. ),
  199. $_lb;
  200. /**
  201. * builds a new Node object
  202. *
  203. * @param LoadBalancer $lb the parent LB object
  204. * @param mixed $info either an ID or an array of values
  205. * @returns void
  206. */
  207. public function __construct(LoadBalancer $lb, $info=NULL) {
  208. $this->_lb = $lb;
  209. parent::__construct($lb->Service(), $info);
  210. }
  211. /**
  212. * returns the parent LoadBalancer object
  213. *
  214. * @return LoadBalancer
  215. */
  216. public function Parent() {
  217. return $this->_lb;
  218. }
  219. /**
  220. * returns the Node name
  221. *
  222. * @return string
  223. */
  224. public function Name() {
  225. return get_class().'['.$this->Id().']';
  226. }
  227. /**
  228. * returns the object for the Create JSON
  229. *
  230. * @return \stdClass
  231. */
  232. protected function CreateJson() {
  233. $obj = new \stdClass();
  234. $obj->nodes = array();
  235. $node = new \stdClass();
  236. $node->node = new \stdClass();
  237. foreach($this->_create_keys as $key) {
  238. $node->node->$key = $this->$key;
  239. }
  240. $obj->nodes[] = $node;
  241. return $obj;
  242. }
  243. /**
  244. * factory method to create a new Metadata child of the Node
  245. *
  246. * @api
  247. * @return Metadata
  248. */
  249. public function Metadata($data=NULL) {
  250. return new Metadata($this, $data);
  251. }
  252. /**
  253. * factory method to create a Collection of Metadata object
  254. *
  255. * Note that these are metadata children of the Node, not of the
  256. * LoadBalancer.
  257. *
  258. * @api
  259. * @return Collection of Metadata
  260. */
  261. public function MetadataList() {
  262. return $this->Service()->Collection(
  263. '\OpenCloud\LoadBalancerService\Metadata', NULL, $this);
  264. }
  265. }
  266. /**
  267. * a single node event, usually called as part of a Collection
  268. *
  269. * This is a read-only subresource.
  270. */
  271. class NodeEvent extends ReadonlySubResource {
  272. public
  273. $detailedMessage,
  274. $nodeId,
  275. $id,
  276. $type,
  277. $description,
  278. $category,
  279. $severity,
  280. $relativeUri,
  281. $accountId,
  282. $loadbalancerId,
  283. $title,
  284. $author,
  285. $created;
  286. protected static
  287. $json_name = 'nodeServiceEvent',
  288. $url_resource = 'nodes/events';
  289. }
  290. /**
  291. * sub-resource to manage allowed domains
  292. *
  293. * Note that this is actually a sub-resource of the load balancers service,
  294. * and not of the load balancer object. It's included here for convenience,
  295. * since it matches the pattern of the other LB subresources.
  296. *
  297. * @api
  298. */
  299. class AllowedDomain extends \OpenCloud\PersistentObject {
  300. public
  301. $name;
  302. protected static
  303. $json_name = 'allowedDomain',
  304. $json_collection_name = 'allowedDomains',
  305. $json_collection_element = 'allowedDomain',
  306. $url_resource = 'loadbalancers/alloweddomains';
  307. public function Create($params=array()) { $this->NoCreate(); }
  308. public function Update($params=array()) { $this->NoUpdate(); }
  309. public function Delete() { $this->NoDelete(); }
  310. }
  311. /**
  312. * VirtualIp represents a single virtual IP (usually returned in a Collection)
  313. *
  314. * Virtual IPs can be added to a load balancer when it is created; however,
  315. * this subresource allows the user to add or update them one at a time.
  316. *
  317. * @api
  318. */
  319. class VirtualIp extends SubResource {
  320. public
  321. $id,
  322. $address,
  323. $type,
  324. $ipVersion;
  325. protected static
  326. $json_collection_name = 'virtualIps',
  327. $json_name = FALSE,
  328. $url_resource = 'virtualips';
  329. protected
  330. $_create_keys = array('type', 'ipVersion');
  331. public function Update($params=array()) { $this->NoUpdate(); }
  332. }
  333. /**
  334. * used to get usage data for a load balancer
  335. */
  336. class Usage extends ReadonlySubResource {
  337. public
  338. $id,
  339. $averageNumConnections,
  340. $incomingTransfer,
  341. $outgoingTransfer,
  342. $averageNumConnectionsSsl,
  343. $incomingTransferSsl,
  344. $outgoingTransferSsl,
  345. $numVips,
  346. $numPolls,
  347. $startTime,
  348. $endTime,
  349. $vipType,
  350. $sslMode,
  351. $eventType;
  352. protected static
  353. $json_name = 'loadBalancerUsageRecord',
  354. $url_resource = 'usage';
  355. } // end Usage
  356. /**
  357. * sub-resource to manage access lists
  358. *
  359. * @api
  360. */
  361. class Access extends SubResource {
  362. public
  363. $id,
  364. $type,
  365. $address;
  366. protected static
  367. $json_name = "accessList",
  368. $url_resource = "accesslist";
  369. protected
  370. $_create_keys = array('type', 'address');
  371. public function Update($params=array()) { $this->NoUpdate(); }
  372. }
  373. /**
  374. * sub-resource to read health monitor info
  375. */
  376. class HealthMonitor extends ReadonlySubResource {
  377. public
  378. $type;
  379. protected static
  380. $json_name = 'healthMonitor',
  381. $url_resource = 'healthmonitor';
  382. } // end HealthMonitor
  383. /**
  384. * sub-resource to manage connection throttling
  385. *
  386. * @api
  387. */
  388. class ConnectionThrottle extends SubResource {
  389. public
  390. $minConnections,
  391. $maxConnections,
  392. $maxConnectionRate,
  393. $rateInterval;
  394. protected static
  395. $json_name = "connectionThrottle",
  396. $url_resource = "connectionthrottle";
  397. protected
  398. $_create_keys = array(
  399. 'minConnections',
  400. 'maxConnections',
  401. 'maxConnectionRate',
  402. 'rateInterval'
  403. );
  404. /**
  405. * create uses PUT like Update
  406. */
  407. public function Create($parm=array()) { $this->Update($parm); }
  408. }
  409. /**
  410. * sub-resource to manage connection logging
  411. *
  412. * @api
  413. */
  414. class ConnectionLogging extends SubResource {
  415. public
  416. $enabled;
  417. protected static
  418. $json_name = "connectionLogging",
  419. $url_resource = "connectionlogging";
  420. protected
  421. $_create_keys = array( 'enabled' );
  422. public function Create($params=array()) { $this->Update($params); }
  423. public function Delete() { $this->NoDelete(); }
  424. }
  425. /**
  426. * sub-resource to manage content caching
  427. *
  428. * @api
  429. */
  430. class ContentCaching extends SubResource {
  431. public
  432. $enabled;
  433. protected static
  434. $json_name = "contentCaching",
  435. $url_resource = "contentcaching";
  436. protected
  437. $_create_keys = array( 'enabled' );
  438. public function Create($parm=array()) { $this->Update($parm); }
  439. public function Delete() { $this->NoDelete(); }
  440. }
  441. /**
  442. * sub-resource to manage session persistence setting
  443. */
  444. class SessionPersistence extends SubResource {
  445. public
  446. $persistenceType;
  447. protected static
  448. $json_name = 'sessionPersistence',
  449. $url_resource = 'sessionpersistence';
  450. private
  451. $_create_keys = array('persistenceType');
  452. }
  453. /**
  454. * sub-resource to manage protocols (read-only)
  455. */
  456. class Protocol extends \OpenCloud\PersistentObject {
  457. public
  458. $name,
  459. $port;
  460. protected static
  461. $json_name = 'protocol',
  462. $url_resource = 'loadbalancers/protocols';
  463. public function Create($params=array()) { $this->NoCreate(); }
  464. public function Update($params=array()) { $this->NoUpdate(); }
  465. public function Delete() { $this->NoDelete(); }
  466. }
  467. /**
  468. * sub-resource to manage algorithms (read-only)
  469. */
  470. class Algorithm extends \OpenCloud\PersistentObject {
  471. public
  472. $name;
  473. protected static
  474. $json_name = 'algorithm',
  475. $url_resource = 'loadbalancers/algorithms';
  476. public function Create($params=array()) { $this->NoCreate(); }
  477. public function Update($params=array()) { $this->NoUpdate(); }
  478. public function Delete() { $this->NoDelete(); }
  479. }
  480. /**
  481. * sub-resource to manage SSL termination
  482. */
  483. class SSLTermination extends SubResource {
  484. public
  485. $certificate,
  486. $enabled,
  487. $secureTrafficOnly,
  488. $privatekey,
  489. $intermediateCertificate,
  490. $securePort;
  491. protected static
  492. $json_name = "sslTermination",
  493. $url_resource = "ssltermination";
  494. protected
  495. $_create_keys = array(
  496. 'certificate',
  497. 'enabled',
  498. 'secureTrafficOnly',
  499. 'privatekey',
  500. 'intermediateCertificate',
  501. 'securePort'
  502. );
  503. public function Create($params=array()) { $this->Update($params); }
  504. }
  505. /**
  506. * sub-resource to manage Metadata
  507. */
  508. class Metadata extends SubResource {
  509. public
  510. $id,
  511. $key,
  512. $value;
  513. protected static
  514. $json_name = 'meta',
  515. $json_collection_name = 'metadata',
  516. $url_resource = 'metadata';
  517. protected
  518. $_create_keys = array('key', 'value');
  519. public function Name() {
  520. return $this->key;
  521. }
  522. }