def update(self, request, pk=None): """ Update Node Update special node with id. """ serializer = NodeUpdateBody(data=request.data) if serializer.is_valid(raise_exception=True): node_status = serializer.validated_data.get("status") ports = serializer.validated_data.get("ports", []) try: node = Node.objects.get(id=pk) except ObjectDoesNotExist: raise ResourceNotFound node.status = node_status node.save() for port_item in ports: port = Port( external=port_item.get("external"), internal=port_item.get("internal"), node=node, ) port.save() return Response(status=status.HTTP_202_ACCEPTED)
def find_available_ports( ip=None, node_id=None, agent_id=None, request_count=1, exclude_ports=None, retry=MAX_RETRY, ): if node_id is None or agent_id is None or retry == 0: return [] all_port_is_free = True if exclude_ports is None: exclude_ports = [] ports = port_picker(agent_id, request_count, exclude_ports) for port in ports: if not port_is_free(ip, port): exclude_ports.append(port) all_port_is_free = False if not all_port_is_free: retry -= 1 return find_available_ports(ip, node_id, agent_id, request_count, exclude_ports, retry) try: node = Node.objects.get(id=node_id) except ObjectDoesNotExist: return [] else: port_objects = [Port(external=port, node=node) for port in ports] Port.objects.bulk_create(port_objects) return ports
def set_ports_mapping(node_id=None, mapping=None, new=False): if mapping is None: mapping = [] if new: try: node = Node.objects.get(id=node_id) except ObjectDoesNotExist: LOG.error("Node not found") else: port_objects = [ Port( external=port.get("external"), internal=port.get("internal"), node=node, ) for port in mapping ] Port.objects.bulk_create(port_objects) else: for port in mapping: Port.objects.filter(node__id=node_id, external=port.get("external")).update( internal=port.get("internal"))