@@ -11,6 +11,7 @@ import (
1111 "github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1212 "github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
1313 "github.com/hashicorp/terraform-plugin-go/tfprotov5"
14+ sdk "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415)
1516
1617// ListRequestErrorDiagnostics returns a value suitable for
@@ -25,6 +26,21 @@ func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic)
2526 }, nil
2627}
2728
29+ type SDKContext string
30+
31+ var SDKResourceKey SDKContext = "sdk_resource"
32+
33+ // NewContextWithSDKResource returns a new Context that carries value r
34+ func NewContextWithSDKResource (ctx context.Context , r * sdk.Resource ) context.Context {
35+ return context .WithValue (ctx , SDKResourceKey , r )
36+ }
37+
38+ // FromContext returns the SDK Resource value stored in ctx, if any.
39+ func SDKResourceFromContext (ctx context.Context ) (* sdk.Resource , bool ) {
40+ r , ok := ctx .Value (SDKResourceKey ).(* sdk.Resource )
41+ return r , ok
42+ }
43+
2844func (s * Server ) ListResource (ctx context.Context , protoReq * tfprotov5.ListResourceRequest ) (* tfprotov5.ListResourceServerStream , error ) {
2945 protoStream := & tfprotov5.ListResourceServerStream {Results : tfprotov5 .NoListResults }
3046 allDiags := diag.Diagnostics {}
@@ -47,43 +63,52 @@ func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov5.ListResou
4763 return ListRequestErrorDiagnostics (ctx , allDiags ... )
4864 }
4965
50- resourceSchema , diags := s .FrameworkServer .ResourceSchema (ctx , protoReq .TypeName )
51- allDiags .Append (diags ... )
52- if diags .HasError () {
53- return ListRequestErrorDiagnostics (ctx , allDiags ... )
54- }
66+ _ , ok := SDKResourceFromContext (ctx )
67+ switch ok {
68+ case true :
69+ protoStream .Results = func (push func (tfprotov5.ListResourceResult ) bool ) {
70+ listResult := tfprotov5.ListResourceResult {}
71+ push (listResult )
72+ }
73+ case false :
74+ resourceSchema , diags := s .FrameworkServer .ResourceSchema (ctx , protoReq .TypeName )
75+ allDiags .Append (diags ... )
76+ if diags .HasError () {
77+ return ListRequestErrorDiagnostics (ctx , allDiags ... )
78+ }
5579
56- identitySchema , diags := s .FrameworkServer .ResourceIdentitySchema (ctx , protoReq .TypeName )
57- allDiags .Append (diags ... )
58- if diags .HasError () {
59- return ListRequestErrorDiagnostics (ctx , allDiags ... )
60- }
80+ identitySchema , diags := s .FrameworkServer .ResourceIdentitySchema (ctx , protoReq .TypeName )
81+ allDiags .Append (diags ... )
82+ if diags .HasError () {
83+ return ListRequestErrorDiagnostics (ctx , allDiags ... )
84+ }
6185
62- req := & fwserver.ListRequest {
63- Config : config ,
64- ListResource : listResource ,
65- ResourceSchema : resourceSchema ,
66- ResourceIdentitySchema : identitySchema ,
67- IncludeResource : protoReq .IncludeResource ,
68- }
69- stream := & fwserver.ListResultsStream {}
86+ req := & fwserver.ListRequest {
87+ Config : config ,
88+ ListResource : listResource ,
89+ ResourceSchema : resourceSchema ,
90+ ResourceIdentitySchema : identitySchema ,
91+ IncludeResource : protoReq .IncludeResource ,
92+ }
93+ stream := & fwserver.ListResultsStream {}
7094
71- err := s .FrameworkServer .ListResource (ctx , req , stream )
72- if err != nil {
73- return protoStream , err
74- }
95+ err := s .FrameworkServer .ListResource (ctx , req , stream )
96+ if err != nil {
97+ return protoStream , err
98+ }
7599
76- protoStream .Results = func (push func (tfprotov5.ListResourceResult ) bool ) {
77- for result := range stream .Results {
78- var protoResult tfprotov5.ListResourceResult
79- if req .IncludeResource {
80- protoResult = toproto5 .ListResourceResultWithResource (ctx , & result )
81- } else {
82- protoResult = toproto5 .ListResourceResult (ctx , & result )
83- }
100+ protoStream .Results = func (push func (tfprotov5.ListResourceResult ) bool ) {
101+ for result := range stream .Results {
102+ var protoResult tfprotov5.ListResourceResult
103+ if req .IncludeResource {
104+ protoResult = toproto5 .ListResourceResultWithResource (ctx , & result )
105+ } else {
106+ protoResult = toproto5 .ListResourceResult (ctx , & result )
107+ }
84108
85- if ! push (protoResult ) {
86- return
109+ if ! push (protoResult ) {
110+ return
111+ }
87112 }
88113 }
89114 }
0 commit comments