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

encoding of changes should always return a value #36212

Merged
merged 1 commit into from
Dec 13, 2024
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
10 changes: 6 additions & 4 deletions internal/plans/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func NewChanges() *Changes {

// Encode encodes all the stored resource and output changes into a new *ChangeSrc value
func (c *Changes) Encode(schemas *schemarepo.Schemas) (*ChangesSrc, error) {
// a plan is always built even when there are errors, so make sure to return
// a valid changesSrc.
changesSrc := NewChangesSrc()

for _, rc := range c.Resources {
p, ok := schemas.Providers[rc.ProviderAddr.Provider]
if !ok {
return nil, fmt.Errorf("Changes.Encode: missing provider %s for %s", rc.ProviderAddr, rc.Addr)
return changesSrc, fmt.Errorf("Changes.Encode: missing provider %s for %s", rc.ProviderAddr, rc.Addr)
}

var schema providers.Schema
Expand All @@ -59,12 +61,12 @@ func (c *Changes) Encode(schemas *schemarepo.Schemas) (*ChangesSrc, error) {
}

if schema.Block == nil {
return nil, fmt.Errorf("Changes.Encode: missing schema for %s", rc.Addr)
return changesSrc, fmt.Errorf("Changes.Encode: missing schema for %s", rc.Addr)
}

rcs, err := rc.Encode(schema.Block.ImpliedType())
if err != nil {
return nil, fmt.Errorf("Changes.Encode: %w", err)
return changesSrc, fmt.Errorf("Changes.Encode: %w", err)
}

changesSrc.Resources = append(changesSrc.Resources, rcs)
Expand All @@ -73,7 +75,7 @@ func (c *Changes) Encode(schemas *schemarepo.Schemas) (*ChangesSrc, error) {
for _, ocs := range c.Outputs {
oc, err := ocs.Encode()
if err != nil {
return nil, err
return changesSrc, err
}
changesSrc.Outputs = append(changesSrc.Outputs, oc)
}
Expand Down
24 changes: 24 additions & 0 deletions internal/plans/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@ func TestChangeEncodeSensitive(t *testing.T) {
})
}
}

// make sure we get a valid value back even when faced with an error
func TestChangeEncodeError(t *testing.T) {
changes := &Changes{
Outputs: []*OutputChange{
{
// Missing Addr
Change: Change{
Before: cty.NullVal(cty.DynamicPseudoType),
// can't encode a marked value
After: cty.StringVal("test").Mark("shoult not be here"),
},
},
},
}
// no resources so we can get by with no schemas
changesSrc, err := changes.Encode(nil)
if err == nil {
t.Fatal("expected error")
}
if changesSrc == nil {
t.Fatal("changesSrc should not be nil")
}
}
Loading