Story #1334: As a user I can access a list of CDN-enabled containers.

This commit is contained in:
Matt Butcher
2012-01-30 20:04:42 -06:00
parent ecf3e5ce4e
commit 9e6ac45979
2 changed files with 164 additions and 3 deletions

View File

@@ -29,6 +29,10 @@ class CDN {
* The name of the CDN service type.
*/
const SERVICE_TYPE = 'hpext:cdn';
/**
* The API version.
*/
const API_VERSION = '1.0';
/**
* The URL to the CDN endpoint.
@@ -56,7 +60,13 @@ class CDN {
if ($catalog[$i]['type'] == self::SERVICE_TYPE) {
foreach ($catalog[$i]['endpoints'] as $endpoint) {
if (isset($endpoint['publicURL'])) {
return new CDN($endpoint['publicURL'], $token);
//$parts = parse_url($endpoint['publicURL']);
//$base = $parts['scheme'] . '://' . $parts['host'];
//if (isset($parts['port'])) {
//$base .= ':' . $parts['port'];
//}
$base = $endpoint['publicURL'];
return new CDN($base, $token);
}
}
}
@@ -71,13 +81,91 @@ class CDN {
*
* @param string $endpoint
* The URL of the CDN service. It should look something like this:
* @code https://cdnmgmt.rndd.aw1.hpcloud.net @endcode
* NOT
* @code https://cdnmgmt.rndd.aw1.hpcloud.net/v1.0/72020596871800 @endcode
* @param string $token
* The authentication token. This can be retrieved from IdentityServices::token().
*/
public function __construct($endpoint, $token) {
public function __construct($endpoint, $token/*, $account*/) {
//$this->url = $endpoint . '/v' . self::API_VERSION . '/' . $account;
$this->url = $endpoint;
$this->token = $token;
}
public function containers() {
$client = \HPCloud\Transport::instance();
$url = $this->url . '/?format=json';
//$url = 'https://cdnmgmt.rndd.aw1.hpcloud.net/v1.0/';
$headers = array(
'X-Auth-Token' => $this->token,
);
$response = $client->doRequest($url, 'GET', $headers);
$raw = $response->content();
// throw new \Exception($url . ' ' . $raw);
$json = json_decode($raw, TRUE);
}
/**
* Enable a container.
*
* This turns on caching for the specified container.
*
* @param string $name
* The name of the container.
* @param int $ttl
* Time to live.
* The number of seconds an object may stay in the cache. This is the
* maximum amount of time. There is, however, no assurance that the object
* will remain for the full TTL. 15 minutes is the minimum time. Five years
* is the max.
* @return boolean
* TRUE if the container was enabled, FALSE if the container was already
* CDN-enabled (and thus nothing happened).
* @throws HPCloud::Exception
* Several HTTP-level exceptions can be thrown.
*/
public function enable($name, $ttl = 3600) {
$url = $this->url . '/' . urlencode(rtrim($name, '/'));
$headers = array(
'X-Auth-Token' => $this->token,
'X-TTL' => (int) $ttl,
);
$client = \HPCloud\Transport::instance();
$response = $client->doRequest($url, 'PUT', $headers);
// 201 = success, 202 = already enabled.
return $response->status() == 201;
}
/**
* Attempt to remove a container from CDN.
*
* This will remove a container from CDN services,
* completely stopping all caching on that container.
*
* @param string $name
* The Container name.
* @retval boolean
* TRUE if the container was successfully deleted,
* FALSE if the container was not removed, but no
* error occurred.
* @throws HPCloud::Exception
* Any of the HTTP error subclasses can be thrown.
*/
public function delete($name) {
$url = $this->url . '/' . urlencode($name);
$headers = array(
'X-Auth-Token' => $this->token,
);
$client = \HPCloud\Transport::instance();
$response = $client->doRequest($url, 'DELETE', $headers);
return $response->status() == 204;
}
}

View File

@@ -16,6 +16,13 @@ use \HPCloud\Storage\CDN;
*/
class CDNTest extends \HPCloud\Tests\TestCase {
const TTL = 1234;
protected function destroyCDNFixture($cdn) {
$cname = $this->conf('hpcloud.swift.container');
$cdn->delete($cname);
}
public function testConstructor() {
$ident = $this->identity();
@@ -23,7 +30,8 @@ class CDNTest extends \HPCloud\Tests\TestCase {
$token = $ident->token();
$this->assertNotEmpty($catalog[0]['endpoints'][0]['publicURL']);
$url = $catalog[0]['endpoints'][0]['publicURL'];
$parts = parse_url($catalog[0]['endpoints'][0]['publicURL']);
$url = 'https://' . $parts['host'];
$cdn = new CDN($url, $token);
@@ -31,6 +39,9 @@ class CDNTest extends \HPCloud\Tests\TestCase {
}
/**
* @depends testConstructor
*/
public function testNewFromServiceCatalog() {
$ident = $this->identity();
$token = $ident->token();
@@ -39,6 +50,68 @@ class CDNTest extends \HPCloud\Tests\TestCase {
$cdn = CDN::newFromServiceCatalog($catalog, $token);
$this->assertInstanceOf('\HPCloud\Storage\CDN', $cdn);
return $cdn;
}
/**
* @depends testNewFromServiceCatalog
*/
public function testEnable($cdn) {
$container = $this->conf('hpcloud.swift.container');
$this->destroyCDNFixture($cdn);
$retval = $cdn->enable($container, self::TTL);
$this->assertTrue($retval);
$retval = $cdn->enable($container);
$this->assertFalse($retval);
return $cdn;
}
/**
* @depends testEnable
*/
public function testContainers($cdn) {
$containerList = $cdn->containers();
$cname = $this->conf('hpcloud.swift.container');
$this->assertTrue(is_array($containerList));
$this->assertGreaterThanOrEquals(1, count($containerList));
$find = NULL;
foreach ($containerList as $container) {
if ($container['name'] == $cname) {
$find = $container;
}
}
$this->assertNotEmpty($find);
$this->assertEquals(self::TTL, $find['ttl']);
$this->assertNotEmpty($find['x-cdn-uri']);
$this->assertFalse($find['log_retention']);
$this->assertTrue($find['cdn_enabled']);
return $cdn;
}
/**
* @depend testContainers
*/
public function testDisable($cdn) {
$this->markTestIncomplete();
return $cdn;
}
/**
* @depend testDisableContainer
*/
public function testDelete($cdn) {
$this->markTestIncomplete();
return $cdn;
}
}