1 <?php
2 /**
3 * Copyright 2012-2014 Rackspace US, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace OpenCloud\Orchestration;
19
20 use Guzzle\Http\Exception\ClientErrorResponseException;
21 use OpenCloud\Common\Exceptions\InvalidTemplateError;
22 use OpenCloud\Common\Http\Message\Formatter;
23 use OpenCloud\Common\Service\CatalogService;
24 use OpenCloud\Orchestration\Resource\ResourceType;
25 use OpenCloud\Orchestration\Resource\Stack;
26
27 /**
28 * The Orchestration class represents the OpenStack Heat service.
29 *
30 * Heat is a service to orchestrate multiple composite cloud applications using
31 * the AWS CloudFormation template format, through both an OpenStack-native ReST
32 * API and a CloudFormation-compatible Query API.
33 *
34 * @codeCoverageIgnore
35 */
36 class Service extends CatalogService
37 {
38 const DEFAULT_TYPE = 'orchestration';
39 const DEFAULT_NAME = 'cloudOrchestration';
40
41 /**
42 * Returns a Stack object associated with this Orchestration service
43 *
44 * @param string $name Name of stack to retrieve
45 * @return Stack object
46 */
47 public function stack($name = null)
48 {
49 return $this->resource('Stack', $name);
50 }
51
52 /**
53 * Previews a Stack from a template and returns it.
54 *
55 * @param array $params Stack preview parameters
56 * @return Stack Object representing previewed stack
57 */
58 public function previewStack($params = array())
59 {
60 $stack = $this->stack();
61 $stack->preview($params);
62 return $stack;
63 }
64
65 /**
66 * Creates a new Stack and returns it.
67 *
68 * @param array $params Stack creation parameters
69 * @return Stack Object representing created stack
70 */
71 public function createStack($params = array())
72 {
73 $stack = $this->stack();
74 $stack->create($params);
75 return $stack;
76 }
77
78 /**
79 * Adopts a Stack and returns it.
80 *
81 * @param array $params Stack adoption parameters
82 * @return Stack Object representing adopted stack
83 */
84 public function adoptStack($params = array())
85 {
86 $stack = $this->stack();
87 $stack->adopt($params);
88 return $stack;
89 }
90
91 /**
92 * Returns a Stack object associated with this Orchestration service
93 *
94 * @param string $name Name of stack to retrieve
95 * @return Stack object
96 */
97 public function getStack($name)
98 {
99 return $this->stack($name);
100 }
101
102 /**
103 * Returns a list of stacks you created
104 *
105 * @param array $params
106 * @return \OpenCloud\Common\Collection\PaginatedIterator
107 */
108 public function listStacks(array $params = array())
109 {
110 $url = clone $this->getUrl();
111 $url->addPath(Stack::resourceName())->setQuery($params);
112
113 return $this->resourceList('Stack', $url);
114 }
115
116 /**
117 * Returns a list of resource types available
118 *
119 * @param array $params
120 * @return \OpenCloud\Common\Collection\PaginatedIterator
121 */
122 public function listResourceTypes(array $params = array())
123 {
124 $url = clone $this->getUrl();
125 $url->addPath(ResourceType::resourceName())->setQuery($params);
126
127 return $this->resourceList('ResourceType', $url);
128 }
129
130 /**
131 * Returns a ResourceType object associated with this Orchestration service
132 *
133 * @param string $id - the resource type with the ID is retrieved
134 * @return ResourceType object
135 */
136 public function getResourceType($id)
137 {
138 return $this->resource('ResourceType', $id);
139 }
140
141 /**
142 * Returns a BuildInfo object associated with this Orchestration service
143 *
144 * @return BuildInfo object
145 */
146 public function getBuildInfo()
147 {
148 $buildInfo = $this->resource('BuildInfo');
149 $buildInfo->refresh();
150 return $buildInfo;
151 }
152
153 /**
154 * Validates the given template
155 *
156 * @throws InvalidTemplateError if template is invalid
157 */
158 public function validateTemplate(array $params = array())
159 {
160 $url = clone $this->getUrl();
161 $url->addPath('validate');
162
163 // Aliases
164 if (array_key_exists('templateUrl', $params)) {
165 $params['template_url'] = $params['templateUrl'];
166 }
167
168 $json = json_encode($params);
169
170 try {
171 $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
172 } catch (ClientErrorResponseException $e) {
173 $response = Formatter::decode($e->getResponse());
174 throw new InvalidTemplateError($response->explanation, $response->code);
175 }
176 }
177
178 /**
179 * Return namespaces.
180 *
181 * @return array
182 */
183 public function namespaces()
184 {
185 return array();
186 }
187 }
188