def test_sets_gateway_links_on_node_when_no_link_id(self): interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL) ipv4_link = self.make_ip_family_link(interface, factory.make_ipv4_network()) ipv6_link = self.make_ip_family_link(interface, factory.make_ipv6_network()) 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)
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)