From b7e8eceb250b83c405585f5c0811662d47f4aecc Mon Sep 17 00:00:00 2001 From: Modulos Date: Thu, 11 May 2017 17:39:59 +0200 Subject: [PATCH] Add opennebula api urls and add delete vm api --- dynamicweb/urls.py | 2 ++ opennebula_api/models.py | 10 ++++++++-- opennebula_api/serializers.py | 11 ----------- opennebula_api/views.py | 9 +++++++++ 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/dynamicweb/urls.py b/dynamicweb/urls.py index 5e6b99ee..adbe0242 100644 --- a/dynamicweb/urls.py +++ b/dynamicweb/urls.py @@ -12,6 +12,8 @@ import debug_toolbar urlpatterns = [ url(r'^index.html$', LandingView.as_view()), url(r'^hosting/', include('hosting.urls', namespace="hosting")), + url(r'^open_api/', include('opennebula_api.urls', + namespace='opennebula_api')), url(r'^railshosting/', RailsHostingView.as_view(), name="rails.hosting"), url(r'^nodehosting/', NodeJSHostingView.as_view(), name="node.hosting"), url(r'^djangohosting/', DjangoHostingView.as_view(), name="django.hosting"), diff --git a/opennebula_api/models.py b/opennebula_api/models.py index babec433..87fd867a 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -213,6 +213,9 @@ class OpenNebulaManager(): vm_id = self.oneadmin_client.call( oca.VmTemplate.METHODS['instantiate'], template_id, + '', + False, + '' ) try: self.oneadmin_client.call( @@ -226,8 +229,11 @@ class OpenNebulaManager(): return vm_id def delete_vm(self, vm_id): - vm = self._get_vm(vm_id) - vm.delete() + self.oneadmin_client.call( + oca.VirtualMachine.METHODS['action'], + 'terminate', + vm_id + ) def _get_template_pool(self): try: diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 376bf471..df587821 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -98,14 +98,3 @@ class VirtualMachineSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): pass - def delete(self, instance, validated_data): - try: - owner = instance.owner - manager = OpenNebulaManager(email=owner.email, - password=owner.password[0:20], - create_user = True) - manager.delete_vm(template_id) - except OpenNebulaException as err: - raise serializers.ValidationError("OpenNebulaException occured. {0}".format(err)) - - diff --git a/opennebula_api/views.py b/opennebula_api/views.py index ee6f487a..563f6b8c 100644 --- a/opennebula_api/views.py +++ b/opennebula_api/views.py @@ -49,3 +49,12 @@ class VmDetailsView(generics.RetrieveUpdateDestroyAPIView): queryset = VirtualMachine.objects.all() serializer_class = VirtualMachineSerializer + + def perform_destroy(self, instance): + owner = instance.owner + manager = OpenNebulaManager(email=owner.email, + password=owner.password[0:20], + create_user = True) + manager.delete_vm(instance.opennebula_id) + instance.delete() +