1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: namespace OpenCloud\Orchestration\Resource;
19:
20: use OpenCloud\Common\Resource\PersistentResource;
21:
22: 23: 24: 25: 26: 27:
28: class Stack extends PersistentResource
29: {
30: protected static $url_resource = 'stacks';
31: protected static $json_name = 'stack';
32:
33: protected $id;
34: protected $parentStack;
35: protected $disableRollback;
36: protected $description;
37: protected $parameters;
38: protected $environment;
39: protected $files;
40: protected $name;
41: protected $status;
42: protected $statusReason;
43: protected $outputs;
44: protected $creationTime;
45: protected $updatedTime;
46: protected $timeoutMins;
47: protected $templateUrl;
48: protected $template;
49: protected $adoptStackData;
50: protected $links;
51:
52: protected $aliases = array(
53: 'parent' => 'parentStack',
54: 'disable_rollback' => 'disableRollback',
55: 'stack_name' => 'name',
56: 'stack_status' => 'status',
57: 'stack_status_reason' => 'statusReason',
58: 'creation_time' => 'creationTime',
59: 'updated_time' => 'updatedTime',
60: 'timeout_mins' => 'timeoutMins',
61: 'template_url' => 'templateUrl',
62: 'adopt_stack_data' => 'adoptStackData'
63: );
64:
65: protected $createKeys = array(
66: 'name',
67: 'templateUrl',
68: 'template',
69: 'environment',
70: 'files',
71: 'parameters',
72: 'timeoutMins',
73: 'adoptStackData'
74: );
75:
76: protected $updateKeys = array(
77: 'templateUrl',
78: 'template',
79: 'environment',
80: 'files',
81: 'parameters',
82: 'timeoutMins'
83: );
84:
85: protected function createJson()
86: {
87: $createJson = parent::createJson();
88: return $createJson->{self::$json_name};
89: }
90:
91: protected function updateJson($params = array())
92: {
93: $updateJson = parent::updateJson($params);
94: return $updateJson->{self::$json_name};
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function adopt($params)
104: {
105:
106: $requiredParameterName = 'adoptStackData';
107: if (!array_key_exists($requiredParameterName, $params)) {
108: throw new \InvalidArgumentException($requiredParameterName . ' is a required option');
109: }
110:
111: return $this->create($params);
112: }
113:
114: 115: 116: 117: 118: 119:
120: public function preview($params = array())
121: {
122:
123: if (!empty($params)) {
124: $this->populate($params, false);
125: }
126:
127:
128: $json = json_encode($this->createJson());
129: $this->checkJsonError();
130:
131: $previewUrl = $this->previewUrl();
132: $response = $this->getClient()->post($previewUrl, self::getJsonHeader(), $json)->send();
133:
134: $decoded = $this->parseResponse($response);
135: $this->populate($decoded);
136:
137: return $response;
138: }
139:
140: 141: 142: 143: 144:
145: public function abandon()
146: {
147: $abandonUrl = $this->abandonUrl();
148: $response = $this->getClient()->delete($abandonUrl)->send();
149: return $response->getBody(true);
150: }
151:
152: 153: 154: 155: 156: 157:
158: public function getResource($name)
159: {
160: return $this->getService()->resource('Resource', $name, $this);
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function listResources(array $params = array())
170: {
171: $url = clone $this->getUrl();
172: $url->addPath(Resource::resourceName())->setQuery($params);
173:
174: return $this->getService()->resourceList('Resource', $url, $this);
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function listEvents(array $params = array())
184: {
185: $url = clone $this->getUrl();
186: $url->addPath(Event::resourceName())->setQuery($params);
187:
188: return $this->getService()->resourceList('Event', $url, $this);
189: }
190:
191: 192: 193:
194: public function event($id)
195: {
196: return $this->getService()->resource('Event', $id, $this);
197: }
198:
199: 200: 201: 202: 203:
204: public function getStackTemplate()
205: {
206: $url = clone $this->getUrl();
207: $url->addPath('template');
208:
209: $response = $this->getClient()->get($url)->send();
210: return $response->getBody(true);
211: }
212:
213: protected function previewUrl()
214: {
215: $url = clone $this->getParent()->getUrl();
216: $url->addPath(self::resourceName());
217: $url->addPath('preview');
218:
219: return $url;
220: }
221:
222: protected function abandonUrl()
223: {
224: $url = clone $this->getUrl();
225: $url->addPath('abandon');
226:
227: return $url;
228: }
229:
230: protected function primaryKeyField()
231: {
232: return 'name';
233: }
234: }
235: