diff --git a/README.md b/README.md index 58a6226..5c7391a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/powerdns.go b/powerdns.go index bb5cb7f..ce55b87 100644 --- a/powerdns.go +++ b/powerdns.go @@ -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 @@ -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) diff --git a/powerdns_example_test.go b/powerdns_example_test.go index b71a5e8..07ac7f2 100644 --- a/powerdns_example_test.go +++ b/powerdns_example_test.go @@ -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) } diff --git a/tsigkeys.go b/tsigkeys.go index b2877f3..0514be2 100644 --- a/tsigkeys.go +++ b/tsigkeys.go @@ -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 { @@ -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 @@ -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 @@ -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, @@ -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 @@ -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 diff --git a/tsigkeys_example_test.go b/tsigkeys_example_test.go index eaf50e5..facb9dd 100644 --- a/tsigkeys_example_test.go +++ b/tsigkeys_example_test.go @@ -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) } } diff --git a/tsigkeys_test.go b/tsigkeys_test.go index 2a96006..51e7bc9 100644 --- a/tsigkeys_test.go +++ b/tsigkeys_test.go @@ -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) @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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") @@ -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) } @@ -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") } @@ -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) } @@ -279,7 +276,7 @@ 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 @@ -287,7 +284,7 @@ func TestDeleteTSIGKey(t *testing.T) { }) 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") }