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\Networking;
19
20 use OpenCloud\Common\Service\CatalogService;
21 use OpenCloud\Common\Http\Message\Formatter;
22 use OpenCloud\Networking\Resource\Network;
23 use OpenCloud\Networking\Resource\Subnet;
24 use OpenCloud\Networking\Resource\Port;
25
26 /**
27 * The Networking class represents the OpenNetwork Neutron service.
28 *
29 * Neutron is a service that provides networking between devices managed by other
30 * OpenNetwork services (e.g. Compute).
31 */
32 class Service extends CatalogService
33 {
34 const SUPPORTED_VERSION = 'v2.0';
35 const DEFAULT_TYPE = 'network';
36 const DEFAULT_NAME = 'cloudNetworks';
37
38 /**
39 * Returns a Network object associated with this Networking service
40 *
41 * @param string $id ID of network to retrieve
42 * @return \OpenCloud\Networking\Resource\Network object
43 */
44 public function network($id = null)
45 {
46 return $this->resource('Network', $id);
47 }
48
49 /**
50 * Creates a new Network and returns it.
51 *
52 * @param array $params Network creation parameters. @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-network
53 * @return \OpenCloud\Networking\Resource\Network Object representing created network
54 */
55 public function createNetwork(array $params = array())
56 {
57 $network = $this->network();
58 $network->create($params);
59 return $network;
60 }
61
62 /**
63 * Creates multiple new Networks and returns their list.
64 *
65 * @param array $networksParams Array of network creation parameters' arrays
66 * @return \OpenCloud\Common\Collection\PaginatedIterator
67 */
68 public function createNetworks(array $networksParams = array())
69 {
70 // Form URL
71 $url = clone $this->getUrl();
72 $url->addPath(Network::resourceName());
73
74 // Form JSON
75 $singleNetworkJsonName = Network::jsonName();
76 $networksJsonCollectionName = Network::jsonCollectionName();
77 $networks = array();
78 foreach ($networksParams as $networkParams) {
79 $network = $this->network();
80 $network->populate($networkParams);
81 $networks[] = $network->createJson()->{$singleNetworkJsonName};
82 }
83 $json = json_encode(array(
84 $networksJsonCollectionName => $networks
85 ));
86
87 // Call the API
88 $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
89
90 // Parse the response into a collection of created networks
91 $responseJson = Formatter::decode($response);
92 $createdNetworksJson = $responseJson->{$networksJsonCollectionName};
93
94 // Return collection of created networks
95 return $this->collection('Network', $url, $this, $createdNetworksJson);
96 }
97
98 /**
99 * Returns a Network object associated with this Networking service
100 *
101 * @param string $id ID of network to retrieve
102 * @return \OpenCloud\Networking\Resource\Network object
103 */
104 public function getNetwork($id)
105 {
106 return $this->network($id);
107 }
108
109 /**
110 * Returns a list of networks you created
111 *
112 * @param array $params
113 * @return \OpenCloud\Common\Collection\PaginatedIterator
114 */
115 public function listNetworks(array $params = array())
116 {
117 $url = clone $this->getUrl();
118 $url->addPath(Network::resourceName())->setQuery($params);
119
120 return $this->resourceList('Network', $url);
121 }
122
123 /**
124 * Returns a Subnet object associated with this Networking service
125 *
126 * @param string $id ID of subnet to retrieve
127 * @return \OpenCloud\Networking\Resource\Subnet object
128 */
129 public function subnet($id = null)
130 {
131 return $this->resource('Subnet', $id);
132 }
133
134 /**
135 * Creates a new Subnet and returns it.
136 *
137 * @param array $params Subnet creation parameters. @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-subnet
138 * @return \OpenCloud\Networking\Resource\Subnet Object representing created subnet
139 */
140 public function createSubnet(array $params = array())
141 {
142 $subnet = $this->subnet();
143 $subnet->create($params);
144 return $subnet;
145 }
146
147 /**
148 * Creates multiple new Subnets and returns their list.
149 *
150 * @param array $subnetsParams Array of subnet creation parameters' arrays
151 * @return \OpenCloud\Common\Collection\PaginatedIterator
152 */
153 public function createSubnets(array $subnetsParams = array())
154 {
155 // Form URL
156 $url = clone $this->getUrl();
157 $url->addPath(Subnet::resourceName());
158
159 // Form JSON
160 $singleSubnetJsonName = Subnet::jsonName();
161 $subnetsJsonCollectionName = Subnet::jsonCollectionName();
162 $subnets = array();
163 foreach ($subnetsParams as $subnetParams) {
164 $subnet = $this->subnet();
165 $subnet->populate($subnetParams);
166 $subnets[] = $subnet->createJson()->{$singleSubnetJsonName};
167 }
168 $json = json_encode(array(
169 $subnetsJsonCollectionName => $subnets
170 ));
171
172 // Call the API
173 $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
174
175 // Parse the response into a collection of created subnets
176 $responseJson = Formatter::decode($response);
177 $createdSubnetsJson = $responseJson->{$subnetsJsonCollectionName};
178
179 // Return collection of created subnets
180 return $this->collection('Subnet', $url, $this, $createdSubnetsJson);
181 }
182
183 /**
184 * Returns a Subnet object associated with this Networking service
185 *
186 * @param string $id ID of subnet to retrieve
187 * @return \OpenCloud\Networking\Resource\Subnet object
188 */
189 public function getSubnet($id)
190 {
191 return $this->subnet($id);
192 }
193
194 /**
195 * Returns a list of subnets you created
196 *
197 * @param array $params
198 * @return \OpenCloud\Common\Collection\PaginatedIterator
199 */
200 public function listSubnets(array $params = array())
201 {
202 $url = clone $this->getUrl();
203 $url->addPath(Subnet::resourceName())->setQuery($params);
204
205 return $this->resourceList('Subnet', $url);
206 }
207
208 /**
209 * Returns a Port object associated with this Networking service
210 *
211 * @param string $id ID of port to retrieve
212 * @return \OpenCloud\Networking\Resource\Port object
213 */
214 public function port($id = null)
215 {
216 return $this->resource('Port', $id);
217 }
218
219 /**
220 * Creates a new Port and returns it.
221 *
222 * @param array $params Port creation parameters. @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-port
223 * @return \OpenCloud\Networking\Resource\Port Object representing created port
224 */
225 public function createPort(array $params = array())
226 {
227 $port = $this->port();
228 $port->create($params);
229 return $port;
230 }
231
232 /**
233 * Creates multiple new Ports and returns their list.
234 *
235 * @param array $portsParams Array of port creation parameters' arrays
236 * @return \OpenCloud\Common\Collection\PaginatedIterator
237 */
238 public function createPorts(array $portsParams = array())
239 {
240 // Form URL
241 $url = clone $this->getUrl();
242 $url->addPath(Port::resourceName());
243
244 // Form JSON
245 $singlePortJsonName = Port::jsonName();
246 $portsJsonCollectionName = Port::jsonCollectionName();
247 $ports = array();
248 foreach ($portsParams as $portParams) {
249 $port = $this->port();
250 $port->populate($portParams);
251 $ports[] = $port->createJson()->{$singlePortJsonName};
252 }
253 $json = json_encode(array(
254 $portsJsonCollectionName => $ports
255 ));
256
257 // Call the API
258 $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
259
260 // Parse the response into a collection of created ports
261 $responseJson = Formatter::decode($response);
262 $createdPortsJson = $responseJson->{$portsJsonCollectionName};
263
264 // Return collection of created ports
265 return $this->collection('Port', $url, $this, $createdPortsJson);
266 }
267
268 /**
269 * Returns a Port object associated with this Networking service
270 *
271 * @param string $id ID of port to retrieve
272 * @return \OpenCloud\Networking\Resource\Port object
273 */
274 public function getPort($id)
275 {
276 return $this->port($id);
277 }
278
279 /**
280 * Returns a list of ports you created
281 *
282 * @param array $params
283 * @return \OpenCloud\Common\Collection\PaginatedIterator
284 */
285 public function listPorts(array $params = array())
286 {
287 $url = clone $this->getUrl();
288 $url->addPath(Port::resourceName())->setQuery($params);
289
290 return $this->resourceList('Port', $url);
291 }
292
293 /**
294 * Return namespaces.
295 *
296 * @return array
297 */
298 public function namespaces()
299 {
300 return array();
301 }
302 }
303