Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 19 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ type VercelRecord struct {
TTL int `json:"ttl"`
}

func doRequest(token string, request *http.Request) ([]byte, error) {
func doRequest(token string, teamId string, request *http.Request) ([]byte, error) {
// Bearer Token for Vercel Authorization: Bearer <TOKEN>
request.Header.Add("Authorization", "Bearer "+token)
request.Header.Add("Content-Type", "application/json")

if teamId != "" {
q := request.URL.Query()
q.Add("teamId", teamId)
request.URL.RawQuery = q.Encode()
}

client := &http.Client{}
response, err := client.Do(request)
if err != nil {
Expand All @@ -56,13 +62,13 @@ func doRequest(token string, request *http.Request) ([]byte, error) {
return data, nil
}

func getAllRecords(ctx context.Context, token string, zone string) ([]libdns.Record, error) {
func getAllRecords(ctx context.Context, token string, zone string, teamId string) ([]libdns.Record, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.vercel.com/v4/domains/%s/records", zone), nil)
if err != nil {
return nil, err
}

data, err := doRequest(token, req)
data, err := doRequest(token, teamId, req)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +92,7 @@ func getAllRecords(ctx context.Context, token string, zone string) ([]libdns.Rec
return records, nil
}

func createRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
func createRecord(ctx context.Context, token string, zone string, r libdns.Record, teamId string) (libdns.Record, error) {
reqData := VercelRecord{
Type: r.Type,
Name: normalizeRecordName(r.Name, zone),
Expand All @@ -103,7 +109,7 @@ func createRecord(ctx context.Context, token string, zone string, r libdns.Recor
if err != nil {
return libdns.Record{}, err
}
data, err := doRequest(token, req)
data, err := doRequest(token, teamId, req)
if err != nil {
return libdns.Record{}, err
}
Expand All @@ -121,41 +127,41 @@ func createRecord(ctx context.Context, token string, zone string, r libdns.Recor
}, nil
}

func deleteRecord(ctx context.Context, zone string, token string, record libdns.Record) error {
func deleteRecord(ctx context.Context, zone string, token string, record libdns.Record, teamId string) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("https://api.vercel.com/v2/domains/%s/records/%s", zone, record.ID), nil)
if err != nil {
return err
}

_, err = doRequest(token, req)
_, err = doRequest(token, teamId, req)
if err != nil {
return err
}

return nil
}

func updateRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
err := deleteRecord(ctx, zone, token, r)
func updateRecord(ctx context.Context, token string, zone string, r libdns.Record, teamId string) (libdns.Record, error) {
err := deleteRecord(ctx, zone, token, r, teamId)

if err != nil {
return libdns.Record{}, err
}

newRecord, err := createRecord(ctx, token, zone, r)
newRecord, err := createRecord(ctx, token, zone, r, teamId)
if err != nil {
return libdns.Record{}, err
}

return newRecord, nil
}

func createOrUpdateRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
func createOrUpdateRecord(ctx context.Context, token string, zone string, r libdns.Record, teamId string) (libdns.Record, error) {
if len(r.ID) == 0 {
return createRecord(ctx, token, zone, r)
return createRecord(ctx, token, zone, r, teamId)
}

return updateRecord(ctx, token, zone, r)
return updateRecord(ctx, token, zone, r, teamId)
}

func normalizeRecordName(recordName string, zone string) string {
Expand Down
12 changes: 7 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
// Provider implements the libdns interfaces for Vercel
type Provider struct {
// AuthAPIToken is the Vercel Authentication Token - see https://vercel.com/docs/api#api-basics/authentication
AuthAPIToken string `json:"auth_api_token"`
AuthAPIToken string `json:"auth_api_token,omitempty"`
// Optional, TeamId is the Vercel Team ID - see https://vercel.com/docs/rest-api#introduction/api-basics/authentication/accessing-resources-owned-by-a-team
TeamId string `json:"team_id,omitempty"`
}

// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
records, err := getAllRecords(ctx, p.AuthAPIToken, unFQDN(zone))
records, err := getAllRecords(ctx, p.AuthAPIToken, unFQDN(zone), p.TeamId)
if err != nil {
return nil, err
}
Expand All @@ -28,7 +30,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
var appendedRecords []libdns.Record

for _, record := range records {
newRecord, err := createRecord(ctx, p.AuthAPIToken, unFQDN(zone), record)
newRecord, err := createRecord(ctx, p.AuthAPIToken, unFQDN(zone), record, p.TeamId)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +43,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
// DeleteRecords deletes the records from the zone.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
for _, record := range records {
err := deleteRecord(ctx, unFQDN(zone), p.AuthAPIToken, record)
err := deleteRecord(ctx, unFQDN(zone), p.AuthAPIToken, record, p.TeamId)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +58,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns
var setRecords []libdns.Record

for _, record := range records {
setRecord, err := createOrUpdateRecord(ctx, p.AuthAPIToken, unFQDN(zone), record)
setRecord, err := createOrUpdateRecord(ctx, p.AuthAPIToken, unFQDN(zone), record, p.TeamId)
if err != nil {
return setRecords, err
}
Expand Down