Skip to content

Commit

Permalink
feat: add FillID() method for Consumer Group (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 authored Jul 17, 2023
1 parent 5236d86 commit 6b3d3a6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@

> Release date: TBD
- Added support for scoping plugins to consumer-groups.
- Added method `FillID()` method for Consumer Group.
[#357](https://github.com/Kong/go-kong/pull/357)
- Added support for scoping plugins to Consumer Groups.
[#352](https://github.com/Kong/go-kong/pull/352)

## [v0.45.0]
Expand Down
10 changes: 5 additions & 5 deletions kong/consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type ConsumerGroupPlugin struct {
}

// FriendlyName returns the endpoint key name or ID.
func (s *ConsumerGroup) FriendlyName() string {
if s.Name != nil {
return *s.Name
func (cg *ConsumerGroup) FriendlyName() string {
if cg.Name != nil {
return *cg.Name
}
if s.ID != nil {
return *s.ID
if cg.ID != nil {
return *cg.ID
}
return ""
}
31 changes: 28 additions & 3 deletions kong/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ func (c *Consumer) FillID() error {
return nil
}

// FillID fills the ID of an entity. It is a no-op if the entity already has an ID.
// ID is generated in a deterministic way using UUIDv5. The UUIDv5 namespace is different for each entity type.
// The name used to generate the ID for ConsumerGroup is ConsumerGroup.Name.
func (cg *ConsumerGroup) FillID() error {
if cg == nil {
return fmt.Errorf("consumer group is nil")
}
if cg.ID != nil {
// ID already set, do nothing.
return nil
}
if cg.Name == nil || *cg.Name == "" {
return fmt.Errorf("consumer group name is required")
}

gen, err := idGeneratorFor(cg)
if err != nil {
return fmt.Errorf("could not get id generator: %w", err)
}

cg.ID = gen.buildIDFor(*cg.Name)
return nil
}

var (
// _kongEntitiesNamespace is the UUIDv5 namespace used to generate IDs for Kong entities.
_kongEntitiesNamespace = uuid.MustParse("fd02801f-0957-4a15-a55a-c8d9606f30b5")
Expand All @@ -88,9 +112,10 @@ var (
// names for that purpose.
// See https://github.com/Kong/kong/blob/master/kong/db/schema/others/declarative_config.lua for reference.
_idGenerators = map[reflect.Type]idGenerator{
reflect.TypeOf(Service{}): newIDGeneratorFor("services"),
reflect.TypeOf(Route{}): newIDGeneratorFor("routes"),
reflect.TypeOf(Consumer{}): newIDGeneratorFor("consumers"),
reflect.TypeOf(Service{}): newIDGeneratorFor("services"),
reflect.TypeOf(Route{}): newIDGeneratorFor("routes"),
reflect.TypeOf(Consumer{}): newIDGeneratorFor("consumers"),
reflect.TypeOf(ConsumerGroup{}): newIDGeneratorFor("consumergroups"),
}
)

Expand Down
29 changes: 29 additions & 0 deletions kong/ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ func TestFillEntityID(t *testing.T) {
require.Equal(t, expectedID, *consumer.ID, "ID should be deterministic")
},
},
// Consumer Group
{
name: "consumer group nil pointer",
entity: (*kong.ConsumerGroup)(nil),
expectErr: true,
},
{
name: "consumer group with nil name",
entity: &kong.ConsumerGroup{},
expectErr: true,
},
{
name: "consumer group with empty name",
entity: &kong.ConsumerGroup{Name: kong.String("")},
expectErr: true,
},
{
name: "consumer group with name",
entity: &kong.ConsumerGroup{
Name: kong.String("some.consumer.group"),
},
assertEntity: func(t *testing.T, e kong.IDFillable) {
cg := e.(*kong.ConsumerGroup)
require.NotNil(t, cg.ID)

const expectedID = "e5643801-37c6-5d04-9d3f-c1c84c747e90"
require.Equal(t, expectedID, *cg.ID, "ID should be deterministic")
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 6b3d3a6

Please sign in to comment.