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

Go templates in config values for edge and resource templates #732

Merged
merged 4 commits into from
Sep 19, 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
6 changes: 6 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[linters-settings.lll]
line-length = 120
tab-width = 2

[linters]
enable = ["lll"]
22 changes: 21 additions & 1 deletion pkg/construct/resource_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type (
Expand Down Expand Up @@ -491,6 +491,26 @@ func (rg *ResourceGraph) ReplaceConstruct(resource Resource, new Resource) error
return rg.RemoveResource(resource)
}

func (rg *ResourceGraph) ReplaceConstructId(oldId ResourceId, new Resource) error {
rg.AddResource(new)
// Since its a construct we just assume every single edge can be removed
for _, edge := range rg.underlying.OutgoingEdgesById(oldId.String()) {
rg.AddDependency(new, edge.Destination)
err := rg.RemoveDependency(edge.Source.Id(), edge.Destination.Id())
if err != nil {
return err
}
}
for _, edge := range rg.underlying.IncomingEdgesById(oldId.String()) {
rg.AddDependency(edge.Source, new)
err := rg.RemoveDependency(edge.Source.Id(), edge.Destination.Id())
if err != nil {
return err
}
}
return rg.underlying.RemoveVertex(oldId.String())
}

// CreateResource is a wrapper around a Resources .Create method
//
// CreateResource provides safety in assuring that the up to date resource (which is inline with what exists in the ResourceGraph) is returned
Expand Down
79 changes: 59 additions & 20 deletions pkg/construct/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ type ResourceId struct {
Name string `yaml:"name" toml:"name"`
}

var zeroId = ResourceId{}

func (id ResourceId) IsZero() bool {
return id == ResourceId{}
return id == zeroId
}

func (id ResourceId) String() string {
s := id.Provider + ":" + id.Type
if id.IsZero() {
return ""
}

sb := strings.Builder{}
sb.Grow(len(id.Provider) + len(id.Type) + len(id.Namespace) + len(id.Name) + 3)

sb.WriteString(id.Provider)
sb.WriteByte(':')
sb.WriteString(id.Type)
if id.Namespace != "" || strings.Contains(id.Name, ":") {
s += ":" + id.Namespace
sb.WriteByte(':')
sb.WriteString(id.Namespace)
}
if id.Name != "" {
sb.WriteByte(':')
sb.WriteString(id.Name)
}
return s + ":" + id.Name
return sb.String()
}

func (id ResourceId) QualifiedTypeName() string {
Expand All @@ -37,28 +53,51 @@ func (id ResourceId) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}

// Matches uses `id` (the receiver) as a filter for `other` (the argument) and returns true if all the non-empty fields from
// `id` match the corresponding fields in `other`.
func (id ResourceId) Matches(other ResourceId) bool {
if id.Provider != "" && id.Provider != other.Provider {
return false
}
if id.Type != "" && id.Type != other.Type {
return false
}
if id.Namespace != "" && id.Namespace != other.Namespace {
return false
}
if id.Name != "" && id.Name != other.Name {
return false
}
return true
}

var (
resourceProviderPattern = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
resourceTypePattern = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
resourceNamespacePattern = regexp.MustCompile(`^[a-zA-Z0-9_#./\-:\[\]]*$`)
resourceNamePattern = regexp.MustCompile(`^[a-zA-Z0-9_#./\-:\[\]]*$`)
resourceNamespacePattern = regexp.MustCompile(`^[a-zA-Z0-9_./\-\[\]]*$`) // like name, but `:` not allowed
resourceNamePattern = regexp.MustCompile(`^[a-zA-Z0-9_./\-:\[\]#]*$`)
)

