Skip to content

Commit

Permalink
Merge pull request cloudflare#610 from jacobbednarz/secondary-zone-su…
Browse files Browse the repository at this point in the history
…pport-tsig

Add support for secondary DNS TSIG
  • Loading branch information
jacobbednarz authored Mar 17, 2021
2 parents 5ecfed6 + 93c0060 commit 4b30b7b
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 0 deletions.
132 changes: 132 additions & 0 deletions secondary_dns_tsig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
)

const (
errSecondaryDNSTSIGMissingID = "secondary DNS TSIG ID is required"
)

// SecondaryDNSTSIG contains the structure for a secondary DNS TSIG.
type SecondaryDNSTSIG struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Secret string `json:"secret"`
Algo string `json:"algo"`
}

// SecondaryDNSTSIGDetailResponse is the API response for a single secondary
// DNS TSIG.
type SecondaryDNSTSIGDetailResponse struct {
Response
Result SecondaryDNSTSIG `json:"result"`
}

// SecondaryDNSTSIGListResponse is the API response for all secondary DNS TSIGs.
type SecondaryDNSTSIGListResponse struct {
Response
Result []SecondaryDNSTSIG `json:"result"`
}

// GetSecondaryDNSTSIG gets a single account level TSIG for a secondary DNS
// configuration.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--tsig-details
func (api *API) GetSecondaryDNSTSIG(ctx context.Context, accountID, tsigID string) (SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsigID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return SecondaryDNSTSIG{}, err
}

var r SecondaryDNSTSIGDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}

// ListSecondaryDNSTSIGs gets all account level TSIG for a secondary DNS
// configuration.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--list-tsigs
func (api *API) ListSecondaryDNSTSIGs(ctx context.Context, accountID string) ([]SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs", accountID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []SecondaryDNSTSIG{}, err
}

var r SecondaryDNSTSIGListResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}

// CreateSecondaryDNSTSIG creates a secondary DNS TSIG at the account level.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--create-tsig
func (api *API) CreateSecondaryDNSTSIG(ctx context.Context, accountID string, tsig SecondaryDNSTSIG) (SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs", accountID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, tsig)

if err != nil {
return SecondaryDNSTSIG{}, err
}

result := SecondaryDNSTSIGDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}

return result.Result, nil
}

// UpdateSecondaryDNSTSIG updates an existing secondary DNS TSIG at
// the account level.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--update-tsig
func (api *API) UpdateSecondaryDNSTSIG(ctx context.Context, accountID string, tsig SecondaryDNSTSIG) (SecondaryDNSTSIG, error) {
if tsig.ID == "" {
return SecondaryDNSTSIG{}, errors.New(errSecondaryDNSTSIGMissingID)
}

uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsig.ID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, tsig)

if err != nil {
return SecondaryDNSTSIG{}, err
}

result := SecondaryDNSTSIGDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}

return result.Result, nil
}

// DeleteSecondaryDNSTSIG deletes a secondary DNS TSIG.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--delete-tsig
func (api *API) DeleteSecondaryDNSTSIG(ctx context.Context, accountID, tsigID string) error {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsigID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)

if err != nil {
return err
}

return nil
}
186 changes: 186 additions & 0 deletions secondary_dns_tsig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetSecondaryDNSTSIG(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodGet, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "69cd1e104af3e6ed3cb344f263fd0d5a",
"name": "tsig.customer.cf.",
"secret": "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
"algo": "hmac-sha512."
}
}
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/secondary_dns/tsigs/69cd1e104af3e6ed3cb344f263fd0d5a", handler)

want := SecondaryDNSTSIG{
ID: "69cd1e104af3e6ed3cb344f263fd0d5a",
Name: "tsig.customer.cf.",
Secret: "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
Algo: "hmac-sha512.",
}

actual, err := client.GetSecondaryDNSTSIG(context.Background(), "01a7362d577a6c3019a474fd6f485823", "69cd1e104af3e6ed3cb344f263fd0d5a")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestListSecondaryDNSTSIGs(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodGet, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"id": "69cd1e104af3e6ed3cb344f263fd0d5a",
"name": "tsig.customer.cf.",
"secret": "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
"algo": "hmac-sha512."
}
]
}
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/secondary_dns/tsigs", handler)

want := []SecondaryDNSTSIG{{
ID: "69cd1e104af3e6ed3cb344f263fd0d5a",
Name: "tsig.customer.cf.",
Secret: "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
Algo: "hmac-sha512.",
}}

actual, err := client.ListSecondaryDNSTSIGs(context.Background(), "01a7362d577a6c3019a474fd6f485823")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestCreateSecondaryDNSTSIG(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodPost, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "69cd1e104af3e6ed3cb344f263fd0d5a",
"name": "tsig.customer.cf.",
"secret": "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
"algo": "hmac-sha512."
}
}
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/secondary_dns/tsigs", handler)

want := SecondaryDNSTSIG{
ID: "69cd1e104af3e6ed3cb344f263fd0d5a",
Name: "tsig.customer.cf.",
Secret: "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
Algo: "hmac-sha512.",
}

actual, err := client.CreateSecondaryDNSTSIG(context.Background(), "01a7362d577a6c3019a474fd6f485823", SecondaryDNSTSIG{
Name: "tsig.customer.cf.",
Secret: "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
Algo: "hmac-sha512.",
})

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestUpdateSecondaryDNSTSIG(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodPut, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "69cd1e104af3e6ed3cb344f263fd0d5a",
"name": "tsig.customer.cf.",
"secret": "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
"algo": "hmac-sha512."
}
}
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/secondary_dns/tsigs/69cd1e104af3e6ed3cb344f263fd0d5a", handler)

want := SecondaryDNSTSIG{
ID: "69cd1e104af3e6ed3cb344f263fd0d5a",
Name: "tsig.customer.cf.",
Secret: "caf79a7804b04337c9c66ccd7bef9190a1e1679b5dd03d8aa10f7ad45e1a9dab92b417896c15d4d007c7c14194538d2a5d0feffdecc5a7f0e1c570cfa700837c",
Algo: "hmac-sha512.",
}

actual, err := client.UpdateSecondaryDNSTSIG(context.Background(), "01a7362d577a6c3019a474fd6f485823", want)

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestDeleteSecondaryDNSTSIG(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodDelete, "Expected method 'DELETE', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "269d8f4853475ca241c4e730be286b20"
}
}
`)
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/secondary_dns/tsigs/69cd1e104af3e6ed3cb344f263fd0d5a", handler)

err := client.DeleteSecondaryDNSTSIG(context.Background(), "01a7362d577a6c3019a474fd6f485823", "69cd1e104af3e6ed3cb344f263fd0d5a")
assert.NoError(t, err)
}

0 comments on commit 4b30b7b

Please sign in to comment.