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