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