Skip to content

Commit 3e114d4

Browse files
authored
PF: import function and timeouts (#15145)
1 parent c879c95 commit 3e114d4

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

mmv1/templates/terraform/resource_fw.go.tmpl

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ func (d *{{$.ResourceName}}FWResource) Schema(ctx context.Context, _ resource.Sc
189189
stringplanmodifier.UseStateForUnknown(),
190190
},
191191
},
192+
"timeouts": timeouts.Block(ctx, timeouts.Opts{
193+
Create: true,
194+
{{- if or $.Updatable $.RootLabels }}
195+
Update: true,
196+
{{- end }}
197+
Delete: true,
198+
}),
192199
},
193200
}
194201
}
@@ -262,9 +269,7 @@ func (r *{{$.ResourceName}}FWResource) Create(ctx context.Context, req resource.
262269
obj["{{ $prop.ApiName -}}"] = {{ $prop.ApiName -}}Prop
263270
{{- end }}
264271

265-
266-
{{/* TODO default timeouts */}}
267-
createTimeout, diags := data.Timeouts.Create(ctx, 20*time.Minute)
272+
createTimeout, diags := data.Timeouts.Create(ctx, {{ $.Timeouts.InsertMinutes }}*time.Minute)
268273
resp.Diagnostics.Append(diags...)
269274
if resp.Diagnostics.HasError() {
270275
return
@@ -480,8 +485,7 @@ func (r *{{$.ResourceName}}FWResource) Update(ctx context.Context, req resource.
480485
}
481486
{{- end }}
482487

483-
{{/* TODO default timeouts */}}
484-
updateTimeout, diags := data.Timeouts.Update(ctx, 20*time.Minute)
488+
updateTimeout, diags := data.Timeouts.Update(ctx, {{ $.Timeouts.UpdateMinutes }}*time.Minute)
485489
resp.Diagnostics.Append(diags...)
486490
if resp.Diagnostics.HasError() {
487491
return
@@ -622,7 +626,7 @@ func (r *{{$.ResourceName}}FWResource) Delete(ctx context.Context, req resource.
622626

623627
obj := make(map[string]interface{})
624628

625-
deleteTimeout, diags := data.Timeouts.Delete(ctx, 20*time.Minute)
629+
deleteTimeout, diags := data.Timeouts.Delete(ctx, {{ $.Timeouts.DeleteMinutes }}*time.Minute)
626630
resp.Diagnostics.Append(diags...)
627631
if resp.Diagnostics.HasError() {
628632
return
@@ -697,7 +701,28 @@ func (r *{{$.ResourceName}}FWResource) Delete(ctx context.Context, req resource.
697701
}
698702

699703
func (r *{{$.ResourceName}}FWResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
700-
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
704+
patterns := []string{
705+
{{- range $id := $.ImportIdFormatsFromResource }}
706+
"^{{ format2regex $id }}$",
707+
{{- end }}
708+
}
709+
710+
var resourceSchemaResp resource.SchemaResponse
711+
r.Schema(ctx, resource.SchemaRequest{}, &resourceSchemaResp)
712+
if resourceSchemaResp.Diagnostics.HasError() {
713+
resp.Diagnostics.Append(resourceSchemaResp.Diagnostics...)
714+
return
715+
}
716+
717+
parsed, diags := fwresource.ParseImportId(ctx, req, resourceSchemaResp.Schema, r.providerConfig, patterns)
718+
resp.Diagnostics.Append(diags...)
719+
if resp.Diagnostics.HasError() {
720+
return
721+
}
722+
723+
for name, value := range parsed {
724+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root(name), value)...)
725+
}
701726
}
702727

703728
func (r *{{$.ResourceName}}FWResource) {{$.ResourceName}}FWRefresh(ctx context.Context, data *{{$.ResourceName}}FWModel, state *tfsdk.State, req interface{}, diag *diag.Diagnostics) {

0 commit comments

Comments
 (0)