Skip to content

Commit

Permalink
feat: add workspace and tag existence fns (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 committed May 3, 2021
1 parent cba87b1 commit 313276e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
17 changes: 9 additions & 8 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions kong/exists.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions kong/tag_service.go
Original file line number Diff line number Diff line change
@@ -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")
}
33 changes: 33 additions & 0 deletions kong/tag_service_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions kong/workspace_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions kong/workspace_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 313276e

Please sign in to comment.