enabled keys
diff --git a/plstackapi/planetstack/admin.py b/plstackapi/planetstack/admin.py
index a42c7d6..4410d97 100644
--- a/plstackapi/planetstack/admin.py
+++ b/plstackapi/planetstack/admin.py
@@ -6,6 +6,8 @@
admin.site.register(Role)
admin.site.register(Site)
+admin.site.register(User)
+admin.site.register(Key)
admin.site.register(Slice)
admin.site.register(Node)
admin.site.register(DeploymentNetwork)
diff --git a/plstackapi/planetstack/api/keys.py b/plstackapi/planetstack/api/keys.py
new file mode 100644
index 0000000..771638e
--- /dev/null
+++ b/plstackapi/planetstack/api/keys.py
@@ -0,0 +1,45 @@
+from plstackapi.openstack.client import OpenStackClient
+from plstackapi.openstack.driver import OpenStackDriver
+from plstackapi.planetstack.api.auth import auth_check
+from plstackapi.planetstack.models import Key, User
+
+def lookup_user(fields):
+ user = None
+ if 'user' in fields:
+ if isinstance(fields['user'], int):
+ users = User.objects.filter(id=fields['user'])
+ else:
+ users = Site.objects.filter(email=fields['user'])
+ if users:
+ user = users[0]
+ return user
+
+def add_key(auth, fields):
+ driver = OpenStackDriver(client = auth_check(auth))
+ user = lookup_user(fields)
+ if user: fields['user'] = user
+ key = Key(**fields)
+ nova_fields = {'name': key.name,
+ 'key': key.key}
+ nova_key = driver.create_keypair(**nova_fields)
+ key.save()
+ return key
+
+def update_key(auth, id, **fields):
+ return
+
+def delete_user(auth, filter={}):
+ driver = OpenStackDriver(client = auth_check(auth))
+ keys = Key.objects.filter(**filter)
+ for key in keys:
+ driver.delete_key(name=key.name)
+ key.delete()
+ return 1
+
+def get_keys(auth, filter={}):
+ client = auth_check(auth)
+ keys = Key.objects.filter(**filter)
+ return keys
+
+
+
diff --git a/plstackapi/planetstack/models.py b/plstackapi/planetstack/models.py
index 1c1774d..7561b5e 100644
--- a/plstackapi/planetstack/models.py
+++ b/plstackapi/planetstack/models.py
@@ -227,7 +227,7 @@
name = models.CharField(max_length=256, unique=True)
key = models.CharField(max_length=512)
type = models.CharField(max_length=256)
- blacklisted = models.BooleanField()
+ blacklisted = models.BooleanField(default=False)
user = models.ForeignKey(User, related_name='keys')
def __unicode__(self): return u'%s' % (self.name)
diff --git a/plstackapi/planetstack/serializers.py b/plstackapi/planetstack/serializers.py
index 81a4ba8..4a38059 100644
--- a/plstackapi/planetstack/serializers.py
+++ b/plstackapi/planetstack/serializers.py
@@ -34,7 +34,18 @@
'slice_memberships',
'site_privileges')
-
+class KeySerializer(serializers.HyperlinkedModelSerializer):
+ id = serializers.Field()
+ user = serializers.HyperlinkedRelatedField(view_name='user-detail')
+ class Meta:
+ model = Key
+ fields = ('id',
+ 'name',
+ 'key',
+ 'type',
+ 'blacklisted',
+ 'user')
+
class SliceSerializer(serializers.HyperlinkedModelSerializer):
# HyperlinkedModelSerializer doesn't include the id by default
@@ -154,6 +165,7 @@
serializerLookUp = {
Role: RoleSerializer,
User: UserSerializer,
+ Key: KeySerializer,
Site: SiteSerializer,
Slice: SliceSerializer,
Node: NodeSerializer,
diff --git a/plstackapi/planetstack/urls.py b/plstackapi/planetstack/urls.py
index dd333e8..7ef94db 100644
--- a/plstackapi/planetstack/urls.py
+++ b/plstackapi/planetstack/urls.py
@@ -5,6 +5,7 @@
from plstackapi.planetstack.views.roles import RoleListCreate, RoleRetrieveUpdateDestroy
from plstackapi.planetstack.views.sites import SiteListCreate, SiteRetrieveUpdateDestroy
from plstackapi.planetstack.views.users import UserListCreate, UserRetrieveUpdateDestroy
+from plstackapi.planetstack.views.users import KeyListCreate, KeyRetrieveUpdateDestroy
from plstackapi.planetstack.views.deployment_networks import DeploymentNetworkListCreate, DeploymentNetworkRetrieveUpdateDestroy
from plstackapi.planetstack.views.images import ImageListCreate, ImageRetrieveUpdateDestroy
from plstackapi.planetstack.views.flavors import FlavorListCreate, FlavorRetrieveUpdateDestroy
@@ -33,9 +34,13 @@
url(r'^plstackapi/users/$', UserListCreate.as_view(), name='user-list'),
url(r'^plstackapi/users/(?P<pk>[a-zA-Z0-9]+)/$', UserRetrieveUpdateDestroy.as_view(), name='user-detail'),
+ url(r'^plstackapi/keys/$', KeyListCreate.as_view(), name='key-list'),
+ url(r'^plstackapi/keys/(?P<pk>[a-zA-Z0-9]+)/$', KeyRetrieveUpdateDestroy.as_view(), name='key-detail'),
+
url(r'^plstackapi/sites/$', SiteListCreate.as_view(), name='site-list'),
url(r'^plstackapi/sites/(?P<pk>[a-zA-Z0-9_]+)/$', SiteRetrieveUpdateDestroy.as_view(), name='site-detail'),
+
#url(r'^plstackapi/slices/$', views.SliceList.as_view(), name='slice-list'),
#url(r'^plstackapi/slices/(?P<pk>[0-9]+)/$', views.SliceDetail.as_view(), name='slice-detail'),
diff --git a/plstackapi/planetstack/views/api_root.py b/plstackapi/planetstack/views/api_root.py
index 560d9eb..10ceae1 100644
--- a/plstackapi/planetstack/views/api_root.py
+++ b/plstackapi/planetstack/views/api_root.py
@@ -7,6 +7,7 @@
return Response({
'roles': reverse('role-list', request=request, format=format),
'users': reverse('user-list', request=request, format=format),
+ 'keys': reverse('key-list', request=request, format=format),
#'nodes': reverse('node-list', request=request, format=format),
'sites': reverse('site-list', request=request, format=format),
'deploymentNetworks': reverse('deploymentnetwork-list', request=request, format=format),
diff --git a/plstackapi/planetstack/views/keys.py b/plstackapi/planetstack/views/keys.py
new file mode 100644
index 0000000..fde6b40
--- /dev/null
+++ b/plstackapi/planetstack/views/keys.py
@@ -0,0 +1,66 @@
+from django.http import Http404
+from rest_framework.views import APIView
+from rest_framework.response import Response
+from rest_framework import status
+
+from plstackapi.planetstack.api.keys import add_key, delete_key, get_keys, update_key
+from plstackapi.planetstack.serializers import KeySerializer
+from plstackapi.util.request import parse_request
+
+
+class KeyListCreate(APIView):
+ """
+ List all users or create a new user.
+ """
+
+ def post(self, request, format = None):
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ elif 'key' in data:
+ key = add_user(data['auth'], data['key'])
+ serializer = UserSerializer(key)
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+ else:
+ keys = get_keys(data['auth'])
+ serializer = UserSerializer(keys, many=True)
+ return Response(serializer.data)
+
+
+class KeyRetrieveUpdateDestroy(APIView):
+ """
+ Retrieve, update or delete a user
+ """
+
+ def post(self, request, pk, format=None):
+ """Retrieve a key"""
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ keys = get_keys(data['auth'], {'id': pk})
+ if not keys:
+ return Response(status=status.HTTP_404_NOT_FOUND)
+ serializer = UserSerializer(keys[0])
+ return Response(serializer.data)
+
+ def put(self, request, pk, format=None):
+ """update a key"""
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ elif 'key' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+
+ key = update_key(pk, data['key'])
+ serializer = UserSerializer(key)
+ return Response(serializer.data)
+
+ def delete(self, request, pk, format=None):
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ delete_key(data['auth'], {'id': pk})
+ return Response(status=status.HTTP_204_NO_CONTENT)
+
+
+