From 891cfdb48e61ba713c133341d82721f20ea0acd3 Mon Sep 17 00:00:00 2001 From: drigato Date: Thu, 11 Dec 2014 13:35:34 -0500 Subject: [PATCH] SAAS-517: Create Provisioning controller with a terminate endpoint --- .../application/cloud_storage/Amazon_S3.php | 43 +++++++++++++++ .../cloud_storage/ProxyStorageBackend.php | 5 ++ .../cloud_storage/StorageBackend.php | 3 ++ airtime_mvc/application/configs/conf.php | 4 +- .../controllers/ProvisioningController.php | 52 ++++++------------- 5 files changed, 70 insertions(+), 37 deletions(-) diff --git a/airtime_mvc/application/cloud_storage/Amazon_S3.php b/airtime_mvc/application/cloud_storage/Amazon_S3.php index f9de30d49..f66dd7f65 100644 --- a/airtime_mvc/application/cloud_storage/Amazon_S3.php +++ b/airtime_mvc/application/cloud_storage/Amazon_S3.php @@ -58,4 +58,47 @@ class Amazon_S3 extends StorageBackend 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)); + } + } } diff --git a/airtime_mvc/application/cloud_storage/ProxyStorageBackend.php b/airtime_mvc/application/cloud_storage/ProxyStorageBackend.php index 78aeb1b35..171bbe64c 100644 --- a/airtime_mvc/application/cloud_storage/ProxyStorageBackend.php +++ b/airtime_mvc/application/cloud_storage/ProxyStorageBackend.php @@ -42,5 +42,10 @@ class ProxyStorageBackend extends StorageBackend { $this->storageBackend->deletePhysicalFile($resourceId); } + + public function deleteObjects() + { + $this->storageBackend->deleteObjects(); + } } diff --git a/airtime_mvc/application/cloud_storage/StorageBackend.php b/airtime_mvc/application/cloud_storage/StorageBackend.php index 84a9a8d72..e078e9abf 100644 --- a/airtime_mvc/application/cloud_storage/StorageBackend.php +++ b/airtime_mvc/application/cloud_storage/StorageBackend.php @@ -22,6 +22,9 @@ abstract class StorageBackend /** Deletes the file from the storage backend. */ abstract public function deletePhysicalFile($resourceId); + + /** Deletes multiple objects (files) stored on the cloud service. */ + abstract public function deleteObjects(); protected function getBucket() { diff --git a/airtime_mvc/application/configs/conf.php b/airtime_mvc/application/configs/conf.php index 932834fe3..c3a6e0796 100644 --- a/airtime_mvc/application/configs/conf.php +++ b/airtime_mvc/application/configs/conf.php @@ -29,8 +29,8 @@ class Config { $cloudStorageConfig = isset($_SERVER['CLOUD_STORAGE_CONF']) ? $_SERVER['CLOUD_STORAGE_CONF'] : "/etc/airtime-saas/cloud_storage.conf"; $cloudStorageValues = parse_ini_file($cloudStorageConfig, true); - $supportedStorageBackends = array('amazon_S3'); - foreach ($supportedStorageBackends as $backend) { + $CC_CONFIG["supportedStorageBackends"] = array('amazon_S3'); + foreach ($CC_CONFIG["supportedStorageBackends"] as $backend) { $CC_CONFIG[$backend] = $cloudStorageValues[$backend]; } diff --git a/airtime_mvc/application/controllers/ProvisioningController.php b/airtime_mvc/application/controllers/ProvisioningController.php index 7ce8a4428..313fae351 100644 --- a/airtime_mvc/application/controllers/ProvisioningController.php +++ b/airtime_mvc/application/controllers/ProvisioningController.php @@ -1,5 +1,7 @@ array() - ); - $CC_CONFIG = Config::getConfig(); - //TODO - dynamically select the storage backend credentials here - $s3Client = S3Client::factory(array( - 'key' => $CC_CONFIG["amazon_S3"]['api_key'], - '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"])); - } + foreach ($CC_CONFIG["supportedStorageBackends"] as $storageBackend) { + $proxyStorageBackend = new ProxyStorageBackend($storageBackend); + $proxyStorageBackend->deleteObjects(); } + + //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()