Skip to content

Commit

Permalink
Handle device unreachability when deleting resources
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Dec 20, 2024
1 parent e6e3ba7 commit b4b0cfc
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Add `catalystcenter_fabric_l3_virtual_network` resource and data source
- Add `Free Space` allowed value to `rf_model` attribute of `catalystcenter_floor` resource
- Add device unreachability warning to 'delete' implementation of `catalystcenter_fabric_l3_handoff_ip_transit`, `catalystcenter_anycast_gateway`, `catalystcenter_fabric_l2_handoff`, `catalystcenter_fabric_device`, `catalystcenter_fabric_port_assignment` and `catalystcenter_provision_device` resources

## 0.1.17

Expand Down
1 change: 1 addition & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description: |-

- Add `catalystcenter_fabric_l3_virtual_network` resource and data source
- Add `Free Space` allowed value to `rf_model` attribute of `catalystcenter_floor` resource
- Add device unreachability warning to 'delete' implementation of `catalystcenter_fabric_l3_handoff_ip_transit`, `catalystcenter_anycast_gateway`, `catalystcenter_fabric_l2_handoff`, `catalystcenter_fabric_device`, `catalystcenter_fabric_port_assignment` and `catalystcenter_provision_device` resources

## 0.1.17

Expand Down
14 changes: 13 additions & 1 deletion gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String()))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", {{- if .PutCreate }} "PUT" {{- else }} "POST" {{- end }}, err, res.String()))
return
}
{{- else}}
Expand Down Expand Up @@ -656,8 +656,20 @@ func (r *{{camelCase .Name}}Resource) Delete(ctx context.Context, req resource.D
res, err := r.client.Delete({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "/" + url.QueryEscape(state.Id.ValueString()))
{{- end}}
if err != nil {
{{- if .DeviceUnreachabilityWarning}}
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
{{- else}}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
{{- end}}
}
{{- end}}

Expand Down
11 changes: 9 additions & 2 deletions internal/provider/resource_catalystcenter_anycast_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,15 @@ func (r *AnycastGatewayResource) Delete(ctx context.Context, req resource.Delete
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
11 changes: 9 additions & 2 deletions internal/provider/resource_catalystcenter_fabric_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,15 @@ func (r *FabricDeviceResource) Delete(ctx context.Context, req resource.DeleteRe
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
11 changes: 9 additions & 2 deletions internal/provider/resource_catalystcenter_fabric_l2_handoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,15 @@ func (r *FabricL2HandoffResource) Delete(ctx context.Context, req resource.Delet
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,15 @@ func (r *FabricL3HandoffIPTransitResource) Delete(ctx context.Context, req resou
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,15 @@ func (r *FabricPortAssignmentResource) Delete(ctx context.Context, req resource.
params := "?fabricId=" + url.QueryEscape(state.FabricId.ValueString()) + "&networkDeviceId=" + url.QueryEscape(state.NetworkDeviceId.ValueString())
res, err := r.client.Delete(state.getPath() + params)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,15 @@ func (r *FabricProvisionDeviceResource) Delete(ctx context.Context, req resource
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Delete(state.getPath() + "/" + url.QueryEscape(state.Id.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (DELETE), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Device Unreachability Warning", fmt.Sprintf("Device unreachability detected (error code: %s, reason %s).", errorCode, failureReason))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (%s), got error: %s, %s", "DELETE", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down
1 change: 1 addition & 0 deletions templates/guides/changelog.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description: |-

- Add `catalystcenter_fabric_l3_virtual_network` resource and data source
- Add `Free Space` allowed value to `rf_model` attribute of `catalystcenter_floor` resource
- Add device unreachability warning to 'delete' implementation of `catalystcenter_fabric_l3_handoff_ip_transit`, `catalystcenter_anycast_gateway`, `catalystcenter_fabric_l2_handoff`, `catalystcenter_fabric_device`, `catalystcenter_fabric_port_assignment` and `catalystcenter_provision_device` resources

## 0.1.17

Expand Down

0 comments on commit b4b0cfc

Please sign in to comment.