From 313276ebfed62f6cf13191e59f1e9136714e7669 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 3 May 2021 22:21:52 +0200 Subject: [PATCH] feat: add workspace and tag existence fns (#56) --- kong/client.go | 17 +++++++++-------- kong/exists.go | 23 +++++++++++++++++++++++ kong/tag_service.go | 19 +++++++++++++++++++ kong/tag_service_test.go | 33 +++++++++++++++++++++++++++++++++ kong/workspace_service.go | 13 +++++++++++++ kong/workspace_service_test.go | 4 ++++ 6 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 kong/exists.go create mode 100644 kong/tag_service.go create mode 100644 kong/tag_service_test.go diff --git a/kong/client.go b/kong/client.go index 4e7a07c5e..e68afa98c 100644 --- a/kong/client.go +++ b/kong/client.go @@ -51,15 +51,15 @@ type Client struct { RBACEndpointPermissions AbstractRBACEndpointPermissionService RBACEntityPermissions AbstractRBACEntityPermissionService - credentials abstractCredentialService - KeyAuths AbstractKeyAuthService - BasicAuths AbstractBasicAuthService - HMACAuths AbstractHMACAuthService - JWTAuths AbstractJWTAuthService - MTLSAuths AbstractMTLSAuthService - ACLs AbstractACLService - + credentials abstractCredentialService + KeyAuths AbstractKeyAuthService + BasicAuths AbstractBasicAuthService + HMACAuths AbstractHMACAuthService + JWTAuths AbstractJWTAuthService + MTLSAuths AbstractMTLSAuthService + ACLs AbstractACLService Oauth2Credentials AbstractOauth2Service + Tags AbstractTagService logger io.Writer debug bool @@ -132,6 +132,7 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) { kong.ACLs = (*ACLService)(&kong.common) kong.Oauth2Credentials = (*Oauth2Service)(&kong.common) + kong.Tags = (*TagService)(&kong.common) kong.CustomEntities = (*CustomEntityService)(&kong.common) kong.Registry = custom.NewDefaultRegistry() diff --git a/kong/exists.go b/kong/exists.go new file mode 100644 index 000000000..6d4a828ee --- /dev/null +++ b/kong/exists.go @@ -0,0 +1,23 @@ +package kong + +import ( + "context" + "net/http" +) + +// exists check the existence with a HEAD HTTP verb +func (c *Client) exists(ctx context.Context, + endpoint string) (bool, error) { + req, err := c.NewRequest("HEAD", endpoint, nil, nil) + if err != nil { + return false, err + } + resp, err := c.Do(ctx, req, nil) + if err != nil { + if IsNotFoundErr(err) { + return false, nil + } + return false, err + } + return resp.StatusCode == http.StatusOK, nil +} diff --git a/kong/tag_service.go b/kong/tag_service.go new file mode 100644 index 000000000..91091bc00 --- /dev/null +++ b/kong/tag_service.go @@ -0,0 +1,19 @@ +package kong + +import ( + "context" +) + +// AbstractTagService handles Tags in Kong. +type AbstractTagService interface { + //Exists checks if the tags exists + Exists(ctx context.Context) (bool, error) +} + +// TagService handles Tags in Kong. +type TagService service + +// Exists checks exitence of the Tags in Kong. +func (s *TagService) Exists(ctx context.Context) (bool, error) { + return s.client.exists(ctx, "/tags") +} diff --git a/kong/tag_service_test.go b/kong/tag_service_test.go new file mode 100644 index 000000000..bbe46f60a --- /dev/null +++ b/kong/tag_service_test.go @@ -0,0 +1,33 @@ +package kong + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTagExists(T *testing.T) { + runWhenKong(T, ">=1.1.0") + assert := assert.New(T) + + client, err := NewTestClient(nil, nil) + assert.Nil(err) + assert.NotNil(client) + + exists, err := client.Tags.Exists(defaultCtx) + assert.Nil(err) + assert.True(exists) +} + +func TestTagDoesNotExists(T *testing.T) { + runWhenKong(T, "<1.1.0") + assert := assert.New(T) + + client, err := NewTestClient(nil, nil) + assert.Nil(err) + assert.NotNil(client) + + exists, err := client.Tags.Exists(defaultCtx) + assert.Nil(err) + assert.False(exists) +} diff --git a/kong/workspace_service.go b/kong/workspace_service.go index f28dd1bf7..b9a424d63 100644 --- a/kong/workspace_service.go +++ b/kong/workspace_service.go @@ -9,6 +9,8 @@ import ( // AbstractWorkspaceService handles Workspaces in Kong. type AbstractWorkspaceService interface { + // Exists checks the exitence of a Workspace in Kong. + Exists(ctx context.Context, nameOrID *string) (bool, error) // Create creates a Workspace in Kong. Create(ctx context.Context, workspace *Workspace) (*Workspace, error) // Get fetches a Workspace in Kong. @@ -41,6 +43,17 @@ type AbstractWorkspaceService interface { // WorkspaceService handles Workspaces in Kong. type WorkspaceService service +// Exists checks the exitence of the Workspace in Kong. +func (s *WorkspaceService) Exists(ctx context.Context, + nameOrID *string) (bool, error) { + if isEmptyString(nameOrID) { + return false, errors.New("nameOrID cannot be nil for Get operation") + } + + endpoint := fmt.Sprintf("/workspaces/%v", *nameOrID) + return s.client.exists(ctx, endpoint) +} + // Create creates a Workspace in Kong. func (s *WorkspaceService) Create(ctx context.Context, workspace *Workspace) (*Workspace, error) { diff --git a/kong/workspace_service_test.go b/kong/workspace_service_test.go index ce1a33daf..28f8863f9 100644 --- a/kong/workspace_service_test.go +++ b/kong/workspace_service_test.go @@ -32,6 +32,10 @@ func TestWorkspaceService(T *testing.T) { assert.Nil(err) assert.NotNil(workspace) + exists, err := client.Workspaces.Exists(defaultCtx, createdWorkspace.ID) + assert.Nil(err) + assert.True(exists) + workspace.Comment = String("new comment") workspace, err = client.Workspaces.Update(defaultCtx, workspace) assert.Nil(err)