Skip to content

Commit

Permalink
add reprovision option to fabric_provision_device
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-mazurkiewicz committed Mar 3, 2025
1 parent 9f0ece5 commit 5887f5e
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/fabric_provision_device.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/fabric_provision_device.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions gen/definitions/fabric_provision_device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions gen/templates/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down Expand Up @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down
17 changes: 17 additions & 0 deletions internal/provider/model_catalystcenter_fabric_provision_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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()))
Expand Down
4 changes: 4 additions & 0 deletions templates/guides/changelog.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5887f5e

Please sign in to comment.