Skip to content

Commit

Permalink
Merge pull request #271 from XuHugo/channel
Browse files Browse the repository at this point in the history
Allow to delete an agent
  • Loading branch information
yeasy authored Jul 23, 2021
2 parents 851f960 + 17dda43 commit 96d9cb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/api-engine/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class Node(models.Model):
Agent,
help_text="Agent of node",
null=True,
related_name="agent",
related_name="node",
on_delete=models.CASCADE
)
# network = models.ForeignKey(
Expand Down
20 changes: 13 additions & 7 deletions src/api-engine/api/routes/agent/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ def update(self, request, pk=None):
serializer = AgentUpdateBody(data=request.data)
if serializer.is_valid(raise_exception=True):
name = serializer.validated_data.get("name")
urls = serializer.validated_data.get("urls")
organization = serializer.validated_data.get("organization")
#urls = serializer.validated_data.get("urls")
#organization = request.user.organization
try:
Agent.objects.get(name=name)
except ObjectDoesNotExist:
pass
Agent.objects.filter(id=pk).update(name=name, urls=urls, organization=organization)
Agent.objects.filter(id=pk).update(name=name)

return Response(status=status.HTTP_202_ACCEPTED)

Expand Down Expand Up @@ -281,16 +281,22 @@ def partial_update(self, request, pk=None):
)
def destroy(self, request, pk=None):
"""
Delete Agent
Delete agent
:param request: destory parameter
:param pk: primary key
:return: none
:rtype: rest_framework.status
"""
try:
agent = Agent.objects.get(id=pk)
if request.user.is_administrator:
agent = Agent.objects.get(id=pk)
else:
raise CustomError("User can't delete agent!")
except ObjectDoesNotExist:
raise ResourceNotFound
else:
if agent.organization is not None:
if agent.node.count():
raise ResourceInUse
agent.delete()

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/pages/Operator/Agent/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class Agent extends PureComponent {
});
} else {
dispatch({
type: 'agent/releaseAgent',
type: 'agent/deleteAgent',
payload: agent.id,
callback: this.deleteCallback,
});
Expand Down

0 comments on commit 96d9cb4

Please sign in to comment.