コード例 #1
0
 def test_allows_selecting_by_system_id(self):
     node = factory.make_Node()
     for _ in range(3):
         factory.make_Node()
     node_field = NodeChoiceField(Node.objects.filter())
     self.assertEqual(node, node_field.clean(node.system_id))
コード例 #2
0
ファイル: clone.py プロジェクト: casual-lemon/maas
class CloneForm(forms.Form):
    """Clone storage/interface form."""

    source = NodeChoiceField(
        label="Source",
        queryset=Machine.objects.all(),
        required=True,
        initial=None,
        help_text="The source machine to clone from.",
    )

    destinations = SimpleArrayField(
        NodeChoiceField(queryset=Machine.objects.all()),
        label="Destinations",
        min_length=1,
        error_messages={
            "item_invalid": "Machine %(nth)s is invalid:",
            "is-source":
            "Source machine %(machine)s cannot be a destination machine.",
            "storage": "%(machine)s is invalid:",
            "networking": "%(machine)s is invalid:",
        },
        help_text="The destinations to clone to.",
    )

    storage = forms.BooleanField(
        label="Storage",
        required=False,
        help_text="Clone the storage configuration.",
    )

    interfaces = forms.BooleanField(
        label="Interfaces",
        required=False,
        help_text="Clone the interfaces configuration.",
    )

    def __init__(self, user, **kwargs):
        self.user = user
        super().__init__(**kwargs)
        self.fields["source"].queryset = Machine.objects.get_nodes(
            self.user, NodePermission.view)
        self.fields[
            "destinations"].base_field.queryset = Machine.objects.get_nodes(
                self.user,
                NodePermission.admin,
                from_nodes=Machine.objects.filter(
                    status__in={NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING
                                }),
            )

    def clean(self):
        """Validate that the form is valid and that the destinations can accept
        the storage and/or interfaces configuration from the source."""
        cleaned_data = super().clean()
        source = self.cleaned_data.get("source")
        if not source:
            # Django should be placing this automatically, but it does not
            # occur. So we force the setting of this error here.
            self.add_error("source", "This field is required.")
        destinations = self.cleaned_data.get("destinations")
        destination_field = self.fields["destinations"]
        storage = self.cleaned_data.get("storage", False)
        interfaces = self.cleaned_data.get("interfaces", False)
        if source and destinations:
            for dest in destinations:
                if source == dest:
                    error = ValidationError(
                        destination_field.error_messages["is-source"],
                        code="is-source",
                        params={
                            "machine": str(dest),
                            "system_id": dest.system_id,
                        },
                    )
                    self.add_error("destinations", error)
                else:
                    if storage:
                        try:
                            dest._get_storage_mapping_between_nodes(source)
                        except ValidationError as exc:
                            error = prefix_validation_error(
                                exc,
                                prefix=destination_field.
                                error_messages["storage"],
                                code="storage",
                                params={
                                    "machine": str(dest),
                                    "system_id": dest.system_id,
                                },
                            )
                            self.add_error("destinations", error)
                    if interfaces:
                        try:
                            dest._get_interface_mapping_between_nodes(source)
                        except ValidationError as exc:
                            error = prefix_validation_error(
                                exc,
                                prefix=destination_field.
                                error_messages["networking"],
                                code="networking",
                                params={
                                    "machine": str(dest),
                                    "system_id": dest.system_id,
                                },
                            )
                            self.add_error("destinations", error)
        if not storage and not interfaces:
            self.add_error(
                "__all__",
                ValidationError(
                    "Either storage or interfaces must be true.",
                    code="required",
                ),
            )
        return cleaned_data

    def strip_failed_destinations(self):
        """Remove destinations that have errors."""
        if "destinations" not in self.cleaned_data:
            submitted_destinations = self.data.get("destinations")
            # Don't try and manipulate empty submission
            if not submitted_destinations:
                return
            errors = self.errors.as_data()
            bogus_system_ids = {
                error.params["system_id"]
                for error in errors["destinations"]
            }
            return [
                dest for dest in submitted_destinations
                if dest not in bogus_system_ids
            ]

    def save(self):
        """Clone the storage and/or interfaces configuration to the
        destinations."""
        source = self.cleaned_data.get("source")
        destinations = self.cleaned_data.get("destinations", [])
        storage = self.cleaned_data.get("storage", False)
        interfaces = self.cleaned_data.get("interfaces", False)
        for dest in destinations:
            if storage:
                dest.set_storage_configuration_from_node(source)
            if interfaces:
                dest.set_networking_configuration_from_node(source)
