Commit ef0ed6be by Szabolcs Gelencser

Enable 'Add network interface' operation

parent cb15e6e4
......@@ -8,15 +8,6 @@
<i class="fa fa-{{op.icon}}"></i> {% trans "add interface" %}</a>
{% endif %}{% endwith %}
</div>
<br />
<br />
<div id="vm-details-add-user-interface">
{% with op=op.add_user_interface %}{% if op %}
<a href="{{op.get_url}}" class="btn btn-{{op.effect}} operation pull-right"
{% if op.disabled %}disabled{% endif %}>
<i class="fa fa-{{op.icon}}"></i> {% trans "add user interface" %}</a>
{% endif %}{% endwith %}
</div>
<h2>
{% trans "Interfaces" %}
</h2>
......
......@@ -60,7 +60,8 @@ from .util import (
FormOperationMixin, FilterMixin, GraphMixin
)
from ..forms import (
AclUserOrGroupAddForm, VmResourcesForm, VmCustomizeForm, VmDeployForm, VmFromPlainImageForm)
AclUserOrGroupAddForm, VmResourcesForm, VmCustomizeForm, VmDeployForm, VmFromPlainImageForm, VmRemoveInterfaceForm,
VmAddInterfaceForm)
logger = logging.getLogger(__name__)
......@@ -308,52 +309,51 @@ def get_operations(instance, user, request):
else:
ops.append(v.bind_to_object(instance))
return ops
#
#
# class VmRemoveInterfaceView(FormOperationMixin, VmOperationView):
# op = 'remove_interface'
# form_class = VmRemoveInterfaceForm
# show_in_toolbar = False
# wait_for_result = 0.5
# icon = 'times'
# effect = "danger"
# with_reload = True
#
# def get_form_kwargs(self):
# instance = self.get_op().instance
# choices = instance.interface_set.all()
# interface_pk = self.request.GET.get('interface')
# if interface_pk:
# try:
# default = choices.get(pk=interface_pk)
# except (ValueError, Interface.DoesNotExist):
# raise Http404()
# else:
# default = None
#
# val = super(VmRemoveInterfaceView, self).get_form_kwargs()
# val.update({'choices': choices, 'default': default})
# return val
#
#
# class VmAddInterfaceView(FormOperationMixin, VmOperationView):
#
# op = 'add_interface'
# form_class = VmAddInterfaceForm
# show_in_toolbar = False
# icon = 'globe'
# effect = 'success'
# with_reload = True
#
# def get_form_kwargs(self):
# inst = self.get_op().instance
# choices = Vlan.get_objects_with_level(
# "user", self.request.user).exclude(
# vm_interface__instance__in=[inst])
# val = super(VmAddInterfaceView, self).get_form_kwargs()
# val.update({'choices': choices})
# return val
#
class VmRemoveInterfaceView(FormOperationMixin, VmOperationView):
op = 'remove_interface'
form_class = VmRemoveInterfaceForm
show_in_toolbar = False
wait_for_result = 0.5
icon = 'times'
effect = "danger"
with_reload = True
def get_form_kwargs(self):
instance = self.get_op().instance
choices = instance.interface_set.all()
interface_pk = self.request.GET.get('interface')
if interface_pk:
try:
default = choices.get(pk=interface_pk)
except (ValueError, Interface.DoesNotExist):
raise Http404()
else:
default = None
val = super(VmRemoveInterfaceView, self).get_form_kwargs()
val.update({'choices': choices, 'default': default})
return val
class VmAddInterfaceView(FormOperationMixin, VmOperationView):
op = 'add_interface'
form_class = VmAddInterfaceForm
show_in_toolbar = False
icon = 'globe'
effect = 'success'
with_reload = True
def get_form_kwargs(self):
inst = self.get_op().instance
choices = Vlan.get_objects_with_level(
"user", self.request.user).exclude(
vm_interface__instance__in=[inst])
val = super(VmAddInterfaceView, self).get_form_kwargs()
val.update({'choices': choices})
return val
#
# class VmDiskModifyView(FormOperationMixin, VmOperationView):
# show_in_toolbar = False
......@@ -761,8 +761,8 @@ vm_ops = OrderedDict([
# ('remove_disk', VmDiskModifyView.factory(
# op='remove_disk', form_class=VmDiskRemoveForm,
# icon='times', effect="danger")),
# ('add_interface', VmAddInterfaceView),
# ('remove_interface', VmRemoveInterfaceView),
('add_interface', VmAddInterfaceView),
('remove_interface', VmRemoveInterfaceView),
# ('remove_port', VmPortRemoveView),
# ('add_port', VmPortAddView),
# ('renew', VmRenewView),
......
......@@ -185,46 +185,12 @@ class RemoteAgentOperation(EnsureAgentMixin, RemoteInstanceOperation):
class AddInterfaceOperation(InstanceOperation):
id = 'add_interface'
name = _("add interface")
description = _("Add a new network interface for the specified VLAN to "
"the VM.")
required_perms = ()
accept_states = ('STOPPED', 'PENDING', 'ACTIVE')
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
network_type = None
def rollback(self, net, activity):
with activity.sub_activity(
'destroying_net',
readable_name=ugettext_noop("destroy network (rollback)")):
net.destroy()
net.delete()
description = _("Add a new network interface for the specified network to the VM.")
accept_states = ('SHUTOFF', 'ACTIVE') #TODO: try other states
os_policy_actions = (("compute", "compute:attach_interface"),)
def _operation(self, activity, user, system, vlan, managed=None):
if not vlan.has_level(user, 'user'):
raise humanize_exception(ugettext_noop(
"User acces to vlan %(vlan)s is required."),
PermissionDenied(), vlan=vlan)
if managed is None:
managed = vlan.managed
net = Interface.create(base_activity=activity, instance=self.instance,
managed=managed, owner=user, vlan=vlan)
if self.instance.is_running:
try:
self.instance._attach_network(
interface=net, parent_activity=activity)
except Exception as e:
if hasattr(e, 'libvirtError'):
self.rollback(net, activity)
raise
net.deploy()
self.instance._change_ip(parent_activity=activity)
self.instance._restart_networking(parent_activity=activity)
def get_activity_name(self, kwargs):
return create_readable(ugettext_noop("add %(vlan)s interface"),
vlan=kwargs['vlan'])
print("Adding interface")
@register_operation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment