Esempio n. 1
0
    def on_put(self, req, resp, vault_id, block_id):
        """Uploads a block into Deuce. The URL of the block
        is returned in the Location header
        """

        vault = Vault.get(vault_id)

        try:
            retval, storage_id = vault.put_block(block_id, req.stream.read(),
                                                 req.content_length)
            resp.set_header('X-Storage-ID', str(storage_id))
            resp.set_header('X-Block-ID', str(block_id))

            block = vault.get_block(block_id)

            ref_cnt = 0
            ref_mod = 0

            if retval:
                ref_cnt = block.get_ref_count()
                ref_mod = block.get_ref_modified()

            resp.set_header('X-Block-Reference-Count', str(ref_cnt))
            resp.set_header('X-Ref-Modified', str(ref_mod))

            resp.status = (falcon.HTTP_201
                           if retval is True else falcon.HTTP_500)
            logger.info('block [{0}] added [{1}]'.format(block_id, storage_id))
        except ValueError as e:
            raise errors.HTTPPreconditionFailed('hash error')
        except BufferError as e:
            raise errors.HTTPPreconditionFailed(
                'content length did not match data length')
Esempio n. 2
0
def decode_service_catalog(catalog):
    """Decode a JSON-based Base64 encoded Service Catalog
    """
    try:
        data = base64.b64decode(catalog)
        utf8_data = data.decode(encoding='utf-8', errors='strict')
    except binascii.Error:
        raise errors.HTTPPreconditionFailed(
            "X-Service-Catalog invalid encoding")
    try:
        json_data = json.loads(utf8_data)
    except ValueError:
        raise errors.HTTPPreconditionFailed("X-Service-Catalog invalid format")
    return json_data
Esempio n. 3
0
    def on_post(self, req, resp, vault_id):
        vault = Vault.get(vault_id)
        try:
            unpacked = msgpack.unpackb(req.stream.read())

            if not isinstance(unpacked, dict):
                raise TypeError

            else:
                block_ids = list(unpacked.keys())
                block_datas = list(unpacked.values())
                try:
                    retval = vault.put_async_block(block_ids, block_datas)
                    if retval:
                        resp.status = falcon.HTTP_201
                    else:
                        raise errors.HTTPInternalServerError('Block '
                                                             'Post Failed')
                    logger.info('blocks [{0}] added'.format(block_ids))
                except ValueError:
                    raise errors.HTTPPreconditionFailed('hash error')
        except (TypeError, ValueError):
            logger.error('Request Body not well formed '
                         'for posting multiple blocks to {0}'.format(vault_id))
            raise errors.HTTPBadRequestBody("Request Body not well formed")
Esempio n. 4
0
def find_storage_url(catalog):
    try:
        main_catalog = catalog
        if 'access' in catalog:
            main_catalog = catalog['access']
        service_catalog = main_catalog['serviceCatalog']
        for service in service_catalog:
            if service['type'] == 'object-store':
                for endpoint in service['endpoints']:
                    if endpoint['region'].lower() == \
                            deuce.context.datacenter:
                        return endpoint['internalURL']
        raise errors.HTTPPreconditionFailed(
            "X-Service-Catalog missing object store")
    except (KeyError, LookupError):
        raise errors.HTTPPreconditionFailed(
            "X-Service-Catalog invalid service catalog")