|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 12 | + listschema "github.com/hashicorp/terraform-plugin-framework/list/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | +) |
| 21 | + |
| 22 | +var _ resource.Resource = ListResource{} |
| 23 | +var _ resource.ResourceWithIdentity = ListResource{} |
| 24 | +var _ resource.ResourceWithImportState = ListResource{} |
| 25 | +var _ list.ListResource = ListResource{} |
| 26 | + |
| 27 | +func NewListResource() resource.Resource { |
| 28 | + return &ListResource{} |
| 29 | +} |
| 30 | + |
| 31 | +func NewListResourceAsListResource() list.ListResource { |
| 32 | + return &ListResource{} |
| 33 | +} |
| 34 | + |
| 35 | +type ListResource struct{} |
| 36 | + |
| 37 | +type ComputeInstanceResource struct { |
| 38 | + ComputeInstanceIdentity |
| 39 | + Name types.String `tfsdk:"name"` |
| 40 | +} |
| 41 | + |
| 42 | +type ComputeInstanceIdentity struct { |
| 43 | + ID types.String `tfsdk:"id"` |
| 44 | +} |
| 45 | + |
| 46 | +type ComputeInstanceListResource struct { |
| 47 | + Filter types.String `tfsdk:"filter"` |
| 48 | +} |
| 49 | + |
| 50 | +var identities = map[string]ComputeInstanceIdentity{ |
| 51 | + "plateau": {ID: types.StringValue("id-001")}, |
| 52 | + "platinum": {ID: types.StringValue("id-002")}, |
| 53 | + "platypus": {ID: types.StringValue("id-003")}, |
| 54 | + "bookworm": {ID: types.StringValue("id-004")}, |
| 55 | + "bookshelf": {ID: types.StringValue("id-005")}, |
| 56 | + "bookmark": {ID: types.StringValue("id-006")}, |
| 57 | +} |
| 58 | + |
| 59 | +var items = map[string]ComputeInstanceResource{ |
| 60 | + "plateau": {ComputeInstanceIdentity: identities["plateau"], Name: types.StringValue("plateau")}, |
| 61 | + "platinum": {ComputeInstanceIdentity: identities["platinum"], Name: types.StringValue("platinum")}, |
| 62 | + "platypus": {ComputeInstanceIdentity: identities["platypus"], Name: types.StringValue("platypus")}, |
| 63 | + "bookworm": {ComputeInstanceIdentity: identities["bookworm"], Name: types.StringValue("bookworm")}, |
| 64 | + "bookshelf": {ComputeInstanceIdentity: identities["bookshelf"], Name: types.StringValue("bookshelf")}, |
| 65 | + "bookmark": {ComputeInstanceIdentity: identities["bookmark"], Name: types.StringValue("bookmark")}, |
| 66 | +} |
| 67 | + |
| 68 | +func (r ListResource) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { |
| 69 | + resp.IdentitySchema = identityschema.Schema{ |
| 70 | + Attributes: map[string]identityschema.Attribute{ |
| 71 | + "id": identityschema.StringAttribute{ |
| 72 | + RequiredForImport: true, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (r ListResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 79 | + resp.TypeName = req.ProviderTypeName + "_list_resource" |
| 80 | +} |
| 81 | + |
| 82 | +func (r ListResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 83 | + resp.Schema = schema.Schema{ |
| 84 | + Attributes: map[string]schema.Attribute{ |
| 85 | + "id": schema.StringAttribute{ |
| 86 | + Computed: true, |
| 87 | + PlanModifiers: []planmodifier.String{ |
| 88 | + stringplanmodifier.UseStateForUnknown(), |
| 89 | + }, |
| 90 | + }, |
| 91 | + "name": schema.StringAttribute{ |
| 92 | + Required: true, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (r ListResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 99 | + resource.ImportStatePassthroughWithIdentity(ctx, path.Root("id"), path.Root("id"), req, resp) |
| 100 | +} |
| 101 | + |
| 102 | +func (r ListResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 103 | + var data ComputeInstanceResource |
| 104 | + |
| 105 | + diags := req.Plan.Get(ctx, &data) |
| 106 | + resp.Diagnostics.Append(diags...) |
| 107 | + if resp.Diagnostics.HasError() { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + data.ID = types.StringValue("id-123") |
| 112 | + diags = resp.State.Set(ctx, &data) |
| 113 | + resp.Diagnostics.Append(diags...) |
| 114 | + |
| 115 | + diags = resp.Identity.Set(ctx, data.ComputeInstanceIdentity) |
| 116 | + resp.Diagnostics.Append(diags...) |
| 117 | +} |
| 118 | + |
| 119 | +func (r ListResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 120 | + var data ComputeInstanceResource |
| 121 | + |
| 122 | + diags := req.State.Get(ctx, &data) |
| 123 | + resp.Diagnostics.Append(diags...) |
| 124 | + if resp.Diagnostics.HasError() { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + if data.ID.ValueString() != "id-123" { |
| 129 | + resp.Diagnostics.AddAttributeError( |
| 130 | + path.Root("id"), "Unexpected ID value", fmt.Sprintf("Expected ID to be \"id-123\", got: %s", data.ID.String()), |
| 131 | + ) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + data.Name = types.StringValue("platform") |
| 136 | + diags = resp.State.Set(ctx, &data) |
| 137 | + resp.Diagnostics.Append(diags...) |
| 138 | + |
| 139 | + diags = resp.Identity.Set(ctx, data.ComputeInstanceIdentity) |
| 140 | + resp.Diagnostics.Append(diags...) |
| 141 | +} |
| 142 | + |
| 143 | +func (r ListResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 144 | + var data ComputeInstanceResource |
| 145 | + |
| 146 | + diags := req.Plan.Get(ctx, &data) |
| 147 | + resp.Diagnostics.Append(diags...) |
| 148 | + if resp.Diagnostics.HasError() { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + diags = resp.State.Set(ctx, &data) |
| 153 | + resp.Diagnostics.Append(diags...) |
| 154 | +} |
| 155 | + |
| 156 | +func (r ListResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 157 | +} |
| 158 | + |
| 159 | +func (r ListResource) ListResourceConfigSchema(_ context.Context, _ list.ListResourceSchemaRequest, resp *list.ListResourceSchemaResponse) { |
| 160 | + resp.Schema = listschema.Schema{ |
| 161 | + Attributes: map[string]listschema.Attribute{ |
| 162 | + "filter": schema.StringAttribute{ |
| 163 | + Required: true, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func (r ListResource) List(ctx context.Context, req list.ListRequest, stream *list.ListResultsStream) { |
| 170 | + var data ComputeInstanceListResource |
| 171 | + |
| 172 | + diags := req.Config.Get(ctx, &data) |
| 173 | + if diags.HasError() { |
| 174 | + stream.Results = list.ListResultsStreamDiagnostics(diags) |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + stream.Results = func(push func(list.ListResult) bool) { |
| 179 | + for name, item := range items { |
| 180 | + if !strings.HasPrefix(name, data.Filter.ValueString()) { |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + result := req.NewListResult() |
| 185 | + result.DisplayName = item.Name.ValueString() |
| 186 | + |
| 187 | + if diags := result.Resource.Set(ctx, item); diags.HasError() { |
| 188 | + result.Diagnostics.Append(diags...) |
| 189 | + } |
| 190 | + |
| 191 | + if diags := result.Identity.Set(ctx, identities[name]); diags.HasError() { |
| 192 | + result.Diagnostics.Append(diags...) |
| 193 | + } |
| 194 | + |
| 195 | + if result.Diagnostics.HasError() { |
| 196 | + result = list.ListResult{Diagnostics: result.Diagnostics} |
| 197 | + } |
| 198 | + |
| 199 | + if !push(result) { |
| 200 | + return |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments