fix add/delete sliver using inline on slice page
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index b45e571..230dbfc 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -63,6 +63,7 @@
class OSModelAdmin(PlanetStackBaseAdmin):
"""Attach client connection to openstack on delete() and save()"""
+
def save_model(self, request, obj, form, change):
client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
obj.driver = OpenStackDriver(client=client)
@@ -164,6 +165,17 @@
list_display = ('name', 'site','slice_url', 'instantiation')
inlines = [SliverInline]
+ def get_formsets(self, request, obj=None):
+ for inline in self.get_inline_instances(request, obj):
+ # hide MyInline in the add view
+ if obj is None:
+ continue
+ # give inline object access to driver and caller
+ client = OpenStackClient(tenant=obj.name, **request.session.get('auth', {}))
+ inline.model.driver = OpenStackDriver(client=client)
+ inline.model.caller = request.user
+ yield inline.get_formset(request, obj)
+
def get_queryset(self, request):
qs = super(SliceAdmin, self).get_queryset(request)
if request.user.is_superuser:
diff --git a/plstackapi/core/api/auth.py b/plstackapi/core/api/auth.py
index 4054da6..412df00 100644
--- a/plstackapi/core/api/auth.py
+++ b/plstackapi/core/api/auth.py
@@ -1,8 +1,8 @@
from plstackapi.openstack.client import OpenStackClient
-def auth_check(auth):
- client = OpenStackClient(username=auth['Username'],
- password=auth['AuthString'],
- tenant=auth['LoginBase'])
+def auth_check(username, password, tenant):
+ client = OpenStackClient(username=username,
+ password=password,
+ tenant=tenant)
client.authenticate()
return client
diff --git a/plstackapi/core/api/slivers.py b/plstackapi/core/api/slivers.py
index da5d17f..cc45b8d 100644
--- a/plstackapi/core/api/slivers.py
+++ b/plstackapi/core/api/slivers.py
@@ -1,4 +1,5 @@
from types import StringTypes
+from django.contrib.auth import authenticate
from plstackapi.openstack.client import OpenStackClient
from plstackapi.openstack.driver import OpenStackDriver
from plstackapi.core.api.auth import auth_check
@@ -25,7 +26,7 @@
def add_sliver(auth, fields):
driver = OpenStackDriver(client = auth_check(auth))
-
+
images = _get_images(fields.get('image'))
if images: fields['image'] = images[0]
keys = _get_keys(fields.get('key'))
@@ -38,14 +39,7 @@
nodes = _get_nodes(fields.get('node'))
if nodes: fields['node'] = nodes[0]
sliver = Sliver(**fields)
- # create quantum sliver
- instance = driver.spawn_instance(name=sliver.name,
- key_name = sliver.key.name,
- image_id = sliver.image.image_id,
- hostname = sliver.node.name )
-
- sliver.instance_id=instance.id
-
+ sliver.driver = driver
sliver.save()
return sliver
@@ -56,12 +50,13 @@
driver = OpenStackDriver(client = auth_check(auth))
slivers = _get_slivers(filter)
for sliver in slivers:
- driver.destroy_instance(sliver.sliver_id)
+ sliver.driver = driver
sliver.delete()
return 1
def get_slivers(auth, filter={}):
- client = auth_check(auth)
+ user = authenticate(username=auth.get('username'),
+ password=auth.get('password'))
if 'slice' in filter:
slices = _get_slices(filter.get('slice'))
if slices: filter['slice'] = slices[0]
diff --git a/plstackapi/core/serializers.py b/plstackapi/core/serializers.py
index 381db05..b4ffe68 100644
--- a/plstackapi/core/serializers.py
+++ b/plstackapi/core/serializers.py
@@ -165,6 +165,7 @@
fields = ('id',
'instance_id',
'name',
+ 'instance_name',
'ip',
'image',
'key',
diff --git a/plstackapi/core/urls.py b/plstackapi/core/urls.py
index 0696c31..0c9b858 100644
--- a/plstackapi/core/urls.py
+++ b/plstackapi/core/urls.py
@@ -55,13 +55,13 @@
url(r'^plstackapi/slice_memberships/(?P<pk>[0-9]+)/$', SliceMembershipRetrieveUpdateDestroy.as_view(), name='slice_membership-detail'),
url(r'^plstackapi/subnets/$', SubnetListCreate.as_view(), name='subnet-list'),
- url(r'^plstackapi/subnets/(?P<pk>[0-9]+)/$', SubnetRetrieveUpdateDestroy.as_view(), name='subnet-detail'),
+ url(r'^plstackapi/subnets/(?P<pk>[a-zA-Z0-9_]+)/$', SubnetRetrieveUpdateDestroy.as_view(), name='subnet-detail'),
url(r'^plstackapi/slivers/$', SliverListCreate.as_view(), name='sliver-list'),
- url(r'^plstackapi/slivers/(?P<pk>[0-9]+)/$', SliverRetrieveUpdateDestroy.as_view(), name='sliver-detail'),
+ url(r'^plstackapi/slivers/(?P<pk>[a-zA-Z0-9_]+)/$', SliverRetrieveUpdateDestroy.as_view(), name='sliver-detail'),
url(r'^plstackapi/nodes/$', NodeListCreate.as_view(), name='node-list'),
- url(r'^plstackapi/nodes/(?P<pk>[0-9]+)/$', NodeRetrieveUpdateDestroy.as_view(), name='node-detail'),
+ url(r'^plstackapi/nodes/(?P<pk>[a-zA-Z0-9_]+)/$', NodeRetrieveUpdateDestroy.as_view(), name='node-detail'),
url(r'^plstackapi/deploymentnetworks/$', DeploymentNetworkListCreate.as_view(), name='deploymentnetwork-list'),
url(r'^plstackapi/deploymentnetworks/(?P<pk>[a-zA-Z0-9]+)/$', DeploymentNetworkRetrieveUpdateDestroy.as_view(), name='deploymentnetwork-detail'),
diff --git a/plstackapi/core/views/slivers.py b/plstackapi/core/views/slivers.py
index 4063419..2f5842c 100644
--- a/plstackapi/core/views/slivers.py
+++ b/plstackapi/core/views/slivers.py
@@ -37,7 +37,7 @@
data = parse_request(request.DATA)
if 'auth' not in data:
return Response(status=status.HTTP_400_BAD_REQUEST)
- slivers = get_slivers(data['auth'], {'id': pk})
+ slivers = get_slivers(data['auth'], pk)
if not slivers:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = SliverSerializer(slivers[0])
@@ -59,7 +59,7 @@
data = parse_request(request.DATA)
if 'auth' not in data:
return Response(status=status.HTTP_400_BAD_REQUEST)
- delete_sliver(data['auth'], {'id': pk})
+ delete_sliver(data['auth'], pk)
return Response(status=status.HTTP_204_NO_CONTENT)