Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework.views import APIView |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from syndicate_storage.models import Volume |
| 10 | from django.core.exceptions import PermissionDenied |
| 11 | from xos.exceptions import XOSNotFound |
| 12 | |
| 13 | class SSHKeyList(APIView): |
| 14 | method_kind = "list" |
| 15 | method_name = "sshkeys" |
| 16 | |
| 17 | def get(self, request, format=None): |
| 18 | instances=[] |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 19 | for sliver in self.get_queryset().all(): |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 20 | if sliver.instance_id: |
| 21 | instances.append( {"id": sliver.instance_id, |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 22 | "public_keys": sliver.get_public_keys(), |
| 23 | "node_name": sliver.node.name } ) |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 24 | |
| 25 | return Response(instances) |
| 26 | |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 27 | def get_queryset(self): |
| 28 | queryset = queryset=Sliver.objects.all() |
| 29 | |
| 30 | node_name = self.request.QUERY_PARAMS.get('node_name', None) |
| 31 | if node_name is not None: |
| 32 | queryset = queryset.filter(node__name = node_name) |
| 33 | |
| 34 | return queryset |
| 35 | |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 36 | class SSHKeyDetail(APIView): |
| 37 | method_kind = "detail" |
| 38 | method_name = "sshkeys" |
| 39 | |
| 40 | def get(self, request, format=None, pk=0): |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 41 | slivers = self.get_queryset().filter(instance_id=pk) |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 42 | if not slivers: |
| 43 | raise XOSNotFound("didn't find sliver for instance %s" % pk) |
| 44 | return Response( [ {"id": slivers[0].instance_id, |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 45 | "public_keys": slivers[0].get_public_keys(), |
Scott Baker | f46cec1 | 2015-05-07 15:31:55 -0700 | [diff] [blame] | 46 | "node_name": slivers[0].node.name } ]) |
Scott Baker | 071dae7 | 2015-05-07 15:28:23 -0700 | [diff] [blame] | 47 | |
| 48 | def get_queryset(self): |
| 49 | queryset = queryset=Sliver.objects.all() |
| 50 | |
| 51 | node_name = self.request.QUERY_PARAMS.get('node_name', None) |
| 52 | if node_name is not None: |
| 53 | queryset = queryset.filter(node__name = node_name) |
| 54 | |
| 55 | return queryset |
Scott Baker | a115c1e | 2015-05-07 14:58:46 -0700 | [diff] [blame] | 56 | |