@@ -5,6 +5,7 @@ package rpcapi
55
66import (
77 "context"
8+ "encoding/json"
89 "net"
910 "testing"
1011
@@ -13,6 +14,10 @@ import (
1314 "google.golang.org/grpc/test/bufconn"
1415 "google.golang.org/protobuf/proto"
1516 "google.golang.org/protobuf/types/known/anypb"
17+
18+ "github.com/hashicorp/terraform/internal/addrs"
19+ "github.com/hashicorp/terraform/internal/stacks/stackaddrs"
20+ "github.com/hashicorp/terraform/internal/stacks/stackstate"
1621)
1722
1823// grpcClientForTesting creates an in-memory-only gRPC server, offers the
@@ -63,6 +68,57 @@ func grpcClientForTesting(ctx context.Context, t *testing.T, registerServices fu
6368 }
6469}
6570
71+ func appliedChangeToRawState (t * testing.T , changes []stackstate.AppliedChange ) map [string ]* anypb.Any {
72+ ret := make (map [string ]* anypb.Any )
73+ for _ , change := range changes {
74+ raw , err := change .AppliedChangeProto ()
75+ if err != nil {
76+ t .Fatalf ("failed to marshal change to proto: %s" , err )
77+ }
78+ for _ , raw := range raw .Raw {
79+ ret [raw .Key ] = raw .Value
80+ }
81+ }
82+ return ret
83+ }
84+
85+ func mustDefaultRootProvider (provider string ) addrs.AbsProviderConfig {
86+ return addrs.AbsProviderConfig {
87+ Module : addrs .RootModule ,
88+ Provider : addrs .NewDefaultProvider (provider ),
89+ }
90+ }
91+
92+ func mustAbsComponentInstance (t * testing.T , addr string ) stackaddrs.AbsComponentInstance {
93+ ret , diags := stackaddrs .ParseAbsComponentInstanceStr (addr )
94+ if len (diags ) > 0 {
95+ t .Fatalf ("failed to parse component instance address %q: %s" , addr , diags )
96+ }
97+ return ret
98+ }
99+
100+ func mustAbsComponent (t * testing.T , addr string ) stackaddrs.AbsComponent {
101+ ret , diags := stackaddrs .ParseAbsComponentInstanceStr (addr )
102+ if len (diags ) > 0 {
103+ t .Fatalf ("failed to parse component instance address %q: %s" , addr , diags )
104+ }
105+ if ret .Item .Key != addrs .NoKey {
106+ t .Fatalf ("expected component address %q to have no key, but got %q" , addr , ret .Item .Key )
107+ }
108+ return stackaddrs.AbsComponent {
109+ Stack : ret .Stack ,
110+ Item : ret .Item .Component ,
111+ }
112+ }
113+
114+ func mustAbsResourceInstanceObject (t * testing.T , addr string ) stackaddrs.AbsResourceInstanceObject {
115+ ret , diags := stackaddrs .ParseAbsResourceInstanceObjectStr (addr )
116+ if len (diags ) > 0 {
117+ t .Fatalf ("failed to parse resource instance object address %q: %s" , addr , diags )
118+ }
119+ return ret
120+ }
121+
66122func mustMarshalAnyPb (msg proto.Message ) * anypb.Any {
67123 var ret anypb.Any
68124 err := anypb .MarshalFrom (& ret , msg , proto.MarshalOptions {})
@@ -71,3 +127,11 @@ func mustMarshalAnyPb(msg proto.Message) *anypb.Any {
71127 }
72128 return & ret
73129}
130+
131+ func mustMarshalJSONAttrs (attrs map [string ]interface {}) []byte {
132+ jsonAttrs , err := json .Marshal (attrs )
133+ if err != nil {
134+ panic (err )
135+ }
136+ return jsonAttrs
137+ }
0 commit comments