// Convert a nameserver request object into a nameserver model object. It can return // errors related to the conversion of IP addresses and normalization of nameserver's // hostname func (n *NameserverRequest) toNameserverModel() (model.Nameserver, error) { var nameserver model.Nameserver host, err := model.NormalizeDomainName(n.Host) if err != nil { return nameserver, err } nameserver.Host = host if len(n.IPv4) > 0 { ipv4 := net.ParseIP(n.IPv4) if ipv4 == nil { return nameserver, ErrInvalidIP } nameserver.IPv4 = ipv4 } if len(n.IPv6) > 0 { ipv6 := net.ParseIP(n.IPv6) if ipv6 == nil { return nameserver, ErrInvalidIP } nameserver.IPv6 = ipv6 } return nameserver, nil }
func (i *FQDN) Before(w http.ResponseWriter, r *http.Request) { fqdn, err := model.NormalizeDomainName(i.fqdnHandler.GetFQDN()) if err != nil { if err := i.fqdnHandler.MessageResponse("invalid-uri", r.URL.RequestURI()); err == nil { w.WriteHeader(http.StatusBadRequest) } else { log.Println("Error while writing response. Details:", err) w.WriteHeader(http.StatusInternalServerError) } return } i.fqdnHandler.SetFQDN(fqdn) }
// Merge is used to merge a domain request object sent by the user into a domain object of // the database. It can return errors related to merge problems that are problem caused by // data format of the user input func Merge(domain model.Domain, domainRequest DomainRequest) (model.Domain, error) { var err error if domainRequest.FQDN, err = model.NormalizeDomainName(domainRequest.FQDN); err != nil { return domain, err } // Detect when the domain object is empty, that is the case when we are creating a new // domain in the Shelter project if len(domain.FQDN) == 0 { domain.FQDN = domainRequest.FQDN } else { // Cannot merge domains with different FQDNs if domain.FQDN != domainRequest.FQDN { return domain, ErrDomainsFQDNDontMatch } } nameservers, err := toNameserversModel(domainRequest.Nameservers) if err != nil { return domain, err } for index, userNameserver := range nameservers { for _, nameserver := range domain.Nameservers { if nameserver.Host == userNameserver.Host { // Found the same nameserver in the user domain object, maybe the user updated the // IP addresses nameserver.IPv4 = userNameserver.IPv4 nameserver.IPv6 = userNameserver.IPv6 nameservers[index] = nameserver break } } } domain.Nameservers = nameservers dsSet, err := toDSSetModel(domainRequest.DSSet) if err != nil { return domain, err } dnskeysDSSet, err := dnskeysRequestsToDSSetModel(domain.FQDN, domainRequest.DNSKEYS) if err != nil { return domain, err } dsSet = append(dsSet, dnskeysDSSet...) for index, userDS := range dsSet { for _, ds := range domain.DSSet { if ds.Keytag == userDS.Keytag { // Found the same DS in the user domain object ds.Algorithm = userDS.Algorithm ds.Digest = userDS.Digest ds.DigestType = userDS.DigestType dsSet[index] = ds break } } } domain.DSSet = dsSet // We can replace the whole structure of the e-mail every time that a new UPDATE arrives // because there's no extra information in server side that we need to keep domain.Owners, err = toOwnersModel(domainRequest.Owners) if err != nil { return domain, err } return domain, nil }