Skip to content

Commit e472964

Browse files
committed
Refactor test provider functions
1 parent 90f5656 commit e472964

File tree

2 files changed

+76
-124
lines changed

2 files changed

+76
-124
lines changed

helper/resource/importstate/examplecloud_test.go

Lines changed: 43 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ func examplecloudDataSource() testprovider.DataSource {
3131
Schema: &tfprotov6.Schema{
3232
Block: &tfprotov6.SchemaBlock{
3333
Attributes: []*tfprotov6.SchemaAttribute{
34-
{
35-
Name: "id",
36-
Type: tftypes.String,
37-
Computed: true,
38-
},
34+
ComputedStringAttribute("id"),
3935
},
4036
},
4137
},
@@ -97,21 +93,9 @@ func examplecloudResource() testprovider.Resource {
9793
Schema: &tfprotov6.Schema{
9894
Block: &tfprotov6.SchemaBlock{
9995
Attributes: []*tfprotov6.SchemaAttribute{
100-
{
101-
Name: "id",
102-
Type: tftypes.String,
103-
Computed: true,
104-
},
105-
{
106-
Name: "location",
107-
Type: tftypes.String,
108-
Required: true,
109-
},
110-
{
111-
Name: "name",
112-
Type: tftypes.String,
113-
Required: true,
114-
},
96+
ComputedStringAttribute("id"),
97+
RequiredStringAttribute("location"),
98+
RequiredStringAttribute("name"),
11599
},
116100
},
117101
},
@@ -121,63 +105,35 @@ func examplecloudResource() testprovider.Resource {
121105

122106
// examplecloudZone is a test resource that mimics a DNS zone resource.
123107
func examplecloudZone() testprovider.Resource {
108+
value := tftypes.NewValue(
109+
tftypes.Object{
110+
AttributeTypes: map[string]tftypes.Type{
111+
"id": tftypes.String,
112+
"name": tftypes.String,
113+
},
114+
},
115+
map[string]tftypes.Value{
116+
"id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
117+
"name": tftypes.NewValue(tftypes.String, "example.net"),
118+
},
119+
)
120+
124121
return testprovider.Resource{
125122
CreateResponse: &resource.CreateResponse{
126-
NewState: tftypes.NewValue(
127-
tftypes.Object{
128-
AttributeTypes: map[string]tftypes.Type{
129-
"id": tftypes.String,
130-
"name": tftypes.String,
131-
},
132-
},
133-
map[string]tftypes.Value{
134-
"id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
135-
"name": tftypes.NewValue(tftypes.String, "example.net"),
136-
},
137-
),
123+
NewState: value,
138124
},
139125
ReadResponse: &resource.ReadResponse{
140-
NewState: tftypes.NewValue(
141-
tftypes.Object{
142-
AttributeTypes: map[string]tftypes.Type{
143-
"id": tftypes.String,
144-
"name": tftypes.String,
145-
},
146-
},
147-
map[string]tftypes.Value{
148-
"id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
149-
"name": tftypes.NewValue(tftypes.String, "example.net"),
150-
},
151-
),
126+
NewState: value,
152127
},
153128
ImportStateResponse: &resource.ImportStateResponse{
154-
State: tftypes.NewValue(
155-
tftypes.Object{
156-
AttributeTypes: map[string]tftypes.Type{
157-
"id": tftypes.String,
158-
"name": tftypes.String,
159-
},
160-
},
161-
map[string]tftypes.Value{
162-
"id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
163-
"name": tftypes.NewValue(tftypes.String, "example.net"),
164-
},
165-
),
129+
State: value,
166130
},
167131
SchemaResponse: &resource.SchemaResponse{
168132
Schema: &tfprotov6.Schema{
169133
Block: &tfprotov6.SchemaBlock{
170134
Attributes: []*tfprotov6.SchemaAttribute{
171-
{
172-
Name: "id",
173-
Type: tftypes.String,
174-
Computed: true,
175-
},
176-
{
177-
Name: "name",
178-
Type: tftypes.String,
179-
Required: true,
180-
},
135+
ComputedStringAttribute("id"),
136+
RequiredStringAttribute("name"),
181137
},
182138
},
183139
},
@@ -189,80 +145,43 @@ func examplecloudZone() testprovider.Resource {
189145
// It models a resource dependency; specifically, it depends on a DNS zone ID and will
190146
// plan a replacement if the zone ID changes.
191147
func examplecloudZoneRecord() testprovider.Resource {
148+
value := tftypes.NewValue(
149+
tftypes.Object{
150+
AttributeTypes: map[string]tftypes.Type{
151+
"id": tftypes.String,
152+
"zone_id": tftypes.String,
153+
"name": tftypes.String,
154+
},
155+
},
156+
map[string]tftypes.Value{
157+
"id": tftypes.NewValue(tftypes.String, "f00911be-e188-433d-9ccd-d0393a9f5d05"),
158+
"zone_id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
159+
"name": tftypes.NewValue(tftypes.String, "www"),
160+
},
161+
)
162+
192163
return testprovider.Resource{
193164
CreateResponse: &resource.CreateResponse{
194-
NewState: tftypes.NewValue(
195-
tftypes.Object{
196-
AttributeTypes: map[string]tftypes.Type{
197-
"id": tftypes.String,
198-
"zone_id": tftypes.String,
199-
"name": tftypes.String,
200-
},
201-
},
202-
map[string]tftypes.Value{
203-
"id": tftypes.NewValue(tftypes.String, "f00911be-e188-433d-9ccd-d0393a9f5d05"),
204-
"zone_id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
205-
"name": tftypes.NewValue(tftypes.String, "www"),
206-
},
207-
),
165+
NewState: value,
208166
},
209-
210167
PlanChangeFunc: func(ctx context.Context, req resource.PlanChangeRequest, resp *resource.PlanChangeResponse) {
211168
resp.RequiresReplace = []*tftypes.AttributePath{
212169
tftypes.NewAttributePath().WithAttributeName("zone_id"),
213170
}
214171
},
215172
ReadResponse: &resource.ReadResponse{
216-
NewState: tftypes.NewValue(
217-
tftypes.Object{
218-
AttributeTypes: map[string]tftypes.Type{
219-
"id": tftypes.String,
220-
"zone_id": tftypes.String,
221-
"name": tftypes.String,
222-
},
223-
},
224-
map[string]tftypes.Value{
225-
"id": tftypes.NewValue(tftypes.String, "f00911be-e188-433d-9ccd-d0393a9f5d05"),
226-
"zone_id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
227-
"name": tftypes.NewValue(tftypes.String, "www"),
228-
},
229-
),
173+
NewState: value,
230174
},
231175
ImportStateResponse: &resource.ImportStateResponse{
232-
State: tftypes.NewValue(
233-
tftypes.Object{
234-
AttributeTypes: map[string]tftypes.Type{
235-
"id": tftypes.String,
236-
"zone_id": tftypes.String,
237-
"name": tftypes.String,
238-
},
239-
},
240-
map[string]tftypes.Value{
241-
"id": tftypes.NewValue(tftypes.String, "f00911be-e188-433d-9ccd-d0393a9f5d05"),
242-
"zone_id": tftypes.NewValue(tftypes.String, "5381dd14-6d75-4f32-9096-47f5500b1507"),
243-
"name": tftypes.NewValue(tftypes.String, "www"),
244-
},
245-
),
176+
State: value,
246177
},
247178
SchemaResponse: &resource.SchemaResponse{
248179
Schema: &tfprotov6.Schema{
249180
Block: &tfprotov6.SchemaBlock{
250181
Attributes: []*tfprotov6.SchemaAttribute{
251-
{
252-
Name: "id",
253-
Type: tftypes.String,
254-
Computed: true,
255-
},
256-
{
257-
Name: "zone_id",
258-
Type: tftypes.String,
259-
Required: true,
260-
},
261-
{
262-
Name: "name",
263-
Type: tftypes.String,
264-
Required: true,
265-
},
182+
ComputedStringAttribute("id"),
183+
RequiredStringAttribute("zone_id"),
184+
RequiredStringAttribute("name"),
266185
},
267186
},
268187
},
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package importstate_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
)
10+
11+
func ComputedStringAttribute(name string) *tfprotov6.SchemaAttribute {
12+
return &tfprotov6.SchemaAttribute{
13+
Name: name,
14+
Type: tftypes.String,
15+
Computed: true,
16+
}
17+
}
18+
19+
func OptionalStringAttribute(name string) *tfprotov6.SchemaAttribute {
20+
return &tfprotov6.SchemaAttribute{
21+
Name: name,
22+
Type: tftypes.String,
23+
Optional: true,
24+
}
25+
}
26+
27+
func RequiredStringAttribute(name string) *tfprotov6.SchemaAttribute {
28+
return &tfprotov6.SchemaAttribute{
29+
Name: name,
30+
Type: tftypes.String,
31+
Required: true,
32+
}
33+
}

0 commit comments

Comments
 (0)