コード例 #3
0
ファイル: clone.py プロジェクト: th3architect/maas
class CloneForm(forms.Form):
    """Clone storage/interface form."""

    source = NodeChoiceField(
        label="Source",
        queryset=Machine.objects.all(),
        required=True,
        initial=None,
        help_text="The source machine to clone from.",
    )

    destinations = SimpleArrayField(
        NodeChoiceField(queryset=Machine.objects.all()),
        label="Destinations",
        min_length=1,
        error_messages={
            "item_invalid": "Machine %(nth)s in the array did not validate:"
        },
        help_text="The destinations to clone to.",
    )

    storage = forms.BooleanField(
        label="Storage",
        required=False,
        help_text="Clone the storage configuration.",
    )

    interfaces = forms.BooleanField(
        label="Interfaces",
        required=False,
        help_text="Clone the interfaces configuration.",
    )

    def __init__(self, user, **kwargs):
        self.user = user
        super().__init__(**kwargs)
        self.fields["source"].queryset = Machine.objects.get_nodes(
            self.user, NodePermission.view)
        self.fields[
            "destinations"].base_field.queryset = Machine.objects.get_nodes(
                self.user,
                NodePermission.admin,
                from_nodes=Machine.objects.filter(
                    status__in={NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING
                                }),
            )

    def clean(self):
        """Validate that the form is valid and that the destinations can accept
        the storage and/or interfaces configuration from the source."""
        cleaned_data = super().clean()
        source = self.cleaned_data.get("source")
        if not source:
            # Django should be placing this automatically, but it does not
            # occur. So we force the setting of this error here.
            set_form_error(self, "source", "This field is required.")
        destinations = self.cleaned_data.get("destinations")
        destination_field = self.fields["destinations"]
        item_invalid = destination_field.error_messages["item_invalid"]
        storage = self.cleaned_data.get("storage", False)
        interfaces = self.cleaned_data.get("interfaces", False)
        if source and destinations:
            for index, dest in enumerate(destinations, 1):
                if source == dest:
                    error = prefix_validation_error(
                        ValidationError(
                            "Source machine cannot be a destination machine."),
                        prefix=item_invalid,
                        code="item_invalid",
                        params={"nth": index},
                    )
                    set_form_error(self, "destinations", error)
                else:
                    if storage:
                        try:
                            dest._get_storage_mapping_between_nodes(source)
                        except ValidationError as exc:
                            error = prefix_validation_error(
                                exc,
                                prefix=item_invalid,
                                code="item_invalid",
                                params={"nth": index},
                            )
                            set_form_error(self, "destinations", error)
                    if interfaces:
                        try:
                            dest._get_interface_mapping_between_nodes(source)
                        except ValidationError as exc:
                            error = prefix_validation_error(
                                exc,
                                prefix=item_invalid,
                                code="item_invalid",
                                params={"nth": index},
                            )
                            set_form_error(self, "destinations", error)
        if not storage and not interfaces:
            set_form_error(self, "__all__",
                           "Either storage or interfaces must be true.")
        return cleaned_data

    def save(self):
        """Clone the storage and/or interfaces configuration to the
        destinations."""
        source = self.cleaned_data.get("source")
        destinations = self.cleaned_data.get("destinations")
        storage = self.cleaned_data.get("storage", False)
        interfaces = self.cleaned_data.get("interfaces", False)
        for dest in destinations:
            if storage:
                dest.set_storage_configuration_from_node(source)
            if interfaces:
                dest.set_networking_configuration_from_node(source)
コード例 #4
0
class DHCPSnippetForm(MAASModelForm):
    """DHCP snippet creation/edition form."""

    name = forms.CharField(label="Name",
                           required=False,
                           help_text=("The name of the DHCP snippet."))

    value = VersionedTextFileField(label="DHCP Snippet",
                                   required=False,
                                   help_text="The DHCP Snippet")

    description = forms.CharField(
        label="Description",
        required=False,
        help_text=("The description of what the DHCP snippet does."))

    enabled = forms.BooleanField(
        label="Enabled",
        required=False,
        help_text=("Whether or not the DHCP snippet is enabled."))

    node = NodeChoiceField(
        label="Node",
        queryset=Node.objects.all(),
        required=False,
        initial=None,
        help_text=("The node which the DHCP snippet is for."))

    subnet = SpecifierOrModelChoiceField(
        label="Subnet",
        queryset=Subnet.objects.all(),
        required=False,
        help_text="The subnet which the DHCP snippet is for.")

    global_snippet = forms.BooleanField(
        label="Global DHCP Snippet",
        required=False,
        help_text=(
            "Set the DHCP snippet to be global, removes links to nodes or "
            "subnets"))

    class Meta:
        model = DHCPSnippet
        fields = (
            'name',
            'value',
            'description',
            'enabled',
            'node',
            'subnet',
            'global_snippet',
        )

    def __init__(self, instance=None, request=None, **kwargs):
        super().__init__(instance=instance, **kwargs)
        if instance is None:
            for field in ['name', 'value']:
                self.fields[field].required = True
            self.initial['enabled'] = True
        else:
            self.fields['value'].initial = self.instance.value
        if instance is not None and instance.node is not None:
            self.initial['node'] = self.instance.node.system_id

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get('global_snippet', False):
            cleaned_data['node'] = None
            self.instance.node = None
            cleaned_data['subnet'] = None
            self.instance.subnet = None
        elif (self.instance.subnet == cleaned_data.get('subnet')
              and cleaned_data.get('node') is not None):
            cleaned_data['subnet'] = None
            self.instance.subnet = None
        elif (self.instance.node == cleaned_data.get('node')
              and cleaned_data.get('subnet') is not None):
            cleaned_data['node'] = None
            self.instance.node = None
        return cleaned_data

    def is_valid(self):
        valid = super().is_valid()
        if valid:
            # Often the first error can cause cascading errors. Showing all of
            # these errors can be confusing so only show the first if there is
            # one.
            first_error = None
            for error in validate_dhcp_config(self.instance):
                valid = False
                if first_error is None:
                    first_error = error
                else:
                    if error['line_num'] < first_error['line_num']:
                        first_error = error
            if first_error is not None:
                set_form_error(self, 'value', first_error['error'])

        # If the DHCPSnippet isn't valid cleanup the value
        if not valid and self.initial.get('value') != self.instance.value_id:
            self.instance.value.delete()
        return valid
