Example #1
0
 def get_container(self, container_name):
     try:
         return self.containers[container_name]
     except KeyError:
         raise ContainerDoesNotExistError(
             container_name=container_name, driver=self, value=None
         )
Example #2
0
    def iterate_container_objects(self, container):
        """
        @inherits: L{StorageDriver.iterate_container_objects}
        """
        params = {'restype': 'container',
                  'comp': 'list',
                  'maxresults': RESPONSES_PER_REQUEST,
                  'include': 'metadata'}

        container_path = self._get_container_path(container)

        while True:
            response = self.connection.request(container_path,
                                               params=params)

            if response.status == httplib.NOT_FOUND:
                raise ContainerDoesNotExistError(value=None,
                                                 driver=self,
                                                 container_name=container.name)

            elif response.status != httplib.OK:
                raise LibcloudError('Unexpected status code: %s' %
                                    (response.status), driver=self)

            body = response.parse_body()
            blobs = body.find(fixxpath(xpath='Blobs'))
            blobs = blobs.findall(fixxpath(xpath='Blob'))

            for blob in blobs:
                yield self._xml_to_object(container, blob)

            params['marker'] = body.findtext('NextMarker')
            if not params['marker']:
                break
Example #3
0
    def _make_container(self, container_name):
        """
        Create a container instance

        @param container_name: Container name.
        @type container_name: C{str}

        @return: Container instance.
        @rtype: L{Container}
        """

        self._check_container_name(container_name)

        full_path = os.path.join(self.base_path, container_name)

        try:
            stat = os.stat(full_path)
            if not os.path.isdir(full_path):
                raise OSError('Target path is not a directory')
        except OSError:
            raise ContainerDoesNotExistError(value=None,
                                             driver=self,
                                             container_name=container_name)

        extra = {}
        extra['creation_time'] = stat.st_ctime
        extra['access_time'] = stat.st_atime
        extra['modify_time'] = stat.st_mtime

        return Container(name=container_name, extra=extra, driver=self)
            def get_container(*args, **kwargs):
                if not container['get_was_called']:
                    container['get_was_called'] = True
                    raise ContainerDoesNotExistError(None, driver,
                                                     self._container)

                return container
Example #5
0
    def delete_container(self, container):
        """
        @inherits: :class:`StorageDriver.delete_container`
        """
        # Azure does not check if the container is empty. So, we will do
        # a check to ensure that the behaviour is similar to other drivers
        for obj in container.iterate_objects():
            raise ContainerIsNotEmptyError(
                value='Container must be empty before it can be deleted.',
                container_name=container.name,
                driver=self)

        params = {'restype': 'container'}
        container_path = self._get_container_path(container)

        # Note: All the objects in the container must be deleted first
        response = self.connection.request(container_path,
                                           params=params,
                                           method='DELETE')

        if response.status == httplib.ACCEPTED:
            return True
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(value=None,
                                             driver=self,
                                             container_name=container.name)

        return False
Example #6
0
            def get_container(*args, **kwargs):
                if not container['get_was_called']:
                    container['get_was_called'] = True
                    # noinspection PyTypeChecker
                    raise ContainerDoesNotExistError(None, driver, self._container)

                return container
Example #7
0
 def get_container(self, container_name):
     for container in self.iterate_containers():
         if container.name == container_name:
             return container
     raise ContainerDoesNotExistError(value=None,
                                      driver=self,
                                      container_name=container_name)
Example #8
0
 def get_container(self, container_name):
     if self.container_exists(container_name):
         ret = Container(container_name, None, self)
         return ret
     raise ContainerDoesNotExistError(value=None,
                                      driver=self,
                                      container_name=container_name)
     return None
Example #9
0
 def get_container(self, container_name):
     path = self._namespace_path(container_name + '/?metadata/system')
     try:
         result = self.connection.request(path)
     except AtmosError, e:
         if e.code != 1003:
             raise
         raise ContainerDoesNotExistError(e, self, container_name)
Example #10
0
 def get_container(self, container_name):
     containers = self.iterate_containers()
     container = next((c for c in containers if c.name == container_name),
                      None)
     if container:
         return container
     else:
         raise ContainerDoesNotExistError(value=None, driver=self,
                                          container_name=container_name)
Example #11
0
 def delete_container(self, container):
     try:
         self.connection.request(self._namespace_path(container.name + '/'),
                                 method='DELETE')
     except AtmosError, e:
         if e.code == 1003:
             raise ContainerDoesNotExistError(e, self, container.name)
         elif e.code == 1023:
             raise ContainerIsNotEmptyError(e, self, container.name)
Example #12
0
 def get_container(self, container_path):
     try:
         collection = self.connection.session.collections.get(
             container_path)
         return self._to_container(collection)
     except CollectionDoesNotExist:
         raise ContainerDoesNotExistError(value=None,
                                          driver=self,
                                          container_name=container_path)
Example #13
0
 def delete_container(self, container):
     try:
         self.connection.request(self._namespace_path(container.name) + "/",
                                 method="DELETE")
     except AtmosError as e:
         if e.code == 1003:
             raise ContainerDoesNotExistError(e, self, container.name)
         elif e.code == 1023:
             raise ContainerIsNotEmptyError(e, self, container.name)
     return True
