Skip to content

Commit

Permalink
introduce check blocks into the terraform node and transform graph
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante committed Feb 22, 2023
1 parent 9abf674 commit d2db348
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 150 deletions.
16 changes: 16 additions & 0 deletions internal/checks/state_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ func collectInitialStatuses(into addrs.Map[addrs.ConfigCheckable, *configCheckab
into.Put(addr, st)
}

for _, c := range cfg.Module.Checks {
addr := c.Addr().InModule(moduleAddr)

st := &configCheckableState{
checkTypes: map[addrs.CheckRuleType]int{
addrs.CheckAssertion: len(c.Asserts),
},
}

if c.DataResource != nil {
st.checkTypes[addrs.CheckDataResource] = 1
}

into.Put(addr, st)
}

// Must also visit child modules to collect everything
for _, child := range cfg.Children {
collectInitialStatuses(into, child)
Expand Down
15 changes: 15 additions & 0 deletions internal/command/jsonchecks/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func makeStaticObjectAddr(addr addrs.ConfigCheckable) staticObjectAddr {
if !addr.Module.IsRoot() {
ret["module"] = addr.Module.String()
}
case addrs.ConfigCheck:
if kind := addr.CheckableKind(); kind != addrs.CheckableCheck {
// Something has gone very wrong
panic(fmt.Sprintf("%T has CheckableKind %s", addr, kind))
}

ret["kind"] = "check"
ret["name"] = addr.Check.Name
if !addr.Module.IsRoot() {
ret["module"] = addr.Module.String()
}
default:
panic(fmt.Sprintf("unsupported ConfigCheckable implementation %T", addr))
}
Expand All @@ -71,6 +82,10 @@ func makeDynamicObjectAddr(addr addrs.Checkable) dynamicObjectAddr {
if !addr.Module.IsRoot() {
ret["module"] = addr.Module.String()
}
case addrs.AbsCheck:
if !addr.Module.IsRoot() {
ret["module"] = addr.Module.String()
}
default:
panic(fmt.Sprintf("unsupported Checkable implementation %T", addr))
}
Expand Down
137 changes: 70 additions & 67 deletions internal/plans/internal/planproto/planfile.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/plans/internal/planproto/planfile.proto
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ message CheckResults {
UNSPECIFIED = 0;
RESOURCE = 1;
OUTPUT_VALUE = 2;
CHECK = 3;
}

message ObjectResult {
Expand Down
6 changes: 5 additions & 1 deletion internal/plans/planfile/tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"

"github.com/zclconf/go-cty/cty"
"google.golang.org/protobuf/proto"

"github.com/hashicorp/terraform/internal/addrs"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/hashicorp/terraform/internal/plans/internal/planproto"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/version"
"github.com/zclconf/go-cty/cty"
)

const tfplanFormatVersion = 3
Expand Down Expand Up @@ -114,6 +114,8 @@ func readTfplan(r io.Reader) (*plans.Plan, error) {
objKind = addrs.CheckableResource
case planproto.CheckResults_OUTPUT_VALUE:
objKind = addrs.CheckableOutputValue
case planproto.CheckResults_CHECK:
objKind = addrs.CheckableCheck
default:
return nil, fmt.Errorf("aggregate check results for %s have unsupported object kind %s", rawCRs.ConfigAddr, objKind)
}
Expand Down Expand Up @@ -524,6 +526,8 @@ func writeTfplan(plan *plans.Plan, w io.Writer) error {
pcrs.Kind = planproto.CheckResults_RESOURCE
case addrs.CheckableOutputValue:
pcrs.Kind = planproto.CheckResults_OUTPUT_VALUE
case addrs.CheckableCheck:
pcrs.Kind = planproto.CheckResults_CHECK
default:
return fmt.Errorf("checkable configuration %s has unsupported object type kind %s", configElem.Key, kind)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/states/statefile/version4.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ func encodeCheckResultsV4(in *states.CheckResults) []checkResultsV4 {
ret := make([]checkResultsV4, 0, in.ConfigResults.Len())

for _, configElem := range in.ConfigResults.Elems {
if configElem.Key.CheckableKind() == addrs.CheckableCheck {
// Don't write check blocks into state, but checks for
// preconditions, postconditions, and variable validation should
// still be.
continue
}

configResultsOut := checkResultsV4{
ObjectKind: encodeCheckableObjectKindV4(configElem.Key.CheckableKind()),
ConfigAddr: configElem.Key.String(),
Expand Down

0 comments on commit d2db348

Please sign in to comment.