def update(self, request, id): """@description-title Update a domain @description Update a domain with the given id. @param (int) "{id}" [required=true] A domain id. @param (string) "name" [required=true] Name of the domain. @param (string) "authoritative" [required=false] True if we are authoritative for this domain. @param (string) "ttl" [required=false] The default TTL for this domain. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing information about the updated domain. @success-example "success-json" [exkey=domains-update] placeholder text @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have the permissions required to update the domain. @error (http-status-code) "404" 404 @error (content) "not-found" The requested domain name is not found. @error-example "not-found" Not Found """ domain = Domain.objects.get_domain_or_404( id, request.user, NodePermission.admin ) form = DomainForm(instance=domain, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test_accepts_ttl_equals_none(self): name = factory.make_name("domain") ttl = random.randint(0, 10000000) authoritative = factory.pick_bool() form = DomainForm({ "name": name, "authoritative": authoritative, "ttl": ttl, }) self.assertTrue(form.is_valid(), form.errors) domain = form.save() form = DomainForm(instance=domain, data={"ttl": None}) self.assertTrue(form.is_valid(), form.errors) domain = form.save() self.assertEqual(name, domain.name) self.assertEqual(authoritative, domain.authoritative) self.assertEqual(None, domain.ttl)
def test__updates_domain(self): new_name = factory.make_name("domain") old_authoritative = factory.pick_bool() domain = factory.make_Domain(authoritative=old_authoritative) new_authoritative = not old_authoritative new_ttl = random.randint(0, 10000000) form = DomainForm(instance=domain, data={ "name": new_name, "authoritative": new_authoritative, "ttl": new_ttl, }) self.assertTrue(form.is_valid(), form.errors) form.save() domain = reload_object(domain) self.assertEqual(new_name, domain.name) self.assertEqual(new_authoritative, domain.authoritative) self.assertEqual(new_ttl, domain.ttl)
def create(self, request): """Create a domain. :param name: Name of the domain. :param authoritative: Class type of the domain. """ form = DomainForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__creates_domain(self): domain_name = factory.make_name("domain") domain_authoritative = factory.pick_bool() ttl = random.randint(0, 10000000) form = DomainForm({ "name": domain_name, "authoritative": domain_authoritative, "ttl": ttl, }) self.assertTrue(form.is_valid(), form.errors) domain = form.save() self.assertEqual(domain_name, domain.name) self.assertEqual(domain_authoritative, domain.authoritative) self.assertEqual(ttl, domain.ttl)
def test_accepts_ttl(self): name = factory.make_name("domain") ttl = random.randint(1, 604800) authoritative = factory.pick_bool() form = DomainForm({ "name": name, "authoritative": authoritative, "ttl": ttl }) self.assertTrue(form.is_valid(), form.errors) domain = form.save() self.assertEqual(name, domain.name) self.assertEqual(authoritative, domain.authoritative) self.assertEqual(ttl, domain.ttl)
def update(self, request, id): """Update domain. :param name: Name of the domain. :param authoritative: True if we are authoritative for this domain. :param ttl: The default TTL for this domain. Returns 403 if the user does not have permission to update the dnsresource. Returns 404 if the domain is not found. """ domain = Domain.objects.get_domain_or_404(id, request.user, NodePermission.admin) form = DomainForm(instance=domain, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def create(self, request): """@description-title Create a domain @description Create a domain. @param (string) "name" [required=true] Name of the domain. @param (string) "authoritative" [required=false] Class type of the domain. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the new domain object. @success-example "success-json" [exkey=domains-create] placeholder text """ form = DomainForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test_can_create_forward_dns_server(self): name = factory.make_name("domain") forward_dns_servers = [factory.make_ip_address() for _ in range(0, 2)] form = DomainForm({ "name": name, "authoritative": False, "forward_dns_servers": " ".join(forward_dns_servers), }) self.assertTrue(form.is_valid(), form.errors) domain = form.save() self.assertEqual( forward_dns_servers, [ fwd_dns_srvr.ip_address for fwd_dns_srvr in domain.forward_dns_servers ], )