{% trans "Status" %}:
- {% if order.status == 'Approved' %}
+ {% if vm.terminated_at %}
+ {% trans "Terminated" %}
+ {% elif order.status == 'Approved' %}
{% trans "Approved" %}
{% else %}
{% trans "Declined" %}
@@ -160,22 +162,19 @@
-
{% trans "Processing..." %}
-
-
+
{% trans "Hold tight, we are processing your request" %}
@@ -202,4 +201,4 @@
-{% endblock js_extra %}
\ No newline at end of file
+{% endblock js_extra %}
diff --git a/hosting/templates/hosting/virtual_machine_detail.html b/hosting/templates/hosting/virtual_machine_detail.html
index 06d86032..0b882055 100644
--- a/hosting/templates/hosting/virtual_machine_detail.html
+++ b/hosting/templates/hosting/virtual_machine_detail.html
@@ -109,7 +109,7 @@
{{virtual_machine.name}}
@@ -123,8 +123,9 @@
-
-
{% blocktrans with machine_name=virtual_machine.name %}Your Virtual Machine {{machine_name}} is successfully terminated!{% endblocktrans %}
+
+
+
{% blocktrans with machine_name=virtual_machine.name %}Your Virtual Machine {{machine_name}} is successfully terminated!{% endblocktrans %}
diff --git a/hosting/views.py b/hosting/views.py
index c89073e9..e694f55b 100644
--- a/hosting/views.py
+++ b/hosting/views.py
@@ -1,6 +1,7 @@
import json
import logging
import uuid
+from datetime import datetime
from time import sleep
from django import forms
@@ -47,10 +48,11 @@ from utils.views import (
from .forms import HostingUserSignupForm, HostingUserLoginForm, \
UserHostingKeyForm, generate_ssh_key_name
from .mixins import ProcessVMSelectionMixin
-from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
+from .models import (
+ HostingOrder, HostingBill, HostingPlan, UserHostingKey, VMDetail
+)
from datacenterlight.models import VMTemplate
-
logger = logging.getLogger(__name__)
CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a \
@@ -690,25 +692,29 @@ class OrdersHostingDetailView(LoginRequiredMixin,
if obj is not None:
# invoice for previous order
try:
- manager = OpenNebulaManager(
- email=owner.email, password=owner.password
- )
- vm = manager.get_vm(obj.vm_id)
- context['vm'] = VirtualMachineSerializer(vm).data
- except WrongIdError:
- messages.error(
- self.request,
- _('The VM you are looking for is unavailable at the '
- 'moment. Please contact Data Center Light support.')
- )
- self.kwargs['error'] = 'WrongIdError'
- context['error'] = 'WrongIdError'
- except ConnectionRefusedError:
- messages.error(
- self.request,
- _('In order to create a VM, you need to create/upload '
- 'your SSH KEY first.')
- )
+ vm_detail = VMDetail.objects.get(vm_id=obj.vm_id)
+ context['vm'] = vm_detail.__dict__
+ except VMDetail.DoesNotExist:
+ try:
+ manager = OpenNebulaManager(
+ email=owner.email, password=owner.password
+ )
+ vm = manager.get_vm(obj.vm_id)
+ context['vm'] = VirtualMachineSerializer(vm).data
+ except WrongIdError:
+ messages.error(
+ self.request,
+ _('The VM you are looking for is unavailable at the '
+ 'moment. Please contact Data Center Light support.')
+ )
+ self.kwargs['error'] = 'WrongIdError'
+ context['error'] = 'WrongIdError'
+ except ConnectionRefusedError:
+ messages.error(
+ self.request,
+ _('In order to create a VM, you need to create/upload '
+ 'your SSH KEY first.')
+ )
elif not card_details.get('response_object'):
# new order, failed to get card details
context['failed_payment'] = True
@@ -1055,6 +1061,10 @@ class VirtualMachineView(LoginRequiredMixin, View):
except WrongIdError:
response['status'] = True
response['text'] = ugettext('Terminated')
+ vm_detail_obj = VMDetail.objects.filter(
+ vm_id=opennebula_vm_id).first()
+ vm_detail_obj.terminated_at = datetime.utcnow()
+ vm_detail_obj.save()
break
except BaseException:
break
diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py
index dae8e412..3c193ad7 100644
--- a/utils/hosting_utils.py
+++ b/utils/hosting_utils.py
@@ -1,4 +1,10 @@
-from hosting.models import UserHostingKey
+import logging
+from oca.pool import WrongIdError
+
+from hosting.models import UserHostingKey, VMDetail
+from opennebula_api.serializers import VirtualMachineSerializer
+
+logger = logging.getLogger(__name__)
def get_all_public_keys(customer):
@@ -11,6 +17,38 @@ def get_all_public_keys(customer):
"public_key", flat=True)
+def get_or_create_vm_detail(user, manager, vm_id):
+ """
+ Returns VMDetail object related to given vm_id. Creates the object
+ if it does not exist
+
+ :param vm_id: The ID of the VM which should be greater than 0.
+ :param user: The CustomUser object that owns this VM
+ :param manager: The OpenNebulaManager object
+ :return: The VMDetail object. None if vm_id is less than or equal to 0.
+ Also, for the cases where the VMDetail does not exist and we can not
+ fetch data about the VM from OpenNebula, the function returns None
+ """
+ if vm_id <= 0:
+ return None
+ try:
+ vm_detail_obj = VMDetail.objects.get(vm_id=vm_id)
+ except VMDetail.DoesNotExist:
+ try:
+ vm_obj = manager.get_vm(vm_id)
+ except (WrongIdError, ConnectionRefusedError) as e:
+ logger.error(str(e))
+ return None
+ vm = VirtualMachineSerializer(vm_obj).data
+ vm_detail_obj = VMDetail.objects.create(
+ user=user, vm_id=vm_id, disk_size=vm['disk_size'],
+ cores=vm['cores'], memory=vm['memory'],
+ configuration=vm['configuration'], ipv4=vm['ipv4'],
+ ipv6=vm['ipv6']
+ )
+ return vm_detail_obj
+
+
def get_vm_price(cpu, memory, disk_size):
"""
A helper function that computes price of a VM from given cpu, ram and