func (id *ResourceId) UnmarshalText(data []byte) error {
parts := strings.Split(string(data), ":")
if len(parts) < 3 {
return fmt.Errorf("invalid number of parts (%d) in resource id '%s'", len(parts), string(data))
}
if len(parts) > 4 {
parts = append(parts[:3], strings.Join(parts[3:], ":"))
}
id.Provider = parts[0]
id.Type = parts[1]
if len(parts) == 4 {
id.Namespace = parts[2]
parts := strings.SplitN(string(data), ":", 4)
switch len(parts) {
case 4:
id.Name = parts[3]
} else {
id.Name = parts[2]
fallthrough
case 3:
if len(parts) == 4 {
id.Namespace = parts[2]
} else {
id.Name = parts[2]
}
fallthrough
case 2:
id.Type = parts[1]
id.Provider = parts[0]
case 1:
if parts[0] != "" {
return fmt.Errorf("must have trailing ':' for provider-only ID")
}
}
if id.IsZero() {
return nil
Expand All @@ -67,7 +106,7 @@ func (id *ResourceId) UnmarshalText(data []byte) error {
if !resourceProviderPattern.MatchString(id.Provider) {
err = errors.Join(err, fmt.Errorf("invalid provider '%s' (must match %s)", id.Provider, resourceProviderPattern))
}
if !resourceTypePattern.MatchString(id.Type) {
if id.Type != "" && !resourceTypePattern.MatchString(id.Type) {
err = errors.Join(err, fmt.Errorf("invalid type '%s' (must match %s)", id.Type, resourceTypePattern))
}
if id.Namespace != "" && !resourceNamespacePattern.MatchString(id.Namespace) {
Expand Down
198 changes: 198 additions & 0 deletions pkg/construct/resource_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package construct

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestResourceId_UnmarshalText(t *testing.T) {
tests := []struct {
name string
str string
want ResourceId
wantErr bool
}{
{
name: "full id",
str: "aws:subnet:my_vpc:my_subnet",
want: ResourceId{
Provider: "aws",
Type: "subnet",
Namespace: "my_vpc",
Name: "my_subnet",
},
},
{
name: "no namespace",
str: "aws:subnet:my_subnet",
want: ResourceId{
Provider: "aws",
Type: "subnet",
Name: "my_subnet",
},
},
{
name: "namespace with colon in name",
str: "aws:subnet:my_vpc:my_subnet:with:colons",
want: ResourceId{
Provider: "aws",
Type: "subnet",
Namespace: "my_vpc",
Name: "my_subnet:with:colons",
},
},
{
name: "no namespace with colon in name",
str: "aws:subnet::my_subnet:with:colons",
want: ResourceId{
Provider: "aws",
Type: "subnet",
Name: "my_subnet:with:colons",
},
},
{
name: "no name or namespace",
str: "aws:subnet",
want: ResourceId{
Provider: "aws",
Type: "subnet",
},
},
{
name: "no type",
str: "aws:",
want: ResourceId{
Provider: "aws",
},
},
{
name: "no trailing colon",
str: "aws",
wantErr: true,
},
{
name: "empty is zero id",
str: "",
want: ResourceId{},
},
{
name: "invalid provider",
str: "aws$:subnet:my_subnet",
wantErr: true,
},
{
name: "invalid type",
str: "aws:subnet$:my_subnet",
wantErr: true,
},
{
name: "invalid namespace",
str: "aws:subnet:my_vpc$:my_subnet",
wantErr: true,
},
{
name: "invalid name",
str: "aws:subnet:my_vpc:my_subnet$",
wantErr: true,
},
{
name: "missing provider",
str: ":subnet:my_vpc:my_subnet",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

var id ResourceId
err := id.UnmarshalText([]byte(tt.str))
if tt.wantErr {
assert.Error(err)
return
}
if !assert.NoError(err) {
return
}
assert.Equal(tt.want, id)
// Test the round trip to make sure that String() matches exactly the input string
assert.Equal(tt.str, id.String())
})
}
}

func TestResourceId_Matches(t *testing.T) {
tests := []struct {
name string
selector string
resource string
want bool
}{
{
name: "provider match",
selector: "a:",
resource: "a:b:c",
want: true,
},
{
name: "provider mismatch",
selector: "a:",
resource: "x:b:c",
want: false,
},
{
name: "type match",
selector: "a:b",
resource: "a:b:c",
want: true,
},
{
name: "type mismatch",
selector: "a:b",
resource: "a:x:c",
want: false,
},
{
name: "namespace match",
selector: "a:b:c:",
resource: "a:b:c:d",
want: true,
},
{
name: "namespace mismatch",
selector: "a:b:c:",
resource: "a:b:x:d",
want: false,
},
{
name: "name match",
selector: "a:b:c:d",
resource: "a:b:c:d",
want: true,
},
{
name: "name mismatch",
selector: "a:b:c:d",
resource: "a:b:c:x",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

var sel, res ResourceId
err := sel.UnmarshalText([]byte(tt.selector))
if !assert.NoError(err) {
return
}
err = res.UnmarshalText([]byte(tt.resource))
if !assert.NoError(err) {
return
}

assert.Equal(tt.want, sel.Matches(res))
})
}
}
23 changes: 23 additions & 0 deletions pkg/construct/resources.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package construct

import (
"bytes"
"fmt"
"reflect"

"github.com/klothoplatform/klotho/pkg/io"
Expand Down Expand Up @@ -313,3 +315,24 @@ func getNestedResources(resolver resourceResolver, source BaseConstruct, targetV
}
return
}

func (v IaCValue) String() string {
return v.ResourceId.String() + "#" + v.Property
}

func (v IaCValue) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}

func (v *IaCValue) UnmarshalText(b []byte) error {
parts := bytes.SplitN(b, []byte("#"), 2)
if len(parts) != 2 {
return fmt.Errorf("invalid IaCValue format: %s", string(b))
}
err := v.ResourceId.UnmarshalText(parts[0])
if err != nil {
return err
}
v.Property = string(parts[1])
return nil
}
Loading