Skip to content

Commit

Permalink
fix: added new method to fetch a consumer-group without consumers. (#494
Browse files Browse the repository at this point in the history
)

* fix: added new method to fetch a consumer-group without consumers.

Reverted the existing `Get` method to keep listing
consumers and added a new one that uses the query
parameter `list_consumers=false` to skip listing
consumers for performance reasons.
This is done so as to fix dump errors coming in
deck when using gw 3.9+ where consumer-groups
do not show the associated consumers. We will
push the performant option behind a flag.

For: Kong/deck#1483

* chore: added changelog update

* chore: improved test readability

* chore: fixed lint error

* chore: updated changelog file with change links

* Update CHANGELOG.md

Co-authored-by: Patryk Małek <patryk.malek@konghq.com>

---------

Co-authored-by: Patryk Małek <patryk.malek@konghq.com>
  • Loading branch information
Prashansa-K and pmalek authored Jan 13, 2025
1 parent 0008da3 commit 16fdf6e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Table of Contents

- [v0.63.0](#v0630)
- [v0.62.0](#v0620)
- [v0.61.0](#v0610)
- [v0.60.0](#v0600)
- [v0.59.1](#v0591)
Expand Down Expand Up @@ -75,6 +77,17 @@
- [0.2.0](#020)
- [0.1.0](#010)

## [v0.63.0]

> Release date: 2025/01/10
- Added `GetWithNoConsumers` function to AbstractConsumerGroupService
to allow the fetching a consumer-group without listing its
consumers.
- Reverted `GET /consumer_groups` to older way that lists consumers
along with consumer-group information.
[#494](https://github.com/Kong/go-kong/pull/494)

## [v0.62.0]

> Release date: 2024/12/11
Expand Down Expand Up @@ -988,6 +1001,8 @@ authentication credentials in Kong.
releases of Kong since every release of Kong is introducing breaking changes
to the Admin API.

[v0.63.0]: https://github.com/Kong/go-kong/compare/v0.62.0...v0.63.0
[v0.62.0]: https://github.com/Kong/go-kong/compare/v0.61.0...v0.62.0
[v0.61.0]: https://github.com/Kong/go-kong/compare/v0.60.0...v0.61.0
[v0.60.0]: https://github.com/Kong/go-kong/compare/v0.59.1...v0.60.0
[v0.59.1]: https://github.com/Kong/go-kong/compare/v0.59.0...v0.59.1
Expand Down
24 changes: 24 additions & 0 deletions kong/consumer_group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type AbstractConsumerGroupService interface {
Create(ctx context.Context, consumerGroup *ConsumerGroup) (*ConsumerGroup, error)
// Get fetches a ConsumerGroup from Kong.
Get(ctx context.Context, nameOrID *string) (*ConsumerGroupObject, error)
// GetWithNoConsumers fetches a ConsumerGroup with no consumers listed from Kong.
GetWithNoConsumers(ctx context.Context, nameOrID *string) (*ConsumerGroupObject, error)
// Update updates a ConsumerGroup in Kong
Update(ctx context.Context, consumerGroup *ConsumerGroup) (*ConsumerGroup, error)
// Delete deletes a ConsumerGroup in Kong
Expand Down Expand Up @@ -63,6 +65,28 @@ func (s *ConsumerGroupService) Get(ctx context.Context,
return nil, fmt.Errorf("nameOrID cannot be nil for Get operation")
}

endpoint := fmt.Sprintf("/consumer_groups/%v", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}

var cg ConsumerGroupObject
_, err = s.client.Do(ctx, req, &cg)
if err != nil {
return nil, err
}
return &cg, nil
}

// Get fetches a ConsumerGroup from Kong, but skips the associated consumers.
func (s *ConsumerGroupService) GetWithNoConsumers(ctx context.Context,
nameOrID *string,
) (*ConsumerGroupObject, error) {
if isEmptyString(nameOrID) {
return nil, fmt.Errorf("nameOrID cannot be nil for Get operation")
}

endpoint := fmt.Sprintf("/consumer_groups/%v?list_consumers=false", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
Expand Down
23 changes: 16 additions & 7 deletions kong/consumer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,20 @@ func TestConsumerGroupGetEndpointPostGW39(t *testing.T) {
assert.Equal(response.Consumers[0].Username, createdConsumer.Username)
assert.Equal(response.ConsumerGroup.ID, createdConsumerGroup.ID)

// Check get endpoint
consumerGroupFromKong, err := client.ConsumerGroups.Get(defaultCtx, createdConsumerGroup.ID)
require.NoError(err)
assert.NotNil(consumerGroupFromKong)
assert.Equal(consumerGroupFromKong.ConsumerGroup.ID, createdConsumerGroup.ID)
// Consumers are not listed in a get operation
assert.Nil(consumerGroupFromKong.Consumers)
t.Run("Get", func(_ *testing.T) {
consumerGroupFromKong, err := client.ConsumerGroups.Get(defaultCtx, createdConsumerGroup.ID)
require.NoError(err)
assert.NotNil(consumerGroupFromKong)
assert.Equal(consumerGroupFromKong.ConsumerGroup.ID, createdConsumerGroup.ID)
assert.NotNil(consumerGroupFromKong.Consumers)
assert.Len(consumerGroupFromKong.Consumers, 1, "consumers are listed")
})

t.Run("GetWithNoConsumers", func(_ *testing.T) {
consumerGroupFromKong, err := client.ConsumerGroups.GetWithNoConsumers(defaultCtx, createdConsumerGroup.ID)
require.NoError(err)
assert.NotNil(consumerGroupFromKong)
assert.Equal(consumerGroupFromKong.ConsumerGroup.ID, createdConsumerGroup.ID)
assert.Nil(consumerGroupFromKong.Consumers, "consumers should not be listed")
})
}

0 comments on commit 16fdf6e

Please sign in to comment.