Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent #212

Merged
merged 2 commits into from
Jan 25, 2021
Merged

Agent #212

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 13 additions & 35 deletions src/api-engine/api/routes/agent/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def validate(self, attrs):


class AgentCreateBody(serializers.ModelSerializer):
organization = serializers.UUIDField(help_text=IDHelpText)

def to_form_paras(self):
custom_paras = to_form_paras(self)

Expand All @@ -118,21 +120,16 @@ class Meta:
model = Agent
fields = (
"name",
"capacity",
"node_capacity",
"log_level",
"type",
"schedulable",
"ip",
"image",
"urls",
"config_file",
"organization",
)
extra_kwargs = {
"capacity": {"required": True},
"node_capacity": {"required": True},
"type": {"required": True},
"ip": {"required": True},
"image": {"required": True},
"urls": {"required": True},
"name": {"required": True},
"organization": {"required": False},
"config_file": {
"required": False,
"validators": [
Expand All @@ -145,13 +142,7 @@ class Meta:
}

def validate(self, attrs):
capacity = attrs.get("capacity")
node_capacity = attrs.get("node_capacity")
if node_capacity < capacity:
raise serializers.ValidationError(
"Node capacity must larger than capacity"
)

pass
return attrs


Expand Down Expand Up @@ -187,42 +178,29 @@ class AgentUpdateBody(AgentPatchBody):


class AgentResponseSerializer(AgentIDSerializer, serializers.ModelSerializer):
organization_id = serializers.UUIDField(
organization = serializers.UUIDField(
help_text="Organization ID", required=False, allow_null=True
)
config_file = serializers.URLField(
help_text="Config file of agent", required=False
)

class Meta:
model = Agent
fields = (
"id",
"name",
"capacity",
"node_capacity",
"status",
"created_at",
"log_level",
"type",
"schedulable",
"organization_id",
"config_file",
"ip",
"image",
"urls",
"organization",
)
extra_kwargs = {
"id": {"required": True},
"name": {"required": True},
"status": {"required": True},
"capacity": {"required": True},
"node_capacity": {"required": True},
"created_at": {"required": True, "read_only": False},
"type": {"required": True},
"log_level": {"required": True},
"schedulable": {"required": True},
"ip": {"required": True},
"image": {"required": True},
"organization": {"required": True},
"urls": {"required": True},
}


Expand Down
77 changes: 36 additions & 41 deletions src/api-engine/api/routes/agent/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
NoResource,
ResourceInUse,
)
from api.models import Agent, KubernetesConfig
from api.models import Agent, KubernetesConfig, Organization
from api.routes.agent.serializers import (
AgentQuery,
AgentListResponse,
Expand All @@ -39,6 +39,7 @@


class AgentViewSet(viewsets.ViewSet):
"""Class represents agent related operations."""
authentication_classes = (JSONWebTokenAuthentication,)

def get_permissions(self):
Expand All @@ -62,7 +63,9 @@ def list(self, request):
"""
List Agents
Filter agents with query parameters.
:param request: query parameter
:return: agent list
:rtype: list
"""
serializer = AgentQuery(data=request.GET)
if serializer.is_valid(raise_exception=True):
Expand All @@ -74,18 +77,18 @@ def list(self, request):
organization = serializer.validated_data.get("organization")

query_filters = {}
if organization:
if not request.user.is_operator:
raise PermissionDenied()
query_filters.update({"organization": organization})
else:
org_name = (
request.user.organization.name
if request.user.organization
else ""
)
if request.user.is_administrator:
query_filters.update({"organization__name": org_name})
# if organization:
# if not request.user.is_operator:
# raise PermissionDenied()
# query_filters.update({"organization": organization})
# else:
# org_name = (
# request.user.organization.name
# if request.user.organization
# else ""
# )
# if request.user.is_administrator:
# query_filters.update({"organization__name": org_name})
if name:
query_filters.update({"name__icontains": name})
if agent_status:
Expand All @@ -100,13 +103,6 @@ def list(self, request):
agent_list = []
for agent in agents:
agent_dict = agent.__dict__
agent_dict.update(
{
"config_file": request.build_absolute_uri(
agent.config_file.url
)
}
)
agent_list.append(agent_dict)

response = AgentListResponse(
Expand All @@ -127,45 +123,41 @@ def create(self, request):
"""
Create Agent
Create new 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")
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")
urls = serializer.validated_data.get("urls")
config_file = serializer.validated_data.get("config_file")
organization = serializer.validated_data.get("organization")

body = {
"capacity": capacity,
"node_capacity": node_capacity,
"type": agent_type,
"ip": ip,
"image": image,
"urls": urls,
"name": name,
}
if name:
agent_count = Agent.objects.filter(
name=name, organization=request.user.organization
name=name
).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})
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()
Expand All @@ -185,7 +177,10 @@ def retrieve(self, request, pk=None):
"""
Retrieve agent
Retrieve agent
:param request: destory parameter
:param pk: primary key
:return: none
:rtype: rest_framework.status
"""
try:
if request.user.is_operator:
Expand Down