Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TransIP provider #921

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix duplicate token labels, working update call
  • Loading branch information
LucaScorpion committed Jan 21, 2025
commit 7f780f5ab7d14e989354ba08679213aa6bfb3e65
50 changes: 41 additions & 9 deletions internal/provider/providers/transip/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (p *Provider) createAccessToken(ctx context.Context, client *http.Client) (
"login": p.username,
"nonce": strconv.FormatInt(time.Now().UnixNano(), 10),
"global_key": true,
"read_only": true, // TODO: set to false
"label": "ddns-updater",
"read_only": false,
"label": fmt.Sprintf("ddns-updater %d", time.Now().Unix()),
})
if err != nil {
return "", fmt.Errorf("json encoding request body: %w", err)
Expand Down Expand Up @@ -196,17 +196,49 @@ func (p *Provider) createAccessToken(ctx context.Context, client *http.Client) (
}

func (p *Provider) Update(ctx context.Context, client *http.Client, ip netip.Addr) (newIP netip.Addr, err error) {
//recordType := constants.A
//if ip.Is6() {
// recordType = constants.AAAA
//}
recordType := constants.A
if ip.Is6() {
recordType = constants.AAAA
}

token, err := p.createAccessToken(ctx, client)
if err != nil {
return netip.Addr{}, err
}

// TODO: List DNS entries, check if a new one should be created or one should be updated.

requestBody, err := json.Marshal(map[string]any{
"dnsEntry": map[string]any{
"name": p.owner,
"expire": 300, // TODO: Use expire from existing entry.
"type": recordType,
"content": ip.String(),
},
})
if err != nil {
return netip.Addr{}, fmt.Errorf("json encoding request body: %w", err)
}

url := fmt.Sprintf("https://api.transip.nl/v6/domains/%s/dns", p.domain)
request, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewReader(requestBody))
if err != nil {
return netip.Addr{}, fmt.Errorf("creating http request: %w", err)
}
headers.SetUserAgent(request)
headers.SetContentType(request, "application/json")
headers.SetAuthBearer(request, token)

_, err = p.createAccessToken(ctx, client)
response, err := client.Do(request)
if err != nil {
return netip.Addr{}, err
}
defer response.Body.Close()

// TODO
if response.StatusCode != http.StatusNoContent {
return netip.Addr{}, fmt.Errorf("%w: %d: %s",
errors.ErrHTTPStatusNotValid, response.StatusCode, utils.BodyToSingleLine(response.Body))
}

return netip.Addr{}, fmt.Errorf("implement me")
return ip, nil
}