From 5887f5eaf5d714d92ce67d9b5fccaa95db08d0bb Mon Sep 17 00:00:00 2001 From: kuba-mazurkiewicz Date: Mon, 3 Mar 2025 10:25:23 +0100 Subject: [PATCH] add reprovision option to fabric_provision_device --- CHANGELOG.md | 4 ++++ docs/data-sources/fabric_provision_device.md | 1 + docs/guides/changelog.md | 4 ++++ docs/resources/fabric_provision_device.md | 4 ++++ gen/definitions/fabric_provision_device.yaml | 5 +++++ gen/templates/model.go | 4 ++++ gen/templates/resource.go | 15 +++++++++++++ ..._catalystcenter_fabric_provision_device.go | 4 ++++ ..._catalystcenter_fabric_provision_device.go | 17 +++++++++++++++ ..._catalystcenter_fabric_provision_device.go | 21 ++++++++++++------- templates/guides/changelog.md.tmpl | 4 ++++ 11 files changed, 76 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12c1b022..3b24edb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.1 (unreleased) + +- Add `reprovision` attribute to `catalystcenter_fabric_provision_device` + ## 0.2.0 - Fix issue with security group name in `catalystcenter_anycast_gateway` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/179) diff --git a/docs/data-sources/fabric_provision_device.md b/docs/data-sources/fabric_provision_device.md index 8dd71a0c..e8ffd888 100644 --- a/docs/data-sources/fabric_provision_device.md +++ b/docs/data-sources/fabric_provision_device.md @@ -30,3 +30,4 @@ data "catalystcenter_fabric_provision_device" "example" { ### Read-Only - `id` (String) The id of the object +- `reprovision` (Boolean) Flag to indicate whether the device should be reprovisioned. If set to `true`, reprovisioning will be triggered on every Terraform apply diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 06b11412..6f40c1da 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.2.1 (unreleased) + +- Add `reprovision` attribute to `catalystcenter_fabric_provision_device` + ## 0.2.0 - Fix issue with security group name in `catalystcenter_anycast_gateway` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/179) diff --git a/docs/resources/fabric_provision_device.md b/docs/resources/fabric_provision_device.md index 63a085b2..eb9d636e 100644 --- a/docs/resources/fabric_provision_device.md +++ b/docs/resources/fabric_provision_device.md @@ -27,6 +27,10 @@ resource "catalystcenter_fabric_provision_device" "example" { - `network_device_id` (String) ID of network device to be provisioned - `site_id` (String) ID of the site this network device needs to be provisioned +### Optional + +- `reprovision` (Boolean) Flag to indicate whether the device should be reprovisioned. If set to `true`, reprovisioning will be triggered on every Terraform apply + ### Read-Only - `id` (String) The id of the object diff --git a/gen/definitions/fabric_provision_device.yaml b/gen/definitions/fabric_provision_device.yaml index 2f748213..fbdb7ba6 100644 --- a/gen/definitions/fabric_provision_device.yaml +++ b/gen/definitions/fabric_provision_device.yaml @@ -30,6 +30,11 @@ attributes: description: ID of network device to be provisioned type: String example: 4cb565d3-1944-42be-be9f-a87cff79e831 + - tf_name: reprovision + description: Flag to indicate whether the device should be reprovisioned. If set to `true`, reprovisioning will be triggered on every Terraform apply + type: Bool + example: false + exclude_test: true test_prerequisites: | resource "catalystcenter_area" "test" { name = "Area1" diff --git a/gen/templates/model.go b/gen/templates/model.go index dee68c93..be636682 100644 --- a/gen/templates/model.go +++ b/gen/templates/model.go @@ -475,7 +475,11 @@ func (data *{{camelCase .Name}}) updateFromBody(ctx context.Context, res gjson.R if value := res.Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}"); value.Exists() && !data.{{toGoName .TfName}}.IsNull() { data.{{toGoName .TfName}} = types.{{.Type}}Value(value.{{if eq .Type "Int64"}}Int{{else if eq .Type "Float64"}}Float{{else}}{{.Type}}{{end}}()) } else {{if .DefaultValue}}if data.{{toGoName .TfName}}.Value{{.Type}}() != {{if eq .Type "String"}}"{{end}}{{.DefaultValue}}{{if eq .Type "String"}}"{{end}} {{end}}{ + {{- if (eq (toGoName .TfName) "Reprovision") }} + data.{{toGoName .TfName}} = types.{{.Type}}Value(false) + {{- else}} data.{{toGoName .TfName}} = types.{{.Type}}Null() + {{- end}} } {{- else if isListSet .}} if value := res.Get("{{if .ResponseDataPath}}{{.ResponseDataPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}"); value.Exists() && !data.{{toGoName .TfName}}.IsNull() { diff --git a/gen/templates/resource.go b/gen/templates/resource.go index a62e46e7..d6c5ee6e 100644 --- a/gen/templates/resource.go +++ b/gen/templates/resource.go @@ -622,7 +622,9 @@ func (r *{{camelCase .Name}}Resource) Update(ctx context.Context, req resource.U } tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + {{- if not .NoUpdate}} + {{- if not (strContains (camelCase .Name) "FabricProvision") }} body := plan.toBody(ctx, state) params := "" @@ -652,6 +654,19 @@ func (r *{{camelCase .Name}}Resource) Update(ctx context.Context, req resource.U {{- if and .IdPath .PutUpdateId}} plan.Id = types.StringValue(res.Get("{{.IdPath}}").String()) {{- end}} + {{- else}} + if plan.Reprovision.ValueBool() { + body := plan.toBody(ctx, state) + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Fabric Device Re-Provisioining", plan.Id.ValueString())) + params := "" + res, err := r.client.Put(plan.getPath()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) + return + } + tflog.Debug(ctx, fmt.Sprintf("%s: Fabric Device Re-Provisioining finished successfully", plan.Id.ValueString())) + } + {{- end}} {{- end}} tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) diff --git a/internal/provider/data_source_catalystcenter_fabric_provision_device.go b/internal/provider/data_source_catalystcenter_fabric_provision_device.go index 1014b180..c94e7d78 100644 --- a/internal/provider/data_source_catalystcenter_fabric_provision_device.go +++ b/internal/provider/data_source_catalystcenter_fabric_provision_device.go @@ -69,6 +69,10 @@ func (d *FabricProvisionDeviceDataSource) Schema(ctx context.Context, req dataso MarkdownDescription: "ID of network device to be provisioned", Required: true, }, + "reprovision": schema.BoolAttribute{ + MarkdownDescription: "Flag to indicate whether the device should be reprovisioned. If set to `true`, reprovisioning will be triggered on every Terraform apply", + Computed: true, + }, }, } } diff --git a/internal/provider/model_catalystcenter_fabric_provision_device.go b/internal/provider/model_catalystcenter_fabric_provision_device.go index c510b123..a4da3f9d 100644 --- a/internal/provider/model_catalystcenter_fabric_provision_device.go +++ b/internal/provider/model_catalystcenter_fabric_provision_device.go @@ -33,6 +33,7 @@ type FabricProvisionDevice struct { Id types.String `tfsdk:"id"` SiteId types.String `tfsdk:"site_id"` NetworkDeviceId types.String `tfsdk:"network_device_id"` + Reprovision types.Bool `tfsdk:"reprovision"` } // End of section. //template:end types @@ -63,6 +64,9 @@ func (data FabricProvisionDevice) toBody(ctx context.Context, state FabricProvis if !data.NetworkDeviceId.IsNull() { body, _ = sjson.Set(body, "0.networkDeviceId", data.NetworkDeviceId.ValueString()) } + if !data.Reprovision.IsNull() { + body, _ = sjson.Set(body, "", data.Reprovision.ValueBool()) + } return body } @@ -86,6 +90,11 @@ func (data *FabricProvisionDevice) fromBody(ctx context.Context, res gjson.Resul } else { data.NetworkDeviceId = types.StringNull() } + if value := res.Get(""); value.Exists() { + data.Reprovision = types.BoolValue(value.Bool()) + } else { + data.Reprovision = types.BoolNull() + } } // End of section. //template:end fromBody @@ -102,12 +111,20 @@ func (data *FabricProvisionDevice) updateFromBody(ctx context.Context, res gjson } else { data.NetworkDeviceId = types.StringNull() } + if value := res.Get(""); value.Exists() && !data.Reprovision.IsNull() { + data.Reprovision = types.BoolValue(value.Bool()) + } else { + data.Reprovision = types.BoolValue(false) + } } // End of section. //template:end updateFromBody // Section below is generated&owned by "gen/generator.go". //template:begin isNull func (data *FabricProvisionDevice) isNull(ctx context.Context, res gjson.Result) bool { + if !data.Reprovision.IsNull() { + return false + } return true } diff --git a/internal/provider/resource_catalystcenter_fabric_provision_device.go b/internal/provider/resource_catalystcenter_fabric_provision_device.go index cac759ae..d640825a 100644 --- a/internal/provider/resource_catalystcenter_fabric_provision_device.go +++ b/internal/provider/resource_catalystcenter_fabric_provision_device.go @@ -82,6 +82,10 @@ func (r *FabricProvisionDeviceResource) Schema(ctx context.Context, req resource stringplanmodifier.RequiresReplace(), }, }, + "reprovision": schema.BoolAttribute{ + MarkdownDescription: helpers.NewAttributeDescription("Flag to indicate whether the device should be reprovisioned. If set to `true`, reprovisioning will be triggered on every Terraform apply").String, + Optional: true, + }, }, } } @@ -199,13 +203,16 @@ func (r *FabricProvisionDeviceResource) Update(ctx context.Context, req resource } tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) - - body := plan.toBody(ctx, state) - params := "" - res, err := r.client.Put(plan.getPath()+params, body) - if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) - return + if plan.Reprovision.ValueBool() { + body := plan.toBody(ctx, state) + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Fabric Device Re-Provisioining", plan.Id.ValueString())) + params := "" + res, err := r.client.Put(plan.getPath()+params, body) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (PUT), got error: %s, %s", err, res.String())) + return + } + tflog.Debug(ctx, fmt.Sprintf("%s: Fabric Device Re-Provisioining finished successfully", plan.Id.ValueString())) } tflog.Debug(ctx, fmt.Sprintf("%s: Update finished successfully", plan.Id.ValueString())) diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 06b11412..6f40c1da 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.2.1 (unreleased) + +- Add `reprovision` attribute to `catalystcenter_fabric_provision_device` + ## 0.2.0 - Fix issue with security group name in `catalystcenter_anycast_gateway` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/179)