Skip to content

Commit

Permalink
feat: add FillID() method for vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Dec 28, 2023
1 parent abc671e commit 48db900
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions kong/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ func (cg *ConsumerGroup) 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 Vault is Vault.Prefix.
func (v *Vault) FillID() error {

Check failure on line 109 in kong/ids.go

View workflow job for this annotation

GitHub Actions / lint

ST1016: methods on the same type should have the same receiver name (seen 1x "s", 1x "v") (stylecheck)
if v == nil {
return fmt.Errorf("vault is nil")
}
if v.ID != nil && *v.ID != "" {
// ID already set, do nothing.
return nil
}
if v.Prefix == nil || *v.Prefix == "" {
return fmt.Errorf("vault prefix is required")
}

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

v.ID = gen.buildIDFor(*v.Prefix)
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 @@ -116,6 +140,7 @@ var (
reflect.TypeOf(Route{}): newIDGeneratorFor("routes"),
reflect.TypeOf(Consumer{}): newIDGeneratorFor("consumers"),
reflect.TypeOf(ConsumerGroup{}): newIDGeneratorFor("consumergroups"),
reflect.TypeOf(Vault{}): newIDGeneratorFor("vaults"),
}
)

Expand Down
32 changes: 32 additions & 0 deletions kong/ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ func TestFillEntityID(t *testing.T) {
require.Equal(t, expectedID, *cg.ID, "ID should be deterministic")
},
},
// Vault
{
name: "vault with nil pointer",
entity: (*kong.Vault)(nil),
expectErr: true,
},
{
name: "vault with nil prefix",
entity: &kong.Vault{},
expectErr: true,
},
{
name: "vault with empty prefix",
entity: &kong.Vault{
Name: kong.String(""),
},
expectErr: true,
},
{
name: "vault with prefix",
entity: &kong.Vault{
Name: kong.String("env"),
Prefix: kong.String("test-env"),
},
assertEntity: func(t *testing.T, e kong.IDFillable) {
v := e.(*kong.Vault)
require.NotNil(t, v.ID)

const expectedID = "837665c3-856f-5ca2-9db4-52a1cf8a32be"
require.Equal(t, expectedID, *v.ID, "ID should be deterministic")
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 48db900

Please sign in to comment.