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;
19:
20: use OpenCloud\Common\Exceptions\CredentialError;
21: use OpenCloud\Common\Service\ServiceBuilder;
22:
23: /**
24: * Rackspace extends the OpenStack class with support for Rackspace's
25: * API key and tenant requirements.
26: *
27: * The only difference between Rackspace and OpenStack is that the
28: * Rackspace class generates credentials using the username
29: * and API key, as required by the Rackspace authentication
30: * service.
31: *
32: * Example:
33: * <pre><code>
34: * $client = new Rackspace(
35: * 'https://identity.api.rackspacecloud.com/v2.0/',
36: * array(
37: * 'username' => 'FRED',
38: * 'apiKey' => '0900af093093788912388fc09dde090ffee09'
39: * )
40: * );
41: * </code></pre>
42: */
43: class Rackspace extends OpenStack
44: {
45: const US_IDENTITY_ENDPOINT = 'https://identity.api.rackspacecloud.com/v2.0/';
46: const UK_IDENTITY_ENDPOINT = 'https://lon.identity.api.rackspacecloud.com/v2.0/';
47:
48: /**
49: * Generates Rackspace API key credentials
50: * {@inheritDoc}
51: */
52: public function getCredentials()
53: {
54: $secret = $this->getSecret();
55:
56: if (!empty($secret['username']) && !empty($secret['apiKey'])) {
57:
58: $credentials = array('auth' => array(
59: 'RAX-KSKEY:apiKeyCredentials' => array(
60: 'username' => $secret['username'],
61: 'apiKey' => $secret['apiKey']
62: )
63: ));
64:
65: if (!empty($secret['tenantName'])) {
66: $credentials['auth']['tenantName'] = $secret['tenantName'];
67: } elseif (!empty($secret['tenantId'])) {
68: $credentials['auth']['tenantId'] = $secret['tenantId'];
69: }
70:
71: return json_encode($credentials);
72: } else {
73: throw new CredentialError('Unrecognized credential secret');
74: }
75: }
76:
77: /**
78: * Creates a new Database service. Note: this is a Rackspace-only feature.
79: *
80: * @param string $name The name of the service as it appears in the Catalog
81: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
82: * @param string $urltype The URL type ("publicURL" or "internalURL")
83: * @return \OpenCloud\Database\Service
84: */
85: public function databaseService($name = null, $region = null, $urltype = null)
86: {
87: return ServiceBuilder::factory($this, 'OpenCloud\Database\Service', array(
88: 'name' => $name,
89: 'region' => $region,
90: 'urlType' => $urltype
91: ));
92: }
93:
94: /**
95: * Creates a new Load Balancer service. Note: this is a Rackspace-only feature.
96: *
97: * @param string $name The name of the service as it appears in the Catalog
98: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
99: * @param string $urltype The URL type ("publicURL" or "internalURL")
100: * @return \OpenCloud\LoadBalancer\Service
101: */
102: public function loadBalancerService($name = null, $region = null, $urltype = null)
103: {
104: return ServiceBuilder::factory($this, 'OpenCloud\LoadBalancer\Service', array(
105: 'name' => $name,
106: 'region' => $region,
107: 'urlType' => $urltype
108: ));
109: }
110:
111: /**
112: * Creates a new DNS service. Note: this is a Rackspace-only feature.
113: *
114: * @param string $name The name of the service as it appears in the Catalog
115: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
116: * @param string $urltype The URL type ("publicURL" or "internalURL")
117: * @return OpenCloud\DNS\Service
118: */
119: public function dnsService($name = null, $region = null, $urltype = null)
120: {
121: return ServiceBuilder::factory($this, 'OpenCloud\DNS\Service', array(
122: 'name' => $name,
123: 'region' => $region,
124: 'urlType' => $urltype
125: ));
126: }
127:
128: /**
129: * Creates a new CloudMonitoring service. Note: this is a Rackspace-only feature.
130: *
131: * @param string $name The name of the service as it appears in the Catalog
132: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
133: * @param string $urltype The URL type ("publicURL" or "internalURL")
134: * @return \OpenCloud\CloudMonitoring\Service
135: */
136: public function cloudMonitoringService($name = null, $region = null, $urltype = null)
137: {
138: return ServiceBuilder::factory($this, 'OpenCloud\CloudMonitoring\Service', array(
139: 'name' => $name,
140: 'region' => $region,
141: 'urlType' => $urltype
142: ));
143: }
144:
145: /**
146: * Creates a new CloudQueues service. Note: this is a Rackspace-only feature.
147: *
148: * @param string $name The name of the service as it appears in the Catalog
149: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
150: * @param string $urltype The URL type ("publicURL" or "internalURL")
151: * @return \OpenCloud\Autoscale\Service
152: */
153: public function autoscaleService($name = null, $region = null, $urltype = null)
154: {
155: return ServiceBuilder::factory($this, 'OpenCloud\Autoscale\Service', array(
156: 'name' => $name,
157: 'region' => $region,
158: 'urlType' => $urltype
159: ));
160: }
161:
162: /**
163: * Creates a new CloudQueues service. Note: this is a Rackspace-only feature.
164: *
165: * @param string $name The name of the service as it appears in the Catalog
166: * @param string $region The region (DFW, IAD, ORD, LON, SYD)
167: * @param string $urltype The URL type ("publicURL" or "internalURL")
168: * @return \OpenCloud\Queues\Service
169: */
170: public function queuesService($name = null, $region = null, $urltype = null)
171: {
172: return ServiceBuilder::factory($this, 'OpenCloud\Queues\Service', array(
173: 'name' => $name,
174: 'region' => $region,
175: 'urlType' => $urltype
176: ));
177: }
178: }
179: