blob: 73dce9532a9211ec5845b21944134b5f32b11d12 [file] [log] [blame]
Tony Mack2d81e632013-04-09 02:34:10 -04001from django.http import Http404
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5
Tony Mackef301972013-04-09 02:43:40 -04006from plstackapi.planetstack.api.deployment_networks import add_deployment_network, delete_deployment_network, get_deployment_networks
Tony Mack2d81e632013-04-09 02:34:10 -04007from plstackapi.planetstack.serializers import DeploymentNetworkSerializer
8from plstackapi.util.request import parse_request
9
10
11class DeploymentNetworkListCreate(APIView):
12 """
13 List all deployment networks or create a new role.
14 """
15
16 def post(self, request, format = None):
17 data = parse_request(request.DATA)
18 if 'auth' not in data:
19 return Response(status=status.HTTP_400_BAD_REQUEST)
20 elif 'deployment_network' in data:
Tony Mack48f85032013-04-09 07:52:58 -040021 deployment = add_deployment_network(data['auth'], data['deployment_network'].get('name'))
Tony Mack2d81e632013-04-09 02:34:10 -040022 serializer = DeploymentNetworkSerializer(deployment)
23 return Response(serializer.data, status=status.HTTP_201_CREATED)
24 else:
25 deployment_networks = get_deployment_networks(data['auth'])
26 serializer = DeploymentNetworkSerializer(deployment_networks, many=True)
27 return Response(serializer.data)
28
29
30class DeploymentNetworkRetrieveUpdateDestroy(APIView):
31 """
32 Retrieve, update or delete a deployment network
33 """
34
35 def post(self, request, pk, format=None):
36 """Retrieve a deployment network"""
37 data = parse_request(request.DATA)
38 if 'auth' not in data:
39 return Response(status=status.HTTP_400_BAD_REQUEST)
40 deployment_networks = get_deployment_networks(data['auth'], {'name': pk})
41 if not deployment_networks:
42 return Response(status=status.HTTP_404_NOT_FOUND)
43 serializer = DeploymentNetworkSerializer(deployment_networks[0])
44 return Response(serializer.data)
45
46 def put(self, request, pk, format=None):
47 """deployment network update not implemnted"""
48 return Response(status=status.HTTP_404_NOT_FOUND)
49
50 def delete(self, request, pk, format=None):
51 data = parse_request(request.DATA)
52 if 'auth' not in data:
53 return Response(status=status.HTTP_400_BAD_REQUEST)
54 delete_deployment_network(data['auth'], {'name': pk})
55 return Response(status=status.HTTP_204_NO_CONTENT)
56
57
58