diff options
author | Robin Appelman <robin@icewind.nl> | 2017-03-28 11:39:25 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-03-28 17:00:11 +0200 |
commit | 8dbca71a77418b3f7ccd8be8a5dc0ab93858513e (patch) | |
tree | 6fa775fb166edfd86964b70c7ed745b9607f689c /lib/private/Files | |
parent | 5062d0ac50facc1944fe9e721c19164675894f8b (diff) | |
download | nextcloud-server-8dbca71a77418b3f7ccd8be8a5dc0ab93858513e.tar.gz nextcloud-server-8dbca71a77418b3f7ccd8be8a5dc0ab93858513e.zip |
better error messages for invalid regions, urltypes and service names
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Files')
-rw-r--r-- | lib/private/Files/ObjectStore/Swift.php | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php index 4f994b6f40c..43ce32af941 100644 --- a/lib/private/Files/ObjectStore/Swift.php +++ b/lib/private/Files/ObjectStore/Swift.php @@ -27,7 +27,11 @@ namespace OC\Files\ObjectStore; use Guzzle\Http\Exception\ClientErrorResponseException; use OCP\Files\ObjectStore\IObjectStore; use OCP\Files\StorageAuthException; +use OCP\Files\StorageNotAvailableException; use OpenCloud\Common\Exceptions\EndpointError; +use OpenCloud\Common\Service\Catalog; +use OpenCloud\Common\Service\CatalogItem; +use OpenCloud\ObjectStore\Service; use OpenCloud\OpenStack; use OpenCloud\Rackspace; @@ -91,16 +95,32 @@ class Swift implements IObjectStore { } } - // the OpenCloud client library will default to 'cloudFiles' if $serviceName is null - $serviceName = null; + /** @var Catalog $catalog */ + $catalog = $this->client->getCatalog(); + if (isset($this->params['serviceName'])) { $serviceName = $this->params['serviceName']; + } else { + $serviceName = Service::DEFAULT_NAME; } - // the OpenCloud client library will default to 'publicURL' if $urlType is null - $urlType = null; if (isset($this->params['urlType'])) { $urlType = $this->params['urlType']; + if ($urlType !== 'internalURL' && $urlType !== 'publicURL') { + throw new StorageNotAvailableException('Invalid url type'); + } + } else { + $urlType = Service::DEFAULT_URL_TYPE; + } + + $catalogItem = $this->getCatalogForService($catalog, $serviceName); + if (!$catalogItem) { + $available = implode(', ', $this->getAvailableServiceNames($catalog)); + throw new StorageNotAvailableException( + "Service $serviceName not found in service catalog, available services: $available" + ); + } else if (isset($this->params['region'])) { + $this->validateRegion($catalogItem, $this->params['region']); } $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType); @@ -118,6 +138,45 @@ class Swift implements IObjectStore { } /** + * @param Catalog $catalog + * @param $name + * @return null|CatalogItem + */ + private function getCatalogForService(Catalog $catalog, $name) { + foreach ($catalog->getItems() as $item) { + /** @var CatalogItem $item */ + if ($item->hasType(Service::DEFAULT_TYPE) && $item->hasName($name)) { + return $item; + } + } + + return null; + } + + private function validateRegion(CatalogItem $item, $region) { + $endPoints = $item->getEndpoints(); + foreach ($endPoints as $endPoint) { + if ($endPoint->region === $region) { + return; + } + } + + $availableRegions = implode(', ', array_map(function ($endpoint) { + return $endpoint->region; + }, $endPoints)); + + throw new StorageNotAvailableException("Invalid region '$region', available regions: $availableRegions"); + } + + private function getAvailableServiceNames(Catalog $catalog) { + return array_map(function (CatalogItem $item) { + return $item->getName(); + }, array_filter($catalog->getItems(), function (CatalogItem $item) { + return $item->hasType(Service::DEFAULT_TYPE); + })); + } + + /** * @return string the container name where objects are stored */ public function getStorageId() { |