def replace_disks(request, cluster_slug, instance): """ view used for initiating a Replace Disks job """ vm, cluster = get_vm_and_cluster_or_404(cluster_slug, instance) user = request.user if not (user.is_superuser or user.has_any_perms(cluster, ['admin', 'replace_disks'])): raise Http403(NO_PRIVS) if request.method == 'POST': form = ReplaceDisksForm(vm, request.POST) if form.is_valid(): try: job = form.save() job.refresh() content = json.dumps(job.info) # log information log_action('VM_REPLACE_DISKS', user, vm, job) except GanetiApiError, e: content = json.dumps({'__all__': [str(e)]}) else: # error in form return ajax response content = json.dumps(form.errors) return HttpResponse(content, mimetype='application/json')
def test_replace_new_secondary_without_node(self): data = dict(mode=REPLACE_DISK_CHG, disks='', node=None, iallocator=False) form = ReplaceDisksForm(self.vm, data) self.assertFalse(form.is_valid(), form.errors)
def replace_disks(request, cluster_slug, instance): """ view used for initiating a Replace Disks job """ vm, cluster = get_vm_and_cluster_or_404(cluster_slug, instance) user = request.user if not (user.is_superuser or user.has_any_perms(cluster, ['admin','replace_disks'])): raise Http403(NO_PRIVS) if request.method == 'POST': form = ReplaceDisksForm(vm, request.POST) if form.is_valid(): try: job = form.save() job.refresh() content = json.dumps(job.info) # log information log_action('VM_REPLACE_DISKS', user, vm, job) except GanetiApiError, e: content = json.dumps({'__all__':[str(e)]}) else: # error in form return ajax response content = json.dumps(form.errors) return HttpResponse(content, mimetype='application/json')
def test_replace_new_secondary_with_iallocator(self): data = dict(mode=REPLACE_DISK_CHG, disks='', node='', iallocator=True, iallocator_hostname='foo.bar.com') form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors)
def test_replace_new_secondary_with_node(self): data = dict(mode=REPLACE_DISK_CHG, disks='', node='gtest1.example.bak', iallocator='') form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors) form.save()
def test_replace_new_secondary_without_node(self): data = dict( mode=REPLACE_DISK_CHG, disks='', node=None, iallocator=False ) form = ReplaceDisksForm(self.vm, data) self.assertFalse(form.is_valid(), form.errors)
def test_clean_disks_multiple(self): data = dict(mode=REPLACE_DISK_AUTO, disks=[0, 0], node='', iallocator='') form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors) self.assertEqual('0,0', form.cleaned_data['disks']) form.save()
def test_replace_new_secondary_with_iallocator(self): data = dict( mode=REPLACE_DISK_CHG, disks='', node='', iallocator=True, iallocator_hostname='foo.bar.com' ) form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors)
def test_replace_new_secondary_with_node(self): data = dict( mode=REPLACE_DISK_CHG, disks='', node='gtest1.example.bak', iallocator='' ) form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors) form.save()
def test_clean_disks_single(self): data = dict( mode=REPLACE_DISK_AUTO, disks=[0], node='', iallocator='' ) form = ReplaceDisksForm(self.vm, data) self.assertTrue(form.is_valid(), form.errors) self.assertEqual('0', form.cleaned_data['disks']) form.save()
def test_bound_form(self): """ tests intial values and choices are intitialized correctly """ form = ReplaceDisksForm(self.vm) # disk choices self.assertEqual([(0, 'disk/0')], list(form.fields['disks'].choices)) # node choices self.assertEqual( set([(u'', u'---------'), (u'gtest1.example.bak', u'gtest1.example.bak'), (u'gtest2.example.bak', u'gtest2.example.bak'), (u'gtest3.example.bak', u'gtest3.example.bak')]), set(form.fields['node'].choices))
def test_auto(self): data = dict( mode=REPLACE_DISK_AUTO, disks='', node='', iallocator='' ) form = ReplaceDisksForm(self.vm, data) form.is_valid() self.assertTrue(form.is_valid(), form.errors) form.save()
def test_auto(self): data = dict(mode=REPLACE_DISK_AUTO, disks='', node='', iallocator='') form = ReplaceDisksForm(self.vm, data) form.is_valid() self.assertTrue(form.is_valid(), form.errors) form.save()
try: job = form.save() job.refresh() content = json.dumps(job.info) # log information log_action('VM_REPLACE_DISKS', user, vm, job) except GanetiApiError, e: content = json.dumps({'__all__':[str(e)]}) else: # error in form return ajax response content = json.dumps(form.errors) return HttpResponse(content, mimetype='application/json') else: form = ReplaceDisksForm(vm) return render_to_response('ganeti/virtual_machine/replace_disks.html', {'form':form, 'vm':vm, 'cluster':cluster}, context_instance=RequestContext(request)) @require_POST @login_required def reboot(request, cluster_slug, instance, rest=False): vm = get_object_or_404(VirtualMachine, hostname=instance, cluster__slug=cluster_slug) user = request.user if not (user.is_superuser or user.has_any_perms(vm, ['admin','power']) or user.has_perm('admin', vm.cluster)):