Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add FillID() method for Consumer Group #357

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading