1: <?php
2:
3: /**
4: * An abstract class that defines shared components for products that use
5: * OpenStack Nova
6: *
7: * @copyright 2012-2013 Rackspace Hosting, Inc.
8: * See COPYING for licensing information
9: *
10: * @package phpOpenCloud
11: * @version 1.0
12: * @author Glen Campbell <glen.campbell@rackspace.com>
13: */
14:
15: namespace OpenCloud\Common;
16:
17: use OpenCloud\OpenStack;
18: use OpenCloud\Common\Lang;
19: use OpenCloud\Compute\Flavor;
20:
21: /**
22: * Nova is an abstraction layer for the OpenStack compute service.
23: *
24: * Nova is used as a basis for several products, including Compute services
25: * as well as Rackspace's Cloud Databases. This class is, in essence, a vehicle
26: * for sharing common code between those other classes.
27: */
28: abstract class Nova extends Service
29: {
30:
31: private $_url;
32:
33: /**
34: * Called when creating a new Compute service object
35: *
36: * _NOTE_ that the order of parameters for this is *different* from the
37: * parent Service class. This is because the earlier parameters are the
38: * ones that most typically change, whereas the later ones are not
39: * modified as often.
40: *
41: * @param \OpenCloud\Identity $conn - a connection object
42: * @param string $serviceRegion - identifies the region of this Compute
43: * service
44: * @param string $urltype - identifies the URL type ("publicURL",
45: * "privateURL")
46: * @param string $serviceName - identifies the name of the service in the
47: * catalog
48: */
49: public function __construct(
50: OpenStack $conn,
51: $serviceType,
52: $serviceName,
53: $serviceRegion,
54: $urltype
55: ) {
56: parent::__construct(
57: $conn,
58: $serviceType,
59: $serviceName,
60: $serviceRegion,
61: $urltype
62: );
63:
64: $this->_url = Lang::noslash(parent::Url());
65:
66: $this->getLogger()->info(Lang::translate('Initializing Nova...'));
67: }
68:
69: /**
70: * Returns a flavor from the service
71: *
72: * This is a factory method and should generally be called instead of
73: * creating a Flavor object directly.
74: *
75: * @api
76: * @param string $id - if supplied, the Flavor identified by this is
77: * retrieved
78: * @return Compute\Flavor object
79: */
80: public function Flavor($id = null)
81: {
82: return new Flavor($this, $id);
83: }
84:
85: /**
86: * Returns a list of Flavor objects
87: *
88: * This is a factory method and should generally be called instead of
89: * creating a FlavorList object directly.
90: *
91: * @api
92: * @param boolean $details - if TRUE (the default), returns full details.
93: * Set to FALSE to retrieve minimal details and possibly improve
94: * performance.
95: * @param array $filter - optional key/value pairs for creating query
96: * strings
97: * @return Collection (or FALSE on an error)
98: */
99: public function FlavorList($details = true, array $filter = array())
100: {
101: if ($details) {
102: $url = $this->Url(Flavor::ResourceName().'/detail', $filter);
103: } else {
104: $url = $this->Url(Flavor::ResourceName(), $filter);
105: }
106: return $this->Collection('\OpenCloud\Compute\Flavor', $url);
107: }
108:
109: /**
110: * Gets a request from an HTTP source and ensures that the
111: * content type is always "application/json"
112: *
113: * This is a simple subclass of the parent::Request() method that ensures
114: * that all Compute requests use application/json as the Content-Type:
115: *
116: * @param string $url - the URL of the request
117: * @param string $method - the HTTP method ("GET" by default)
118: * @param array $headers - an associative array of headers to pass to
119: * the request
120: * @param string $body - optional body for POST or PUT requests
121: * @return \Rackspace\HttpResult object
122: */
123: public function Request($url, $method = 'GET', array $headers = array(), $body = null)
124: {
125: $headers['Content-Type'] = RAXSDK_CONTENT_TYPE_JSON;
126: return parent::Request($url, $method, $headers, $body);
127: }
128:
129: /**
130: * Loads the available namespaces from the /extensions resource
131: */
132: protected function load_namespaces()
133: {
134: $ext = $this->Extensions();
135: foreach($ext as $obj) {
136: $this->_namespaces[] = $obj->alias;
137: }
138: }
139:
140: }
141: