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 AddReference method to References #415

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,26 @@ type ExternalName struct {
IdentifierFields []string
}

var ErrReferenceAlreadyExists = errors.New("reference for field already exists")

// References represents reference resolver configurations for the fields of a
// given resource. Key should be the field path of the field to be referenced.
type References map[string]Reference

// AddReference adds a reference configuration for the given field path.
// The fieldPath is the Terraform field path of the field to be referenced.
//
// Example: "vpc_id" or "forwarding_rule.certificate_name" in case of nested
// in another object.
func (r References) AddReference(fieldPath string, ref Reference) error {
if _, ok := r[fieldPath]; ok {
return ErrReferenceAlreadyExists
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't love returning an error in this situation. My instinct is that the action of setting a reference should be idempotent; as you've implemented it all times it's invoked but the first will fail with an error.

Maybe it's ok because it would happen at generation time, although I don't know if we could guarantee that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no strong opinions; either way it is fine by me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @mbbush, what would be the resolution here? I am not sure if you requested some changes or not

}

r[fieldPath] = ref
return nil
}

// Reference represents the Crossplane options used to generate
// reference resolvers for fields
type Reference struct {
Expand Down
31 changes: 31 additions & 0 deletions pkg/config/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,34 @@ func TestSetExternalTagsWithPaved(t *testing.T) {
})
}
}

func TestReferences_AddReference(t *testing.T) {
t.Run("Adding a reference", func(t *testing.T) {
r := &Resource{References: References{}}
err := r.References.AddReference("forwarding_rule.certificate_name", Reference{
TerraformName: "digitalocean_certificate",
})
if err != nil {
t.Fatalf("AddReference() got error: %v", err)
}
if len(r.References) != 1 {
t.Fatalf("AddReference() got error: %v", err)
}
})
t.Run("Adding twice a reference for a given field", func(t *testing.T) {
r := &Resource{References: References{}}
err := r.References.AddReference("forwarding_rule.certificate_name", Reference{
TerraformName: "digitalocean_certificate",
})
if err != nil {
t.Fatalf("AddReference() got error: %v", err)
}

err = r.References.AddReference("forwarding_rule.certificate_name", Reference{
TerraformName: "digitalocean_certificate",
})
if !errors.Is(err, ErrReferenceAlreadyExists) {
t.Fatalf("AddReference() should have returned an error")
}
})
}
Loading