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 | ef30197 | 2013-04-09 02:43:40 -0400 | [diff] [blame] | 6 | from plstackapi.planetstack.api.deployment_networks import add_deployment_network, delete_deployment_network, get_deployment_networks |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 7 | from plstackapi.planetstack.serializers import DeploymentNetworkSerializer |
| 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) |
| 18 | if 'auth' not in data: |
| 19 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 20 | elif 'deployment_network' in data: |
Tony Mack | 48f8503 | 2013-04-09 07:52:58 -0400 | [diff] [blame] | 21 | deployment = add_deployment_network(data['auth'], data['deployment_network'].get('name')) |
Tony Mack | 2d81e63 | 2013-04-09 02:34:10 -0400 | [diff] [blame] | 22 | 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 | |
| 30 | class 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 | |