SAAS-517: Create Provisioning controller with a terminate endpoint

This commit is contained in:
drigato 2014-12-11 13:35:34 -05:00
parent 008ffa025a
commit 891cfdb48e
5 changed files with 70 additions and 37 deletions

View file

@ -58,4 +58,47 @@ class Amazon_S3 extends StorageBackend
throw new Exception("ERROR: Could not locate file to delete."); throw new Exception("ERROR: Could not locate file to delete.");
} }
} }
public function deleteObjects()
{
$cloudFilePager = CloudFileQuery::create()
->filterByStorageBackend("amazon_S3")
->paginate($page=1, $maxPerPage=1000);
if ($cloudFilePager->haveToPaginate()) {
$numPages = $cloudFilePager->getLastPage();
$currentPage = 1;
while ($currentPage <= $numPages) {
$cloudFilePager = CloudFileQuery::create()
->filterByStorageBackend("amazon_S3")
->paginate($page = $currentPage, $maxPerPage = 1000);
$this->deleteObjectSet($cloudFilePager->getResults());
$currentPage += 1;
}
} else {
$this->deleteObjectSet($cloudFilePager->getResults());
}
}
/**
* Deletes objects from Amazon S3 1000 at a time.
* 1000 is the max number of objects that can be deleted using the aws sdk
* api, per request.
*/
private function deleteObjectSet($cloudFiles)
{
if (!$cloudFiles->isEmpty()) {
$cloudFilesToDelete = array();
foreach ($cloudFiles as $cloudFile) {
array_push($cloudFilesToDelete, array("Key" => $cloudFile->getResourceId()));
}
$this->s3Client->deleteObjects(array(
"Bucket" => $this->getBucket(),
"Objects" => $cloudFilesToDelete));
}
}
} }

View file

@ -42,5 +42,10 @@ class ProxyStorageBackend extends StorageBackend
{ {
$this->storageBackend->deletePhysicalFile($resourceId); $this->storageBackend->deletePhysicalFile($resourceId);
} }
public function deleteObjects()
{
$this->storageBackend->deleteObjects();
}
} }

View file

@ -22,6 +22,9 @@ abstract class StorageBackend
/** Deletes the file from the storage backend. */ /** Deletes the file from the storage backend. */
abstract public function deletePhysicalFile($resourceId); abstract public function deletePhysicalFile($resourceId);
/** Deletes multiple objects (files) stored on the cloud service. */
abstract public function deleteObjects();
protected function getBucket() protected function getBucket()
{ {

View file

@ -29,8 +29,8 @@ class Config {
$cloudStorageConfig = isset($_SERVER['CLOUD_STORAGE_CONF']) ? $_SERVER['CLOUD_STORAGE_CONF'] : "/etc/airtime-saas/cloud_storage.conf"; $cloudStorageConfig = isset($_SERVER['CLOUD_STORAGE_CONF']) ? $_SERVER['CLOUD_STORAGE_CONF'] : "/etc/airtime-saas/cloud_storage.conf";
$cloudStorageValues = parse_ini_file($cloudStorageConfig, true); $cloudStorageValues = parse_ini_file($cloudStorageConfig, true);
$supportedStorageBackends = array('amazon_S3'); $CC_CONFIG["supportedStorageBackends"] = array('amazon_S3');
foreach ($supportedStorageBackends as $backend) { foreach ($CC_CONFIG["supportedStorageBackends"] as $backend) {
$CC_CONFIG[$backend] = $cloudStorageValues[$backend]; $CC_CONFIG[$backend] = $cloudStorageValues[$backend];
} }

View file

@ -1,5 +1,7 @@
<?php <?php
require_once 'ProxyStorageBackend.php';
use Aws\S3\S3Client; use Aws\S3\S3Client;
class ProvisioningController extends Zend_Controller_Action class ProvisioningController extends Zend_Controller_Action
@ -19,44 +21,24 @@ class ProvisioningController extends Zend_Controller_Action
return; return;
} }
//TODO - don't hardcode this here. maybe set it in $CC_CONFIG
$cloudFiles = array(
"amazon_S3" => array()
);
$CC_CONFIG = Config::getConfig(); $CC_CONFIG = Config::getConfig();
//TODO - dynamically select the storage backend credentials here foreach ($CC_CONFIG["supportedStorageBackends"] as $storageBackend) {
$s3Client = S3Client::factory(array( $proxyStorageBackend = new ProxyStorageBackend($storageBackend);
'key' => $CC_CONFIG["amazon_S3"]['api_key'], $proxyStorageBackend->deleteObjects();
'secret' => $CC_CONFIG["amazon_S3"]['api_key_secret'],
));
$cloudFilePager = CloudFileQuery::create()
->paginate($page=1, $maxPerPage=1000);
if ($cloudFilePager->haveToPaginate()) {
$numPages = $cloudFilePager->getLastPage();
$currentPage = 1;
while ($currentPage <= $numPages) {
$cloudFilePager = CloudFileQuery::create()
->paginate($page = $currentPage, $maxPerPage = 1000);
//TODO - delete objects here
$currentPage += 1;
}
} else {
//TODO - move this into function so it can be reused above
foreach ($cloudFilePager->getResults() as $cloudFile) {
array_push($cloudFiles[$cloudFile->getStorageBackend()],
array("Key" => $cloudFile->getResourceId()));
$result = $s3Client->deleteObjects(array(
"Bucket" => $CC_CONFIG["amazon_S3"]["bucket"],
"Objects" => $cloudFiles["amazon_S3"]));
}
} }
//check at to make sure cloud_file table is empty
if (CloudFileQuery::create()->count() > 0) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("ERROR: Not all cloud files were deleted.");
return;
}
$this->getResponse()
->setHttpResponseCode(200)
->appendBody("OK");
} }
private function verifyAPIKey() private function verifyAPIKey()