def test_accepts_ttl(self): name = factory.make_name("dnsdata") (rrtype, rrdata) = factory.pick_rrset() dnsrr = factory.make_DNSResource(no_ip_addresses=True) ttl = random.randint(1, 10000) form = DNSDataForm({ "name": name, "dnsresource": dnsrr.id, "ttl": ttl, "rrtype": rrtype, "rrdata": rrdata, }) self.assertTrue(form.is_valid(), form.errors) dnsdata = form.save() self.assertEqual(dnsrr.id, dnsdata.dnsresource.id) self.assertEqual(rrtype, dnsdata.rrtype) self.assertEqual(rrdata, dnsdata.rrdata) self.assertEqual(ttl, dnsdata.ttl)
def update(self, request, id): """Update dnsresourcerecord. :param rrtype: Resource Type :param rrdata: Resource Data (everything to the right of Type.) Returns 403 if the user does not have permission to update the dnsresourcerecord. Returns 404 if the dnsresourcerecord is not found. """ dnsdata = DNSData.objects.get_dnsdata_or_404(id, request.user, NodePermission.admin) data = request.data.copy() data['dnsresource'] = dnsdata.dnsresource.id form = DNSDataForm(instance=dnsdata, data=data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def update(self, request, id): """@description-title Update a DNS resource record @description Update a DNS resource record with the given id. @param (int) "{id}" [required=true] The DNS resource record id. @param (string) "rrtype" [required=false] Resource type. @param (string) "rrdata" [required=false] Resource data (everything to the right of type.) @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the updated DNS resource record object. @success-example "success-json" [exkey=dnsresourcerecords-update] placeholder text @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have permission to update the requested DNS resource record. @error (http-status-code) "404" 404 @error (content) "not-found" The requested DNS resource record is not found. @error-example "not-found" Not Found """ dnsdata = DNSData.objects.get_dnsdata_or_404( id, request.user, NodePermission.admin ) data = request.data.copy() data["dnsresource"] = dnsdata.dnsresource.id form = DNSDataForm(instance=dnsdata, data=data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test__doesnt_require_name_on_update(self): dnsdata = factory.make_DNSData() form = DNSDataForm(instance=dnsdata, data={}) self.assertTrue(form.is_valid(), form.errors)
def create(self, request): """Create a DNS resource record. :param fqdn: Hostname (with domain) for the dnsresource. Either fqdn or (name, domain) must be specified. Fqdn is ignored if either name or domain is given (e.g. www.your-maas.maas). :param name: The name (or hostname without a domain) of the DNS resource record (e.g. www.your-maas) :param domain: The domain (name or id) where to create the DNS resource record (Domain (e.g. 'maas') :param rrtype: The resource record type (e.g 'cname', 'mx', 'ns', 'srv', 'sshfp', 'txt') :param rrdata: The resource record data (e.g. 'your-maas', '10 mail.your-maas.maas') """ data = request.data.copy() domain = None fqdn = data.get('fqdn', None) name = data.get('name', None) domainname = data.get('domain', None) rrtype = data.get('rrtype', None) rrdata = data.get('rrdata', None) if rrtype is None: raise MAASAPIBadRequest("rrtype must be provided.") if rrdata is None: raise MAASAPIBadRequest("rrdata must be provided.") # If the user gave us fqdn and did not give us name/domain, expand # fqdn. if domainname is None and name is None and fqdn is not None: # We need a type for resource separation. If the user didn't give # us a rrtype, then assume it's an address of some sort. rrtype = data.get('rrtype', None) (name, domainname) = separate_fqdn(fqdn, rrtype) data['domain'] = domainname data['name'] = name # If the domain is a name, make it an id. if domainname is not None: if domainname.isdigit(): domain = Domain.objects.get_domain_or_404( domainname, user=request.user, perm=NodePermission.view) else: domain = Domain.objects.get_domain_or_404( "name:%s" % domainname, user=request.user, perm=NodePermission.view) data['domain'] = domain.id if domain is None or name is None: raise MAASAPIValidationError( "Either name and domain (or fqdn) must be specified") # Do we already have a DNSResource for this fqdn? dnsrr = DNSResource.objects.filter(name=name, domain__id=domain.id) if not dnsrr.exists(): form = DNSResourceForm(data=data, request=request) if form.is_valid(): form.save() else: raise MAASAPIValidationError(form.errors) dnsrr = DNSResource.objects.filter(name=name, domain__id=domain.id) data['dnsresource'] = dnsrr form = DNSDataForm(data=data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def create(self, request): """@description-title Create a DNS resource record @description Create a new DNS resource record. @param (string) "fqdn" [required=false] Hostname (with domain) for the dnsresource. Either ``fqdn`` or ``name`` and ``domain`` must be specified. ``fqdn`` is ignored if either name or domain is given (e.g. www.your-maas.maas). @param (string) "name" [required=false] The name (or hostname without a domain) of the DNS resource record (e.g. www.your-maas) @param (string) "domain" [required=false] The domain (name or id) where to create the DNS resource record (Domain (e.g. 'maas') @param (string) "rrtype" [required=false] The resource record type (e.g ``cname``, ``mx``, ``ns``, ``srv``, ``sshfp``, ``txt``). @param (string) "rrdata" [required=false] The resource record data (e.g. 'your-maas', '10 mail.your-maas.maas') @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the new DNS resource record object. @success-example "success-json" [exkey=dnsresourcerecords-create] placeholder text """ data = request.data.copy() domain = None fqdn = data.get("fqdn", None) name = data.get("name", None) domainname = data.get("domain", None) rrtype = data.get("rrtype", None) rrdata = data.get("rrdata", None) if rrtype is None: raise MAASAPIBadRequest("rrtype must be provided.") if rrdata is None: raise MAASAPIBadRequest("rrdata must be provided.") # If the user gave us fqdn and did not give us name/domain, expand # fqdn. if domainname is None and name is None and fqdn is not None: # We need a type for resource separation. If the user didn't give # us a rrtype, then assume it's an address of some sort. rrtype = data.get("rrtype", None) (name, domainname) = separate_fqdn(fqdn, rrtype) data["domain"] = domainname data["name"] = name # If the domain is a name, make it an id. if domainname is not None: if domainname.isdigit(): domain = Domain.objects.get_domain_or_404( domainname, user=request.user, perm=NodePermission.view ) else: domain = Domain.objects.get_domain_or_404( "name:%s" % domainname, user=request.user, perm=NodePermission.view, ) data["domain"] = domain.id if domain is None or name is None: raise MAASAPIValidationError( "Either name and domain (or fqdn) must be specified" ) # Do we already have a DNSResource for this fqdn? dnsrr = DNSResource.objects.filter(name=name, domain__id=domain.id) if not dnsrr.exists(): form = DNSResourceForm(data=data, request=request) if form.is_valid(): form.save() else: raise MAASAPIValidationError(form.errors) dnsrr = DNSResource.objects.filter(name=name, domain__id=domain.id) data["dnsresource"] = dnsrr form = DNSDataForm(data=data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)