From 5722e31d1a277199e9b8ae85a21685abf3436418 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 13 Jun 2014 17:22:21 +0200 Subject: [PATCH] add autocreate config option for containers, implement autocreate and delete of containers, use generated container names for tests --- config/config.sample.php | 1 + lib/private/files/objectstore/swift.php | 32 +++++++++++++++++++++---- tests/lib/files/objectstore/swift.php | 8 +++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/config/config.sample.php b/config/config.sample.php index da7d1c38220..f58d09f16d0 100755 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -320,6 +320,7 @@ $CONFIG = array( 'username' => 'facebook100000123456789', // trystack will user your facebook id as the user name 'password' => 'Secr3tPaSSWoRdt7', // in the trystack dashboard go to user -> settings -> API Password to generate a password 'container' => 'owncloud', // must already exist in the objectstore, name can be different + 'autocreate' => true, // create the container if it does not exist. default is false 'region' => 'RegionOne', //required, dev-/trystack defaults to 'RegionOne' 'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint 'tenantName' => 'facebook100000123456789', // required on dev-/trystack diff --git a/lib/private/files/objectstore/swift.php b/lib/private/files/objectstore/swift.php index b4ef8996cb8..53367367af0 100644 --- a/lib/private/files/objectstore/swift.php +++ b/lib/private/files/objectstore/swift.php @@ -20,10 +20,15 @@ namespace OC\Files\ObjectStore; +use Guzzle\Http\Exception\ClientErrorResponseException; use OpenCloud\OpenStack; class Swift extends AbstractObjectStore { + /** + * @var \OpenCloud\ObjectStore\Service + */ + private $objectStoreService; /** * @var \OpenCloud\ObjectStore\Resource\Container @@ -34,6 +39,13 @@ class Swift extends AbstractObjectStore { if (!isset($params['username']) || !isset($params['password']) ) { throw new \Exception("Access Key and Secret have to be configured."); } + if (!isset($params['container'])) { + $params['container'] = 'owncloud'; + } + if (!isset($params['autocreate'])) { + // should only be true for tests + $params['autocreate'] = false; + } $secret = array( 'username' => $params['username'], @@ -54,10 +66,18 @@ class Swift extends AbstractObjectStore { $client = new OpenStack($params['url'], $secret); - /** @var $objectStoreService \OpenCloud\ObjectStore\Service **/ - $objectStoreService = $client->objectStoreService($serviceName, $params['region']); - - $this->container = $objectStoreService->getContainer($params['container']); + $this->objectStoreService = $client->objectStoreService($serviceName, $params['region']); + + try { + $this->container = $this->objectStoreService->getContainer($params['container']); + } catch (ClientErrorResponseException $ex) { + // if the container does not exist and autocreate is true try to create the container on the fly + if (isset($params['autocreate']) && $params['autocreate'] === true) { + $this->container = $this->objectStoreService->createContainer($params['container']); + } else { + throw $ex; + } + } //set the user via parent constructor, also initializes the root of the filecache parent::__construct($params); @@ -105,4 +125,8 @@ class Swift extends AbstractObjectStore { $this->container->uploadObject($urn, $fileData); } + public function deleteContainer($recursive = false) { + $this->container->delete($recursive); + } + } diff --git a/tests/lib/files/objectstore/swift.php b/tests/lib/files/objectstore/swift.php index 1e7495e0a22..497ea7f241c 100644 --- a/tests/lib/files/objectstore/swift.php +++ b/tests/lib/files/objectstore/swift.php @@ -53,11 +53,14 @@ class Swift extends PHPUnit_Framework_TestCase { \OC_User::setUserId(''); \OC\Files\Filesystem::tearDown(); \OC_User::setUserId('test'); - + + $testContainer = 'oc-test-container-'.substr( md5(rand()), 0, 7); + $params = array( 'username' => 'facebook100000330192569', 'password' => 'Dbdj1sXnRSHxIGc4', - 'container' => 'owncloud', + 'container' => $testContainer, + 'autocreate' => true, 'region' => 'RegionOne', //required, trystack defaults to 'RegionOne' 'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint 'tenantName' => 'facebook100000330192569', // required on trystack @@ -71,6 +74,7 @@ class Swift extends PHPUnit_Framework_TestCase { if (is_null($this->storage)) { return; } + $this->storage->deleteContainer(true); $this->storage->getCache()->clear(); //TODO how do I clear hooks? } -- 2.39.5