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\CDN;
19:
20: use OpenCloud\Common\Service\CatalogService;
21: use OpenCloud\Common\Http\Message\Formatter;
22: use OpenCloud\CDN\Resource\Service as ServiceResource;
23: use OpenCloud\CDN\Resource\Flavor;
24:
25: /**
26: * The CDN class represents the OpenStack Poppy service.
27: *
28: * Poppy is a service that abstracts various CDN providers APIs
29: */
30: class Service extends CatalogService
31: {
32: const SUPPORTED_VERSION = 'v1.0';
33: const DEFAULT_TYPE = 'cdn';
34: const DEFAULT_NAME = 'rackCDN';
35: const MAX_LIMIT = 20;
36:
37: protected $regionless = true;
38:
39: /**
40: * Returns a Service object associated with this CDN service
41: *
42: * @param string $id ID of service to retrieve
43: * @return \OpenCloud\CDN\Resource\Service object
44: */
45: public function service($id = null)
46: {
47: return $this->resource('Service', $id);
48: }
49:
50: /**
51: * Creates a new Service and returns it.
52: *
53: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/CDN/USERGUIDE.md#create-a-service
54: * @param array $params Service creation parameters.
55: * @return \OpenCloud\CDN\Resource\Service Object representing created service
56: */
57: public function createService(array $params = array())
58: {
59: $service = $this->service();
60: $service->create($params);
61: return $service;
62: }
63:
64: /**
65: * Returns a Service object associated with this CDN service
66: *
67: * @param string $id ID of service to retrieve
68: * @return \OpenCloud\CDN\Resource\Service object
69: */
70: public function getService($id)
71: {
72: return $this->service($id);
73: }
74:
75: /**
76: * Returns a list of services you created
77: *
78: * @param array $params
79: * @return \OpenCloud\Common\Collection\PaginatedIterator
80: */
81: public function listServices(array $params = array())
82: {
83: $params['limit'] = isset($params['limit']) && $params['limit'] <= self::MAX_LIMIT ?: self::MAX_LIMIT;
84:
85: $url = clone $this->getUrl();
86: $url->addPath(ServiceResource::resourceName())->setQuery($params);
87:
88: return $this->resourceList('Service', $url);
89: }
90:
91: /**
92: * Returns a Flavor object associated with this CDN service
93: *
94: * @param string $id ID of flavor to retrieve
95: * @return \OpenCloud\CDN\Resource\Flavor object
96: */
97: public function flavor($id = null)
98: {
99: return $this->resource('Flavor', $id);
100: }
101:
102: /**
103: * Creates a new Flavor and returns it.
104: *
105: * @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/CDN/USERGUIDE.md#create-a-flavor
106: * @param array $params Flavor creation parameters.
107: * @return \OpenCloud\CDN\Resource\Flavor Object representing created flavor
108: */
109: public function createFlavor(array $params = array())
110: {
111: $flavor = $this->flavor();
112: $flavor->create($params);
113: return $flavor;
114: }
115:
116: /**
117: * Returns a Flavor object associated with this CDN service
118: *
119: * @param string $id ID of flavor to retrieve
120: * @return \OpenCloud\CDN\Resource\Flavor object
121: */
122: public function getFlavor($id)
123: {
124: return $this->flavor($id);
125: }
126:
127: /**
128: * Returns a list of flavors you created
129: *
130: * @param array $params
131: * @return \OpenCloud\Common\Collection\PaginatedIterator
132: */
133: public function listFlavors(array $params = array())
134: {
135: $url = clone $this->getUrl();
136: $url->addPath(Flavor::resourceName())->setQuery($params);
137:
138: return $this->resourceList('Flavor', $url);
139: }
140:
141: /**
142: * Returns the home document for the CDN service
143: *
144: * @return \stdClass home document response
145: */
146: public function getHomeDocument()
147: {
148: $url = clone $this->getUrl();
149:
150: // This hack is necessary otherwise Guzzle will remove the trailing
151: // slash from the URL and the request will fail because the service
152: // expects the trailing slash :(
153: $url->setPath($url->getPath() . '/');
154:
155: $response = $this->getClient()->get($url)->send();
156: return Formatter::decode($response);
157: }
158:
159: /**
160: * Returns the ping (status) response for the CDN service
161: *
162: * @return Guzzle\Http\Message\Response
163: */
164: public function getPing()
165: {
166: $url = clone $this->getUrl();
167: $url->addPath('ping');
168:
169: $request = $this->getClient()->get($url);
170:
171: // This is necessary because the response does not include a body
172: // and fails with a 406 Not Acceptable if the default
173: // 'Accept: application/json' header is used in the request.
174: $request->removeHeader('Accept');
175:
176: return $request->send();
177: }
178:
179: /**
180: * Return namespaces.
181: *
182: * @return array
183: */
184: public function namespaces()
185: {
186: return array();
187: }
188: }
189: