@@ -5,14 +5,15 @@ package fwserver
55
66import (
77 "context"
8- "github.com/hashicorp/terraform-plugin-go/tftypes"
98 "iter"
109
1110 "github.com/hashicorp/terraform-plugin-framework/diag"
1211 "github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1312 "github.com/hashicorp/terraform-plugin-framework/internal/logging"
1413 "github.com/hashicorp/terraform-plugin-framework/list"
14+ "github.com/hashicorp/terraform-plugin-framework/resource"
1515 "github.com/hashicorp/terraform-plugin-framework/tfsdk"
16+ "github.com/hashicorp/terraform-plugin-go/tftypes"
1617)
1718
1819// ListRequest is the framework server request for the ListResource RPC.
@@ -99,6 +100,47 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
99100 }
100101 }
101102
103+ // TODO verdict is still out on how to handle diagnostics that pertain to the List call as a whole and not individual list results
104+ diagsStream := & list.ListResultsStream {}
105+
106+ if listResourceWithConfigure , ok := listResource .(list.ListResourceWithConfigure ); ok {
107+ logging .FrameworkTrace (ctx , "ListResource implements ListResourceWithConfigure" )
108+
109+ configureReq := resource.ConfigureRequest {
110+ ProviderData : s .ListResourceConfigureData ,
111+ }
112+
113+ configureResp := resource.ConfigureResponse {}
114+
115+ logging .FrameworkTrace (ctx , "Called provider defined ListResource Configure" )
116+ listResourceWithConfigure .Configure (ctx , configureReq , & configureResp )
117+ logging .FrameworkTrace (ctx , "Called provider defined ListResource Configure" )
118+
119+ if len (configureResp .Diagnostics ) > 0 {
120+ diagsResp := list.ListResult {}
121+
122+ diagsResp .Diagnostics .Append (configureResp .Diagnostics ... )
123+
124+ // Captures any diags from the Configure call
125+ diagsStream .Results = func (push func (list.ListResult ) bool ) {
126+ if ! push (diagsResp ) {
127+ return
128+ }
129+ }
130+
131+ if diagsResp .Diagnostics .HasError () {
132+ fwStream .Results = func (push func (ListResult ) bool ) {
133+ for result := range diagsStream .Results {
134+ if ! push (ListResult (result )) {
135+ return
136+ }
137+ }
138+ }
139+ return
140+ }
141+ }
142+ }
143+
102144 req := list.ListRequest {
103145 Config : * fwReq .Config ,
104146 IncludeResource : fwReq .IncludeResource ,
@@ -118,14 +160,20 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
118160 stream .Results = list .NoListResults
119161 }
120162
121- fwStream .Results = processListResults (req , stream .Results )
163+ if diagsStream .Results == nil {
164+ diagsStream .Results = list .NoListResults
165+ }
166+
167+ fwStream .Results = processListResults (req , stream .Results , diagsStream .Results )
122168}
123169
124- func processListResults (req list.ListRequest , stream iter.Seq [list.ListResult ]) iter.Seq [ListResult ] {
170+ func processListResults (req list.ListRequest , streams ... iter.Seq [list.ListResult ]) iter.Seq [ListResult ] {
125171 return func (push func (ListResult ) bool ) {
126- for result := range stream {
127- if ! push (processListResult (req , result )) {
128- return
172+ for _ , stream := range streams {
173+ for result := range stream {
174+ if ! push (processListResult (req , result )) {
175+ return
176+ }
129177 }
130178 }
131179 }
@@ -138,6 +186,11 @@ func processListResult(req list.ListRequest, result list.ListResult) ListResult
138186 return ListResult (result )
139187 }
140188
189+ // Allow any non-error diags to pass through
190+ if len (result .Diagnostics ) > 0 && result .DisplayName == "" && result .Identity == nil && result .Resource == nil {
191+ return ListResult (result )
192+ }
193+
141194 if result .Identity == nil || result .Identity .Raw .IsNull () {
142195 return ListResultError (
143196 "Incomplete List Result" ,
0 commit comments