Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

make sure no resource has the same id for capability type #12

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions pkg/validation/validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"fmt"
"strings"

"github.com/klothoplatform/klotho/pkg/annotation"
Expand All @@ -27,6 +28,8 @@ func (p Plugin) Transform(result *core.CompilationResult, deps *core.Dependencie
var errs multierr.Error
err := p.handleAnnotations(result)
errs.Append(err)
err = p.handleResources(result)
errs.Append(err)
err = p.handleProviderValidation(result)
errs.Append(err)
p.validateConfigOverrideResourcesExist(result, zap.L().Sugar())
Expand Down Expand Up @@ -88,6 +91,22 @@ func (p *Plugin) handleAnnotations(result *core.CompilationResult) error {
return errs.ErrOrNil()
}

// handleResources ensures that every resource has a unique id and capability pair.
func (p *Plugin) handleResources(result *core.CompilationResult) error {
var errs multierr.Error
err := validateNoDuplicateIds[*core.Persist](result)
errs.Append(err)
err = validateNoDuplicateIds[*core.Gateway](result)
errs.Append(err)
err = validateNoDuplicateIds[*core.ExecutionUnit](result)
errs.Append(err)
err = validateNoDuplicateIds[*core.PubSub](result)
errs.Append(err)
err = validateNoDuplicateIds[*core.StaticUnit](result)
errs.Append(err)
return errs.ErrOrNil()
}

func (p *Plugin) validateConfigOverrideResourcesExist(result *core.CompilationResult, log *zap.SugaredLogger) {
for unit := range p.UserConfigOverrides.ExecutionUnits {
resources := result.GetResourcesOfType(core.ExecutionUnitKind)
Expand Down Expand Up @@ -178,3 +197,16 @@ func getResourceById(id string, resources []core.CloudResource) core.ResourceKey
}
return resource
}

func validateNoDuplicateIds[T core.CloudResource](result *core.CompilationResult) error {
unitIds := make(map[string]struct{})
units := core.GetResourcesOfType[T](result)
for _, unit := range units {
fmt.Println(unit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Println(unit)

if id, ok := unitIds[unit.Key().Name]; ok {
return fmt.Errorf("Multiple Persist objects with the same name, '%s'", id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't id be a struct{}{}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah, meant to swithc that and log name. will update

}
unitIds[unit.Key().Name] = struct{}{}
}
return nil
}
69 changes: 69 additions & 0 deletions pkg/validation/validation_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"fmt"
"testing"

"github.com/klothoplatform/klotho/pkg/annotation"
Expand Down Expand Up @@ -286,6 +287,74 @@ func Test_validation_handleProviderValidation(t *testing.T) {
}
}

func Test_validation_handleResources(t *testing.T) {
tests := []struct {
name string
result []core.CloudResource
wantErr bool
}{
{
name: "diff resources duplicate ids",
result: []core.CloudResource{
&core.ExecutionUnit{
Name: "test",
},
&core.Gateway{
Name: "test",
},
},
wantErr: false,
},
{
name: "persist different ids",
result: []core.CloudResource{
&core.Persist{
Name: "test",
Kind: core.PersistFileKind,
},
&core.Persist{
Name: "another",
Kind: core.PersistORMKind,
},
},
wantErr: false,
},
{
name: "persist duplicate ids",
result: []core.CloudResource{
&core.Persist{
Name: "test",
Kind: core.PersistFileKind,
},
&core.Persist{
Name: "test",
Kind: core.PersistORMKind,
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

p := Plugin{}
result := core.CompilationResult{}
result.AddAll(tt.result)

err := p.handleResources(&result)
if tt.wantErr {
assert.Error(err)
fmt.Println(err.Error())
return
} else {
assert.NoError(err)
return
}
})
}
}

func Test_validateConfigOverrideResourcesExist(t *testing.T) {
tests := []struct {
name string
Expand Down