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