Skip to content

Commit

Permalink
fix: handle different consumer group consumer list responses
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Apr 1, 2024
1 parent dc9c2c1 commit 673f0f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions kong/consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ func (cg *ConsumerGroup) FriendlyName() string {
}
return ""
}

type consumerGroupConsumers struct {
Consumers []*Consumer `json:"consumers,omitempty" yaml:"consumers,omitempty"`
Data []*Consumer `json:"data,omitempty" yaml:"data,omitempty"`
}
21 changes: 17 additions & 4 deletions kong/consumer_group_consumer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type AbstractConsumerGroupConsumerService interface {
// Delete deletes a ConsumerGroupConsumer in Kong
Delete(ctx context.Context, consumerGroupNameOrID *string, consumerNameOrID *string) error
// ListAll fetches all ConsumerGroup's Consumers in Kong.
ListAll(ctx context.Context, consumerGroupNameOrID *string) (*ConsumerGroupObject, error)
ListAll(ctx context.Context, consumerGroupNameOrID *string) ([]*Consumer, error)
}

// ConsumerGroupService handles ConsumerGroup in Kong.
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *ConsumerGroupConsumerService) Delete(ctx context.Context,
// List fetches a list all of ConsumerGroup's consumers in Kong.
func (s *ConsumerGroupConsumerService) ListAll(
ctx context.Context, consumerGroupNameOrID *string,
) (*ConsumerGroupObject, error) {
) ([]*Consumer, error) {
if isEmptyString(consumerGroupNameOrID) {
return nil, fmt.Errorf("consumerGroupNameOrID cannot be nil for ListAll operation")
}
Expand All @@ -89,11 +89,24 @@ func (s *ConsumerGroupConsumerService) ListAll(
return nil, err
}

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

return &cg, nil
var consumers []*Consumer

// See https://github.com/Kong/kong-ee/pull/6421
if cg.Consumers != nil {
// Kong < 3.5
consumers = cg.Consumers
} else if cg.Data != nil {
// Kong >= 3.5
consumers = cg.Data
} else {
consumers = make([]*Consumer, 0)
}

return consumers, nil
}
6 changes: 3 additions & 3 deletions kong/consumer_group_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ func TestConsumerGroupConsumersListEndpoint(t *testing.T) {
consumerGroupConsumersFromKong, err := client.ConsumerGroupConsumers.ListAll(defaultCtx, cg.Name)
require.NoError(t, err)
assert.NotNil(consumerGroupConsumersFromKong)
assert.Equal(3, len(consumerGroupConsumersFromKong.Consumers))
assert.Equal(3, len(consumerGroupConsumersFromKong))

// check if we see all consumer groups
assert.True(compareConsumers(consumers, consumerGroupConsumersFromKong.Consumers))
assert.True(compareConsumers(consumers, consumerGroupConsumersFromKong))

for i := 0; i < len(consumerGroupConsumersFromKong.Consumers); i++ {
for i := 0; i < len(consumerGroupConsumersFromKong); i++ {
assert.NoError(client.Consumers.Delete(defaultCtx, consumers[i].ID))
}

Expand Down

0 comments on commit 673f0f1

Please sign in to comment.