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\Compute\Resource;
19:
20: use Guzzle\Http\Url;
21: use OpenCloud\Common\Exceptions;
22: use OpenCloud\Common\PersistentObject;
23: use OpenCloud\Compute\Constants\Network as NetworkConst;
24: use OpenCloud\Compute\Service;
25:
26: /**
27: * The Network class represents a single virtual network
28: */
29: class Network extends PersistentObject
30: {
31:
32: public $id;
33: public $label;
34: public $cidr;
35:
36: protected static $json_name = 'network';
37: protected static $url_resource = 'os-networksv2';
38: protected static $openStackResourcePath = 'os-networks';
39:
40: /**
41: * Creates a new isolated Network object
42: *
43: * NOTE: contains hacks to recognize the Rackspace public and private
44: * networks. These are not really networks, but they show up in lists.
45: *
46: * @param \OpenCloud\Compute\Service $service The compute service associated with
47: * the network
48: * @param string|null $id The ID of the network (this handles the pseudo-networks
49: * Network::RAX_PUBLIC and Network::RAX_PRIVATE
50: * @return Network
51: */
52: public function __construct(Service $service, $id = null)
53: {
54: $this->id = $id;
55:
56: switch ($id) {
57: case NetworkConst::RAX_PUBLIC:
58: $this->label = 'public';
59: $this->cidr = 'NA';
60: break;
61: case NetworkConst::RAX_PRIVATE:
62: $this->label = 'private';
63: $this->cidr = 'NA';
64: break;
65: default:
66: return parent::__construct($service, $id);
67: }
68:
69: return;
70: }
71:
72: /**
73: * Always throws an error; updates are not permitted
74: *
75: * @throws Exceptions\NetworkUpdateError always
76: */
77: public function update($params = array())
78: {
79: throw new Exceptions\NetworkUpdateError('Isolated networks cannot be updated');
80: }
81:
82: /**
83: * Deletes an isolated network
84: *
85: * @api
86: * @return \OpenCloud\HttpResponse
87: * @throws NetworkDeleteError if HTTP status is not Success
88: */
89: public function delete()
90: {
91: switch ($this->id) {
92: case NetworkConst::RAX_PUBLIC:
93: case NetworkConst::RAX_PRIVATE:
94: throw new Exceptions\DeleteError('Network may not be deleted');
95: default:
96: return parent::delete();
97: }
98: }
99:
100: /**
101: * returns the visible name (label) of the network
102: *
103: * @api
104: * @return string
105: */
106: public function name()
107: {
108: return $this->label;
109: }
110:
111: /**
112: * Creates the JSON object for the Create() method
113: */
114: protected function createJson()
115: {
116: return (object) array(
117: 'network' => (object) array(
118: 'cidr' => $this->cidr,
119: 'label' => $this->label
120: )
121: );
122: }
123:
124: /**
125: * Rackspace Cloud Networks operates on a different URI than OpenStack Neutron.
126: * {@inheritDoc}
127: */
128: public function getUrl($path = null, array $query = array())
129: {
130: if (!$url = $this->findLink('self')) {
131:
132: $url = $this->getParent()->getUrl($this->getResourcePath());
133:
134: if (null !== ($primaryKey = $this->getProperty($this->primaryKeyField()))) {
135: $url->addPath($primaryKey);
136: }
137: }
138:
139: if (!$url instanceof Url) {
140: $url = Url::factory($url);
141: }
142:
143: return $url->addPath($path)->setQuery($query);
144: }
145:
146: /**
147: * Ascertain the correct URI path.
148: *
149: * @return string
150: */
151: public function getResourcePath()
152: {
153: if (strpos((string) $this->getService()->getUrl(), 'rackspacecloud.com') !== false) {
154: return self::$url_resource;
155: } else {
156: return self::$openStackResourcePath;
157: }
158: }
159: }
160: