def delete_node(store, lb_id, node_id, current_timestamp): """ Determines whether the node to be deleted exists in mimic store and returns the response code. """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) if store.lbs[lb_id]["status"] != "ACTIVE": resource = invalid_resource( "Load Balancer '{0}' has a status of {1} and is considered " "immutable.".format(lb_id, store.lbs[lb_id]["status"]), 422) return (resource, 422) _verify_and_update_lb_state(store, lb_id, current_timestamp=current_timestamp) if store.lbs[lb_id].get("nodes"): for each in store.lbs[lb_id]["nodes"]: if each["id"] == node_id: index = store.lbs[lb_id]["nodes"].index(each) del store.lbs[lb_id]["nodes"][index] if not store.lbs[lb_id]["nodes"]: del store.lbs[lb_id]["nodes"] store.lbs[lb_id].update( {"nodeCount": len(store.lbs[lb_id].get("nodes", []))}) return None, 202 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def delete_node(self, lb_id, node_id): """ Determines whether the node to be deleted exists in the session store, deletes the node, and returns the response code. """ current_timestamp = self.clock.seconds() if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 return considered_immutable_error(self.lbs[lb_id]["status"], lb_id) _verify_and_update_lb_state(self, lb_id, current_timestamp=current_timestamp) if _delete_node(self, lb_id, node_id): return None, 202 else: return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def delete_node(store, lb_id, node_id, current_timestamp): """ Determines whether the node to be deleted exists in mimic store and returns the response code. """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) if store.lbs[lb_id]["status"] != "ACTIVE": resource = invalid_resource( "Load Balancer '{0}' has a status of {1} and is considered " "immutable.".format(lb_id, store.lbs[lb_id]["status"]), 422) return (resource, 422) _verify_and_update_lb_state(store, lb_id, current_timestamp=current_timestamp) if store.lbs[lb_id].get("nodes"): for each in store.lbs[lb_id]["nodes"]: if each["id"] == node_id: index = store.lbs[lb_id]["nodes"].index(each) del store.lbs[lb_id]["nodes"][index] if not store.lbs[lb_id]["nodes"]: del store.lbs[lb_id]["nodes"] store.lbs[lb_id].update({"nodeCount": len(store.lbs[lb_id].get("nodes", []))}) return None, 202 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def delete_node(self, lb_id, node_id, current_timestamp): """ Determines whether the node to be deleted exists in the session store, deletes the node, and returns the response code. """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 resource = invalid_resource( "Load Balancer '{0}' has a status of '{1}' and is considered " "immutable.".format(lb_id, self.lbs[lb_id]["status"]), 422) return (resource, 422) _verify_and_update_lb_state(self, lb_id, current_timestamp=current_timestamp) if _delete_node(self, lb_id, node_id): return None, 202 else: return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def add_node(self, node_list, lb_id, current_timestamp): """ Returns the canned response for add nodes """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": resource = invalid_resource( "Load Balancer '{0}' has a status of {1} and is considered " "immutable.".format(lb_id, self.lbs[lb_id]["status"]), 422) return (resource, 422) nodes = _format_nodes_on_lb(node_list) if self.lbs[lb_id].get("nodes"): for existing_node in self.lbs[lb_id]["nodes"]: for new_node in node_list: if (existing_node["address"] == new_node["address"] and existing_node["port"] == new_node["port"]): resource = invalid_resource( "Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413) return (resource, 413) self.lbs[lb_id]["nodes"] = self.lbs[lb_id]["nodes"] + nodes else: self.lbs[lb_id]["nodes"] = nodes self.lbs[lb_id]["nodeCount"] = len(self.lbs[lb_id]["nodes"]) _verify_and_update_lb_state(self, lb_id, current_timestamp=current_timestamp) return {"nodes": nodes}, 202 return not_found_response("loadbalancer"), 404
def del_load_balancer(self, lb_id, current_timestamp): """ Returns response for a load balancer is in building status for 20 seconds and response code 202, and adds the new lb to ``self.lbs``. A loadbalancer, on delete, goes into PENDING-DELETE and remains in DELETED status until a nightly job(maybe?) """ if lb_id in self.lbs: if self.lbs[lb_id]["status"] == "PENDING-DELETE": msg = ("Must provide valid load balancers: {0} are immutable and " "could not be processed.".format(lb_id)) # Dont doubt this to be 422, it is 400! return invalid_resource(msg, 400), 400 _verify_and_update_lb_state(self, lb_id, True, current_timestamp) if any([self.lbs[lb_id]["status"] == "ACTIVE", self.lbs[lb_id]["status"] == "ERROR", self.lbs[lb_id]["status"] == "PENDING-UPDATE"]): del self.lbs[lb_id] return EMPTY_RESPONSE, 202 if self.lbs[lb_id]["status"] == "PENDING-DELETE": return EMPTY_RESPONSE, 202 if self.lbs[lb_id]["status"] == "DELETED": _verify_and_update_lb_state(self, lb_id, current_timestamp=current_timestamp) msg = "Must provide valid load balancers: {0} could not be found.".format(lb_id) # Dont doubt this to be 422, it is 400! return invalid_resource(msg, 400), 400 return not_found_response("loadbalancer"), 404
def list_addresses(server_id): """ Returns the public and private ip address for the given server """ if server_id in s_cache: return {'addresses': s_cache[server_id]['addresses']}, 200 else: return not_found_response(), 404
def list_addresses(server_id): """ Returns the public and private ip address for the given server """ if server_id in s_cache: return {'addresses': s_cache[server_id]['addresses']}, 200 else: return not_found_response(), 404
def list_nodes(self, lb_id, current_timestamp): """ Returns the list of nodes remaining on the load balancer """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 if self.lbs[lb_id]["status"] == "DELETED": return invalid_resource("The loadbalancer is marked as deleted.", 410), 410 node_list = [] if self.lbs[lb_id].get("nodes"): node_list = self.lbs[lb_id]["nodes"] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def delete_health_monitor(self, lb_id): """ Delete LB's health monitor configuration """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self.lbs[lb_id].health_monitor = {} return EMPTY_RESPONSE, 202
def list_nodes(self, lb_id): """ Returns the list of nodes remaining on the load balancer """ if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 if self.lbs[lb_id]["status"] == "DELETED": return invalid_resource("The loadbalancer is marked as deleted.", 410), 410 node_list = [node.as_json() for node in self.lbs[lb_id].nodes] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def delete_health_monitor(self, lb_id): """ Delete LB's health monitor configuration """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self.lbs[lb_id].health_monitor = {} return EMPTY_RESPONSE, 202
def update_health_monitor(self, lb_id, health_monitor): """ Update LB's health monitor setting """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self.lbs[lb_id].health_monitor = health_monitor["healthMonitor"] return EMPTY_RESPONSE, 202
def get_health_monitor(self, lb_id): """ Return LB's health monitor setting """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) return {"healthMonitor": self.lbs[lb_id].health_monitor}, 200
def list_nodes(store, lb_id, current_timestamp): """ Returns the list of nodes remaining on the load balancer """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) if lb_id not in store.lbs: return not_found_response("loadbalancer"), 404 if store.lbs[lb_id]["status"] == "DELETED": return invalid_resource("The loadbalancer is marked as deleted.", 410), 410 node_list = [] if store.lbs[lb_id].get("nodes"): node_list = store.lbs[lb_id]["nodes"] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def update_health_monitor(self, lb_id, health_monitor): """ Update LB's health monitor setting """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self.lbs[lb_id].health_monitor = health_monitor["healthMonitor"] return EMPTY_RESPONSE, 202
def get_nodes(store, lb_id, node_id, current_timestamp): """ Returns the node on the load balancer """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) if store.lbs[lb_id]["status"] == "DELETED": return (invalid_resource("The loadbalancer is marked as deleted.", 410), 410) if store.lbs[lb_id].get("nodes"): for each in store.lbs[lb_id]["nodes"]: if node_id == each["id"]: return {"node": each}, 200 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def list_nodes(self, lb_id): """ Returns the list of nodes remaining on the load balancer """ if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 if self.lbs[lb_id]["status"] == "DELETED": return invalid_resource("The loadbalancer is marked as deleted.", 410), 410 node_list = [node.as_json() for node in self.lbs[lb_id].nodes] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def get_health_monitor(self, lb_id): """ Return LB's health monitor setting """ if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) return {"healthMonitor": self.lbs[lb_id].health_monitor}, 200
def get_server(server_id): """ Verify if the given server_id exists in the server cache. If true, return server data else return None """ if server_id in s_cache: set_server_state(server_id) return {'server': s_cache[server_id]}, 200 else: return not_found_response(), 404
def get_nodes(self, lb_id, node_id): """ Returns the node on the load balancer """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, self.clock.seconds()) if self.lbs[lb_id]["status"] == "DELETED": return (invalid_resource( "The loadbalancer is marked as deleted.", 410), 410) for each in self.lbs[lb_id].nodes: if node_id == each.id: return {"node": each.as_json()}, 200 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def del_load_balancer(lb_id): """ Returns response for a load balancer that is in building status for 20 seconds and response code 202, and adds the new lb to the lb_cache """ if lb_id in lb_cache: del lb_cache[lb_id] return None, 202 else: return not_found_response(), 404
def get_load_balancers(self, lb_id): """ Returns the load balancers with the given lb id, with response code 200. If no load balancers are found returns 404. """ if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) log.msg(self.lbs[lb_id]["status"]) return {'loadBalancer': self.lbs[lb_id].full_json()}, 200 return not_found_response("loadbalancer"), 404
def get_load_balancers(self, lb_id): """ Returns the load balancers with the given lb id, with response code 200. If no load balancers are found returns 404. """ if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) log.msg(self.lbs[lb_id]["status"]) return {'loadBalancer': self.lbs[lb_id].full_json()}, 200 return not_found_response("loadbalancer"), 404
def get_server(server_id): """ Verify if the given server_id exists in the server cache. If true, return server data else return None """ if server_id in s_cache: set_server_state(server_id) return {'server': s_cache[server_id]}, 200 else: return not_found_response(), 404
def del_load_balancer(lb_id): """ Returns response for a load balancer that is in building status for 20 seconds and response code 202, and adds the new lb to the lb_cache """ if lb_id in lb_cache: del lb_cache[lb_id] return None, 202 else: return not_found_response(), 404
def get_image(image_id): """ Canned response for get image. The image id provided is substituted in the response, if not one of the invalid image ids specified in mimic_presets. """ if ( image_id in get_presets['servers']['invalid_image_ref'] or image_id.endswith('Z') ): return not_found_response('images'), 404 return {'image': {'status': 'ACTIVE', 'id': image_id, 'name': 'mimic-test-image'}}, 200
def get_flavor(flavor_id): """ Canned response for get flavor. The flavor id provided is substituted in the response """ if flavor_id in get_presets['servers']['invalid_flavor_ref']: return not_found_response('flavors'), 404 return ({'flavor': {'name': '512MB Standard Instance', 'id': flavor_id, 'name': 'mimic-test-flavor'}}, 200)
def list_nodes(lb_id): """ Returns the list of nodes remaining on the load balancer """ if lb_id in lb_cache: node_list = [] if lb_cache[lb_id].get("nodes"): node_list = lb_cache[lb_id]["nodes"] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def get_load_balancers(store, lb_id, current_timestamp): """ Returns the load balancers with the given lb id, with response code 200. If no load balancers are found returns 404. """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) log.msg(store.lbs[lb_id]["status"]) new_lb = _lb_without_tenant(store, lb_id) return {'loadBalancer': new_lb}, 200 return not_found_response("loadbalancer"), 404
def get_nodes(self, lb_id, node_id, current_timestamp): """ Returns the node on the load balancer """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] == "DELETED": return ( invalid_resource( "The loadbalancer is marked as deleted.", 410), 410) if self.lbs[lb_id].get("nodes"): for each in self.lbs[lb_id]["nodes"]: if node_id == each["id"]: return {"node": each}, 200 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def list_nodes(lb_id): """ Returns the list of nodes remaining on the load balancer """ if lb_id in lb_cache: node_list = [] if lb_cache[lb_id].get("nodes"): node_list = lb_cache[lb_id]["nodes"] return {"nodes": node_list}, 200 else: return not_found_response("loadbalancer"), 404
def get_node(self, lb_id, node_id): """ Returns the node on the load balancer """ if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, self.clock.seconds()) if self.lbs[lb_id]["status"] == "DELETED": return ( invalid_resource( "The loadbalancer is marked as deleted.", 410), 410) for each in self.lbs[lb_id].nodes: if node_id == each.id: return {"node": each.as_json()}, 200 return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def get_load_balancers(self, lb_id, current_timestamp): """ Returns the load balancers with the given lb id, with response code 200. If no load balancers are found returns 404. """ if lb_id in self.lbs: _verify_and_update_lb_state(self, lb_id, False, current_timestamp) log.msg(self.lbs[lb_id]["status"]) new_lb = _lb_without_tenant(self, lb_id) return {'loadBalancer': new_lb}, 200 return not_found_response("loadbalancer"), 404
def add_node(self, node_list, lb_id): """ Add one or more nodes to a load balancer. Fails if one or more of the nodes provided has the same address/port as an existing node. Also fails if adding the nodes would exceed the maximum number of nodes on the CLB. :param list node_list: a `list` of `dict` containing specification for nodes :return: a `tuple` of (json response as a dict, http status code) """ if lb_id in self.lbs: current_timestamp = self.clock.seconds() self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": return considered_immutable_error( self.lbs[lb_id]["status"], lb_id) nodes = [Node.from_json(blob) for blob in node_list] for existing_node in self.lbs[lb_id].nodes: for new_node in nodes: if existing_node.same_as(new_node): resource = invalid_resource( "Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413) return (resource, 413) # If there were no duplicates new_nodeCount = len(self.lbs[lb_id].nodes) + len(nodes) if new_nodeCount <= self.node_limit: self.lbs[lb_id].nodes = self.lbs[lb_id].nodes + nodes else: resource = invalid_resource( "Nodes must not exceed {0} " "per load balancer.".format(self.node_limit), 413) return (resource, 413) # Node status will be OFFLINE if health monitor is enabled in CLB # ONLINE if health monitor is disabled if self.lbs[lb_id].health_monitor != {}: for node in nodes: node.status = "OFFLINE" self._add_node_created_feeds(nodes) self._verify_and_update_lb_state( lb_id, current_timestamp=current_timestamp) return {"nodes": [node.as_json() for node in nodes]}, 202 return not_found_response("loadbalancer"), 404
def add_node(self, node_list, lb_id): """ Add one or more nodes to a load balancer. Fails if one or more of the nodes provided has the same address/port as an existing node. Also fails if adding the nodes would exceed the maximum number of nodes on the CLB. :param list node_list: a `list` of `dict` containing specification for nodes :return: a `tuple` of (json response as a dict, http status code) """ if lb_id in self.lbs: current_timestamp = self.clock.seconds() self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": return considered_immutable_error(self.lbs[lb_id]["status"], lb_id) nodes = [Node.from_json(blob) for blob in node_list] for existing_node in self.lbs[lb_id].nodes: for new_node in nodes: if existing_node.same_as(new_node): resource = invalid_resource( "Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413) return (resource, 413) # If there were no duplicates new_nodeCount = len(self.lbs[lb_id].nodes) + len(nodes) if new_nodeCount <= self.node_limit: self.lbs[lb_id].nodes = self.lbs[lb_id].nodes + nodes else: resource = invalid_resource( "Nodes must not exceed {0} " "per load balancer.".format(self.node_limit), 413) return (resource, 413) # Node status will be OFFLINE if health monitor is enabled in CLB # ONLINE if health monitor is disabled if self.lbs[lb_id].health_monitor != {}: for node in nodes: node.status = "OFFLINE" self._add_node_created_feeds(nodes) self._verify_and_update_lb_state( lb_id, current_timestamp=current_timestamp) return {"nodes": [node.as_json() for node in nodes]}, 202 return not_found_response("loadbalancer"), 404
def delete_node(lb_id, node_id): """ Determines whether the node to be deleted exists in mimic cache and returns the response code. Note : Currently even if node does not exist, return 202 on delete. """ if lb_id in lb_cache: lb_cache[lb_id]["nodes"] = [x for x in lb_cache[lb_id]["nodes"] if not (node_id == x.get("id"))] if not lb_cache[lb_id]["nodes"]: del lb_cache[lb_id]["nodes"] return None, 202 else: return not_found_response("loadbalancer"), 404
def delete_nodes(self, lb_id, node_ids): """ Bulk-delete multiple LB nodes. """ if not node_ids: resp = { "message": "Must supply one or more id's to process this request.", "code": 400 } return resp, 400 if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 current_timestamp = self.clock.seconds() _verify_and_update_lb_state(self, lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 resp = {"message": "LoadBalancer is not ACTIVE", "code": 422} return resp, 422 # We need to verify all the deletions up front, and only allow it through # if all of them are valid. all_ids = [node.id for node in self.lbs[lb_id].nodes] non_nodes = set(node_ids).difference(all_ids) if non_nodes: nodes = ','.join(map(str, non_nodes)) resp = { "validationErrors": { "messages": [ "Node ids {0} are not a part of your loadbalancer". format(nodes) ] }, "message": "Validation Failure", "code": 400, "details": "The object is not valid" } return resp, 400 for node_id in node_ids: # It should not be possible for this to fail, since we've already # checked that they all exist. assert _delete_node(self, lb_id, node_id) is True _verify_and_update_lb_state(self, lb_id, current_timestamp=current_timestamp) return EMPTY_RESPONSE, 202
def get_flavor(flavor_id): """ Canned response for get flavor. The flavor id provided is substituted in the response """ if flavor_id in get_presets['servers']['invalid_flavor_ref']: return not_found_response('flavors'), 404 return ({ 'flavor': { 'name': '512MB Standard Instance', 'id': flavor_id, 'name': 'mimic-test-flavor' } }, 200)
def get_image(image_id): """ Canned response for get image. The image id provided is substituted in the response, if not one of the invalid image ids specified in mimic_presets. """ if (image_id in get_presets['servers']['invalid_image_ref'] or image_id.endswith('Z')): return not_found_response('images'), 404 return { 'image': { 'status': 'ACTIVE', 'id': image_id, 'name': 'mimic-test-image' } }, 200
def delete_node(self, lb_id, node_id): """ Determines whether the node to be deleted exists in the session store, deletes the node, and returns the response code. """ current_timestamp = self.clock.seconds() if lb_id in self.lbs: self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 return considered_immutable_error( self.lbs[lb_id]["status"], lb_id) self._verify_and_update_lb_state( lb_id, current_timestamp=current_timestamp) if self._delete_node(lb_id, node_id): return None, 202 else: return not_found_response("node"), 404 return not_found_response("loadbalancer"), 404
def delete_server(server_id): """ Returns True if the server was deleted from the cache, else returns false. """ if server_id in s_cache: if 'delete_server_failure' in s_cache[server_id]['metadata']: del_meta = json.loads(s_cache[server_id]['metadata']['delete_server_failure']) if del_meta['times'] != 0: del_meta['times'] = del_meta['times'] - 1 s_cache[server_id]['metadata']['delete_server_failure'] = json.dumps(del_meta) return invalid_resource('server error', del_meta['code']), del_meta['code'] del s_cache[server_id] return True, 204 else: return not_found_response(), 404
def delete_node(lb_id, node_id): """ Determines whether the node to be deleted exists in mimic cache and returns the response code. Note : Currently even if node does not exist, return 202 on delete. """ if lb_id in lb_cache: lb_cache[lb_id]["nodes"] = [ x for x in lb_cache[lb_id]["nodes"] if not (node_id == x.get("id")) ] if not lb_cache[lb_id]["nodes"]: del lb_cache[lb_id]["nodes"] return None, 202 else: return not_found_response("loadbalancer"), 404
def delete_nodes(self, lb_id, node_ids): """ Bulk-delete multiple LB nodes. """ if not node_ids: resp = { "message": "Must supply one or more id's to process this request.", "code": 400} return resp, 400 if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 current_timestamp = self.clock.seconds() self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 resp = {"message": "LoadBalancer is not ACTIVE", "code": 422} return resp, 422 # We need to verify all the deletions up front, and only allow it through # if all of them are valid. all_ids = [node.id for node in self.lbs[lb_id].nodes] non_nodes = set(node_ids).difference(all_ids) if non_nodes: nodes = ','.join(map(str, sorted(non_nodes))) resp = { "validationErrors": { "messages": [ "Node ids {0} are not a part of your loadbalancer".format(nodes) ] }, "message": "Validation Failure", "code": 400, "details": "The object is not valid"} return resp, 400 for node_id in node_ids: # It should not be possible for this to fail, since we've already # checked that they all exist. assert self._delete_node(lb_id, node_id) is True self._verify_and_update_lb_state( lb_id, current_timestamp=current_timestamp) return EMPTY_RESPONSE, 202
def delete_server(server_id): """ Returns True if the server was deleted from the cache, else returns false. """ if server_id in s_cache: if 'delete_server_failure' in s_cache[server_id]['metadata']: del_meta = json.loads( s_cache[server_id]['metadata']['delete_server_failure']) if del_meta['times'] != 0: del_meta['times'] = del_meta['times'] - 1 s_cache[server_id]['metadata'][ 'delete_server_failure'] = json.dumps(del_meta) return invalid_resource('server error', del_meta['code']), del_meta['code'] del s_cache[server_id] return True, 204 else: return not_found_response(), 404
def delete_nodes(self, lb_id, node_ids): """ Bulk-delete multiple LB nodes. """ if not node_ids: resp = { "message": "Must supply one or more id's to process this request.", "code": 400 } return resp, 400 if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 current_timestamp = self.clock.seconds() self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 resp = {"message": "LoadBalancer is not ACTIVE", "code": 422} return resp, 422 # We need to verify all the deletions up front, and only allow it through # if all of them are valid. all_ids = [node.id for node in self.lbs[lb_id].nodes] non_nodes = set(node_ids).difference(all_ids) if non_nodes: return validation_errors([invalid_node_ids_error(non_nodes)]) # Allow only batch_delete_limit nodes at a time if len(node_ids) > self.batch_delete_limit: err = batch_delete_limit_error(len(node_ids), self.batch_delete_limit) return validation_errors([err]) for node_id in node_ids: # It should not be possible for this to fail, since we've already # checked that they all exist. assert self._delete_node(lb_id, node_id) is True self._verify_and_update_lb_state(lb_id, current_timestamp=current_timestamp) return EMPTY_RESPONSE, 202
def add_node(node_list, lb_id): """ Returns the canned response for add nodes """ if lb_id in lb_cache: nodes = _format_nodes_on_lb(node_list) if lb_cache[lb_id].get("nodes"): for existing_node in lb_cache[lb_id]["nodes"]: for new_node in node_list: if (existing_node["address"] == new_node["address"] and existing_node["port"] == new_node["port"]): return invalid_resource("Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413), 413 lb_cache[lb_id]["nodes"] = lb_cache[lb_id]["nodes"] + nodes else: lb_cache[lb_id]["nodes"] = nodes return {"nodes": nodes}, 200 else: return not_found_response("loadbalancer"), 404
def delete_nodes(self, lb_id, node_ids): """ Bulk-delete multiple LB nodes. """ if not node_ids: resp = { "message": "Must supply one or more id's to process this request.", "code": 400} return resp, 400 if lb_id not in self.lbs: return not_found_response("loadbalancer"), 404 current_timestamp = self.clock.seconds() self._verify_and_update_lb_state(lb_id, False, current_timestamp) if self.lbs[lb_id]["status"] != "ACTIVE": # Error message verified as of 2015-04-22 resp = {"message": "LoadBalancer is not ACTIVE", "code": 422} return resp, 422 # We need to verify all the deletions up front, and only allow it through # if all of them are valid. all_ids = [node.id for node in self.lbs[lb_id].nodes] non_nodes = set(node_ids).difference(all_ids) if non_nodes: return validation_errors([invalid_node_ids_error(non_nodes)]) # Allow only batch_delete_limit nodes at a time if len(node_ids) > self.batch_delete_limit: err = batch_delete_limit_error(len(node_ids), self.batch_delete_limit) return validation_errors([err]) for node_id in node_ids: # It should not be possible for this to fail, since we've already # checked that they all exist. assert self._delete_node(lb_id, node_id) is True self._verify_and_update_lb_state( lb_id, current_timestamp=current_timestamp) return EMPTY_RESPONSE, 202
def add_node(node_list, lb_id): """ Returns the canned response for add nodes """ if lb_id in lb_cache: nodes = _format_nodes_on_lb(node_list) if lb_cache[lb_id].get("nodes"): for existing_node in lb_cache[lb_id]["nodes"]: for new_node in node_list: if (existing_node["address"] == new_node["address"] and existing_node["port"] == new_node["port"]): return invalid_resource( "Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413), 413 lb_cache[lb_id]["nodes"] = lb_cache[lb_id]["nodes"] + nodes else: lb_cache[lb_id]["nodes"] = nodes return {"nodes": nodes}, 200 else: return not_found_response("loadbalancer"), 404
def del_load_balancer(self, lb_id): """ Returns response for a load balancer is in building status for 20 seconds and response code 202, and adds the new lb to ``self.lbs``. A loadbalancer, on delete, goes into PENDING-DELETE and remains in DELETED status until a nightly job(maybe?) """ if lb_id in self.lbs: current_timestamp = self.clock.seconds() if self.lbs[lb_id]["status"] == "PENDING-DELETE": msg = ( "Must provide valid load balancers: {0} are immutable and " "could not be processed.".format(lb_id)) # Dont doubt this to be 422, it is 400! return invalid_resource(msg, 400), 400 _verify_and_update_lb_state(self, lb_id, True, current_timestamp) if any([ self.lbs[lb_id]["status"] == "ACTIVE", self.lbs[lb_id]["status"] == "ERROR", self.lbs[lb_id]["status"] == "PENDING-UPDATE" ]): del self.lbs[lb_id] return EMPTY_RESPONSE, 202 if self.lbs[lb_id]["status"] == "PENDING-DELETE": return EMPTY_RESPONSE, 202 if self.lbs[lb_id]["status"] == "DELETED": _verify_and_update_lb_state( self, lb_id, current_timestamp=current_timestamp) msg = "Must provide valid load balancers: {0} could not be found.".format( lb_id) # Dont doubt this to be 422, it is 400! return invalid_resource(msg, 400), 400 return not_found_response("loadbalancer"), 404
def add_node(store, node_list, lb_id, current_timestamp): """ Returns the canned response for add nodes """ if lb_id in store.lbs: _verify_and_update_lb_state(store, lb_id, False, current_timestamp) if store.lbs[lb_id]["status"] != "ACTIVE": resource = invalid_resource( "Load Balancer '{0}' has a status of {1} and is considered " "immutable.".format(lb_id, store.lbs[lb_id]["status"]), 422) return (resource, 422) nodes = _format_nodes_on_lb(node_list) if store.lbs[lb_id].get("nodes"): for existing_node in store.lbs[lb_id]["nodes"]: for new_node in node_list: if (existing_node["address"] == new_node["address"] and existing_node["port"] == new_node["port"]): resource = invalid_resource( "Duplicate nodes detected. One or more nodes " "already configured on load balancer.", 413) return (resource, 413) store.lbs[lb_id]["nodes"] = store.lbs[lb_id]["nodes"] + nodes else: store.lbs[lb_id]["nodes"] = nodes store.lbs[lb_id]["nodeCount"] = len(store.lbs[lb_id]["nodes"]) _verify_and_update_lb_state(store, lb_id, current_timestamp=current_timestamp) return {"nodes": nodes}, 200 return not_found_response("loadbalancer"), 404