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: use OpenCloud\Networking\Resource\SecurityGroup;
26: use OpenCloud\Networking\Resource\SecurityGroupRule;
27:
28: /**
29: * The Networking class represents the OpenNetwork Neutron service.
30: *
31: * Neutron is a service that provides networking between devices managed by other
32: * OpenNetwork services (e.g. Compute).
33: */
34: class Service extends CatalogService
35: {
36: const SUPPORTED_VERSION = 'v2.0';
37: const DEFAULT_TYPE = 'network';
38: const DEFAULT_NAME = 'cloudNetworks';
39:
40: /**
41: * Returns a Network object associated with this Networking service
42: *
43: * @param string $id ID of network to retrieve
44: * @return \OpenCloud\Networking\Resource\Network object
45: */
46: public function network($id = null)
47: {
48: return $this->resource('Network', $id);
49: }
50:
51: /**
52: * Creates a new Network and returns it.
53: *
54: * @param array $params Network creation parameters.
55: * @return \OpenCloud\Networking\Resource\Network Object representing created network
56: *
57: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-network
58: */
59: public function createNetwork(array $params = array())
60: {
61: $network = $this->network();
62: $network->create($params);
63: return $network;
64: }
65:
66: /**
67: * Creates multiple new Networks and returns their list.
68: *
69: * @param array $networksParams Array of network creation parameters' arrays
70: * @return \OpenCloud\Common\Collection\PaginatedIterator
71: */
72: public function createNetworks(array $networksParams = array())
73: {
74: // Form URL
75: $url = clone $this->getUrl();
76: $url->addPath(Network::resourceName());
77:
78: // Form JSON
79: $singleNetworkJsonName = Network::jsonName();
80: $networksJsonCollectionName = Network::jsonCollectionName();
81: $networks = array();
82: foreach ($networksParams as $networkParams) {
83: $network = $this->network();
84: $network->populate($networkParams);
85: $networks[] = $network->createJson()->{$singleNetworkJsonName};
86: }
87: $json = json_encode(array(
88: $networksJsonCollectionName => $networks
89: ));
90:
91: // Call the API
92: $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
93:
94: // Parse the response into a collection of created networks
95: $responseJson = Formatter::decode($response);
96: $createdNetworksJson = $responseJson->{$networksJsonCollectionName};
97:
98: // Return collection of created networks
99: return $this->collection('Network', $url, $this, $createdNetworksJson);
100: }
101:
102: /**
103: * Returns a Network object associated with this Networking service
104: *
105: * @param string $id ID of network to retrieve
106: * @return \OpenCloud\Networking\Resource\Network object
107: */
108: public function getNetwork($id)
109: {
110: return $this->network($id);
111: }
112:
113: /**
114: * Returns a list of networks you created
115: *
116: * @param array $params
117: * @return \OpenCloud\Common\Collection\PaginatedIterator
118: */
119: public function listNetworks(array $params = array())
120: {
121: $url = clone $this->getUrl();
122: $url->addPath(Network::resourceName())->setQuery($params);
123:
124: return $this->resourceList('Network', $url);
125: }
126:
127: /**
128: * Returns a Subnet object associated with this Networking service
129: *
130: * @param string $id ID of subnet to retrieve
131: * @return \OpenCloud\Networking\Resource\Subnet object
132: */
133: public function subnet($id = null)
134: {
135: return $this->resource('Subnet', $id);
136: }
137:
138: /**
139: * Creates a new Subnet and returns it.
140: *
141: * @param array $params Subnet creation parameters.
142: * @return \OpenCloud\Networking\Resource\Subnet Object representing created subnet
143: *
144: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-subnet
145: */
146: public function createSubnet(array $params = array())
147: {
148: $subnet = $this->subnet();
149: $subnet->create($params);
150: return $subnet;
151: }
152:
153: /**
154: * Creates multiple new Subnets and returns their list.
155: *
156: * @param array $subnetsParams Array of subnet creation parameters' arrays
157: * @return \OpenCloud\Common\Collection\PaginatedIterator
158: */
159: public function createSubnets(array $subnetsParams = array())
160: {
161: // Form URL
162: $url = clone $this->getUrl();
163: $url->addPath(Subnet::resourceName());
164:
165: // Form JSON
166: $singleSubnetJsonName = Subnet::jsonName();
167: $subnetsJsonCollectionName = Subnet::jsonCollectionName();
168: $subnets = array();
169: foreach ($subnetsParams as $subnetParams) {
170: $subnet = $this->subnet();
171: $subnet->populate($subnetParams);
172: $subnets[] = $subnet->createJson()->{$singleSubnetJsonName};
173: }
174: $json = json_encode(array(
175: $subnetsJsonCollectionName => $subnets
176: ));
177:
178: // Call the API
179: $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
180:
181: // Parse the response into a collection of created subnets
182: $responseJson = Formatter::decode($response);
183: $createdSubnetsJson = $responseJson->{$subnetsJsonCollectionName};
184:
185: // Return collection of created subnets
186: return $this->collection('Subnet', $url, $this, $createdSubnetsJson);
187: }
188:
189: /**
190: * Returns a Subnet object associated with this Networking service
191: *
192: * @param string $id ID of subnet to retrieve
193: * @return \OpenCloud\Networking\Resource\Subnet object
194: */
195: public function getSubnet($id)
196: {
197: return $this->subnet($id);
198: }
199:
200: /**
201: * Returns a list of subnets you created
202: *
203: * @param array $params
204: * @return \OpenCloud\Common\Collection\PaginatedIterator
205: */
206: public function listSubnets(array $params = array())
207: {
208: $url = clone $this->getUrl();
209: $url->addPath(Subnet::resourceName())->setQuery($params);
210:
211: return $this->resourceList('Subnet', $url);
212: }
213:
214: /**
215: * Returns a Port object associated with this Networking service
216: *
217: * @param string $id ID of port to retrieve
218: * @return \OpenCloud\Networking\Resource\Port object
219: */
220: public function port($id = null)
221: {
222: return $this->resource('Port', $id);
223: }
224:
225: /**
226: * Creates a new Port and returns it.
227: *
228: * @param array $params Port creation parameters.
229: * @return \OpenCloud\Networking\Resource\Port Object representing created port
230: *
231: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-port
232: */
233: public function createPort(array $params = array())
234: {
235: $port = $this->port();
236: $port->create($params);
237: return $port;
238: }
239:
240: /**
241: * Creates multiple new Ports and returns their list.
242: *
243: * @param array $portsParams Array of port creation parameters' arrays
244: * @return \OpenCloud\Common\Collection\PaginatedIterator
245: */
246: public function createPorts(array $portsParams = array())
247: {
248: // Form URL
249: $url = clone $this->getUrl();
250: $url->addPath(Port::resourceName());
251:
252: // Form JSON
253: $singlePortJsonName = Port::jsonName();
254: $portsJsonCollectionName = Port::jsonCollectionName();
255: $ports = array();
256: foreach ($portsParams as $portParams) {
257: $port = $this->port();
258: $port->populate($portParams);
259: $ports[] = $port->createJson()->{$singlePortJsonName};
260: }
261: $json = json_encode(array(
262: $portsJsonCollectionName => $ports
263: ));
264:
265: // Call the API
266: $response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
267:
268: // Parse the response into a collection of created ports
269: $responseJson = Formatter::decode($response);
270: $createdPortsJson = $responseJson->{$portsJsonCollectionName};
271:
272: // Return collection of created ports
273: return $this->collection('Port', $url, $this, $createdPortsJson);
274: }
275:
276: /**
277: * Returns a Port object associated with this Networking service
278: *
279: * @param string $id ID of port to retrieve
280: * @return \OpenCloud\Networking\Resource\Port object
281: */
282: public function getPort($id)
283: {
284: return $this->port($id);
285: }
286:
287: /**
288: * Returns a list of ports you created
289: *
290: * @param array $params
291: * @return \OpenCloud\Common\Collection\PaginatedIterator
292: */
293: public function listPorts(array $params = array())
294: {
295: $url = clone $this->getUrl();
296: $url->addPath(Port::resourceName())->setQuery($params);
297:
298: return $this->resourceList('Port', $url);
299: }
300:
301: /**
302: * Returns a SecurityGroup object associated with this Networking service
303: *
304: * @param string $id ID of security group to retrieve
305: * @return \OpenCloud\Networking\Resource\SecurityGroup object
306: */
307: public function securityGroup($id = null)
308: {
309: return $this->resource('SecurityGroup', $id);
310: }
311:
312: /**
313: * Creates a new SecurityGroup and returns it.
314: *
315: * @param array $params SecurityGroup creation parameters.
316: * @return \OpenCloud\Networking\Resource\SecurityGroup Object representing created security group
317: *
318: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-security-group
319: */
320: public function createSecurityGroup(array $params = array())
321: {
322: $securityGroup = $this->securityGroup();
323: $securityGroup->create($params);
324: return $securityGroup;
325: }
326:
327: /**
328: * Returns a SecurityGroup object associated with this Networking service
329: *
330: * @param string $id ID of security group to retrieve
331: * @return \OpenCloud\Networking\Resource\SecurityGroup object
332: */
333: public function getSecurityGroup($id)
334: {
335: return $this->securityGroup($id);
336: }
337:
338: /**
339: * Returns a list of security groups you created
340: *
341: * @param array $params
342: * @return \OpenCloud\Common\Collection\PaginatedIterator
343: */
344: public function listSecurityGroups(array $params = array())
345: {
346: $url = clone $this->getUrl();
347: $url->addPath(SecurityGroup::resourceName())->setQuery($params);
348:
349: return $this->resourceList('SecurityGroup', $url);
350: }
351:
352: /**
353: * Returns a SecurityGroupRule object associated with this Networking service
354: *
355: * @param string $id ID of security group rule to retrieve
356: * @return \OpenCloud\Networking\Resource\SecurityGroupRule object
357: */
358: public function securityGroupRule($id = null)
359: {
360: return $this->resource('SecurityGroupRule', $id);
361: }
362:
363: /**
364: * Creates a new SecurityGroupRule and returns it.
365: *
366: * @param array $params SecurityGroupRule creation parameters.
367: * @return \OpenCloud\Networking\Resource\SecurityGroupRule Object representing created security group rule
368: *
369: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/Networking/USERGUIDE.md#create-a-security-group-rule
370: */
371: public function createSecurityGroupRule(array $params = array())
372: {
373: $securityGroupRule = $this->securityGroupRule();
374: $securityGroupRule->create($params);
375: return $securityGroupRule;
376: }
377:
378: /**
379: * Returns a SecurityGroupRule object associated with this Networking service
380: *
381: * @param string $id ID of security group rule to retrieve
382: * @return \OpenCloud\Networking\Resource\SecurityGroupRule object
383: */
384: public function getSecurityGroupRule($id)
385: {
386: return $this->securityGroupRule($id);
387: }
388:
389: /**
390: * Returns a list of security group rules you created
391: *
392: * @param array $params
393: * @return \OpenCloud\Common\Collection\PaginatedIterator
394: */
395: public function listSecurityGroupRules(array $params = array())
396: {
397: $url = clone $this->getUrl();
398: $url->addPath(SecurityGroupRule::resourceName())->setQuery($params);
399:
400: return $this->resourceList('SecurityGroupRule', $url);
401: }
402:
403: /**
404: * Return namespaces.
405: *
406: * @return array
407: */
408: public function namespaces()
409: {
410: return array();
411: }
412: }
413: