Esempio n. 1
0
 def create_physical(self, params):
     """Create physical interface."""
     node = self._get_node_or_permission_error(params)
     form = PhysicalInterfaceForm(node=node, data=params)
     if form.is_valid():
         interface = form.save()
         self._update_obj_tags(interface, params)
         self._create_link_on_interface(interface, params)
     else:
         raise ValidationError(form.errors)
Esempio n. 2
0
 def create_interface(self, params):
     """Create an interface on a device."""
     device = self.get_object(params, permission=self._meta.edit_permission)
     form = PhysicalInterfaceForm(node=device, data=params)
     if form.is_valid():
         interface = form.save()
         self._configure_interface(interface, params)
         return self.full_dehydrate(reload_object(device))
     else:
         raise HandlerValidationError(form.errors)
Esempio n. 3
0
    def create_physical(self, params):
        """Create physical interface."""
        # Only admin users can perform create.
        if not reload_object(self.user).is_superuser:
            raise HandlerPermissionError()

        node = self.get_object(params)
        form = PhysicalInterfaceForm(node=node, data=params)
        if form.is_valid():
            interface = form.save()
            self._update_obj_tags(interface, params)
            self._create_link_on_interface(interface, params)
        else:
            raise ValidationError(form.errors)
Esempio n. 4
0
    def create_physical(self, request, system_id):
        """Create a physical interface on a machine and device.

        :param name: Name of the interface.
        :param mac_address: MAC address of the interface.
        :param tags: Tags for the interface.
        :param vlan: Untagged VLAN the interface is connected to.  If not
            provided then the interface is considered disconnected.

        Following are extra parameters that can be set on the interface:

        :param mtu: Maximum transmission unit.
        :param accept_ra: Accept router advertisements. (IPv6 only)
        :param autoconf: Perform stateless autoconfiguration. (IPv6 only)

        Returns 404 if the node is not found.
        """
        node = Node.objects.get_node_or_404(
            system_id, request.user, NodePermission.edit)
        raise_error_if_controller(node, "create")
        # Machine type nodes require the node needs to be in the correct state
        # and that the user has admin permissions.
        if node.node_type == NODE_TYPE.MACHINE:
            if not request.user.has_perm(NodePermission.admin, node):
                raise MAASAPIForbidden()
            raise_error_for_invalid_state_on_allocated_operations(
                node, request.user, "create")
        form = PhysicalInterfaceForm(node=node, data=request.data)
        if form.is_valid():
            return form.save()
        else:
            # The Interface model validation is so strict that it will cause
            # the mac_address field to include two messages about it being
            # required. We clean up this response to not provide duplicate
            # information.
            if "mac_address" in form.errors:
                if (MISSING_FIELD in form.errors["mac_address"] and
                        BLANK_FIELD in form.errors["mac_address"]):
                    form.errors["mac_address"] = ErrorList([
                        error
                        for error in form.errors["mac_address"]
                        if error != BLANK_FIELD
                    ])
            raise MAASAPIValidationError(form.errors)
Esempio n. 5
0
    def create_physical(self, request, system_id):
        """@description-title Create a physical interface
        @description Create a physical interface on a machine and device.

        @param (string) "{system_id}" [required=true] A system_id.

        @param (string) "name" [required=false] Name of the interface.

        @param (string) "mac_address" [required=true] MAC address of the
        interface.

        @param (string) "tags" [required=false] Tags for the interface.

        @param (string) "vlan" [required=false] Untagged VLAN the interface is
        connected to. If not provided then the interface is considered
        disconnected.

        @param (int) "mtu" [required=false] Maximum transmission unit.

        @param (boolean) "accept_ra" [required=false] Accept router
        advertisements. (IPv6 only)

        @param (boolean) "autoconf" [required=false] Perform stateless
        autoconfiguration. (IPv6 only)

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the new
        interface object.
        @success-example "success-json" [exkey=interfaces-create-physical]
        placeholder text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested machine is not found.
        @error-example "not-found"
            Not Found
        """
        node = Node.objects.get_node_or_404(
            system_id, request.user, NodePermission.edit)
        raise_error_if_controller(node, "create")
        # Machine type nodes require the node needs to be in the correct state
        # and that the user has admin permissions.
        if node.node_type == NODE_TYPE.MACHINE:
            if not request.user.has_perm(NodePermission.admin, node):
                raise MAASAPIForbidden()
            raise_error_for_invalid_state_on_allocated_operations(
                node, request.user, "create")
        form = PhysicalInterfaceForm(node=node, data=request.data)
        if form.is_valid():
            return form.save()
        else:
            # The Interface model validation is so strict that it will cause
            # the mac_address field to include two messages about it being
            # required. We clean up this response to not provide duplicate
            # information.
            if "mac_address" in form.errors:
                if (MISSING_FIELD in form.errors["mac_address"] and
                        BLANK_FIELD in form.errors["mac_address"]):
                    form.errors["mac_address"] = ErrorList([
                        error
                        for error in form.errors["mac_address"]
                        if error != BLANK_FIELD
                    ])
            raise MAASAPIValidationError(form.errors)