@@ -35,6 +35,22 @@ func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
3535
3636 var _ = plan
3737 return func (ctx * tfcontext.Context , blocks terraform.Blocks , inputVars map [string ]cty.Value ) {
38+ for _ , block := range blocks {
39+ if block .InModule () {
40+
41+ x := block .ModuleKey ()
42+ y := block .ModuleBlock ().FullName ()
43+ var _ , _ = x , y
44+ fmt .Println (block .ModuleKey ())
45+ continue
46+ }
47+
48+ err = loadResourcesToContext (block .Context ().Parent (), plan .PriorState .Values .RootModule .Resources )
49+ if err != nil {
50+ panic (fmt .Sprintf ("unable to load resources to context: %v" , err ))
51+ }
52+ }
53+
3854 // 'data' blocks are loaded into prior state
3955 //plan.PriorState.Values.RootModule.Resources
4056 for _ , resource := range plan .PriorState .Values .RootModule .Resources {
@@ -63,6 +79,42 @@ func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
6379 }, nil
6480}
6581
82+ func planResources (plan * tfjson.Plan , block * terraform.Block ) error {
83+ if ! block .InModule () {
84+ return loadResourcesToContext (block .Context ().Parent (), plan .PriorState .Values .RootModule .Resources )
85+ }
86+
87+ var path []string
88+ mod := block .ModuleBlock ()
89+
90+ for {
91+ path = append ([]string {mod .FullName ()}, path ... )
92+ break
93+ }
94+ return nil
95+ }
96+
97+ func loadResourcesToContext (ctx * tfcontext.Context , resources []* tfjson.StateResource ) error {
98+ for _ , resource := range resources {
99+ if resource .Mode != "data" {
100+ continue
101+ }
102+
103+ if strings .HasPrefix (resource .Type , "coder_" ) {
104+ // Ignore coder blocks
105+ continue
106+ }
107+
108+ val , err := toCtyValue (resource .AttributeValues )
109+ if err != nil {
110+ return fmt .Errorf ("unable to determine value of resource %q: %w" , resource .Address , err )
111+ }
112+
113+ ctx .Set (val , string (resource .Mode ), resource .Type , resource .Name )
114+ }
115+ return nil
116+ }
117+
66118func toCtyValue (a any ) (cty.Value , error ) {
67119 if a == nil {
68120 return cty .NilVal , nil
0 commit comments