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;
19:
20: use OpenCloud\Common\Exceptions;
21: use OpenCloud\Common\Http\Client;
22: use OpenCloud\Common\Service\NovaService;
23:
24: /**
25: * The Compute class represents the OpenStack Nova service.
26: *
27: * It is constructed from a OpenStack object and requires a service name,
28: * region, and URL type to select the proper endpoint from the service
29: * catalog. However, constants can be used to define default values for
30: * these to make it easier to use:
31: *
32: * Creating a compute object:
33: *
34: * <code>
35: * $rackspace = new OpenCloud\Rackspace(...);
36: * $dallas = new Compute(
37: * $rackspace, // connection
38: * 'cloudServersOpenStack', // the service's name
39: * 'DFW', // region identifier
40: * 'publicURL' // URL type
41: * );
42: * </code>
43: *
44: * The easy way (with defaults); this assumes that the constants (RAXSDK_...)
45: * are defined elsewhere *before* the inclusion of the first SDK library file:
46: *
47: * <code>
48: * $rackspace = new OpenCloud\Rackspace(...);
49: * $dallas = new OpenCloud\Compute($rackspace); // uses defaults
50: * </code>
51: *
52: */
53: class Service extends NovaService
54: {
55: const DEFAULT_TYPE = 'compute';
56: const DEFAULT_NAME = 'cloudServersOpenStack';
57:
58: protected $additionalExtensions = array('OS-FLV-DISABLED');
59:
60: public function __construct(Client $client, $type = null, $name = null, $region = null, $urlType = null)
61: {
62: parent::__construct($client, $type, $name, $region, $urlType);
63:
64: // @see https://github.com/rackspace/php-opencloud/issues/353
65: if (strpos($this->getUrl()->getPath(), '/v1.0/') !== false) {
66: throw new Exceptions\UnsupportedVersionError(sprintf(
67: 'Sorry; API version /v1.0 is not supported [%s]',
68: $this->getUrl()
69: ));
70: }
71:
72: $this->loadNamespaces();
73: }
74:
75: /**
76: * Returns a Server object associated with this Compute service
77: *
78: * This is a factory method and should generally be used to create server
79: * objects (thus ensuring that they are correctly associated with the
80: * server) instead of calling the Server class explicitly.
81: *
82: * @api
83: * @param string $id - if specified, the server with the ID is retrieved
84: * @returns Resource\Server object
85: */
86: public function server($id = null)
87: {
88: return new Resource\Server($this, $id);
89: }
90:
91: /**
92: * Returns a Collection of server objects, filtered by the specified
93: * parameters
94: *
95: * This is a factory method and should normally be called instead of
96: * creating a ServerList object directly.
97: *
98: * @api
99: * @param boolean $details - if TRUE, full server details are returned; if
100: * FALSE, just the minimal set of info is listed. Defaults to TRUE;
101: * you might set this to FALSE to improve performance at the risk of
102: * not having all the information you need.
103: * @param array $filter - a set of key/value pairs that is passed to the
104: * servers list for filtering
105: * @returns \OpenCloud\Common\Collection
106: */
107: public function serverList($details = true, array $filter = array())
108: {
109: $url = $this->getUrl(Resource\Server::resourceName() . (($details) ? '/detail' : ''), $filter);
110:
111: return $this->collection('OpenCloud\Compute\Resource\Server', $url);
112: }
113:
114: /**
115: * Returns a Network object
116: *
117: * @api
118: * @param string $id the network ID
119: * @return Resource\Network
120: */
121: public function network($id = null)
122: {
123: return new Resource\Network($this, $id);
124: }
125:
126: /**
127: * Returns a Collection of Network objects
128: *
129: * @api
130: * @param array $filter array of filter key/value pairs
131: * @return \OpenCloud\Common\Collection
132: */
133: public function networkList($filter = array())
134: {
135: return $this->collection('OpenCloud\Compute\Resource\Network');
136: }
137:
138: /**
139: * Returns an image from the service
140: *
141: * This is a factory method and should normally be called instead of
142: * creating an Image object directly.
143: *
144: * @api
145: * @param string $id - if supplied, returns the image with the specified ID.
146: * @return Resource\Image object
147: */
148: public function image($id = null)
149: {
150: return new Resource\Image($this, $id);
151: }
152:
153: /**
154: * Returns a Collection of images (class Image)
155: *
156: * This is a factory method and should normally be used instead of creating
157: * an ImageList object directly.
158: *
159: * @api
160: * @param boolean $details - if TRUE (the default), returns complete image
161: * details. Set to FALSE to improve performance, but only return a
162: * minimal set of data
163: * @param array $filter - key/value pairs to pass to the images resource.
164: * The actual values available here are determined by the OpenStack
165: * code and any extensions installed by your cloud provider;
166: * see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Images-d1e4435.html
167: * for current filters available.
168: * @return \OpenCloud\Common\Collection
169: */
170: public function imageList($details = true, array $filter = array())
171: {
172: $url = clone $this->getUrl();
173: $url->addPath('images');
174:
175: if ($details === true) {
176: $url->addPath('detail');
177: }
178:
179: if (count($filter)) {
180: $url->setQuery($filter);
181: }
182:
183: return $this->resourceList('Image', $url);
184: }
185:
186: public function keypair($data = null)
187: {
188: return $this->resource('KeyPair', $data);
189: }
190:
191: public function listKeypairs()
192: {
193: return $this->resourceList('KeyPair', null, $this);
194: }
195: }
196: