edge cases handled
This commit is contained in:
parent
852c1228ba
commit
6136a48961
2 changed files with 62 additions and 31 deletions
|
@ -1,42 +1,72 @@
|
|||
$(document).ready(function() {
|
||||
function VMTerminateStatus($container, url) {
|
||||
$.get(url)
|
||||
.done(function(data) {
|
||||
setTimeout(function(){
|
||||
VMTerminateStatus($container, url);
|
||||
}, 2000);
|
||||
})
|
||||
.fail(function(data) {
|
||||
VMTerminateSuccess($container, data)
|
||||
window.location.reload(true);
|
||||
});
|
||||
}
|
||||
|
||||
$('#confirm-cancel').on('click', '.btn-ok', function(e) {
|
||||
var url = $('#virtual_machine_cancel_form').attr('action');
|
||||
var $container = $('#terminate-VM');
|
||||
var $btn = $container.find('.btn');
|
||||
var text = $container.find('.vm-item-lg').text();
|
||||
var altText = $container.attr('data-alt');
|
||||
function VMTerminateActive($container, altText) {
|
||||
$container.find('.alert-danger').addClass('hide');
|
||||
$container.addClass('processing')
|
||||
.find('.vm-item-lg').attr('class', '')
|
||||
.addClass('vm-item-lg vm-color-failed')
|
||||
.text(altText);
|
||||
$btn.prop('disabled', true);
|
||||
$container.find('.btn').prop('disabled', true);
|
||||
$('#confirm-cancel').modal('hide');
|
||||
$.post(url)
|
||||
.done(function(data) {
|
||||
console.log("success", data);
|
||||
if (data.status == true) {
|
||||
}
|
||||
|
||||
function VMTerminateSuccess($container, data) {
|
||||
$container.addClass('terminate-success')
|
||||
.find('.vm-item-lg').text(data.text);
|
||||
$btn.remove();
|
||||
window.location = data.redirect;
|
||||
} else {
|
||||
$container.find('.btn').remove();
|
||||
}
|
||||
|
||||
function VMTerminateFail($container, data, text) {
|
||||
$container.addClass('terminate-fail')
|
||||
.find('.vm-item-lg').text(text);
|
||||
$container.find('.btn').prop('disabled', false);
|
||||
$container.find('.alert-danger').text(data.text).removeClass('hide');
|
||||
$container.removeClass('processing');
|
||||
}
|
||||
|
||||
|
||||
$(document).ready(function() {
|
||||
|
||||
$('#confirm-cancel').on('click', '.btn-ok', function(e) {
|
||||
var url = $('#virtual_machine_cancel_form').attr('action');
|
||||
var $container = $('#terminate-VM');
|
||||
var text = $container.find('.vm-item-lg').text();
|
||||
var altText = $container.attr('data-alt');
|
||||
VMTerminateActive($container, altText);
|
||||
|
||||
$.post(url)
|
||||
.done(function(data) {
|
||||
console.log("success", data);
|
||||
if (data.status == true) {
|
||||
VMTerminateSuccess($container, data);
|
||||
window.location = data.redirect;
|
||||
} else {
|
||||
if ('text' in data) {
|
||||
VMTerminateFail($container, data, text);
|
||||
} else {
|
||||
VMTerminateStatus($container, url);
|
||||
}
|
||||
}
|
||||
})
|
||||
.fail(function(data) {
|
||||
$container.addClass('terminate-fail')
|
||||
.find('.vm-item-lg').text(text);
|
||||
$container.find('.btn').prop('disabled', false);
|
||||
$container.find('.alert-danger').removeClass('hide');
|
||||
console.log(data)
|
||||
if (data.status==504) {
|
||||
VMTerminateStatus($container, url);
|
||||
} else {
|
||||
VMTerminateFail($container, data, text);
|
||||
}
|
||||
})
|
||||
.always(function(data) {
|
||||
$container.removeClass('processing');
|
||||
});
|
||||
});
|
||||
|
||||
var hash = window.location.hash;
|
||||
|
|
|
@ -923,6 +923,9 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
|||
def get(self, request, *args, **kwargs):
|
||||
vm = self.get_object()
|
||||
if vm is None:
|
||||
if self.request.is_ajax():
|
||||
raise Http404()
|
||||
else:
|
||||
return redirect(reverse('hosting:virtual_machines'))
|
||||
try:
|
||||
serializer = VirtualMachineSerializer(vm)
|
||||
|
@ -938,7 +941,7 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
|||
return render(request, self.template_name, context)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
response = {}
|
||||
response = {'status': False}
|
||||
owner = self.request.user
|
||||
vm = self.get_object()
|
||||
|
||||
|
@ -948,23 +951,25 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
|||
email=owner.email,
|
||||
password=owner.password
|
||||
)
|
||||
|
||||
vm_data = VirtualMachineSerializer(manager.get_vm(vm.id)).data
|
||||
|
||||
terminated = manager.delete_vm(vm.id)
|
||||
|
||||
if not terminated:
|
||||
response['status'] = False
|
||||
response['text'] = ugettext(
|
||||
'Error terminating VM') + opennebula_vm_id
|
||||
else:
|
||||
for t in range(50):
|
||||
for t in range(15):
|
||||
try:
|
||||
manager.get_vm(self.kwargs.get('pk'))
|
||||
except BaseException:
|
||||
response['status'] = True
|
||||
response['redirect'] = self.get_success_url()
|
||||
response['text'] = ugettext('Terminated')
|
||||
break
|
||||
else:
|
||||
sleep(2)
|
||||
|
||||
context = {
|
||||
'vm': vm_data,
|
||||
'base_url': "{0}://{1}".format(self.request.scheme,
|
||||
|
@ -981,10 +986,6 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
|||
}
|
||||
email = BaseEmail(**email_data)
|
||||
email.send()
|
||||
response['status'] = True
|
||||
response['redirect'] = self.get_success_url()
|
||||
response['text'] = ugettext('Terminated')
|
||||
|
||||
return HttpResponse(
|
||||
json.dumps(response),
|
||||
content_type="application/json"
|
||||
|
|
Loading…
Reference in a new issue