Skip to content

Commit

Permalink
feat(dondominio): create record if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 28, 2024
1 parent 7eee3fc commit 8839db9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/provider/providers/dondominio/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dondominio

import (
"context"
"net/http"
"net/netip"

"github.com/qdm12/ddns-updater/internal/provider/constants"
)

// See https://dondominio.dev/en/api/docs/api/#dns-zone-create-service-dnscreate
func (p *Provider) create(ctx context.Context, client *http.Client, ip netip.Addr) (err error) {
recordType := constants.A
if ip.Is6() {
recordType = constants.AAAA
}
requestData := struct {
APIUser string `json:"apiuser"`
APIPasswd string `json:"apipasswd"`
ServiceName string `json:"serviceName"`
Name string `json:"name"` // Name for the DNS zone
Type string `json:"type"`
Value string `json:"value"`
}{
APIUser: p.username,
APIPasswd: p.password,
ServiceName: p.name,
Name: p.BuildDomainName(),
Type: recordType,
Value: ip.String(),
}

_, err = apiCall(ctx, client, "/service/dnscreate", requestData)
if err != nil {
return err
}

return nil
}
9 changes: 9 additions & 0 deletions internal/provider/providers/dondominio/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ func (p *Provider) Update(ctx context.Context, client *http.Client, ip netip.Add
ids = aaaaIDs
}

if len(ids) == 0 {
err = p.create(ctx, client, ip)
if err != nil {
return netip.Addr{}, fmt.Errorf("creating %s record for %s: %w",
recordType, p.BuildDomainName(), err)
}
return ip, nil
}

for _, id := range ids {
err = p.update(ctx, client, id, ip)
if err != nil {
Expand Down

0 comments on commit 8839db9

Please sign in to comment.