Example #14
0
    def get_container(self, container_name):
        # This is very inefficient, but afaik it's the only way to do it
        containers = self.list_containers()

        try:
            container = [c for c in containers if c.name == container_name][0]
        except IndexError:
            raise ContainerDoesNotExistError(value=None, driver=self,
                                             container_name=container_name)

        return container
Example #15
0
 def get_container(self, container_name):
     path = self._namespace_path(container_name) + '/?metadata/system'
     try:
         result = self.connection.request(path)
     except AtmosError as e:
         if e.code != 1003:
             raise
         raise ContainerDoesNotExistError(e, self, container_name)
     meta = self._emc_meta(result)
     extra = {'object_id': meta['objectid']}
     return Container(container_name, extra, self)
Example #16
0
 def delete_container(self, container):
     try:
         self.connection.request(self._namespace_path(container.name) + '/',
                                 method='DELETE')
     except AtmosError:
         e = sys.exc_info()[1]
         if e.code == 1003:
             raise ContainerDoesNotExistError(e, self, container.name)
         elif e.code == 1023:
             raise ContainerIsNotEmptyError(e, self, container.name)
     return True
Example #17
0
 def get_container(self, container_name):
     try:
         response = self.connection.request('/%s' % container_name,
                                            method='HEAD')
         if response.status == httplib.NOT_FOUND:
             raise ContainerDoesNotExistError(value=None, driver=self,
                                              container_name=container_name)
     except InvalidCredsError:
         # This just means the user doesn't have IAM permissions to do a
         # HEAD request but other requests might work.
         pass
     return Container(name=container_name, extra=None, driver=self)
Example #18
0
    def get_container(self, container_name):
        response = self.connection.request('/%s' % (container_name),
                                           method='HEAD')

        if response.status == httplib.NO_CONTENT:
            container = self._headers_to_container(container_name,
                                                   response.headers)
            return container
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(None, self, container_name)

        raise LibcloudError('Unexpected status code: %s' % (response.status))
Example #19
0
 def get_container(self, container_name):
     res = self.list_all_my_buckets()
     if (res.status / 100) == 2:
         body = res.read()
         h = GetServiceXml(body)
         for i in h.list():
             if i[0] == container_name:
                 ret = Container(container_name, None, self)
                 return ret
     raise ContainerDoesNotExistError(value=None,
                                      driver=self,
                                      container_name=container_name)
     return None
Example #20
0
    def delete_container(self, container):
        # Note: All the objects in the container must be deleted first

        for obj in self._get_objects(container):
            raise ContainerIsNotEmptyError(value='Container is not empty',
                                           container_name=container.name,
                                           driver=self)

        try:
            self.connection.session.collections.delete(container.name)
            return True
        except:  #CollectionDoesNotExist
            raise ContainerDoesNotExistError(value=None,
                                             driver=self,
                                             container_name=container.name)
Example #21
0
 def get_object(self, container_path, object_name):
     try:
         container = self.get_container(container_path)
     except CollectionDoesNotExist:
         raise ContainerDoesNotExistError(value=None,
                                          driver=self,
                                          container_name=container_path)
     try:
         obj_path = self._get_object_path(container, object_name)
         data_obj = self.connection.session.data_objects.get(obj_path)
         return self._to_obj(data_obj, container)
     except DataObjectDoesNotExist:
         raise ObjectDoesNotExistError(value=None,
                                       driver=self,
                                       object_name=object_name)
Example #22
0
    def delete_container(self, container):
        name = self._encode_container_name(container.name)

        # Only empty container can be deleted
        response = self.connection.request('/%s' % (name), method='DELETE')

        if response.status == httplib.NO_CONTENT:
            return True
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(value='',
                                             container_name=name, driver=self)
        elif response.status == httplib.CONFLICT:
            # @TODO: Add "delete_all_objects" parameter?
            raise ContainerIsNotEmptyError(value='',
                                           container_name=name, driver=self)
Example #23
0
    def get_container_cdn_url(self, container):
        container_name_encoded = self._encode_container_name(container.name)
        response = self.connection.request('/%s' % (container_name_encoded),
                                           method='HEAD',
                                           cdn_request=True)

        if response.status == httplib.NO_CONTENT:
            cdn_url = response.headers['x-cdn-uri']
            return cdn_url
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(value='',
                                             container_name=container.name,
                                             driver=self)

        raise LibcloudError('Unexpected status code: %s' % (response.status))
Example #24
0
    def delete_container(self, container):
        # Note: All the objects in the container must be deleted first
        response = self.connection.request('/%s' % (container.name),
                                           method='DELETE')
        if response.status == httplib.NO_CONTENT:
            return True
        elif response.status == httplib.CONFLICT:
            raise ContainerIsNotEmptyError(
                value='Container must be empty before it can be deleted.',
                container_name=container.name, driver=self)
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(value=None,
                                             driver=self,
                                             container_name=container.name)

        return False
