1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Glen Campbell <glen.campbell@rackspace.com>
8: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
9: */
10:
11: namespace OpenCloud\Autoscale\Resource;
12:
13: use OpenCloud\Common\Exceptions;
14: use OpenCloud\Common\Http\Message\Formatter;
15:
16: /**
17: * An autoscaling group is monitored by Rackspace CloudMonitoring. When
18: * Monitoring triggers an alarm for high utilization within the autoscaling
19: * group, a webhook is triggered. The webhook stimulates the autoscale service
20: * which consults a policy in accordance with the webhook. The policy determines
21: * how many additional cloud servers should be added or removed in accordance
22: * with the alarm.
23: *
24: * There are three components to Autoscale:
25: *
26: * - The Scaling Group Configuration ($this->groupConfiguration)
27: * - The Scaling Group's Launch Configuration ($this->launchConfiguration)
28: * - The Scaling Group's Policies ($this->scalingPolicies)
29: *
30: * @link https://github.com/rackerlabs/otter/blob/master/doc/getting_started.rst
31: * @link http://docs.autoscale.apiary.io/
32: */
33: class Group extends AbstractResource
34: {
35: private $id;
36: private $links;
37: private $groupConfiguration;
38: private $launchConfiguration;
39: private $scalingPolicies;
40: private $name;
41: protected $metadata;
42:
43: private $active;
44: private $activeCapacity;
45: private $pendingCapacity;
46: private $desiredCapacity;
47: private $paused;
48:
49: protected static $json_name = 'group';
50: protected static $url_resource = 'groups';
51: protected static $json_collection_name = 'groups';
52:
53: /**
54: * {@inheritDoc}
55: */
56: public $createKeys = array(
57: 'groupConfiguration',
58: 'launchConfiguration',
59: 'scalingPolicies'
60: );
61:
62: /**
63: * {@inheritDoc}
64: */
65: public $associatedResources = array(
66: 'groupConfiguration' => 'GroupConfiguration',
67: 'launchConfiguration' => 'LaunchConfiguration',
68:
69: );
70:
71: /**
72: * {@inheritDoc}
73: */
74: public $associatedCollections = array(
75: 'scalingPolicies' => 'ScalingPolicy'
76: );
77:
78: /**
79: * {@inheritDoc}
80: */
81: public function update($params = array())
82: {
83: return $this->noUpdate();
84: }
85:
86: /**
87: * Get the current state of the scaling group, including the current set of
88: * active entities, the number of pending entities, and the desired number
89: * of entities.
90: *
91: * @return object|boolean
92: * @throws Exceptions\HttpError
93: * @throws Exceptions\ServerActionError
94: */
95: public function getState()
96: {
97: $response = $this->getService()
98: ->getClient()
99: ->get($this->url('state'))
100: ->send();
101:
102: $body = Formatter::decode($response);
103:
104: return (!empty($body->group)) ? $body->group : false;
105: }
106:
107: /**
108: * Get the group configuration for this autoscale group.
109: *
110: * @return GroupConfiguration
111: */
112: public function getGroupConfig()
113: {
114: if (($config = $this->getProperty('groupConfiguration')) instanceof GroupConfiguration) {
115: return $config;
116: }
117:
118: $config = $this->getService()->resource('GroupConfiguration');
119: $config->setParent($this);
120: if ($this->getId()) {
121: $config->refresh(null, $config->url());
122: }
123: return $config;
124: }
125:
126: /**
127: * Get the launch configuration for this autoscale group.
128: *
129: * @return LaunchConfiguration
130: */
131: public function getLaunchConfig()
132: {
133: if (($config = $this->getProperty('launchConfiguration')) instanceof LaunchConfiguration) {
134: return $config;
135: }
136:
137: $config = $this->getService()->resource('LaunchConfiguration');
138: $config->setParent($this);
139: if ($this->getId()) {
140: $config->refresh(null, $config->url());
141: }
142: return $config;
143: }
144:
145: /**
146: * NB: NOT SUPPORTED YET.
147: *
148: * @codeCoverageIgnore
149: */
150: public function pause()
151: {
152: return $this->getService()->getClient()->post($this->url('pause'))->send();
153: }
154:
155: /**
156: * NB: NOT SUPPORTED YET.
157: *
158: * @codeCoverageIgnore
159: */
160: public function resume()
161: {
162: return $this->getService()->getClient()->post($this->url('resume'))->send();
163: }
164:
165: /**
166: * Get the scaling policies associated with this autoscale group.
167: *
168: * @return Collection
169: */
170: public function getScalingPolicies($override = false)
171: {
172: if (null === $this->scalingPolicies || $override === true) {
173: $this->scalingPolicies = $this->getService()->resourceList('ScalingPolicy', null, $this);
174: }
175: return $this->scalingPolicies;
176: }
177:
178: /**
179: * Get a particular scaling policy for this autoscale group.
180: *
181: * @param object|int $id
182: * @return ScalingPolicy
183: */
184: public function getScalingPolicy($id = null)
185: {
186: $config = $this->getService()->resource('ScalingPolicy');
187: $config->setParent($this);
188: if ($id) {
189: $config->populate($id);
190: }
191: return $config;
192: }
193:
194: }