Skip to content

Commit

Permalink
remove resource from TF state when not found in OpsLevel during read
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Oct 15, 2024
1 parent 7611af3 commit 7586dac
Show file tree
Hide file tree
Showing 45 changed files with 197 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20241015-110344.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Bugfix
body: correct removal of resource from TF state if resource not found in OpsLevel on read, instead of crashing with error
time: 2024-10-15T11:03:44.659794-05:00
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_alert_source_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ func (r *CheckAlertSourceUsageResource) Read(ctx context.Context, req resource.R

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check alert source usage, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_custom_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func (r *CheckCustomEventResource) Read(ctx context.Context, req resource.ReadRe

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check custom event, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_git_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (r *CheckGitBranchProtectionResource) Read(ctx context.Context, req resourc

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check git branch protection, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_has_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (r *CheckHasDocumentationResource) Read(ctx context.Context, req resource.R

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check has documentation, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_has_recent_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (r *CheckHasRecentDeployResource) Read(ctx context.Context, req resource.Re

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check has recent deploy, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ func (r *CheckManualResource) Read(ctx context.Context, req resource.ReadRequest

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check manual, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_package_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func (r *CheckPackageVersionResource) Read(ctx context.Context, req resource.Rea

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check package_version, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func (r *CheckRepositoryFileResource) Read(ctx context.Context, req resource.Rea

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository file, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_repository_grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func (r *CheckRepositoryGrepResource) Read(ctx context.Context, req resource.Rea

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository grep, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_repository_integrated.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (r *CheckRepositoryIntegratedResource) Read(ctx context.Context, req resour

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository integrated, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_repository_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (r *CheckRepositorySearchResource) Read(ctx context.Context, req resource.R

data, err := r.client.GetCheck(asID(currentStateModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository search, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_service_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (r *CheckServiceConfigurationResource) Read(ctx context.Context, req resour

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check service configuration, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_service_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (r *CheckServiceDependencyResource) Read(ctx context.Context, req resource.

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check service dependency, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_service_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ func (r *CheckServiceOwnershipResource) Read(ctx context.Context, req resource.R

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check service ownership, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_service_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func (r *CheckServicePropertyResource) Read(ctx context.Context, req resource.Re

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check service property, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_tag_defined.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func (r *CheckTagDefinedResource) Read(ctx context.Context, req resource.ReadReq

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check tag defined, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_check_tool_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ func (r *CheckToolUsageResource) Read(ctx context.Context, req resource.ReadRequ

data, err := r.client.GetCheck(asID(planModel.Id))
if err != nil {
if (data == nil || data.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check tool usage, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (r *DomainResource) Read(ctx context.Context, req resource.ReadRequest, res

resource, err := r.client.GetDomain(stateModel.Id.ValueString())
if err != nil {
if (resource == nil || resource.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read domain, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ func (r *FilterResource) Read(ctx context.Context, req resource.ReadRequest, res

filter, err := r.client.GetFilter(opslevel.ID(stateModel.Id.ValueString()))
if err != nil {
if (filter == nil || filter.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read filter, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ func (r *InfrastructureResource) Read(ctx context.Context, req resource.ReadRequ

infrastructure, err := r.client.GetInfrastructure(stateModel.Id.ValueString())
if err != nil {
if (infrastructure == nil || infrastructure.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read infrastructure, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_integration_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func (r *IntegrationAwsResource) Read(ctx context.Context, req resource.ReadRequ

awsIntegration, err := r.client.GetIntegration(opslevel.ID(stateModel.Id.ValueString()))
if err != nil {
if (awsIntegration == nil || awsIntegration.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read AWS integration, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_integration_azure_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ func (r *IntegrationAzureResourcesResource) Read(ctx context.Context, req resour

azureResourcesIntegration, err := r.client.GetIntegration(opslevel.ID(stateModel.Id.ValueString()))
if err != nil {
if (azureResourcesIntegration == nil || azureResourcesIntegration.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read Azure Resources integration, got error: '%s'", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_integration_google_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func (r *integrationGoogleCloudResource) Read(ctx context.Context, req resource.

readIntegration, err := r.client.GetIntegration(asID(stateModel.Id))
if err != nil {
if (readIntegration == nil || readIntegration.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read Google Cloud integration, got error: '%s'", err))
return
}
Expand Down
7 changes: 4 additions & 3 deletions opslevel/resource_opslevel_property_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ 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
} else if assignment == nil || string(assignment.Definition.Id) == "" {
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("property assignment '%s' not found on service '%s'", definition, owner))
return
}
value := *assignment.Value

Expand Down
6 changes: 5 additions & 1 deletion opslevel/resource_opslevel_property_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ func (resource *PropertyDefinitionResource) Read(ctx context.Context, req resour

id := stateModel.Id.ValueString()
definition, err := resource.client.GetPropertyDefinition(id)
if err != nil || definition == nil {
if err != nil {
if (definition == nil || definition.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read definition with id '%s', got error: %s", id, err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ 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
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_rubric_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (r *RubricCategoryResource) Read(ctx context.Context, req resource.ReadRequ

rubricCategory, err := r.client.GetCategory(opslevel.ID(data.Id.ValueString()))
if err != nil {
if (rubricCategory == nil || rubricCategory.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read rubric category, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_rubric_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (r *RubricLevelResource) Read(ctx context.Context, req resource.ReadRequest

rubricLevel, err := r.client.GetLevel(opslevel.ID(stateModel.Id.ValueString()))
if err != nil {
if (rubricLevel == nil || rubricLevel.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read rubric level, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func (r *ScorecardResource) Read(ctx context.Context, req resource.ReadRequest,

readScorecard, err := r.client.GetScorecard(stateModel.Id.ValueString())
if err != nil || readScorecard == nil {
if (readScorecard == nil || readScorecard.Id == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read scorecard, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (r *SecretResource) Read(ctx context.Context, req resource.ReadRequest, res

secret, err := r.client.GetSecret(data.Id.ValueString())
if err != nil {
if (secret == nil || secret.ID == "") && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read secret, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ func (r *ServiceResource) Create(ctx context.Context, req resource.CreateRequest
// fetch the service again, since other mutations are performed after the create/update step
service, err = r.client.GetService(service.Id)
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 after creation, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_service_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ 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
}
Expand Down
4 changes: 4 additions & 0 deletions opslevel/resource_opslevel_service_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ 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
}
Expand Down
12 changes: 10 additions & 2 deletions opslevel/resource_opslevel_service_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,20 @@ func (serviceTagResource *ServiceTagResource) Read(ctx context.Context, req reso
serviceIdentifier = data.ServiceAlias.ValueString()
service, err = serviceTagResource.client.GetServiceWithAlias(serviceIdentifier)
}
if err != nil || service == nil {
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 (%s), got error: %s", serviceIdentifier, err))
return
}
_, err = service.GetTags(serviceTagResource.client, nil)
if err != nil || service.Tags == nil {
if err != nil {
if service.Tags == nil && opslevel.IsOpsLevelApiError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("unable to read tags on service (%s), got error: %s", serviceIdentifier, err))
}
var serviceTag *opslevel.Tag
Expand Down
Loading

0 comments on commit 7586dac

Please sign in to comment.