]> source.dussan.org Git - nextcloud-server.git/commitdiff
add autocreate config option for containers, implement autocreate and delete of conta...
authorJörn Friedrich Dreyer <jfd@butonic.de>
Fri, 13 Jun 2014 15:22:21 +0000 (17:22 +0200)
committerJörn Friedrich Dreyer <jfd@butonic.de>
Wed, 18 Jun 2014 10:53:20 +0000 (12:53 +0200)
config/config.sample.php
lib/private/files/objectstore/swift.php
tests/lib/files/objectstore/swift.php

index da7d1c382205657301d9d898247cf137b22b9a7b..f58d09f16d015821261d43e3e196ebd422f6c505 100755 (executable)
@@ -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
index b4ef8996cb85fae6836f8dcd44cdd98a49870005..53367367af0ea93d278127f00806b170e150f31c 100644 (file)
 
 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);
+       }
+
 }
index 1e7495e0a22c137501ca794bba2f00f2d7de472d..497ea7f241c265aafa64c1e45920c2f35f8fabe7 100644 (file)
@@ -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?
        }