~blake-rouse/maas/fix-1611342

« back to all changes in this revision

Viewing changes to src/maasserver/forms_interface.py

  • Committer: MAAS Lander
  • Author(s): Blake Rouse
  • Date: 2016-08-04 15:07:34 UTC
  • mfrom: (5183.7.5 admin-create-bridges)
  • Revision ID: maas_lander-20160804150734-i3ud7dbedltbeubl
[r=allenap][bug=][author=blake-rouse] Add the ability to create bridge interfaces with stp and forward delay on machines. Pass the bridge creation to curtin when the machine is being deployed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
471
471
class BridgeInterfaceForm(ChildInterfaceForm):
472
472
    """Form used to create/edit a bridge interface."""
473
473
 
 
474
    bridge_stp = forms.NullBooleanField(initial=False, required=False)
 
475
 
 
476
    bridge_fd = forms.IntegerField(min_value=0, initial=15, required=False)
 
477
 
474
478
    class Meta:
475
479
        model = BridgeInterface
476
480
        fields = InterfaceForm.Meta.fields + (
478
482
            'name',
479
483
        )
480
484
 
 
485
    def clean_parents(self):
 
486
        parents = self.get_clean_parents()
 
487
        if parents is None:
 
488
            return
 
489
        if len(parents) != 1:
 
490
            raise ValidationError(
 
491
                "A bridge interface must have exactly one parent.")
 
492
        if parents[0].type == INTERFACE_TYPE.BRIDGE:
 
493
            raise ValidationError(
 
494
                "A bridge interface can't have another bridge interface as "
 
495
                "parent.")
 
496
        instance_id = None if self.instance is None else self.instance.id
 
497
        bond_or_bridge = {INTERFACE_TYPE.BOND, INTERFACE_TYPE.BRIDGE}
 
498
        parent_has_bad_children = any(
 
499
            rel.child.type in bond_or_bridge and rel.child.id != instance_id
 
500
            for rel in parents[0].children_relationships.all())
 
501
        if parent_has_bad_children:
 
502
            raise ValidationError(
 
503
                "A bridge interface can't have a parent that is already "
 
504
                "in a bond or a bridge.")
 
505
        return parents
 
506
 
481
507
    def clean(self):
482
508
        cleaned_data = super().clean()
483
509
        if self.fields_ok(['vlan', 'parents']):
490
516
                self._set_default_vlan(parents)
491
517
        return cleaned_data
492
518
 
 
519
    def set_extra_parameters(self, interface, created):
 
520
        """Set the bridge parameters as well."""
 
521
        super().set_extra_parameters(interface, created)
 
522
        # Set all the bridge_* parameters.
 
523
        bridge_fields = [
 
524
            field_name
 
525
            for field_name in self.fields
 
526
            if field_name.startswith("bridge_")
 
527
        ]
 
528
        for bridge_field in bridge_fields:
 
529
            value = self.cleaned_data.get(bridge_field)
 
530
            if (value is not None and
 
531
                    isinstance(value, str) and
 
532
                    len(value) > 0 and not value.isspace()):
 
533
                interface.params[bridge_field] = value
 
534
            elif (value is not None and
 
535
                    not isinstance(value, str)):
 
536
                interface.params[bridge_field] = value
 
537
            elif created:
 
538
                interface.params[bridge_field] = (
 
539
                    self.fields[bridge_field].initial)
 
540
 
493
541
INTERFACE_FORM_MAPPING = {
494
542
    INTERFACE_TYPE.PHYSICAL: PhysicalInterfaceForm,
495
543
    INTERFACE_TYPE.VLAN: VLANInterfaceForm,