Skip to content

Commit 733ddd6

Browse files
committed
Adding tests and implementation for the CDN service.
1 parent 601e18c commit 733ddd6

File tree

9 files changed

+501
-0
lines changed

9 files changed

+501
-0
lines changed

lib/OpenCloud/CDN/Resource/Flavor.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Resource;
19+
20+
use OpenCloud\Common\Resource\PersistentResource;
21+
22+
/**
23+
* A service represents your application that has its content cached to the
24+
* edge nodes.
25+
*
26+
* @package OpenCloud\CDN\Resource
27+
*/
28+
class Flavor extends PersistentResource
29+
{
30+
protected static $url_resource = 'flavors';
31+
protected static $json_name = 'flavor';
32+
33+
protected $id;
34+
protected $limits;
35+
protected $providers;
36+
37+
protected $createKeys = array(
38+
'id',
39+
'limits',
40+
'providers'
41+
);
42+
43+
public function update($params = array())
44+
{
45+
return $this->noUpdate();
46+
}
47+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Resource;
19+
20+
use OpenCloud\Common\Resource\PersistentResource;
21+
22+
/**
23+
* A service represents your application that has its content cached to the
24+
* edge nodes.
25+
*
26+
* @package OpenCloud\CDN\Resource
27+
*/
28+
class Service extends PersistentResource
29+
{
30+
protected static $url_resource = 'services';
31+
protected static $json_name = 'service';
32+
33+
const UPDATE_METHOD = 'PATCH';
34+
35+
protected $name;
36+
protected $domains;
37+
protected $origins;
38+
protected $caching;
39+
protected $restrictions;
40+
protected $flavorId;
41+
protected $status;
42+
protected $links;
43+
44+
protected $aliases = array(
45+
'flavor_id' => 'flavorId'
46+
);
47+
48+
protected $createKeys = array(
49+
'name',
50+
'domains',
51+
'origins',
52+
'caching',
53+
'restrictions',
54+
'flavorId'
55+
);
56+
57+
protected $updateKeys = array(
58+
'name',
59+
'domains',
60+
'origins',
61+
'caching',
62+
'restrictions',
63+
'flavorId'
64+
);
65+
66+
public function purgeAssets($assetUrl = null)
67+
{
68+
$assetsUrl = $this->assetsUrl();
69+
if (null === $assetUrl) {
70+
$assetsUrl->setQuery(array('all' => 'true'));
71+
} else {
72+
$assetsUrl->setQuery(array('url' => $assetUrl));
73+
}
74+
75+
return $this->getClient()->delete($assetsUrl)->send();
76+
}
77+
78+
protected function assetsUrl()
79+
{
80+
$url = clone $this->getUrl();
81+
$url->addPath('assets');
82+
83+
return $url;
84+
}
85+
86+
protected function primaryKeyField()
87+
{
88+
return 'name';
89+
}
90+
}

lib/OpenCloud/CDN/Service.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
36+
protected $regionless = true;
37+
38+
/**
39+
* Returns a Service object associated with this CDN service
40+
*
41+
* @param string $id ID of service to retrieve
42+
* @return \OpenCloud\CDN\Resource\Service object
43+
*/
44+
public function service($id = null)
45+
{
46+
return $this->resource('Service', $id);
47+
}
48+
49+
/**
50+
* Creates a new Service and returns it.
51+
*
52+
* @param array $params Service creation parameters. @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/CDN/USERGUIDE.md#create-a-service
53+
* @return \OpenCloud\CDN\Resource\Service Object representing created service
54+
*/
55+
public function createService(array $params = array())
56+
{
57+
$service = $this->service();
58+
$service->create($params);
59+
return $service;
60+
}
61+
62+
/**
63+
* Returns a Service object associated with this CDN service
64+
*
65+
* @param string $id ID of service to retrieve
66+
* @return \OpenCloud\CDN\Resource\Service object
67+
*/
68+
public function getService($id)
69+
{
70+
return $this->service($id);
71+
}
72+
73+
/**
74+
* Returns a list of services you created
75+
*
76+
* @param array $params
77+
* @return \OpenCloud\Common\Collection\PaginatedIterator
78+
*/
79+
public function listServices(array $params = array())
80+
{
81+
$url = clone $this->getUrl();
82+
$url->addPath(ServiceResource::resourceName())->setQuery($params);
83+
84+
return $this->resourceList('Service', $url);
85+
}
86+
87+
/**
88+
* Returns a Flavor object associated with this CDN service
89+
*
90+
* @param string $id ID of flavor to retrieve
91+
* @return \OpenCloud\CDN\Resource\Flavor object
92+
*/
93+
public function flavor($id = null)
94+
{
95+
return $this->resource('Flavor', $id);
96+
}
97+
98+
/**
99+
* Creates a new Flavor and returns it.
100+
*
101+
* @param array $params Flavor creation parameters. @see https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/CDN/USERGUIDE.md#create-a-flavor
102+
* @return \OpenCloud\CDN\Resource\Flavor Object representing created flavor
103+
*/
104+
public function createFlavor(array $params = array())
105+
{
106+
$flavor = $this->flavor();
107+
$flavor->create($params);
108+
return $flavor;
109+
}
110+
111+
/**
112+
* Returns a Flavor object associated with this CDN service
113+
*
114+
* @param string $id ID of flavor to retrieve
115+
* @return \OpenCloud\CDN\Resource\Flavor object
116+
*/
117+
public function getFlavor($id)
118+
{
119+
return $this->flavor($id);
120+
}
121+
122+
/**
123+
* Returns a list of flavors you created
124+
*
125+
* @param array $params
126+
* @return \OpenCloud\Common\Collection\PaginatedIterator
127+
*/
128+
public function listFlavors(array $params = array())
129+
{
130+
$url = clone $this->getUrl();
131+
$url->addPath(Flavor::resourceName())->setQuery($params);
132+
133+
return $this->resourceList('Flavor', $url);
134+
}
135+
136+
/**
137+
* Return namespaces.
138+
*
139+
* @return array
140+
*/
141+
public function namespaces()
142+
{
143+
return array();
144+
}
145+
}

