Skip to content

Commit

Permalink
Fix tenant resource failed read issue
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacCalligeros95 committed Aug 11, 2024
1 parent 79fbbf3 commit 3db3bac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
20 changes: 20 additions & 0 deletions internal/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func DeleteFromStateV2(ctx context.Context, resp *resource.ReadResponse, resourc
return nil
}

func DeleteFromUpdateStateV2(ctx context.Context, resp *resource.UpdateResponse, resource schemas.IResourceModel, resourceDescription string) error {
log.Printf("[INFO] %s (%s) not found; deleting from state", resourceDescription, resource.GetID())
resp.State.RemoveResource(ctx)
return nil
}

func ProcessApiErrorV2(ctx context.Context, resp *resource.ReadResponse, resource schemas.IResourceModel, err error, resourceDescription string) error {
if err == nil {
return nil
Expand All @@ -51,3 +57,17 @@ func ProcessApiErrorV2(ctx context.Context, resp *resource.ReadResponse, resourc

return nil
}

func ProcessUpdateApiErrorV2(ctx context.Context, resp *resource.UpdateResponse, resource schemas.IResourceModel, err error, resourceDescription string) error {
if err == nil {
return nil
}

if apiError, ok := err.(*core.APIError); ok {
if apiError.StatusCode == http.StatusNotFound {
return DeleteFromUpdateStateV2(ctx, resp, resource, resourceDescription)
}
}

return nil
}
3 changes: 2 additions & 1 deletion octopusdeploy_framework/resource_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -109,7 +110,7 @@ func (r *tenantTypeResource) Update(ctx context.Context, req resource.UpdateRequ
tenant, err := mapStateToTenant(data)
tenant.ID = state.ID.ValueString()
if err != nil {
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "tenant"); err != nil {
if err := errors.ProcessUpdateApiErrorV2(ctx, resp, data, err, "tenant"); err != nil {
resp.Diagnostics.AddError("unable to load tenant", err.Error())
}
return
Expand Down
4 changes: 2 additions & 2 deletions octopusdeploy_framework/schemas/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
type TenantModel struct {
ClonedFromTenantId types.String `tfsdk:"cloned_from_tenant_id"`
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
SpaceID types.String `tfsdk:"space_id"`
TenantTags types.List `tfsdk:"tenant_tags"`

ResourceModel
}

type TenantsModel struct {
Expand Down Expand Up @@ -133,4 +134,3 @@ func GetTenantResourceSchema() map[string]resourceSchema.Attribute {
},
}
}

0 comments on commit 3db3bac

Please sign in to comment.