示例#1
0
    def set_default_gateway(self, request, system_id, id):
        """Set the node to use this interface as the default gateway.

        If this interface has more than one subnet with a gateway IP in the
        same IP address family then specifying the ID of the link on
        this interface is required.

        :param link_id: ID of the link on this interface to select the
            default gateway IP address from.

        Returns 400 if the interface has not AUTO or STATIC links.
        Returns 404 if the node or interface is not found.
        """
        interface = Interface.objects.get_interface_or_404(
            system_id, id, request.user, NODE_PERMISSION.EDIT)
        node = interface.get_node()
        raise_error_if_controller(node, "link subnet")
        if node.node_type == NODE_TYPE.MACHINE:
            # This node needs to be in the correct state to modify
            # the interface.
            raise_error_for_invalid_state_on_allocated_operations(
                node, request.user, "set default gateway")
        form = InterfaceSetDefaultGatwayForm(instance=interface,
                                             data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
示例#2
0
 def test__interface_needs_gateways(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     form = InterfaceSetDefaultGatwayForm(instance=interface, data={})
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual(
         {"__all__": ["This interface has no usable gateways."]},
         form.errors,
     )
示例#3
0
 def test_sets_gateway_link_v6_on_node_when_link_id(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     ipv6_link = self.make_ip_family_link(interface,
                                          factory.make_ipv6_network())
     self.make_ip_family_link(interface, factory.make_ipv6_network())
     form = InterfaceSetDefaultGatwayForm(instance=interface,
                                          data={"link_id": ipv6_link.id})
     self.assertTrue(form.is_valid(), form.errors)
     interface = form.save()
     node = interface.get_node()
     self.assertEqual(ipv6_link, node.gateway_link_ipv6)
示例#4
0
    def set_default_gateway(self, request, system_id, id):
        """@description-title Set the default gateway on a machine
        @description Set the given interface id on the given system_id as the
        default gateway.

        If this interface has more than one subnet with a gateway IP in the
        same IP address family then specifying the ID of the link on
        this interface is required.

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

        @param (int) "{id}" [required=true] An interface id.

        @param (int) "link_id" [required=false] ID of the link on this
        interface to select the default gateway IP address from.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the updated
        interface object.
        @success-example "success-json" [exkey=interfaces-set-def-gateway]
        placeholder text

        @error (http-status-code) "400" 400
        @error (content) "400" If the interface has no ``AUTO`` or ``STATIC``
        links.

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested machine or interface is not
        found.
        @error-example "not-found"
            Not Found
        """
        interface = Interface.objects.get_interface_or_404(
            system_id,
            id,
            request.user,
            NodePermission.admin,
            NodePermission.edit,
        )
        node = interface.get_node()
        raise_error_if_controller(node, "link subnet")
        if node.node_type == NODE_TYPE.MACHINE:
            # This node needs to be in the correct state to modify
            # the interface.
            raise_error_for_invalid_state_on_allocated_operations(
                node, request.user, "set default gateway")
        form = InterfaceSetDefaultGatwayForm(instance=interface,
                                             data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
示例#5
0
 def test__sets_gateway_links_works_on_dhcp_with_gateway_ip(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     ipv4_link = self.make_ip_family_link(
         interface, factory.make_ipv4_network(), IPADDRESS_TYPE.DHCP
     )
     ipv6_link = self.make_ip_family_link(
         interface, factory.make_ipv6_network(), IPADDRESS_TYPE.DHCP
     )
     form = InterfaceSetDefaultGatwayForm(instance=interface, data={})
     self.assertTrue(form.is_valid(), form.errors)
     interface = form.save()
     node = interface.get_node()
     self.assertEqual(ipv4_link, node.gateway_link_ipv4)
     self.assertEqual(ipv6_link, node.gateway_link_ipv6)
示例#6
0
 def test__requires_link_id_if_more_than_one_gateway_per_family(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     self.make_ip_family_link(interface, factory.make_ipv4_network())
     self.make_ip_family_link(interface, factory.make_ipv6_network())
     self.make_ip_family_link(interface, factory.make_ipv4_network())
     self.make_ip_family_link(interface, factory.make_ipv6_network())
     form = InterfaceSetDefaultGatwayForm(instance=interface, data={})
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual(
         {
             "link_id": [
                 "This field is required; Interface has more than one "
                 "usable IPv4 and IPv6 gateways."
             ],
         }, form.errors)
示例#7
0
 def test__doesnt_require_link_id_if_only_one_gateway_per_family(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     self.make_ip_family_link(interface, factory.make_ipv4_network())
     self.make_ip_family_link(interface, factory.make_ipv6_network())
     form = InterfaceSetDefaultGatwayForm(instance=interface, data={})
     self.assertTrue(form.is_valid(), form.errors)