Esempio n. 1
0
    def update_node(self, lb_id, node_id, node_updates):
        """
        Update the weight, condition, or type of a single node.  The IP, port,
        status, and ID are immutable, and attempting to change them will cause
        a 400 response to be returned.

        All success and error behavior verified as of 2016-06-16.

        :param str lb_id: the load balancer ID
        :param str node_id: the node ID to update
        :param dict node_updates: The JSON dictionary containing node
            attributes to update
        :param current_timestamp: What the current time is

        :return: a `tuple` of (json response as a dict, http status code)
        """
        feed_summary = (
            "Node successfully updated with address: '{address}', port: '{port}', "
            "weight: '{weight}', condition: '{condition}'")
        # first, store whether address and port were provided - if they were
        # that's a validation error not a schema error
        things_wrong = {k: True for k in ("address", "port", "id")
                        if k in node_updates}
        node_updates = {k: node_updates[k] for k in node_updates
                        if k not in ("address", "port")}
        # use the Node.from_json to check the schema
        try:
            Node.from_json(dict(address="1.1.1.1", port=80, **node_updates))
        except (TypeError, ValueError):
            return invalid_json_schema()

        # handle the possible validation (as opposed to schema) errors
        if not 1 <= node_updates.get('weight', 1) <= 100:
            things_wrong["weight"] = True
        if things_wrong:
            return updating_node_validation_error(**things_wrong)

        # Now, finally, check if the LB exists and node exists
        if lb_id in self.lbs:
            self._verify_and_update_lb_state(lb_id, False, self.clock.seconds())

            if self.lbs[lb_id]["status"] != "ACTIVE":
                return considered_immutable_error(
                    self.lbs[lb_id]["status"], lb_id)

            for i, node in enumerate(self.lbs[lb_id].nodes):
                if node.id == node_id:
                    params = attr.asdict(node)
                    params.update(node_updates)
                    self.lbs[lb_id].nodes[i] = Node(**params)
                    self.lbs[lb_id].nodes[i].feed_events.append(
                        (feed_summary.format(**params),
                         seconds_to_timestamp(self.clock.seconds())))
                    return ("", 202)

            return node_not_found()

        return loadbalancer_not_found()
Esempio n. 2
0
    def update_node(self, lb_id, node_id, node_updates):
        """
        Update the weight, condition, or type of a single node.  The IP, port,
        status, and ID are immutable, and attempting to change them will cause
        a 400 response to be returned.

        All success and error behavior verified as of 2016-06-16.

        :param str lb_id: the load balancer ID
        :param str node_id: the node ID to update
        :param dict node_updates: The JSON dictionary containing node
            attributes to update
        :param current_timestamp: What the current time is

        :return: a `tuple` of (json response as a dict, http status code)
        """
        feed_summary = (
            "Node successfully updated with address: '{address}', port: '{port}', "
            "weight: '{weight}', condition: '{condition}'")
        # first, store whether address and port were provided - if they were
        # that's a validation error not a schema error
        things_wrong = dict([(k, True) for k in ("address", "port", "id")
                             if k in node_updates])
        node_updates = dict([(k, v) for k, v in node_updates.items()
                             if k not in ("address", "port")])
        # use the Node.from_json to check the schema
        try:
            Node.from_json(dict(address="1.1.1.1", port=80, **node_updates))
        except (TypeError, ValueError):
            return invalid_json_schema()

        # handle the possible validation (as opposed to schema) errors
        if not 1 <= node_updates.get('weight', 1) <= 100:
            things_wrong["weight"] = True
        if things_wrong:
            return updating_node_validation_error(**things_wrong)

        # Now, finally, check if the LB exists and node exists
        if lb_id in self.lbs:
            self._verify_and_update_lb_state(lb_id, False, self.clock.seconds())

            if self.lbs[lb_id]["status"] != "ACTIVE":
                return considered_immutable_error(
                    self.lbs[lb_id]["status"], lb_id)

            for i, node in enumerate(self.lbs[lb_id].nodes):
                if node.id == node_id:
                    params = attr.asdict(node)
                    params.update(node_updates)
                    self.lbs[lb_id].nodes[i] = Node(**params)
                    self.lbs[lb_id].nodes[i].feed_events.append(
                        (feed_summary.format(**params),
                         seconds_to_timestamp(self.clock.seconds())))
                    return ("", 202)

            return node_not_found()

        return loadbalancer_not_found()
Esempio n. 3
0
 def update_node(self, request, tenant_id, lb_id, node_id):
     """
     Return a 202 response code to updating a node, if successful.
     """
     try:
         content = json.loads(request.content.read())
         assert isinstance(content, dict) and content.keys() == ["node"]
         content = content["node"]
         assert isinstance(content, dict)
     except (ValueError, AssertionError):
         resp_body, resp_code = invalid_json_schema()
     else:
         resp_body, resp_code = self.session(tenant_id).update_node(
             lb_id, node_id, content)
     request.setResponseCode(resp_code)
     if isinstance(resp_body, string_types):
         return resp_body
     return json.dumps(resp_body)
Esempio n. 4
0
 def update_node(self, request, tenant_id, lb_id, node_id):
     """
     Return a 202 response code to updating a node, if successful.
     """
     try:
         content = json.loads(request.content.read())
         assert isinstance(content, dict) and content.keys() == ["node"]
         content = content["node"]
         assert isinstance(content, dict)
     except (ValueError, AssertionError):
         resp_body, resp_code = invalid_json_schema()
     else:
         resp_body, resp_code = self.session(tenant_id).update_node(
             lb_id, node_id, content
         )
     request.setResponseCode(resp_code)
     if isinstance(resp_body, string_types):
         return resp_body
     return json.dumps(resp_body)