lib/OpenCloud/OpenStack.php

+18
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,22 @@ public function networkingService($name = null, $region = null, $urltype = null)
566566
'urlType' => $urltype
567567
));
568568
}
569+
570+
/**
571+
* Creates a new CDN (Poppy) service object
572+
*
573+
* @param string $name The name of the service as it appears in the Catalog
574+
* @param string $region The region (DFW, IAD, ORD, LON, SYD)
575+
* @param string $urltype The URL type ("publicURL" or "internalURL")
576+
* @return \OpenCloud\Cdn\Service
577+
* @codeCoverageIgnore
578+
*/
579+
public function cdnService($name = null, $region = null, $urltype = null)
580+
{
581+
return ServiceBuilder::factory($this, 'OpenCloud\CDN\Service', array(
582+
'name' => $name,
583+
'region' => $region,
584+
'urlType' => $urltype
585+
));
586+
}
569587
}

lib/OpenCloud/Rackspace.php

+19
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,23 @@ public function queuesService($name = null, $region = null, $urltype = null)
174174
'urlType' => $urltype
175175
));
176176
}
177+
178+
/**
179+
* Creates a new CDN (Rackspace CDN) service object
180+
*
181+
* @param string $name The name of the service as it appears in the Catalog
182+
* @param string $region The region (DFW, IAD, ORD, LON, SYD)
183+
* @param string $urltype The URL type ("publicURL" or "internalURL")
184+
* @return \OpenCloud\Cdn\Service
185+
* @codeCoverageIgnore
186+
*/
187+
public function cdnService($name = null, $region = null, $urltype = null)
188+
{
189+
return ServiceBuilder::factory($this, 'OpenCloud\CDN\Service', array(
190+
'name' => $name,
191+
'type' => 'rax:cdn',
192+
'region' => $region,
193+
'urlType' => $urltype
194+
));
195+
}
177196
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Tests\CDN;
19+
20+
use Guzzle\Http\Message\Response;
21+
use OpenCloud\Tests\OpenCloudTestCase;
22+
23+
class CDNTestCase extends OpenCloudTestCase
24+
{
25+
protected $service;
26+
27+
public function setupObjects()
28+
{
29+
$this->service = $this->getClient()->cdnService();
30+
31+
$this->addMockSubscriber($this->makeResponse('{"name":"mywebsite.com","domains":[{"domain":"blog.mywebsite.com"}],"origins":[{"origin":"mywebsite.com","port":80,"ssl":false},{"origin":"77.66.55.44","port":80,"ssl":false,"rules":[{"name":"videos","request_url":"^/videos/*.m3u"}]}],"caching":[{"name":"default","ttl":3600},{"name":"home","ttl":17200,"rules":[{"name":"index","request_url":"/index.htm"}]},{"name":"images","ttl":12800,"rules":[{"name":"images","request_url":"*.png"}]}],"restrictions":[{"name":"website only","rules":[{"name":"mywebsite.com","http_host":"www.mywebsite.com"}]}],"flavor_id":"cdn","status":"deployed","links":[{"href":"https://global.cdn.api.rackspacecloud.com/v1.0/services/mywebsite.com","rel":"self"},{"href":"mywebsite.com","rel":"access_url"}]}'));
32+
$this->serviceResource = $this->service->getService('mywebsite.com');
33+
}
34+
35+
protected function assertIsService($object)
36+
{
37+
$this->assertInstanceOf('OpenCloud\CDN\Service', $object);
38+
}
39+
40+
protected function assertIsServiceResource($object)
41+
{
42+
$this->assertInstanceOf('OpenCloud\CDN\Resource\Service', $object);
43+
}
44+
45+
protected function assertIsFlavorResource($object)
46+
{
47+
$this->assertInstanceOf('OpenCloud\CDN\Resource\Flavor', $object);
48+
}
49+
}

0 commit comments

Comments
 (0)