Exemple #1
0
    def on_patch(self, request, response, partition):
        """Allows one to update a partition's weight and/or hosts.

        This method expects the user to submit a JSON object
        containing both or either of 'hosts' and 'weight'. If neither
        is found, the request is flagged as bad. There is also strict
        format checking through the use of jsonschema. Appropriate
        errors are returned in each case for badly formatted input.

        :returns: HTTP | 200,400

        """
        LOG.debug('PATCH partition - name: {0}'.format(partition))
        data = load(request)

        if 'weight' not in data and 'hosts' not in data:
            LOG.debug('PATCH partition, bad params')
            raise wsgi_errors.HTTPBadRequestBody(
                'One of `hosts` or `weight` needs to be specified'
            )

        utils.validate(self._weight_validator, data)
        utils.validate(self._hosts_validator, data)
        try:
            fields = dict((k, v) for k, v in six.iteritems(data)
                          if k in ('hosts', 'weight')
                          and v is not None)

            self._ctrl.update(partition, **fields)
        except exceptions.PartitionNotFound as ex:
            LOG.exception(ex)
            raise falcon.HTTPNotFound()
Exemple #2
0
    def on_put(self, request, response, partition):
        """Creates a new partition. Expects the following input:

        {"weight": 100, "hosts": [""]}

        :returns: HTTP | [201, 204]
        """
        LOG.debug('PUT partition - name: {0}'.format(partition))
        if self._ctrl.exists(partition):
            LOG.debug('Partition {0} already exists'.format(partition))
            response.status = falcon.HTTP_204
            return

        data = load(request)
        utils.validate(self._put_validator, data)
        self._ctrl.create(partition,
                          weight=data['weight'],
                          hosts=data['hosts'])
        response.status = falcon.HTTP_201