Merge pull request 'Fix DNS Zone matching for PowerDNS API integration' (#8) from bug-1 into main

Reviewed-on: https://codeberg.org/lauralani/ipam/pulls/8
This commit is contained in:
Adora Laura Kalb 2023-04-01 14:36:55 +00:00
commit 34e4e6fc3e
3 changed files with 370 additions and 371 deletions

3
.gitignore vendored
View file

@ -23,5 +23,6 @@ go.work
# build output # build output
bin/ bin/
# vs code stuff # IDE stuff
.vscode/ .vscode/
.idea/

View file

@ -78,7 +78,7 @@ var ipaddCmd = &cobra.Command{
fmt.Printf("added ip:\nip: %v\nhostname: %v\n", ipaddress, hostname) fmt.Printf("added ip:\nip: %v\nhostname: %v\n", ipaddress, hostname)
dnserr := AddDNSFqdn(hostname, ip) dnserr := AddDNSFqdn(hostname, ip)
if dnserr != nil { if dnserr != nil {
fmt.Println("[ERROR]", writeerr) fmt.Println("[ERROR]", dnserr)
os.Exit(1) os.Exit(1)
} }
} }

View file

@ -109,11 +109,7 @@ func (z DNSZone) SendPATCH(record string, value string, recordtype string, chang
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
resp, resperr := client.Do(req) resp, _ := client.Do(req)
if resperr != nil {
return resperr
}
if resp.StatusCode != 204 { if resp.StatusCode != 204 {
defer resp.Body.Close() defer resp.Body.Close()
@ -121,7 +117,7 @@ func (z DNSZone) SendPATCH(record string, value string, recordtype string, chang
if readerr != nil { if readerr != nil {
fmt.Println(readerr) fmt.Println(readerr)
} }
return errors.New("HTTP Error: " + resp.Status + "\n" + string(body)) return fmt.Errorf("[HTTP ERROR] %v: %q", resp.Status, string(body))
} }
return nil return nil
} }
@ -236,7 +232,7 @@ func GetBestDNSZone(fqdn string) (DNSZone, error) {
var matchfound bool = false var matchfound bool = false
for _, zone := range zones { for _, zone := range zones {
if strings.HasSuffix(fqdn, zone.Name) { if strings.HasSuffix(fqdn, "."+zone.Name) {
if !matchfound { if !matchfound {
matchfound = true matchfound = true
bestmatch = zone bestmatch = zone
@ -292,7 +288,9 @@ func AddDNSFqdn(fqdn string, addr netip.Addr) error {
if frecordexists { if frecordexists {
fmt.Printf("[DNS] DNS Record for %v already exists, no need to change DNS.\n", fqdn) fmt.Printf("[DNS] DNS Record for %v already exists, no need to change DNS.\n", fqdn)
} else { } else {
fpatcherr := fzone.SendPATCH(strings.Replace(fqdn+".", "."+fzone.Name, "", 1), addr.String(), recordtype, "REPLACE") var dotfqdn, dotfzone string = fqdn + ".", "." + fzone.Name
record := strings.Replace(dotfqdn, dotfzone, "", 1)
fpatcherr := fzone.SendPATCH(record, addr.String(), recordtype, "REPLACE")
if fpatcherr != nil { if fpatcherr != nil {
return fpatcherr return fpatcherr
} }