Skip to content

Commit 431337d

Browse files
authored
framework5provider: add a ListResource (#358)
* framework5provider: add ListResource * tidy
1 parent b98ec80 commit 431337d

File tree

5 files changed

+335
-29
lines changed

5 files changed

+335
-29
lines changed

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module github.com/hashicorp/terraform-provider-corner
22

3-
go 1.23.7
3+
go 1.24.4
44

55
require (
66
github.com/hashicorp/go-cty v1.5.0
77
github.com/hashicorp/go-memdb v1.3.5
88
github.com/hashicorp/terraform-json v0.25.0
9-
github.com/hashicorp/terraform-plugin-framework v1.15.0
9+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623191831-9c0fecc9e9ba
1010
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0
1111
github.com/hashicorp/terraform-plugin-framework-nettypes v0.2.0
1212
github.com/hashicorp/terraform-plugin-framework-timeouts v0.5.0
1313
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0
1414
github.com/hashicorp/terraform-plugin-framework-validators v0.18.0
15-
github.com/hashicorp/terraform-plugin-go v0.28.0
15+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea
1616
github.com/hashicorp/terraform-plugin-mux v0.20.0
1717
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0
1818
github.com/hashicorp/terraform-plugin-testing v1.13.2
@@ -43,7 +43,7 @@ require (
4343
github.com/hashicorp/logutils v1.0.0 // indirect
4444
github.com/hashicorp/terraform-exec v0.23.0 // indirect
4545
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
46-
github.com/hashicorp/terraform-registry-address v0.2.5 // indirect
46+
github.com/hashicorp/terraform-registry-address v0.3.0 // indirect
4747
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4848
github.com/hashicorp/yamux v0.1.1 // indirect
4949
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -59,13 +59,13 @@ require (
5959
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
6060
golang.org/x/crypto v0.39.0 // indirect
6161
golang.org/x/mod v0.25.0 // indirect
62-
golang.org/x/net v0.40.0 // indirect
62+
golang.org/x/net v0.41.0 // indirect
6363
golang.org/x/sync v0.15.0 // indirect
6464
golang.org/x/sys v0.33.0 // indirect
6565
golang.org/x/text v0.26.0 // indirect
6666
golang.org/x/tools v0.33.0 // indirect
6767
google.golang.org/appengine v1.6.8 // indirect
68-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
69-
google.golang.org/grpc v1.72.1 // indirect
68+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
69+
google.golang.org/grpc v1.73.0 // indirect
7070
google.golang.org/protobuf v1.36.6 // indirect
7171
)

go.sum

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ github.com/hashicorp/terraform-exec v0.23.0 h1:MUiBM1s0CNlRFsCLJuM5wXZrzA3MnPYEs
8787
github.com/hashicorp/terraform-exec v0.23.0/go.mod h1:mA+qnx1R8eePycfwKkCRk3Wy65mwInvlpAeOwmA7vlY=
8888
github.com/hashicorp/terraform-json v0.25.0 h1:rmNqc/CIfcWawGiwXmRuiXJKEiJu1ntGoxseG1hLhoQ=
8989
github.com/hashicorp/terraform-json v0.25.0/go.mod h1:sMKS8fiRDX4rVlR6EJUMudg1WcanxCMoWwTLkgZP/vc=
90-
github.com/hashicorp/terraform-plugin-framework v1.15.0 h1:LQ2rsOfmDLxcn5EeIwdXFtr03FVsNktbbBci8cOKdb4=
91-
github.com/hashicorp/terraform-plugin-framework v1.15.0/go.mod h1:hxrNI/GY32KPISpWqlCoTLM9JZsGH3CyYlir09bD/fI=
90+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250620205228-114d4787e149 h1:l/UKTqAAHWB0Yn5PH6rtrM6WDo51/U3pIS6wEkEk+zI=
91+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250620205228-114d4787e149/go.mod h1:k9itoOx5BgwpinKyLFKdGM7DMKNR1HD/hNAs3KNE8UQ=
92+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623185604-ce2bb12e640e h1:K2F9zgrAANtp4tjDewo2C70zWSnfyluWHfIF+1OAHdU=
93+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623185604-ce2bb12e640e/go.mod h1:k9itoOx5BgwpinKyLFKdGM7DMKNR1HD/hNAs3KNE8UQ=
94+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623191517-3e5c9f7e0a16 h1:Cwq59Sp4e1qdB7aZrBynWuoa40Q2xKn2ZNUpKnEBeoA=
95+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623191517-3e5c9f7e0a16/go.mod h1:k9itoOx5BgwpinKyLFKdGM7DMKNR1HD/hNAs3KNE8UQ=
96+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623191831-9c0fecc9e9ba h1:QZ61jA090rq6LwpndEjctl2O3Aq6cJo1ugZUsz9b4DQ=
97+
github.com/hashicorp/terraform-plugin-framework v1.15.1-0.20250623191831-9c0fecc9e9ba/go.mod h1:k9itoOx5BgwpinKyLFKdGM7DMKNR1HD/hNAs3KNE8UQ=
9298
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0 h1:SJXL5FfJJm17554Kpt9jFXngdM6fXbnUnZ6iT2IeiYA=
9399
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0/go.mod h1:p0phD0IYhsu9bR4+6OetVvvH59I6LwjXGnTVEr8ox6E=
94100
github.com/hashicorp/terraform-plugin-framework-nettypes v0.2.0 h1:Zap24rkky7SvNGGNYHMKFhAriP6+6riI21BMYOYgLRE=
@@ -99,8 +105,8 @@ github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 h1:v3DapR8gsp3E
99105
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0/go.mod h1:c3PnGE9pHBDfdEVG9t1S1C9ia5LW+gkFR0CygXlM8ak=
100106
github.com/hashicorp/terraform-plugin-framework-validators v0.18.0 h1:OQnlOt98ua//rCw+QhBbSqfW3QbwtVrcdWeQN5gI3Hw=
101107
github.com/hashicorp/terraform-plugin-framework-validators v0.18.0/go.mod h1:lZvZvagw5hsJwuY7mAY6KUz45/U6fiDR0CzQAwWD0CA=
102-
github.com/hashicorp/terraform-plugin-go v0.28.0 h1:zJmu2UDwhVN0J+J20RE5huiF3XXlTYVIleaevHZgKPA=
103-
github.com/hashicorp/terraform-plugin-go v0.28.0/go.mod h1:FDa2Bb3uumkTGSkTFpWSOwWJDwA7bf3vdP3ltLDTH6o=
108+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea h1:U9EAAeQtszGlR7mDS7rY77B/a4/XiMDB8HfAtqLAuAQ=
109+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
104110
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
105111
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
106112
github.com/hashicorp/terraform-plugin-mux v0.20.0 h1:3QpBnI9uCuL0Yy2Rq/kR9cOdmOFNhw88A2GoZtk5aXM=
@@ -109,8 +115,8 @@ github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0 h1:NFPMacTrY/IdcIcnUB+7hsor
109115
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0/go.mod h1:QYmYnLfsosrxjCnGY1p9c7Zj6n9thnEE+7RObeYs3fA=
110116
github.com/hashicorp/terraform-plugin-testing v1.13.2 h1:mSotG4Odl020vRjIenA3rggwo6Kg6XCKIwtRhYgp+/M=
111117
github.com/hashicorp/terraform-plugin-testing v1.13.2/go.mod h1:WHQ9FDdiLoneey2/QHpGM/6SAYf4A7AZazVg7230pLE=
112-
github.com/hashicorp/terraform-registry-address v0.2.5 h1:2GTftHqmUhVOeuu9CW3kwDkRe4pcBDq0uuK5VJngU1M=
113-
github.com/hashicorp/terraform-registry-address v0.2.5/go.mod h1:PpzXWINwB5kuVS5CA7m1+eO2f1jKb5ZDIxrOPfpnGkg=
118+
github.com/hashicorp/terraform-registry-address v0.3.0 h1:HMpK3nqaGFPS9VmgRXrJL/dzHNdheGVKk5k7VlFxzCo=
119+
github.com/hashicorp/terraform-registry-address v0.3.0/go.mod h1:jRGCMiLaY9zii3GLC7hqpSnwhfnCN5yzvY0hh4iCGbM=
114120
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
115121
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
116122
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
@@ -175,16 +181,16 @@ github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6
175181
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
176182
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
177183
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
178-
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
179-
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
180-
go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ=
181-
go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE=
182-
go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A=
183-
go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU=
184-
go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk=
185-
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
186-
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
187-
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
184+
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
185+
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
186+
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
187+
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
188+
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
189+
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
190+
go.opentelemetry.io/otel/sdk/metric v1.35.0 h1:1RriWBmCKgkeHEhM7a2uMjMUfP7MsOF5JpUCaEqEI9o=
191+
go.opentelemetry.io/otel/sdk/metric v1.35.0/go.mod h1:is6XYCUMpcKi+ZsOvfluY5YstFnhW0BidkR+gL+qN+w=
192+
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
193+
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
188194
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
189195
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
190196
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
@@ -196,8 +202,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
196202
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
197203
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
198204
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
199-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
200-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
205+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
206+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
201207
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
202208
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
203209
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -235,10 +241,10 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
235241
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
236242
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
237243
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
238-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a h1:51aaUVRocpvUOSQKM6Q7VuoaktNIaMCLuhZB6DKksq4=
239-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a/go.mod h1:uRxBH1mhmO8PGhU89cMcHaXKZqO+OfakD8QQO0oYwlQ=
240-
google.golang.org/grpc v1.72.1 h1:HR03wO6eyZ7lknl75XlxABNVLLFc2PAb6mHlYh756mA=
241-
google.golang.org/grpc v1.72.1/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
244+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 h1:e0AIkUUhxyBKh6ssZNrAMeqhA7RKUj42346d1y02i2g=
245+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A=
246+
google.golang.org/grpc v1.73.0 h1:VIWSmpI2MegBtTuFt5/JWy2oXxtjJ/e89Z70ImfD2ok=
247+
google.golang.org/grpc v1.73.0/go.mod h1:50sbHOUqWoCQGI8V2HQLJM0B+LMlIUjNSZmow7EVBQc=
242248
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
243249
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
244250
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

Comments
 (0)