def apply(self, request): """ Apply Agent Apply Agent """ serializer = AgentApplySerializer(data=request.data) if serializer.is_valid(raise_exception=True): agent_type = serializer.validated_data.get("type") capacity = serializer.validated_data.get("capacity") if request.user.organization is None: raise CustomError(detail="Need join in organization") agent_count = Agent.objects.filter( organization=request.user.organization).count() if agent_count > 0: raise CustomError(detail="Already applied agent.") agents = Agent.objects.filter( organization__isnull=True, type=agent_type, capacity__gte=capacity, schedulable=True, ).order_by("capacity") if len(agents) == 0: raise NoResource agent = agents[0] agent.organization = request.user.organization agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_200_OK)
def create(self, request): serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): agent_name = serializer.validated_data.get("name") agent_type = serializer.validated_data.get("type") agent_urls = serializer.validated_data.get("urls") agent_org = serializer.validated_data.get("organization") org_name = agent_org["name"] try: Agent.objects.get(name=agent_name) except ObjectDoesNotExist: pass org = Organization.objects.get(name=org_name) agent = Agent(name=agent_name, type=agent_type, urls=agent_urls, organization=org) agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED)
def create(self, request): """ Create Agent Create new agent """ serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") worker_api = serializer.validated_data.get("worker_api") capacity = serializer.validated_data.get("capacity") node_capacity = serializer.validated_data.get("node_capacity") log_level = serializer.validated_data.get("log_level") agent_type = serializer.validated_data.get("type") schedulable = serializer.validated_data.get("schedulable") parameters = serializer.validated_data.get("parameters") k8s_config = serializer.validated_data.get("k8s_config") body = { "worker_api": worker_api, "capacity": capacity, "node_capacity": node_capacity, "type": agent_type, } if worker_api: agent_count = Agent.objects.filter( worker_api=worker_api, organization=request.user.organization, ).count() if agent_count > 0: raise ResourceExists(detail="Work api %s already exists" % worker_api) if name: agent_count = Agent.objects.filter( name=name, organization=request.user.organization).count() if agent_count > 0: raise ResourceExists(detail="Name %s already exists" % name) body.update({"name": name}) if schedulable is not None: body.update({"schedulable": schedulable}) if log_level is not None: body.update({"log_level": log_level}) if parameters is not None: body.update({"parameters": parameters}) agent = Agent(**body) agent.save() if agent_type == HostType.Kubernetes.name.lower(): kubernetes_config = KubernetesConfig(**dict(k8s_config), agent=agent) kubernetes_config.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED)
def create(self, request): """ Create Agent Create new agent """ serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") capacity = serializer.validated_data.get("capacity") node_capacity = serializer.validated_data.get("node_capacity") log_level = serializer.validated_data.get("log_level") agent_type = serializer.validated_data.get("type") schedulable = serializer.validated_data.get("schedulable") parameters = serializer.validated_data.get("parameters") ip = serializer.validated_data.get("ip") image = serializer.validated_data.get("image") config_file = serializer.validated_data.get("config_file") body = { "capacity": capacity, "node_capacity": node_capacity, "type": agent_type, "ip": ip, "image": image, } if name: agent_count = Agent.objects.filter( name=name, organization=request.user.organization ).count() if agent_count > 0: raise ResourceExists( detail="Name %s already exists" % name ) body.update({"name": name}) if schedulable is not None: body.update({"schedulable": schedulable}) if log_level is not None: body.update({"log_level": log_level}) if parameters is not None: body.update({"parameters": parameters}) if config_file is not None: body.update({"config_file": config_file}) agent = Agent(**body) agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response( response.validated_data, status=status.HTTP_201_CREATED )
def create(self, request): """ Create Agent :param request: create parameter :return: agent ID :rtype: uuid """ try: serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") agent_type = serializer.validated_data.get("type") urls = serializer.validated_data.get("urls") config_file = serializer.validated_data.get("config_file") body = { "type": agent_type, "urls": urls, "name": name, } if name: agent_count = Agent.objects.filter(name=name).count() if agent_count > 0: raise ResourceExists body.update({"name": name}) if config_file is not None: body.update({"config_file": config_file}) org = request.user.organization if org.agent.all(): raise ResourceExists else: body.update({"organization": org}) agent = Agent(**body) agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response(ok(response.validated_data), status=status.HTTP_201_CREATED) except ResourceExists as e: raise e except Exception as e: return Response(err(e.args), status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """ Create Agent :param request: create parameter :return: agent ID :rtype: uuid """ serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") agent_type = serializer.validated_data.get("type") urls = serializer.validated_data.get("urls") config_file = serializer.validated_data.get("config_file") organization = serializer.validated_data.get("organization") body = { "type": agent_type, "urls": urls, "name": name, } if name: agent_count = Agent.objects.filter(name=name).count() if agent_count > 0: raise ResourceExists(detail="Name %s already exists" % name) body.update({"name": name}) if config_file is not None: body.update({"config_file": config_file}) if organization is not None: org = Organization.objects.get(id=organization) if org.organization.all(): raise ResourceExists else: body.update({"organization": org}) agent = Agent(**body) agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response(response.validated_data, status=status.HTTP_201_CREATED)
def apply(self, request): """ Apply Agent Apply Agent """ serializer = AgentApplySerializer(data=request.data) if serializer.is_valid(raise_exception=True): agent_type = serializer.validated_data.get("type") capacity = serializer.validated_data.get("capacity") if request.user.organization is None: raise CustomError(detail="Need join in organization") agent_count = Agent.objects.filter( organization=request.user.organization ).count() if agent_count > 0: raise CustomError(detail="Already applied agent.") agents = Agent.objects.filter( organization__isnull=True, type=agent_type, capacity__gte=capacity, schedulable=True, ).order_by("capacity") if len(agents) == 0: raise NoResource agent = agents[0] agent.organization = request.user.organization agent.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response( response.validated_data, status=status.HTTP_200_OK )
def create(self, request): """ Create Agent Create new agent """ serializer = AgentCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") worker_api = serializer.validated_data.get("worker_api") capacity = serializer.validated_data.get("capacity") node_capacity = serializer.validated_data.get("node_capacity") log_level = serializer.validated_data.get("log_level") agent_type = serializer.validated_data.get("type") schedulable = serializer.validated_data.get("schedulable") parameters = serializer.validated_data.get("parameters") k8s_config = serializer.validated_data.get("k8s_config") body = { "worker_api": worker_api, "capacity": capacity, "node_capacity": node_capacity, "type": agent_type, } if worker_api: agent_count = Agent.objects.filter( worker_api=worker_api, organization=request.user.organization, ).count() if agent_count > 0: raise ResourceExists( detail="Work api %s already exists" % worker_api ) if name: agent_count = Agent.objects.filter( name=name, organization=request.user.organization ).count() if agent_count > 0: raise ResourceExists( detail="Name %s already exists" % name ) body.update({"name": name}) if schedulable is not None: body.update({"schedulable": schedulable}) if log_level is not None: body.update({"log_level": log_level}) if parameters is not None: body.update({"parameters": parameters}) agent = Agent(**body) agent.save() if agent_type == HostType.Kubernetes.name.lower(): kubernetes_config = KubernetesConfig( **dict(k8s_config), agent=agent ) kubernetes_config.save() response = AgentIDSerializer(data=agent.__dict__) if response.is_valid(raise_exception=True): return Response( response.validated_data, status=status.HTTP_201_CREATED )