initial checkin
diff --git a/plstackapi/planetstack/api/sites.py b/plstackapi/planetstack/api/sites.py
new file mode 100644
index 0000000..e667c72
--- /dev/null
+++ b/plstackapi/planetstack/api/sites.py
@@ -0,0 +1,48 @@
+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 Site
+
+
+def add_site(auth, **fields):
+ driver = OpenStackDriver(client = auth_check(auth))
+ site = Site(**fields)
+ nova_fields = {'tenant_name': fields['login_base'],
+ 'description': fields['name',
+ 'enabled': fields['enabled']}
+ tenant = driver.create_tenant(**nova_fields)
+ site.tenant_id=tenant.id
+ site.save()
+ return role
+
+def update_site(auth, tenant_id, **fields):
+ driver = OpenStackDriver(client = auth_check(auth))
+ sites = Site.objects.filter(tenant_id=tenant_id)
+ if not sites:
+ return
+
+ site = Site[0]
+ nova_fields = {}
+ if 'description' in fields:
+ nova_fields['description'] = fields['name']
+ if 'enabled' in fields:
+ nova_fields['enabled'] = fields['enabled']
+
+ site.updtae(**fields)
+ return site
+
+def delete_site(auth, filter={}):
+ driver = OpenStackDriver(client = auth_check(auth))
+ sites = Site.objects.filter(**filter)
+ for site in sites:
+ driver.delete_tenant({'id': site.tenant_id})
+ site.delete()
+ return 1
+
+def get_sites(auth, filter={}):
+ client = auth_check(auth)
+ sites = Site.objects.filter(**filter)
+ return sites
+
+
+
diff --git a/plstackapi/planetstack/views/sites.py b/plstackapi/planetstack/views/sites.py
new file mode 100644
index 0000000..37b39dd
--- /dev/null
+++ b/plstackapi/planetstack/views/sites.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.roles import add_site, delete_site, get_sites
+from plstackapi.planetstack.serializers import SiteSerializer
+from plstackapi.util.request import parse_request
+
+
+class SiteListCreate(APIView):
+ """
+ List all roles or create a new site.
+ """
+
+ 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 'site' in data:
+ site = add_site(data['auth'], data['site'])
+ serializer = SiteSerializer(data=site)
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+ else:
+ sites = get_sites(data['auth'])
+ serializer = SiteSerializer(sites, many=True)
+ return Response(serializer.data)
+
+
+class SiteRetrieveUpdateDestroy(APIView):
+ """
+ Retrieve, update or delete a site
+ """
+
+ def post(self, request, pk, format=None):
+ """Retrieve a site"""
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ sites = get_sites(data['auth'], {'tenant_id': pk})
+ if not sites:
+ return Response(status=status.HTTP_404_NOT_FOUND)
+ serializer = SiteSerializer(sites[0])
+ return Response(serializer.data)
+
+ def put(self, request, pk, format=None):
+ """update a role"""
+ data = parse_request(request.DATA)
+ if 'auth' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+ elif 'site' not in data:
+ return Response(status=status.HTTP_400_BAD_REQUEST)
+
+ site = update_site(pk, data['site'])
+ serializer = SiteSerializer(data=site)
+ 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_site(data['auth'], {'tenant_id': pk})
+ return Response(status=status.HTTP_204_NO_CONTENT)
+
+
+