diff --git a/opslevel/resource_opslevel_property_assignment.go b/opslevel/resource_opslevel_property_assignment.go index 6cc51bf4..d3765015 100644 --- a/opslevel/resource_opslevel_property_assignment.go +++ b/opslevel/resource_opslevel_property_assignment.go @@ -139,10 +139,6 @@ func (resource *PropertyAssignmentResource) Read(ctx context.Context, req resour owner := planModel.Owner.ValueString() assignment, err := resource.client.GetProperty(owner, definition) if err != nil { - if (assignment == nil || assignment.Definition.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read property assignment '%s' on service '%s', got error: %s", definition, owner, err)) return } diff --git a/opslevel/resource_opslevel_repository.go b/opslevel/resource_opslevel_repository.go index be87c4f3..e908517c 100644 --- a/opslevel/resource_opslevel_repository.go +++ b/opslevel/resource_opslevel_repository.go @@ -94,10 +94,6 @@ func (r *RepositoryResource) Create(ctx context.Context, req resource.CreateRequ repository, err = r.client.GetRepositoryWithAlias(identifier) } if err != nil { - if (repository == nil || repository.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to get repository, got error: %s", err)) return } @@ -143,6 +139,10 @@ func (r *RepositoryResource) Read(ctx context.Context, req resource.ReadRequest, readRepository, err := r.client.GetRepository(opslevel.ID(planModel.Id.ValueString())) if err != nil || readRepository == nil { + if (readRepository == nil || readRepository.Id == "") && opslevel.IsOpsLevelApiError(err) { + resp.State.RemoveResource(ctx) + return + } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read repository, got error: %s", err)) return } diff --git a/opslevel/resource_opslevel_service_dependency.go b/opslevel/resource_opslevel_service_dependency.go index 8fefc930..900efd5e 100644 --- a/opslevel/resource_opslevel_service_dependency.go +++ b/opslevel/resource_opslevel_service_dependency.go @@ -145,10 +145,6 @@ func (r *ServiceDependencyResource) Read(ctx context.Context, req resource.ReadR service, err = r.client.GetServiceWithAlias(serviceIdentifier) } if err != nil { - if (service == nil || service.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read service, got error: %s", err)) return } @@ -160,7 +156,7 @@ func (r *ServiceDependencyResource) Read(ctx context.Context, req resource.ReadR } extractedServiceDependency := extractServiceDependency(planModel.Id.ValueString(), *dependencies) if extractedServiceDependency == nil { - resp.Diagnostics.AddError("opslevel client error", "Unable to extract service dependency") + resp.State.RemoveResource(ctx) return } diff --git a/opslevel/resource_opslevel_service_repository.go b/opslevel/resource_opslevel_service_repository.go index 3cd841fd..b017fd55 100644 --- a/opslevel/resource_opslevel_service_repository.go +++ b/opslevel/resource_opslevel_service_repository.go @@ -203,10 +203,6 @@ func (r *ServiceRepositoryResource) Read(ctx context.Context, req resource.ReadR service, err = r.client.GetServiceWithAlias(currentStateModel.ServiceAlias.ValueString()) } if err != nil { - if (service == nil || service.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read service, got error: %s", err)) return } @@ -224,10 +220,10 @@ func (r *ServiceRepositoryResource) Read(ctx context.Context, req resource.ReadR } } if serviceRepository == nil { - verifiedStateModel = ServiceRepositoryResourceModel{} - } else { - verifiedStateModel = NewServiceRepositoryResourceModel(ctx, *serviceRepository, currentStateModel) + resp.State.RemoveResource(ctx) + return } + verifiedStateModel = NewServiceRepositoryResourceModel(ctx, *serviceRepository, currentStateModel) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &verifiedStateModel)...) diff --git a/opslevel/resource_opslevel_service_tag.go b/opslevel/resource_opslevel_service_tag.go index e898ca91..5f61b195 100644 --- a/opslevel/resource_opslevel_service_tag.go +++ b/opslevel/resource_opslevel_service_tag.go @@ -164,11 +164,7 @@ func (serviceTagResource *ServiceTagResource) Read(ctx context.Context, req reso return } _, err = service.GetTags(serviceTagResource.client, nil) - if err != nil { - if service.Tags == nil && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } + if err != nil || service.Tags == nil { resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read tags on service (%s), got error: %s", serviceIdentifier, err)) } var serviceTag *opslevel.Tag @@ -179,7 +175,7 @@ func (serviceTagResource *ServiceTagResource) Read(ctx context.Context, req reso } } if serviceTag == nil { - resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("service tag (with key '%s') not found on service (%s)", data.Key.ValueString(), serviceIdentifier)) + resp.State.RemoveResource(ctx) return } diff --git a/opslevel/resource_opslevel_service_tool.go b/opslevel/resource_opslevel_service_tool.go index 2c6451ea..c45d5f31 100644 --- a/opslevel/resource_opslevel_service_tool.go +++ b/opslevel/resource_opslevel_service_tool.go @@ -140,10 +140,6 @@ func (r *ServiceToolResource) Create(ctx context.Context, req resource.CreateReq service, err = r.client.GetServiceWithAlias(planModel.ServiceAlias.ValueString()) } if err != nil { - if (service == nil || service.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to get service during create, got error: %s", err)) return } @@ -155,7 +151,11 @@ func (r *ServiceToolResource) Create(ctx context.Context, req resource.CreateReq ServiceId: &service.Id, Url: planModel.Url.ValueString(), }) - if err != nil || serviceTool == nil || string(serviceTool.Id) == "" { + if err != nil { + if (serviceTool == nil || serviceTool.Id == "") && opslevel.IsOpsLevelApiError(err) { + resp.State.RemoveResource(ctx) + return + } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to create service tool, got error: %s", err)) return } diff --git a/opslevel/resource_opslevel_tag.go b/opslevel/resource_opslevel_tag.go index 03ca4b6f..e51f7177 100644 --- a/opslevel/resource_opslevel_tag.go +++ b/opslevel/resource_opslevel_tag.go @@ -149,10 +149,6 @@ func (r *TagResource) Read(ctx context.Context, req resource.ReadRequest, resp * resourceType := opslevel.TaggableResource(planModel.TargetType.ValueString()) data, err := r.client.GetTaggableResource(resourceType, resourceId) if err != nil { - if (data == nil || data.ResourceId() == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read tag, got error: %s", err)) return } diff --git a/opslevel/resource_opslevel_team_contact.go b/opslevel/resource_opslevel_team_contact.go index 4d7b2f61..ca3a29ba 100644 --- a/opslevel/resource_opslevel_team_contact.go +++ b/opslevel/resource_opslevel_team_contact.go @@ -134,11 +134,7 @@ func (teamContactResource *TeamContactResource) Read(ctx context.Context, req re } else { team, err = teamContactResource.client.GetTeamWithAlias(teamIdentifier) } - if err != nil { - if (team == nil || team.Id == "") && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } + if err != nil || team == nil { resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read team (%s), got error: %s", teamIdentifier, err)) return } @@ -154,8 +150,8 @@ func (teamContactResource *TeamContactResource) Read(ctx context.Context, req re break } } - if teamContact == nil { - resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("team contact (with ID '%s') not found on team (%s)", contactID, teamIdentifier)) + if teamContact == nil || teamContact.Id == "" { + resp.State.RemoveResource(ctx) return } diff --git a/opslevel/resource_opslevel_team_tag.go b/opslevel/resource_opslevel_team_tag.go index 2d32c97b..236df882 100644 --- a/opslevel/resource_opslevel_team_tag.go +++ b/opslevel/resource_opslevel_team_tag.go @@ -171,11 +171,7 @@ func (teamTagResource *TeamTagResource) Read(ctx context.Context, req resource.R return } _, err = team.GetTags(teamTagResource.client, nil) - if err != nil { - if team.Tags == nil && opslevel.IsOpsLevelApiError(err) { - resp.State.RemoveResource(ctx) - return - } + if err != nil || team.Tags == nil { resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read tags on team (%s), got error: %s", teamIdentifier, err)) } var teamTag *opslevel.Tag @@ -186,7 +182,7 @@ func (teamTagResource *TeamTagResource) Read(ctx context.Context, req resource.R } } if teamTag == nil { - resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("team tag (with key '%s') not found on team (%s)", data.Key.ValueString(), teamIdentifier)) + resp.State.RemoveResource(ctx) return }