コード例 #5
0
class DHCPSnippetForm(MAASModelForm):
    """DHCP snippet creation/edition form."""

    name = forms.CharField(label="Name",
                           required=False,
                           help_text="The name of the DHCP snippet.")

    value = VersionedTextFileField(label="DHCP Snippet",
                                   required=False,
                                   help_text="The DHCP Snippet")

    description = forms.CharField(
        label="Description",
        required=False,
        help_text="The description of what the DHCP snippet does.",
    )

    enabled = forms.BooleanField(
        label="Enabled",
        required=False,
        help_text="Whether or not the DHCP snippet is enabled.",
    )

    node = NodeChoiceField(
        label="Node",
        queryset=Node.objects.all(),
        required=False,
        initial=None,
        help_text="The node which the DHCP snippet is for.",
    )

    subnet = SpecifierOrModelChoiceField(
        label="Subnet",
        queryset=Subnet.objects.all(),
        required=False,
        help_text="The subnet which the DHCP snippet is for.",
    )

    global_snippet = forms.BooleanField(
        label="Global DHCP Snippet",
        required=False,
        help_text=(
            "Set the DHCP snippet to be global, removes links to nodes or "
            "subnets"),
    )

    class Meta:
        model = DHCPSnippet
        fields = (
            "name",
            "value",
            "description",
            "enabled",
            "node",
            "subnet",
            "global_snippet",
        )

    def __init__(self, instance=None, request=None, **kwargs):
        super().__init__(instance=instance, **kwargs)
        if instance is None:
            for field in ["name", "value"]:
                self.fields[field].required = True
            self.initial["enabled"] = True
        else:
            self.fields["value"].initial = self.instance.value
        if instance is not None and instance.node is not None:
            self.initial["node"] = self.instance.node.system_id

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get("global_snippet", False):
            cleaned_data["node"] = None
            self.instance.node = None
            cleaned_data["subnet"] = None
            self.instance.subnet = None
        elif (self.instance.subnet == cleaned_data.get("subnet")
              and cleaned_data.get("node") is not None):
            cleaned_data["subnet"] = None
            self.instance.subnet = None
        elif (self.instance.node == cleaned_data.get("node")
              and cleaned_data.get("subnet") is not None):
            cleaned_data["node"] = None
            self.instance.node = None
        return cleaned_data

    def is_valid(self):
        valid = super().is_valid()
        if valid:
            # Often the first error can cause cascading errors. Showing all of
            # these errors can be confusing so only show the first if there is
            # one.
            first_error = None
            for error in validate_dhcp_config(self.instance):
                valid = False
                if first_error is None:
                    first_error = error
                else:
                    if error["line_num"] < first_error["line_num"]:
                        first_error = error
            if first_error is not None:
                set_form_error(self, "value", first_error["error"])

        # If the DHCPSnippet isn't valid cleanup the value
        if not valid and self.initial.get("value") != self.instance.value_id:
            self.instance.value.delete()
        return valid

    def save(self, endpoint, request):
        dhcp_snippet = super(DHCPSnippetForm, self).save()
        create_audit_event(
            EVENT_TYPES.SETTINGS,
            endpoint,
            request,
            None,
            description=("%s DHCP snippet '%s'." % (
                "Updated" if self.is_update else "Created",
                dhcp_snippet.name,
            )),
        )
        return dhcp_snippet
コード例 #6
0
ファイル: test_fields.py プロジェクト: zeronewb/maas
 def test_raises_exception_when_not_found(self):
     for _ in range(3):
         factory.make_Node()
     node_field = NodeChoiceField(Node.objects.filter())
     self.assertRaises(
         ValidationError, node_field.clean, factory.make_name('query'))