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

Rename TSIGKey to TSIGKeys #96

Merged
merged 1 commit into from
Sep 21, 2024
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ cryptokey, err := pdns.Cryptokeys.Get(ctx, "example.com", "1337")
err := pdns.Cryptokeys.Delete(ctx, "example.com", "1337")
```

### Create/change/delete tsigkeys
### Create/change/delete TSIG keys

```go
tsigkey, err := pdns.TSIGKey.Create(ctx, "examplekey", "hmac-sha256", "")
tsigkey, err := pdns.TSIGKey.Change(ctx, "examplekey.", powerdns.TSIGKey{Key: powerdns.String("newkey")})
tsigkeys, err := pdns.TSIGKey.List(ctx)
tsigkey, err := pdns.TSIGKey.Get(ctx, "examplekey.")
err := pdns.TSIGKey.Delete(ctx, "examplekey.")
tsigkey, err := pdns.TSIGKeys.Create(ctx, "examplekey", "hmac-sha256", "")
tsigkey, err := pdns.TSIGKeys.Change(ctx, "examplekey.", powerdns.TSIGKey{Key: powerdns.String("newkey")})
tsigkeys, err := pdns.TSIGKeys.List(ctx)
tsigkey, err := pdns.TSIGKeys.Get(ctx, "examplekey.")
err := pdns.TSIGKeys.Delete(ctx, "examplekey.")
```

### More examples
Expand Down
7 changes: 5 additions & 2 deletions powerdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ type Client struct {
Servers *ServersService
Statistics *StatisticsService
Zones *ZonesService
TSIGKey *TSIGKeyService
// Deprecated: Use TSIGKeys instead. TSIGKey will be removed with the next major version.
TSIGKey *TSIGKeysService
TSIGKeys *TSIGKeysService
}

// logFatalf makes log.Fatalf testable
Expand Down Expand Up @@ -100,7 +102,8 @@ func New(baseURL string, vHost string, options ...NewOption) *Client {
client.Servers = (*ServersService)(&client.common)
client.Statistics = (*StatisticsService)(&client.common)
client.Zones = (*ZonesService)(&client.common)
client.TSIGKey = (*TSIGKeyService)(&client.common)
client.TSIGKeys = (*TSIGKeysService)(&client.common)
client.TSIGKey = client.TSIGKeys

for _, option := range options {
option(client)
Expand Down
2 changes: 1 addition & 1 deletion powerdns_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Example() {
}

// Create a TSIG Record
exampleKey, err := pdns.TSIGKey.Create(ctx, "examplekey", "hmac-sha256", "")
exampleKey, err := pdns.TSIGKeys.Create(ctx, "examplekey", "hmac-sha256", "")
if err != nil {
log.Fatalf("%v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions tsigkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
)

// TSIGKeyService handles communication with the tsigs related methods of the Client API
type TSIGKeyService service
// TSIGKeysService handles communication with the tsigs related methods of the Client API
type TSIGKeysService service

// TSIGKey structure with JSON API metadata
type TSIGKey struct {
Expand All @@ -19,7 +19,7 @@ type TSIGKey struct {
}

// List retrieves a list of TSIGKeys
func (t *TSIGKeyService) List(ctx context.Context) ([]TSIGKey, error) {
func (t *TSIGKeysService) List(ctx context.Context) ([]TSIGKey, error) {
req, err := t.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("servers/%s/tsigkeys", t.client.VHost), nil, nil)
if err != nil {
return nil, err
Expand All @@ -31,7 +31,7 @@ func (t *TSIGKeyService) List(ctx context.Context) ([]TSIGKey, error) {
}

// Get returns a certain TSIGKeys
func (t *TSIGKeyService) Get(ctx context.Context, id string) (*TSIGKey, error) {
func (t *TSIGKeysService) Get(ctx context.Context, id string) (*TSIGKey, error) {
req, err := t.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("servers/%s/tsigkeys/%s", t.client.VHost, id), nil, nil)
if err != nil {
return nil, err
Expand All @@ -43,7 +43,7 @@ func (t *TSIGKeyService) Get(ctx context.Context, id string) (*TSIGKey, error) {
}

// Create a new TSIG Key setting empty string for key will generate it
func (t *TSIGKeyService) Create(ctx context.Context, name, algorithm, key string) (*TSIGKey, error) {
func (t *TSIGKeysService) Create(ctx context.Context, name, algorithm, key string) (*TSIGKey, error) {
reqTsigkey := TSIGKey{
Name: &name,
Algorithm: &algorithm,
Expand All @@ -60,7 +60,7 @@ func (t *TSIGKeyService) Create(ctx context.Context, name, algorithm, key string
return &respTsigkey, err
}

func (t *TSIGKeyService) Change(ctx context.Context, id string, newKey TSIGKey) (*TSIGKey, error) {
func (t *TSIGKeysService) Change(ctx context.Context, id string, newKey TSIGKey) (*TSIGKey, error) {
req, err := t.client.newRequest(ctx, http.MethodPut, fmt.Sprintf("servers/%s/tsigkeys/%s", t.client.VHost, id), nil, newKey)
if err != nil {
return nil, err
Expand All @@ -71,7 +71,7 @@ func (t *TSIGKeyService) Change(ctx context.Context, id string, newKey TSIGKey)
return &responseKey, err
}

func (t *TSIGKeyService) Delete(ctx context.Context, id string) error {
func (t *TSIGKeysService) Delete(ctx context.Context, id string) error {
req, err := t.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("servers/%s/tsigkeys/%s", t.client.VHost, id), nil, nil)
if err != nil {
return err
Expand Down
32 changes: 15 additions & 17 deletions tsigkeys_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,55 @@ import (
"github.com/joeig/go-powerdns/v3"
)

var (
exampleTSIGKey = powerdns.TSIGKey{
Name: powerdns.String("examplekey"),
Algorithm: powerdns.String("hmac-sha256"),
Key: powerdns.String("ruTjBX2Jw/2BlE//5255fmKHaSRvLvp6p+YyDDAXThnBN/1Mz/VwMw+HQJVtkpDsAXvpPuNNZhucdKmhiOS4Tg=="),
}
)
var exampleTSIGKey = powerdns.TSIGKey{
Name: powerdns.String("examplekey"),
Algorithm: powerdns.String("hmac-sha256"),
Key: powerdns.String("ruTjBX2Jw/2BlE//5255fmKHaSRvLvp6p+YyDDAXThnBN/1Mz/VwMw+HQJVtkpDsAXvpPuNNZhucdKmhiOS4Tg=="),
}

func ExampleTSIGKeyService_Create() {
func ExampleTSIGKeysService_Create() {
pdns := powerdns.New("http://localhost:8080", "localhost", powerdns.WithAPIKey("apipw"))
ctx := context.Background()

_, err := pdns.TSIGKey.Create(ctx, *exampleTSIGKey.Name, *exampleTSIGKey.Algorithm, "")
_, err := pdns.TSIGKeys.Create(ctx, *exampleTSIGKey.Name, *exampleTSIGKey.Algorithm, "")
if err != nil {
log.Fatalf("%v", err)
}

}

func ExampleTSIGKeyService_List() {
func ExampleTSIGKeysService_List() {
pdns := powerdns.New("http://localhost:8080", "localhost", powerdns.WithAPIKey("apipw"))
ctx := context.Background()

if _, err := pdns.TSIGKey.List(ctx); err != nil {
if _, err := pdns.TSIGKeys.List(ctx); err != nil {
log.Fatalf("%v", err)
}
}

func ExampleTSIGKeyService_Get() {
func ExampleTSIGKeysService_Get() {
pdns := powerdns.New("http://localhost:8080", "localhost", powerdns.WithAPIKey("apipw"))
ctx := context.Background()

if _, err := pdns.TSIGKey.Get(ctx, *exampleTSIGKey.ID); err != nil {
if _, err := pdns.TSIGKeys.Get(ctx, *exampleTSIGKey.ID); err != nil {
log.Fatalf("%v", err)
}
}

func ExampleTSIGKeyService_Change() {
func ExampleTSIGKeysService_Change() {
pdns := powerdns.New("http://localhost:8080", "localhost", powerdns.WithAPIKey("apipw"))
ctx := context.Background()

if _, err := pdns.TSIGKey.Change(ctx, *exampleTSIGKey.ID, exampleTSIGKey); err != nil {
if _, err := pdns.TSIGKeys.Change(ctx, *exampleTSIGKey.ID, exampleTSIGKey); err != nil {
log.Fatalf("%v", err)
}
}

func ExampleTSIGKeyService_Delete() {
func ExampleTSIGKeysService_Delete() {
pdns := powerdns.New("http://localhost:8080", "localhost", powerdns.WithAPIKey("apipw"))
ctx := context.Background()

if err := pdns.TSIGKey.Delete(ctx, *exampleTSIGKey.ID); err != nil {
if err := pdns.TSIGKeys.Delete(ctx, *exampleTSIGKey.ID); err != nil {
log.Fatalf("%v", err)
}
}
31 changes: 14 additions & 17 deletions tsigkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"github.com/jarcoal/httpmock"
)

const (
insecureKey = "ruTjBX2Jw/2BlE//5255fmKHaSRvLvp6p+YyDDAXThnBN/1Mz/VwMw+HQJVtkpDsAXvpPuNNZhucdKmhiOS4Tg=="
)
const insecureKey = "ruTjBX2Jw/2BlE//5255fmKHaSRvLvp6p+YyDDAXThnBN/1Mz/VwMw+HQJVtkpDsAXvpPuNNZhucdKmhiOS4Tg=="

func generateTestTSIGKey(client *Client, name string, key string, autoAddTSIGKey bool) *TSIGKey {
tsigKeyName := fmt.Sprintf("test-%d-%s", rand.Int(), name)
Expand All @@ -25,7 +23,7 @@ func generateTestTSIGKey(client *Client, name string, key string, autoAddTSIGKey
Key: String(key),
}
if autoAddTSIGKey && httpmock.Disabled() {
tsigKey, err := client.TSIGKey.Create(context.Background(), tsigKeyName, "hmac-sha256", key)
tsigKey, err := client.TSIGKeys.Create(context.Background(), tsigKeyName, "hmac-sha256", key)
if err != nil {
log.Printf("Error creating TSIG Key: %s: %v\n", name, err)
} else {
Expand All @@ -40,7 +38,6 @@ func generateTestTSIGKey(client *Client, name string, key string, autoAddTSIGKey
}

func registerTSIGKeyMockResponder(tsigKeys *[]TSIGKey) {

httpmock.RegisterResponder(http.MethodGet, generateTestAPIVHostURL()+"/tsigkeys",
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
Expand Down Expand Up @@ -80,7 +77,6 @@ func registerTSIGKeyMockResponder(tsigKeys *[]TSIGKey) {
)

for _, tsigkey := range *tsigKeys {

httpmock.RegisterResponder(http.MethodGet, generateTestAPIVHostURL()+"/tsigkeys/"+*tsigkey.ID,
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
Expand Down Expand Up @@ -110,6 +106,7 @@ func registerTSIGKeyMockResponder(tsigKeys *[]TSIGKey) {
return httpmock.NewJsonResponse(http.StatusOK, clientTsigkey)
},
)

httpmock.RegisterResponder(http.MethodDelete, generateTestAPIVHostURL()+"/tsigkeys/"+*tsigkey.ID,
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
Expand Down Expand Up @@ -156,7 +153,7 @@ func TestCreateTSIGKey(t *testing.T) {

for i, tc := range testCases {
t.Run(fmt.Sprintf("TestCase%d", i), func(t *testing.T) {
respTSIGKey, err := p.TSIGKey.Create(context.Background(), *tc.tsigkey.Name, *tc.tsigkey.Algorithm, *tc.tsigkey.Key)
respTSIGKey, err := p.TSIGKeys.Create(context.Background(), *tc.tsigkey.Name, *tc.tsigkey.Algorithm, *tc.tsigkey.Key)
if !tc.wantSuccess {
if err == nil {
t.Error("no error on duplicate key")
Expand Down Expand Up @@ -189,7 +186,7 @@ func TestPatchTSIGKey(t *testing.T) {

testPutTSIGKey.Key = String("1yWS55DxB2H40lded3/2IGnhbW6dCntvO+igEcP47n2ikD1EO03NDGKsKValitiqrtAmk41UbYVpREN23GYAdg==")

_, err := p.TSIGKey.Change(context.Background(), *testPutTSIGKey.ID, *testPutTSIGKey)
_, err := p.TSIGKeys.Change(context.Background(), *testPutTSIGKey.ID, *testPutTSIGKey)
if err != nil {
t.Error(err)
}
Expand All @@ -204,31 +201,31 @@ func TestTSIGKeyErrorNewRequests(t *testing.T) {
registerTSIGKeyMockResponder(&[]TSIGKey{})

t.Run("Test Get invalid request", func(t *testing.T) {
_, err := p.TSIGKey.Get(context.Background(), "thiskeydoesnotexist.")
_, err := p.TSIGKeys.Get(context.Background(), "thiskeydoesnotexist.")
if err == nil {
t.Error("error is nil")
}
})
t.Run("Test List invalid request", func(t *testing.T) {
_, err := p.TSIGKey.List(context.Background())
_, err := p.TSIGKeys.List(context.Background())
if err == nil {
t.Error("error is nil")
}
})
t.Run("Test Create invalid request", func(t *testing.T) {
_, err := p.TSIGKey.Create(context.Background(), "test", "hmac-sha256", "")
_, err := p.TSIGKeys.Create(context.Background(), "test", "hmac-sha256", "")
if err == nil {
t.Error("error is nil")
}
})
t.Run("Test Change invalid request", func(t *testing.T) {
_, err := p.TSIGKey.Change(context.Background(), "test", TSIGKey{})
_, err := p.TSIGKeys.Change(context.Background(), "test", TSIGKey{})
if err == nil {
t.Error("error is nil")
}
})
t.Run("Test Delete invalid request", func(t *testing.T) {
err := p.TSIGKey.Delete(context.Background(), "test")
err := p.TSIGKeys.Delete(context.Background(), "test")
if err == nil {
t.Error("error is nil")
}
Expand All @@ -247,14 +244,14 @@ func TestGetTSIGKey(t *testing.T) {
})

t.Run("Test Get", func(t *testing.T) {
_, err := p.TSIGKey.Get(context.Background(), *getTSIGKey.ID)
_, err := p.TSIGKeys.Get(context.Background(), *getTSIGKey.ID)
if err != nil {
t.Error(err)
}
})

t.Run("Test List", func(t *testing.T) {
tsigKeyList, err := p.TSIGKey.List(context.Background())
tsigKeyList, err := p.TSIGKeys.List(context.Background())
if err != nil {
t.Error(err)
}
Expand All @@ -279,15 +276,15 @@ func TestDeleteTSIGKey(t *testing.T) {
})

t.Run("Remove existing TSIG Key", func(t *testing.T) {
err := p.TSIGKey.Delete(context.Background(), *existingTSIGKey.ID)
err := p.TSIGKeys.Delete(context.Background(), *existingTSIGKey.ID)
if err != nil {
t.Errorf("expected successfull delete got error: %v", err)
return
}
})

t.Run("Remove non-existing TSIG Key", func(t *testing.T) {
err := p.TSIGKey.Delete(context.Background(), *missingTSIGKey.ID)
err := p.TSIGKeys.Delete(context.Background(), *missingTSIGKey.ID)
if err == nil {
t.Errorf("expected err. but got nil")
}
Expand Down