Skip to content

Commit

Permalink
patch - recreate resource on not-found error (#227)
Browse files Browse the repository at this point in the history
* patch - recreate resource on not-found error
  • Loading branch information
OrNovo committed Apr 30, 2024
1 parent 6fc6dca commit 30c7c00
Show file tree
Hide file tree
Showing 33 changed files with 82 additions and 114 deletions.
8 changes: 2 additions & 6 deletions coralogix/data_source_coralogix_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -73,11 +72,8 @@ func (d *ActionDataSource) Read(ctx context.Context, req datasource.ReadRequest,
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("Action %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.Diagnostics.AddWarning(err.Error(),
fmt.Sprintf("Action %q is in state, but no longer exists in Coralogix backend", id))
} else {
resp.Diagnostics.AddError(
"Error reading Action",
Expand Down
5 changes: 1 addition & 4 deletions coralogix/data_source_coralogix_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"terraform-provider-coralogix/coralogix/clientset"
Expand Down Expand Up @@ -76,10 +75,8 @@ func (d *ApiKeyDataSource) Read(ctx context.Context, req datasource.ReadRequest,
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
resp.Diagnostics.AddWarning(err.Error(),
fmt.Sprintf("Action %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
3 changes: 1 addition & 2 deletions coralogix/data_source_coralogix_archive_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ func (d *ArchiveLogsDataSource) Read(ctx context.Context, req datasource.ReadReq
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
resp.Diagnostics.AddWarning(err.Error(),
fmt.Sprintf("archive-Logs %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_archive_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -67,10 +66,9 @@ func (d *ArchiveMetricsDataSource) Read(ctx context.Context, req datasource.Read
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("archive-metrics %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
2 changes: 1 addition & 1 deletion coralogix/data_source_coralogix_custom_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (d *CustomRoleDataSource) Read(ctx context.Context, req datasource.ReadRequ
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("Custom role %q is in state, but no longer exists in Coralogix backend", roleId),
fmt.Sprintf("%v will be recreated when you apply", roleId),
)
} else {
resp.Diagnostics.AddError(
Expand Down
8 changes: 2 additions & 6 deletions coralogix/data_source_coralogix_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -72,11 +71,8 @@ func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadReque
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("Dashboard %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.Diagnostics.AddWarning(err.Error(),
fmt.Sprintf("Dashboard %q is in state, but no longer exists in Coralogix backend", id))
} else {
resp.Diagnostics.AddError(
"Error reading Dashboard",
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_events2meric.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -72,10 +71,9 @@ func (d *Events2MetricDataSource) Read(ctx context.Context, req datasource.ReadR
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("Events2Metric %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"terraform-provider-coralogix/coralogix/clientset"
Expand Down Expand Up @@ -67,10 +66,9 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("Group %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_recording_rules_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -71,10 +70,9 @@ func (d *RecordingRuleGroupSetDataSource) Read(ctx context.Context, req datasour
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("recording-rule-group-set %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
3 changes: 1 addition & 2 deletions coralogix/data_source_coralogix_sli.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ func (d *SLIDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("SLI %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -70,10 +69,9 @@ func (d *SLODataSource) Read(ctx context.Context, req datasource.ReadRequest, re
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("SLO %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_tco_policy_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -73,10 +72,9 @@ func (d *TCOPolicyDataSource) Read(ctx context.Context, req datasource.ReadReque
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("tco-policy %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_tco_policy_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -73,10 +72,9 @@ func (d *TCOPolicyTracesDataSource) Read(ctx context.Context, req datasource.Rea
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("tco-policy %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -85,10 +84,9 @@ func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("Team %q is in state, but no longer exists in Coralogix backend", intId),
fmt.Sprintf("%q will be recreated when you apply", intId),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"terraform-provider-coralogix/coralogix/clientset"
Expand Down Expand Up @@ -67,10 +66,9 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("User %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
resp.Diagnostics.AddError(
Expand Down
4 changes: 1 addition & 3 deletions coralogix/data_source_coralogix_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -74,10 +73,9 @@ func (d *WebhookDataSource) Read(ctx context.Context, req datasource.ReadRequest
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
data.ID = types.StringNull()
resp.Diagnostics.AddWarning(
err.Error(),
fmt.Sprintf("Webhook %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
} else {
reqStr := protojson.Format(getWebhookReq)
Expand Down
4 changes: 2 additions & 2 deletions coralogix/resource_coralogix_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ func (r *ActionResource) Read(ctx context.Context, req resource.ReadRequest, res
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
state.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("Action %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading Action",
Expand Down Expand Up @@ -264,11 +264,11 @@ func (r ActionResource) Update(ctx context.Context, req resource.UpdateRequest,
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
plan.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("Action %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading Action",
Expand Down
4 changes: 2 additions & 2 deletions coralogix/resource_coralogix_alerts_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,11 +1093,11 @@ func (r *AlertsSchedulerResource) Read(ctx context.Context, req resource.ReadReq
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
state.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("alerts-scheduler %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading alerts-scheduler",
Expand Down Expand Up @@ -1158,11 +1158,11 @@ func (r *AlertsSchedulerResource) Update(ctx context.Context, req resource.Updat
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
plan.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("alerts-scheduler %s is in state, but no longer exists in Coralogix backend", *id),
fmt.Sprintf("%s will be recreated when you apply", *id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading alerts-scheduler",
Expand Down
5 changes: 5 additions & 0 deletions coralogix/resource_coralogix_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func (r *ApiKeyResource) getKeyInfo(ctx context.Context, id *string, keyValue *s
"Error getting Api Key",
fmt.Sprintf("permission denied for url - %s\ncheck your org-key and permissions", getApiKeyPath),
)
} else if status.Code(err) == codes.NotFound {
diags.AddError(
"Error getting Api Key",
fmt.Sprintf("Api Key with id %s not found", *id),
)
} else {
diags.AddError(
"Error getting Api Key",
Expand Down
2 changes: 1 addition & 1 deletion coralogix/resource_coralogix_archive_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ func (r *ArchiveLogsResource) Read(ctx context.Context, req resource.ReadRequest
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
state.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("archive-logs %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading archive-logs",
Expand Down
2 changes: 1 addition & 1 deletion coralogix/resource_coralogix_archive_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ func (r *ArchiveMetricsResource) Read(ctx context.Context, req resource.ReadRequ
if err != nil {
log.Printf("[ERROR] Received error: %s", err.Error())
if status.Code(err) == codes.NotFound {
state.ID = types.StringNull()
resp.Diagnostics.AddWarning(
fmt.Sprintf("archiveMetrics %q is in state, but no longer exists in Coralogix backend", id),
fmt.Sprintf("%s will be recreated when you apply", id),
)
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Error reading archive-metrics",
Expand Down
Loading

0 comments on commit 30c7c00

Please sign in to comment.