Skip to content

Commit

Permalink
add empty input warning to catalystcenter_*_settings resources, to by…
Browse files Browse the repository at this point in the history
…pass issue with API errors when removing settings from Global level
  • Loading branch information
kuba-mazurkiewicz committed Feb 27, 2025
1 parent f26fab2 commit 23449f1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
14 changes: 13 additions & 1 deletion gen/templates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,20 @@ func (r *{{camelCase .Name}}Resource) Delete(ctx context.Context, req resource.D
res, err := r.client.Put({{if .DeleteRestEndpoint}}state.getPathDelete(){{else}}state.getPath(){{end}} + "/" + url.QueryEscape(state.Id.ValueString()), "{}")
{{- end}}
if err != nil {
{{- if .PutDelete}}
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", err, res.String()))
return
}
{{- else}}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
{{- end}}
}
{{- else}}
{{- if .DeleteNoId}}
Expand All @@ -708,7 +720,7 @@ 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}}
{{- if .DeviceUnreachabilityWarning}}
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCDP10000" {
// Log a warning and continue execution when device is unreachable
Expand Down
11 changes: 9 additions & 2 deletions internal/provider/resource_catalystcenter_aaa_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ func (r *AAASettingsResource) Delete(ctx context.Context, req resource.DeleteReq
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", 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_banner_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,15 @@ func (r *BannerSettingsResource) Delete(ctx context.Context, req resource.Delete
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", 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_dhcp_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,15 @@ func (r *DHCPSettingsResource) Delete(ctx context.Context, req resource.DeleteRe
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", 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_dns_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,15 @@ func (r *DNSSettingsResource) Delete(ctx context.Context, req resource.DeleteReq
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", 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_ntp_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,15 @@ func (r *NTPSettingsResource) Delete(ctx context.Context, req resource.DeleteReq
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", 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_telemetry_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,15 @@ func (r *TelemetrySettingsResource) Delete(ctx context.Context, req resource.Del
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Delete", state.Id.ValueString()))
res, err := r.client.Put(state.getPath(), "{}")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete object (PUT), got error: %s, %s", err, res.String()))
return
errorCode := res.Get("response.errorCode").String()
if errorCode == "NCND01090" {
// Log a warning and continue execution when Empty input - the groupUuid is null or empty
failureReason := res.Get("response.failureReason").String()
resp.Diagnostics.AddWarning("Empty input Warning", fmt.Sprintf("Empty input 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", "PUT", err, res.String()))
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s: Delete finished successfully", state.Id.ValueString()))
Expand Down

0 comments on commit 23449f1

Please sign in to comment.