def operate(self, request, pk=None): """ Operate Node Do some operation on node, start/stop/restart """ try: serializer = NodeOperationSerializer(data=request.data) if serializer.is_valid(raise_exception=True): action = serializer.validated_data.get("action") if action == "start": try: infos = self._agent_params(pk) agent = AgentHandler(infos) cid = agent.create(infos) if cid: Node.objects.filter(id=pk).update(cid=cid) response = NodeCIDSerializer(data={"id": cid}) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED) else: raise ResourceNotFound except Exception as e: raise e if infos.get("status") == "running" or infos.get( "status") == "deleting" or infos.get( "status") == "deploying": raise ResourceInUse elif infos.get("status") == "": pass elif infos.get("status") == "stopped" or infos.get( "status") == "deleted": pass elif infos.get("status") == "error": pass else: pass elif action == "stop": #todo pass elif action == "restart": # todo pass else: # todo pass except Exception as e: return Response(err(e.args), status=status.HTTP_400_BAD_REQUEST)
def _start_node(self, pk): """ start node from agent :param node: node id :return: null """ try: infos = self._agent_params(pk) agent = AgentHandler(infos) cid = agent.create(infos) if cid: Node.objects.filter(id=pk).update(cid=cid) else: raise ResourceNotFound except Exception as e: raise e