Example #25
0
    def delete_container(self, container):
        """
        >>> driver = DummyStorageDriver('key', 'secret')
        >>> container = Container(name = 'test container',
        ...    extra={'object_count': 0}, driver=driver)
        >>> driver.delete_container(container=container)
        ... #doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ContainerDoesNotExistError:
        >>> container = driver.create_container(
        ...      container_name='test container 1')
        ... #doctest: +IGNORE_EXCEPTION_DETAIL
        >>> len(driver._containers)
        1
        >>> driver.delete_container(container=container)
        True
        >>> len(driver._containers)
        0
        >>> container = driver.create_container(
        ...    container_name='test container 1')
        ... #doctest: +IGNORE_EXCEPTION_DETAIL
        >>> obj = container.upload_object_via_stream(
        ...   object_name='test object', iterator=DummyFileObject(5, 10),
        ...   extra={})
        >>> driver.delete_container(container=container)
        ... #doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ContainerIsNotEmptyError:

        @inherits: :class:`StorageDriver.delete_container`
        """

        container_name = container.name
        if container_name not in self._containers:
            raise ContainerDoesNotExistError(container_name=container_name,
                                             value=None,
                                             driver=self)

        container = self._containers[container_name]
        if len(container['objects']) > 0:
            raise ContainerIsNotEmptyError(container_name=container_name,
                                           value=None,
                                           driver=self)

        del self._containers[container_name]
        return True
Example #26
0
    def iterate_container_objects(self,
                                  container,
                                  prefix=None,
                                  ex_prefix=None):
        """
        @inherits: :class:`StorageDriver.iterate_container_objects`
        """
        prefix = self._normalize_prefix_argument(prefix, ex_prefix)

        params = {
            "restype": "container",
            "comp": "list",
            "maxresults": RESPONSES_PER_REQUEST,
            "include": "metadata",
        }

        if prefix:
            params["prefix"] = prefix

        container_path = self._get_container_path(container)

        while True:
            response = self.connection.request(container_path, params=params)

            if response.status == httplib.NOT_FOUND:
                raise ContainerDoesNotExistError(value=None,
                                                 driver=self,
                                                 container_name=container.name)

            elif response.status != httplib.OK:
                raise LibcloudError("Unexpected status code: %s" %
                                    (response.status),
                                    driver=self)

            body = response.parse_body()
            blobs = body.find(fixxpath(xpath="Blobs"))
            blobs = blobs.findall(fixxpath(xpath="Blob"))

            for blob in blobs:
                yield self._xml_to_object(container, blob)

            params["marker"] = body.findtext("NextMarker")
            if not params["marker"]:
                break
Example #27
0
    def get_container_cdn_url(self, container, ex_ssl_uri=False):
        # pylint: disable=unexpected-keyword-arg
        container_name_encoded = self._encode_container_name(container.name)
        response = self.connection.request('/%s' % (container_name_encoded),
                                           method='HEAD',
                                           cdn_request=True)

        if response.status == httplib.NO_CONTENT:
            if ex_ssl_uri:
                cdn_url = response.headers['x-cdn-ssl-uri']
            else:
                cdn_url = response.headers['x-cdn-uri']
            return cdn_url
        elif response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError(value='',
                                             container_name=container.name,
                                             driver=self)

        raise LibcloudError('Unexpected status code: %s' % (response.status))
Example #28
0
    def get_container(self, container_name):
        """
        @inherits: :class:`StorageDriver.get_container`
        """
        params = {'restype': 'container'}

        container_path = '/%s' % (container_name)

        response = self.connection.request(container_path, params=params,
                                           method='HEAD')

        if response.status == httplib.NOT_FOUND:
            raise ContainerDoesNotExistError('Container %s does not exist' %
                                             (container_name), driver=self,
                                             container_name=container_name)
        elif response.status != httplib.OK:
            raise LibcloudError('Unexpected status code: %s' %
                                (response.status), driver=self)

        return self._response_to_container(container_name, response)
Example #29
0
    def get_container_cdn_url(self, container):
        """
        >>> driver = DummyStorageDriver('key', 'secret')
        >>> driver.get_container('unknown') #doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ContainerDoesNotExistError:
        >>> container = driver.create_container(container_name='test container 1')
        >>> container
        <Container: name=test container 1, provider=Dummy Storage Provider>
        >>> container.name
        'test container 1'
        >>> container.get_cdn_url()
        'http://www.test.com/container/test_container_1'
        """

        if container.name not in self._containers:
            raise ContainerDoesNotExistError(driver=self, value=None,
                                             container_name=container.name)

        return self._containers[container.name]['cdn_url']
Example #30
0
    def get_container_cdn_url(self, container, check=False):
        """
        Return a container CDN URL.

        :param container: Container instance
        :type  container: :class:`Container`

        :param check: Indicates if the path's existence must be checked
        :type check: ``bool``

        :return: A CDN URL for this container.
        :rtype: ``str``
        """
        path = os.path.join(self.base_path, container.name)

        if check and not os.path.isdir(path):
            raise ContainerDoesNotExistError(value=None, driver=self,
                                             container_name=container.name)

        return path