Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 1 | from django.http import Http404 |
| 2 | from rest_framework.views import APIView |
| 3 | from rest_framework.response import Response |
| 4 | from rest_framework import status |
| 5 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.api.deployment_networks import add_deployment_network, delete_deployment_network, get_deployment_networks |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 7 | from core.serializers import DeploymentSerializer |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 8 | from util.request import parse_request |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 9 | |
| 10 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 11 | class DeploymentListCreate(APIView): |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 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) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 20 | elif 'deploymentNetwork' in data: |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 21 | |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 22 | deployment = add_deployment_network(data['auth'], data['deploymentNetwork'].get('name')) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 23 | serializer = DeploymentSerializer(deployment) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 24 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 25 | else: |
| 26 | deployment_networks = get_deployment_networks(data['auth']) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 27 | serializer = DeploymentSerializer(deployment_networks, many=True) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 28 | return Response(serializer.data) |
| 29 | |
| 30 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 31 | class DeploymentRetrieveUpdateDestroy(APIView): |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 32 | """ |
| 33 | Retrieve, update or delete a deployment network |
| 34 | """ |
| 35 | |
| 36 | def post(self, request, pk, format=None): |
| 37 | """Retrieve a deployment network""" |
| 38 | data = parse_request(request.DATA) |
| 39 | if 'auth' not in data: |
| 40 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 41 | deployment_networks = get_deployment_networks(data['auth'], pk) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 42 | if not deployment_networks: |
| 43 | return Response(status=status.HTTP_404_NOT_FOUND) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame^] | 44 | serializer = DeploymentSerializer(deployment_networks[0]) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 45 | return Response(serializer.data) |
| 46 | |
| 47 | def put(self, request, pk, format=None): |
| 48 | """deployment network update not implemnted""" |
| 49 | return Response(status=status.HTTP_404_NOT_FOUND) |
| 50 | |
| 51 | def delete(self, request, pk, format=None): |
| 52 | data = parse_request(request.DATA) |
| 53 | if 'auth' not in data: |
| 54 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 55 | delete_deployment_network(data['auth'], pk) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 56 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 57 | |
| 58 | |
| 59 | |