1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: namespace OpenCloud\Autoscale\Resource;
19:
20: use OpenCloud\Common\Exceptions;
21: use OpenCloud\Common\Http\Message\Formatter;
22:
23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: class Group extends AbstractResource
41: {
42: private $id;
43: private $links;
44: private $groupConfiguration;
45: private $launchConfiguration;
46: private $scalingPolicies;
47: private $name;
48: protected $metadata;
49:
50: private $active;
51: private $activeCapacity;
52: private $pendingCapacity;
53: private $desiredCapacity;
54: private $paused;
55:
56: protected static $json_name = 'group';
57: protected static $url_resource = 'groups';
58: protected static $json_collection_name = 'groups';
59:
60: 61: 62:
63: public $createKeys = array(
64: 'groupConfiguration',
65: 'launchConfiguration',
66: 'scalingPolicies'
67: );
68:
69: 70: 71:
72: public $associatedResources = array(
73: 'groupConfiguration' => 'GroupConfiguration',
74: 'launchConfiguration' => 'LaunchConfiguration',
75:
76: );
77:
78: 79: 80:
81: public $associatedCollections = array(
82: 'scalingPolicies' => 'ScalingPolicy'
83: );
84:
85: 86: 87:
88: public function update($params = array())
89: {
90: return $this->noUpdate();
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: public function getState()
103: {
104: $response = $this->getService()
105: ->getClient()
106: ->get($this->url('state'))
107: ->send();
108:
109: $body = Formatter::decode($response);
110:
111: return (!empty($body->group)) ? $body->group : false;
112: }
113:
114: 115: 116: 117: 118:
119: public function getGroupConfig()
120: {
121: if (($config = $this->getProperty('groupConfiguration')) instanceof GroupConfiguration) {
122: return $config;
123: }
124:
125: $config = $this->getService()->resource('GroupConfiguration');
126: $config->setParent($this);
127: if ($this->getId()) {
128: $config->refresh(null, $config->url());
129: }
130:
131: return $config;
132: }
133:
134: 135: 136: 137: 138:
139: public function getLaunchConfig()
140: {
141: if (($config = $this->getProperty('launchConfiguration')) instanceof LaunchConfiguration) {
142: return $config;
143: }
144:
145: $config = $this->getService()->resource('LaunchConfiguration');
146: $config->setParent($this);
147: if ($this->getId()) {
148: $config->refresh(null, $config->url());
149: }
150:
151: return $config;
152: }
153:
154: 155: 156: 157: 158:
159: public function pause()
160: {
161: return $this->getService()->getClient()->post($this->url('pause'))->send();
162: }
163:
164: 165: 166: 167: 168:
169: public function resume()
170: {
171: return $this->getService()->getClient()->post($this->url('resume'))->send();
172: }
173:
174: 175: 176: 177: 178:
179: public function getScalingPolicies($override = false)
180: {
181: if (null === $this->scalingPolicies || $override === true) {
182: $this->scalingPolicies = $this->getService()->resourceList('ScalingPolicy', null, $this);
183: }
184:
185: return $this->scalingPolicies;
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function getScalingPolicy($id = null)
195: {
196: $config = $this->getService()->resource('ScalingPolicy');
197: $config->setParent($this);
198: if ($id) {
199: $config->populate($id);
200: }
201:
202: return $config;
203: }
204:
205: public function createScalingPolicies(array $policies)
206: {
207: $url = clone $this->getUrl();
208: $url->addPath('policies');
209:
210: $body = json_encode($policies);
211: $this->checkJsonError();
212:
213: return $this->getService()
214: ->getClient()
215: ->post($url, self::getJsonHeader(), $body)
216: ->send();
217: }
218: }
219: