Skip to content

Commit 6506c38

Browse files
authored
add default provider value plan modifiers/small utils upgrades to fwprovider templates (#15011)
1 parent 0464191 commit 6506c38

File tree

9 files changed

+358
-21
lines changed

9 files changed

+358
-21
lines changed

mmv1/templates/terraform/resource_fw.go.tmpl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ import (
7474
{{- end}}
7575

7676
var (
77-
_ resource.Resource = &{{$.ResourceName}}FWResource{}
78-
_ resource.ResourceWithConfigure = &{{$.ResourceName}}FWResource{}
77+
_ resource.Resource = &{{$.ResourceName}}FWResource{}
78+
_ resource.ResourceWithConfigure = &{{$.ResourceName}}FWResource{}
79+
{{- if or (and (or $.HasProject $.HasRegion $.HasZone) (not $.ExcludeDefaultCdiff)) $.CustomDiff }}
80+
_ resource.ResourceWithModifyPlan = &{{$.ResourceName}}FWResource{}
81+
{{- end}}
7982
)
8083

8184
func New{{$.ResourceName}}FWResource() resource.Resource {
@@ -128,6 +131,23 @@ func (r *{{$.ResourceName}}FWResource) Configure(ctx context.Context, req resour
128131
r.providerConfig = p
129132
}
130133

134+
{{- if or (and (or $.HasProject $.HasRegion $.HasZone) (not $.ExcludeDefaultCdiff)) $.CustomDiff }}
135+
func (r *{{$.ResourceName}}FWResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
136+
{{- if and ($.HasProject) (not $.ExcludeDefaultCdiff) }}
137+
fwresource.DefaultProjectModify(ctx, req, resp, r.providerConfig.Project)
138+
{{- end -}}
139+
{{if and ($.HasRegion) (not $.ExcludeDefaultCdiff) }}
140+
fwresource.DefaultRegionModify(ctx, req, resp, r.providerConfig.Region)
141+
{{- end -}}
142+
{{if and ($.HasZone) (not $.ExcludeDefaultCdiff) }}
143+
fwresource.DefaultZoneModify(ctx, req, resp, r.providerConfig.Zone)
144+
{{- end }}
145+
if resp.Diagnostics.HasError() {
146+
return
147+
}
148+
}
149+
{{- end}}
150+
131151
func (d *{{$.ResourceName}}FWResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
132152
resp.Schema = schema.Schema{
133153
MarkdownDescription: "A resource to represent a SQL User object.",
@@ -263,7 +283,7 @@ func (r *{{$.ResourceName}}FWResource) Create(ctx context.Context, req resource.
263283
{{- if $.CustomCode.PreCreate }}
264284
{{ $.CustomTemplate $.CustomCode.PreCreate false -}}
265285
{{- end}}
266-
res := fwtransport.SendRequest(fwtransport.SendRequestOptions{
286+
res, err := fwtransport.SendRequest(fwtransport.SendRequestOptions{
267287
Config: r.providerConfig,
268288
Method: "{{ upper $.CreateVerb -}}",
269289
Project: billingProject.ValueString(),
@@ -483,7 +503,7 @@ func (r *{{$.ResourceName}}FWResource) Update(ctx context.Context, req resource.
483503
{{- if $.CustomCode.PreUpdate }}
484504
{{ $.CustomTemplate $.CustomCode.PreUpdate false -}}
485505
{{- end}}
486-
res := fwtransport.SendRequest(fwtransport.SendRequestOptions{
506+
res, err := fwtransport.SendRequest(fwtransport.SendRequestOptions{
487507
Config: r.providerConfig,
488508
Method: "{{ upper $.UpdateVerb -}}",
489509
Project: billingProject.ValueString(),
@@ -622,7 +642,7 @@ func (r *{{$.ResourceName}}FWResource) Delete(ctx context.Context, req resource.
622642
{{- end }}
623643

624644
log.Printf("[DEBUG] Deleting {{ $.Name }} %q", data.Id.ValueString())
625-
res := fwtransport.SendRequest(fwtransport.SendRequestOptions{
645+
_, _ := fwtransport.SendRequest(fwtransport.SendRequestOptions{
626646
Config: r.providerConfig,
627647
Method: "{{ upper $.DeleteVerb -}}",
628648
Project: billingProject.ValueString(),
@@ -727,7 +747,7 @@ func (r *{{$.ResourceName}}FWResource) {{$.ResourceName}}FWRefresh(ctx context.C
727747
{{- if $.CustomCode.PreRead }}
728748
{{ $.CustomTemplate $.CustomCode.PreRead false -}}
729749
{{- end}}
730-
res := fwtransport.SendRequest(fwtransport.SendRequestOptions{
750+
res, err := fwtransport.SendRequest(fwtransport.SendRequestOptions{
731751
Config: r.providerConfig,
732752
Method: "{{ upper $.ReadVerb -}}",
733753
Project: billingProject.ValueString(),

mmv1/third_party/terraform/fwresource/field_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func getProviderDefaultFromFrameworkSchema(schemaField string, rVal, pVal types.
3737
return pVal
3838
}
3939

40-
diags.AddError("required field is not set", fmt.Sprintf("%s is not set", schemaField))
40+
diags.AddError("required field is not set", fmt.Sprintf("%s must be set in at least one of Terraform resource configuration, Terraform provider configuration, or environment variables.", schemaField))
4141
return types.String{}
4242
}
4343

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package fwresource
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
)
10+
11+
func DefaultProjectModify(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse, providerConfigProject string) {
12+
var old types.String
13+
diags := req.State.GetAttribute(ctx, path.Root("project"), &old)
14+
resp.Diagnostics.Append(diags...)
15+
if resp.Diagnostics.HasError() {
16+
return
17+
}
18+
19+
var new types.String
20+
diags = req.Plan.GetAttribute(ctx, path.Root("project"), &new)
21+
resp.Diagnostics.Append(diags...)
22+
if resp.Diagnostics.HasError() {
23+
return
24+
}
25+
26+
if (old.IsUnknown() || old.IsNull()) && new.IsUnknown() {
27+
project := GetProjectFramework(new, types.StringValue(providerConfigProject), &resp.Diagnostics)
28+
if resp.Diagnostics.HasError() {
29+
return
30+
}
31+
resp.Plan.SetAttribute(ctx, path.Root("project"), project)
32+
}
33+
return
34+
}
35+
36+
func DefaultRegionModify(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse, providerConfigRegion string) {
37+
var old types.String
38+
diags := req.State.GetAttribute(ctx, path.Root("region"), &old)
39+
resp.Diagnostics.Append(diags...)
40+
if resp.Diagnostics.HasError() {
41+
return
42+
}
43+
44+
var new types.String
45+
diags = req.Plan.GetAttribute(ctx, path.Root("region"), &new)
46+
resp.Diagnostics.Append(diags...)
47+
if resp.Diagnostics.HasError() {
48+
return
49+
}
50+
51+
if (old.IsUnknown() || old.IsNull()) && new.IsUnknown() {
52+
region := GetRegionFramework(new, types.StringValue(providerConfigRegion), &resp.Diagnostics)
53+
if resp.Diagnostics.HasError() {
54+
return
55+
}
56+
resp.Plan.SetAttribute(ctx, path.Root("region"), region)
57+
}
58+
return
59+
}
60+
61+
func DefaultZoneModify(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse, providerConfigZone string) {
62+
var old types.String
63+
diags := req.State.GetAttribute(ctx, path.Root("zone"), &old)
64+
resp.Diagnostics.Append(diags...)
65+
if resp.Diagnostics.HasError() {
66+
return
67+
}
68+
69+
var new types.String
70+
diags = req.Plan.GetAttribute(ctx, path.Root("zone"), &new)
71+
resp.Diagnostics.Append(diags...)
72+
if resp.Diagnostics.HasError() {
73+
return
74+
}
75+
76+
if (old.IsUnknown() || old.IsNull()) && new.IsUnknown() {
77+
zone := GetZoneFramework(new, types.StringValue(providerConfigZone), &resp.Diagnostics)
78+
if resp.Diagnostics.HasError() {
79+
return
80+
}
81+
resp.Plan.SetAttribute(ctx, path.Root("zone"), zone)
82+
}
83+
return
84+
}

0 commit comments

Comments
 (0)