Skip to content
Snippets Groups Projects
Commit 5722e31d authored by Jörn Friedrich Dreyer's avatar Jörn Friedrich Dreyer
Browse files

add autocreate config option for containers, implement autocreate and delete...

add autocreate config option for containers, implement autocreate and delete of containers, use generated container names for tests
parent 1410cb10
No related branches found
No related tags found
No related merge requests found
...@@ -320,6 +320,7 @@ $CONFIG = array( ...@@ -320,6 +320,7 @@ $CONFIG = array(
'username' => 'facebook100000123456789', // trystack will user your facebook id as the user name '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 '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 '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' 'region' => 'RegionOne', //required, dev-/trystack defaults to 'RegionOne'
'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint 'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint
'tenantName' => 'facebook100000123456789', // required on dev-/trystack 'tenantName' => 'facebook100000123456789', // required on dev-/trystack
......
...@@ -20,10 +20,15 @@ ...@@ -20,10 +20,15 @@
namespace OC\Files\ObjectStore; namespace OC\Files\ObjectStore;
use Guzzle\Http\Exception\ClientErrorResponseException;
use OpenCloud\OpenStack; use OpenCloud\OpenStack;
class Swift extends AbstractObjectStore { class Swift extends AbstractObjectStore {
/**
* @var \OpenCloud\ObjectStore\Service
*/
private $objectStoreService;
/** /**
* @var \OpenCloud\ObjectStore\Resource\Container * @var \OpenCloud\ObjectStore\Resource\Container
...@@ -34,6 +39,13 @@ class Swift extends AbstractObjectStore { ...@@ -34,6 +39,13 @@ class Swift extends AbstractObjectStore {
if (!isset($params['username']) || !isset($params['password']) ) { if (!isset($params['username']) || !isset($params['password']) ) {
throw new \Exception("Access Key and Secret have to be configured."); 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( $secret = array(
'username' => $params['username'], 'username' => $params['username'],
...@@ -54,10 +66,18 @@ class Swift extends AbstractObjectStore { ...@@ -54,10 +66,18 @@ class Swift extends AbstractObjectStore {
$client = new OpenStack($params['url'], $secret); $client = new OpenStack($params['url'], $secret);
/** @var $objectStoreService \OpenCloud\ObjectStore\Service **/ $this->objectStoreService = $client->objectStoreService($serviceName, $params['region']);
$objectStoreService = $client->objectStoreService($serviceName, $params['region']);
try {
$this->container = $objectStoreService->getContainer($params['container']); $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 //set the user via parent constructor, also initializes the root of the filecache
parent::__construct($params); parent::__construct($params);
...@@ -105,4 +125,8 @@ class Swift extends AbstractObjectStore { ...@@ -105,4 +125,8 @@ class Swift extends AbstractObjectStore {
$this->container->uploadObject($urn, $fileData); $this->container->uploadObject($urn, $fileData);
} }
public function deleteContainer($recursive = false) {
$this->container->delete($recursive);
}
} }
...@@ -53,11 +53,14 @@ class Swift extends PHPUnit_Framework_TestCase { ...@@ -53,11 +53,14 @@ class Swift extends PHPUnit_Framework_TestCase {
\OC_User::setUserId(''); \OC_User::setUserId('');
\OC\Files\Filesystem::tearDown(); \OC\Files\Filesystem::tearDown();
\OC_User::setUserId('test'); \OC_User::setUserId('test');
$testContainer = 'oc-test-container-'.substr( md5(rand()), 0, 7);
$params = array( $params = array(
'username' => 'facebook100000330192569', 'username' => 'facebook100000330192569',
'password' => 'Dbdj1sXnRSHxIGc4', 'password' => 'Dbdj1sXnRSHxIGc4',
'container' => 'owncloud', 'container' => $testContainer,
'autocreate' => true,
'region' => 'RegionOne', //required, trystack defaults to 'RegionOne' 'region' => 'RegionOne', //required, trystack defaults to 'RegionOne'
'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint 'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint
'tenantName' => 'facebook100000330192569', // required on trystack 'tenantName' => 'facebook100000330192569', // required on trystack
...@@ -71,6 +74,7 @@ class Swift extends PHPUnit_Framework_TestCase { ...@@ -71,6 +74,7 @@ class Swift extends PHPUnit_Framework_TestCase {
if (is_null($this->storage)) { if (is_null($this->storage)) {
return; return;
} }
$this->storage->deleteContainer(true);
$this->storage->getCache()->clear(); $this->storage->getCache()->clear();
//TODO how do I clear hooks? //TODO how do I clear hooks?
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment