|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fwserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "iter" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 16 | +) |
| 17 | + |
| 18 | +// ListRequest is the framework server request for the ListResource RPC. |
| 19 | +type ListRequest struct { |
| 20 | + // ListResource is an instance of the provider's list resource |
| 21 | + // implementation for a specific managed resource type. |
| 22 | + ListResource list.ListResource |
| 23 | + |
| 24 | + // Config is the configuration the user supplied for listing resource |
| 25 | + // instances. |
| 26 | + Config *tfsdk.Config |
| 27 | + |
| 28 | + // IncludeResource indicates whether the provider should populate the |
| 29 | + // Resource field in the ListResult struct. |
| 30 | + IncludeResource bool |
| 31 | + |
| 32 | + ResourceSchema fwschema.Schema |
| 33 | + ResourceIdentitySchema fwschema.Schema |
| 34 | +} |
| 35 | + |
| 36 | +// ListResultsStream represents a streaming response to a [ListRequest]. An |
| 37 | +// instance of this struct is supplied as an argument to the provider's List |
| 38 | +// function. The provider should set a Results iterator function that pushes |
| 39 | +// zero or more results of type [ListResult]. |
| 40 | +// |
| 41 | +// For convenience, a provider implementation may choose to convert a slice of |
| 42 | +// results into an iterator using [slices.Values]. |
| 43 | +type ListResultsStream struct { |
| 44 | + // Results is a function that emits [ListResult] values via its push |
| 45 | + // function argument. |
| 46 | + Results iter.Seq[ListResult] |
| 47 | +} |
| 48 | + |
| 49 | +func ListResultError(summary string, detail string) ListResult { |
| 50 | + return ListResult{ |
| 51 | + Diagnostics: diag.Diagnostics{ |
| 52 | + diag.NewErrorDiagnostic(summary, detail), |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// ListResult represents a listed managed resource instance. |
| 58 | +type ListResult struct { |
| 59 | + // Identity is the identity of the managed resource instance. A nil value |
| 60 | + // will raise will raise a diagnostic. |
| 61 | + Identity *tfsdk.ResourceIdentity |
| 62 | + |
| 63 | + // Resource is the provider's representation of the attributes of the |
| 64 | + // listed managed resource instance. |
| 65 | + // |
| 66 | + // If [ListRequest.IncludeResource] is true, a nil value will raise |
| 67 | + // a warning diagnostic. |
| 68 | + Resource *tfsdk.Resource |
| 69 | + |
| 70 | + // DisplayName is a provider-defined human-readable description of the |
| 71 | + // listed managed resource instance, intended for CLI and browser UIs. |
| 72 | + DisplayName string |
| 73 | + |
| 74 | + // Diagnostics report errors or warnings related to the listed managed |
| 75 | + // resource instance. An empty slice indicates a successful operation with |
| 76 | + // no warnings or errors generated. |
| 77 | + Diagnostics diag.Diagnostics |
| 78 | +} |
| 79 | + |
| 80 | +// ListResource implements the framework server ListResource RPC. |
| 81 | +func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream *ListResultsStream) error { |
| 82 | + listResource := fwReq.ListResource |
| 83 | + |
| 84 | + if fwReq.Config == nil { |
| 85 | + return errors.New("Invalid ListResource request: Config cannot be nil") |
| 86 | + } |
| 87 | + |
| 88 | + req := list.ListRequest{ |
| 89 | + Config: *fwReq.Config, |
| 90 | + IncludeResource: fwReq.IncludeResource, |
| 91 | + ResourceSchema: fwReq.ResourceSchema, // TODO: revisit |
| 92 | + ResourceIdentitySchema: fwReq.ResourceIdentitySchema, // TODO: revisit |
| 93 | + } |
| 94 | + |
| 95 | + stream := &list.ListResultsStream{} |
| 96 | + |
| 97 | + logging.FrameworkTrace(ctx, "Calling provider defined ListResource") |
| 98 | + listResource.List(ctx, req, stream) |
| 99 | + logging.FrameworkTrace(ctx, "Called provider defined ListResource") |
| 100 | + |
| 101 | + // If the provider returned a nil results stream, we return an empty stream. |
| 102 | + if stream.Results == nil { |
| 103 | + stream.Results = list.NoListResults |
| 104 | + } |
| 105 | + |
| 106 | + fwStream.Results = processListResults(req, stream.Results) |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func processListResults(req list.ListRequest, stream iter.Seq[list.ListResult]) iter.Seq[ListResult] { |
| 111 | + return func(push func(ListResult) bool) { |
| 112 | + for result := range stream { |
| 113 | + if !push(processListResult(req, result)) { |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// processListResult validates the content of a list.ListResult and returns a |
| 121 | +// ListResult |
| 122 | +func processListResult(req list.ListRequest, result list.ListResult) ListResult { |
| 123 | + if result.Diagnostics.HasError() { |
| 124 | + return ListResult(result) |
| 125 | + } |
| 126 | + |
| 127 | + if result.Identity == nil { // TODO: is result.Identity.Raw.IsNull() a practical concern? |
| 128 | + return ListResultError( |
| 129 | + "Incomplete List Result", |
| 130 | + "The provider did not populate the Identity field in the ListResourceResult. This may be due to an error in the provider's implementation.", |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + if req.IncludeResource { |
| 135 | + if result.Resource == nil { // TODO: is result.Resource.Raw.IsNull() a practical concern? |
| 136 | + result.Resource = nil |
| 137 | + result.Diagnostics.AddWarning( |
| 138 | + "Incomplete List Result", |
| 139 | + "The provider did not populate the Resource field in the ListResourceResult. This may be due to the provider not supporting this functionality or an error in the provider's implementation.", |
| 140 | + ) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return ListResult(result) // TODO: do we need to .Copy() the raw Identity and Resource values? |
| 145 | +} |
0 commit comments