Skip to content

Commit

Permalink
Merge pull request #36213 from hashicorp/backport/jbardin/changes-err…
Browse files Browse the repository at this point in the history
…ors/grossly-lucky-shrimp

Backport of encoding of changes should always return a value into v1.10
  • Loading branch information
jbardin authored Dec 13, 2024
2 parents 5318523 + 488fc1e commit bc98401
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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")
}
}

0 comments on commit bc98401

Please sign in to comment.