def create(self, request): """@description-title Create a static route @description Creates a static route. @param (string) "source" [required=true] Source subnet name for the route. @param (string) "destination" [required=true] Destination subnet name for the route. @param (string) "gateway_ip" [required=true] IP address of the gateway on the source subnet. @param (int) "metric" [required=false] Weight of the route on a deployed machine. @success (http-status-code) "200" 200 @success (json) "success-json" A JSON object containing information about the new static route object. @success-example "success-json" [exkey=static-routes-create] placeholder text """ form = StaticRouteForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__requires_source_destination_gateway_ip(self): form = StaticRouteForm({}) self.assertFalse(form.is_valid(), form.errors) self.assertEqual( { "source": ["This field is required."], "destination": ["This field is required."], "gateway_ip": ["This field is required."], }, form.errors)
def create(self, request): """Create a static route. :param source: Source subnet for the route. :param destination: Destination subnet for the route. :param gateway_ip: IP address of the gateway on the source subnet. :param metric: Weight of the route on a deployed machine. """ form = StaticRouteForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__creates_staticroute(self): source = factory.make_Subnet() version = source.get_ipnetwork().version dest = factory.make_Subnet(version=version) gateway_ip = factory.pick_ip_in_Subnet(source) form = StaticRouteForm({ "source": source.id, "destination": dest.id, "gateway_ip": gateway_ip, }) self.assertTrue(form.is_valid(), form.errors) staticroute = form.save() self.assertEqual(source, staticroute.source) self.assertEqual(dest, staticroute.destination) self.assertEqual(gateway_ip, staticroute.gateway_ip) self.assertEqual(0, staticroute.metric)
def update(self, request, id): """Update static route. :param source: Source subnet for the route. :param destination: Destination subnet for the route. :param gateway_ip: IP address of the gateway on the source subnet. :param metric: Weight of the route on a deployed machine. Returns 404 if the static route is not found. """ staticroute = StaticRoute.objects.get_staticroute_or_404( id, request.user, NODE_PERMISSION.ADMIN) form = StaticRouteForm(instance=staticroute, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__updates_staticroute(self): static_route = factory.make_StaticRoute() new_source = factory.make_Subnet() version = new_source.get_ipnetwork().version new_dest = factory.make_Subnet(version=version) new_gateway_ip = factory.pick_ip_in_Subnet(new_source) new_metric = random.randint(0, 500) form = StaticRouteForm(instance=static_route, data={ "source": new_source.id, "destination": new_dest.id, "gateway_ip": new_gateway_ip, "metric": new_metric, }) self.assertTrue(form.is_valid(), form.errors) staticroute = form.save() self.assertEqual(new_source, staticroute.source) self.assertEqual(new_dest, staticroute.destination) self.assertEqual(new_gateway_ip, staticroute.gateway_ip) self.assertEqual(new_metric, staticroute.metric)
def update(self, request, id): """@description-title Update a static route @description Updates a static route with the given ID. @param (int) "{id}" [required=true] A static-route ID. @param (string) "source" [required=false] Source subnet name for the route. @param (string) "destination" [required=false] Destination subnet name for the route. @param (string) "gateway_ip" [required=false] IP address of the gateway on the source subnet. @param (int) "metric" [required=false] Weight of the route on a deployed machine. @success (http-status-code) "200" 200 @success (json) "success-json" A JSON object containing information about the updated static route object. @success-example "success-json" [exkey=static-routes-update] placeholder text @error (http-status-code) "404" 404 @error (content) "not-found" The requested static-route is not found. @error-example "not-found" Not Found """ staticroute = StaticRoute.objects.get_staticroute_or_404( id, request.user, NodePermission.admin ) form = StaticRouteForm(instance=staticroute, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__doest_require_any_fields_on_update(self): static_route = factory.make_StaticRoute() form = StaticRouteForm(instance=static_route, data={}) self.assertTrue(form.is_valid(), form.errors)