Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ensure resources deleted from Octopus are removed from state #720

Merged
merged 8 commits into from
Aug 8, 2024
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v1.9.0
github.com/hashicorp/terraform-plugin-framework v1.11.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smP
github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ=
github.com/hashicorp/terraform-plugin-framework v1.9.0 h1:caLcDoxiRucNi2hk8+j3kJwkKfvHznubyFsJMWfZqKU=
github.com/hashicorp/terraform-plugin-framework v1.9.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework v1.11.0 h1:M7+9zBArexHFXDx/pKTxjE6n/2UCXY6b8FIq9ZYhwfE=
github.com/hashicorp/terraform-plugin-framework v1.11.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
Expand Down
22 changes: 22 additions & 0 deletions internal/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -29,3 +31,23 @@ func ProcessApiError(ctx context.Context, d *schema.ResourceData, err error, res

return diag.FromErr(err)
}

func DeleteFromStateV2(ctx context.Context, resp *resource.ReadResponse, 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
}

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

return nil
}
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_artifactory_generic_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"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 @@ -77,7 +79,9 @@ func (r *artifactoryGenericFeedTypeResource) Read(ctx context.Context, req resou
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load artifactoryGeneric feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "artifactory generic feed"); err != nil {
resp.Diagnostics.AddError("unable to load artifactoryGeneric feed", err.Error())
}
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
"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 @@ -78,7 +80,9 @@ func (r *awsElasticContainerRegistryFeedTypeResource) Read(ctx context.Context,
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load aws elastic container registry", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "aws elastic container registry"); err != nil {
resp.Diagnostics.AddError("unable to load aws elastic container registry", err.Error())
}
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
"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 @@ -76,7 +78,9 @@ func (r *dockerContainerRegistryFeedTypeResource) Read(ctx context.Context, req
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load docker container registry feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "docker container registry feed"); err != nil {
resp.Diagnostics.AddError("unable to load docker container registry feed", err.Error())
}
return
}

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/extensions"
"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/resource"
Expand Down Expand Up @@ -82,7 +83,10 @@ func (r *environmentTypeResource) Read(ctx context.Context, req resource.ReadReq

environment, err := environments.GetByID(r.Config.Client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load environment", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "environment"); err != nil {
resp.Diagnostics.AddError("unable to load environment", err.Error())
}
return
}

updateEnvironment(ctx, &data, environment)
Expand Down
9 changes: 7 additions & 2 deletions octopusdeploy_framework/resource_git_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package octopusdeploy_framework

import (
"context"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials"
"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/resource"
Expand All @@ -18,13 +20,14 @@ type gitCredentialResource struct {
}

type gitCredentialResourceModel struct {
ID types.String `tfsdk:"id"`
SpaceID types.String `tfsdk:"space_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Type types.String `tfsdk:"type"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`

schemas.ResourceModel
}

func NewGitCredentialResource() resource.Resource {
Expand Down Expand Up @@ -92,7 +95,9 @@ func (g *gitCredentialResource) Read(ctx context.Context, req resource.ReadReque

gitCredential, err := credentials.GetByID(g.Client, state.SpaceID.ValueString(), state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error reading Git credential", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, state, err, "git credential"); err != nil {
resp.Diagnostics.AddError("Error reading Git credential", err.Error())
}
return
}

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_github_repository_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
"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 @@ -78,7 +80,9 @@ func (r *githubRepositoryFeedTypeResource) Read(ctx context.Context, req resourc
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load github repository feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "github repository feed"); err != nil {
resp.Diagnostics.AddError("unable to load github repository feed", err.Error())
}
return
}

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_helm_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"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 @@ -77,7 +79,9 @@ func (r *helmFeedTypeResource) Read(ctx context.Context, req resource.ReadReques
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load helm feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "helm feed"); err != nil {
resp.Diagnostics.AddError("unable to load helm feed", err.Error())
}
return
}

Expand Down
8 changes: 6 additions & 2 deletions octopusdeploy_framework/resource_library_variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/libraryvariablesets"
"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/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"log"
)

type libraryVariableSetFeedTypeResource struct {
Expand Down Expand Up @@ -61,7 +63,9 @@ func (r *libraryVariableSetFeedTypeResource) Read(ctx context.Context, req resou

libraryVariableSet, err := libraryvariablesets.GetByID(r.Config.Client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load library variable set", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "library variable set"); err != nil {
resp.Diagnostics.AddError("unable to load library variable set", err.Error())
}
return
}

Expand Down
17 changes: 12 additions & 5 deletions octopusdeploy_framework/resource_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/lifecycles"
"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"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
)

type lifecycleTypeResource struct {
Expand All @@ -23,13 +25,14 @@ var _ resource.Resource = &lifecycleTypeResource{}
var _ resource.ResourceWithImportState = &lifecycleTypeResource{}

type lifecycleTypeResourceModel struct {
ID types.String `tfsdk:"id"`
SpaceID types.String `tfsdk:"space_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Phase types.List `tfsdk:"phase"`
ReleaseRetentionPolicy types.List `tfsdk:"release_retention_policy"`
TentacleRetentionPolicy types.List `tfsdk:"tentacle_retention_policy"`

schemas.ResourceModel
}

func NewLifecycleResource() resource.Resource {
Expand Down Expand Up @@ -88,7 +91,9 @@ func (r *lifecycleTypeResource) Read(ctx context.Context, req resource.ReadReque

lifecycle, err := lifecycles.GetByID(r.Config.Client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load lifecycle", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "lifecycle"); err != nil {
resp.Diagnostics.AddError("unable to load lifecycle", err.Error())
}
return
}
data = flattenLifecycleResource(lifecycle)
Expand Down Expand Up @@ -195,15 +200,17 @@ func setDefaultRetentionPolicies(data *lifecycleTypeResourceModel) {
}

func flattenLifecycleResource(lifecycle *lifecycles.Lifecycle) *lifecycleTypeResourceModel {
return &lifecycleTypeResourceModel{
ID: types.StringValue(lifecycle.ID),
flattenedLifecycle := &lifecycleTypeResourceModel{
SpaceID: types.StringValue(lifecycle.SpaceID),
Name: types.StringValue(lifecycle.Name),
Description: types.StringValue(lifecycle.Description),
Phase: flattenPhases(lifecycle.Phases),
ReleaseRetentionPolicy: flattenRetentionPeriod(lifecycle.ReleaseRetentionPolicy),
TentacleRetentionPolicy: flattenRetentionPeriod(lifecycle.TentacleRetentionPolicy),
}
flattenedLifecycle.ID = types.StringValue(lifecycle.GetID())

return flattenedLifecycle
}

func flattenPhases(phases []*lifecycles.Phase) types.List {
Expand Down
7 changes: 4 additions & 3 deletions octopusdeploy_framework/resource_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package octopusdeploy_framework

import (
"fmt"
"path/filepath"
"testing"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/lifecycles"
Expand All @@ -13,8 +16,6 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/require"
"path/filepath"
"testing"
)

func TestExpandLifecycleWithNil(t *testing.T) {
Expand All @@ -31,7 +32,6 @@ func TestExpandLifecycle(t *testing.T) {
tentacleRetention := core.NewRetentionPeriod(2, "Items", false)

data := &lifecycleTypeResourceModel{
ID: types.StringValue(Id),
Description: types.StringValue(description),
Name: types.StringValue(name),
SpaceID: types.StringValue(spaceID),
Expand Down Expand Up @@ -62,6 +62,7 @@ func TestExpandLifecycle(t *testing.T) {
},
),
}
data.ID = types.StringValue(Id)

lifecycle := expandLifecycle(data)

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_maven_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
"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 @@ -76,7 +78,9 @@ func (r *mavenFeedTypeResource) Read(ctx context.Context, req resource.ReadReque
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load maven feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "maven feed"); err != nil {
resp.Diagnostics.AddError("unable to load maven feed", err.Error())
}
return
}

Expand Down
6 changes: 5 additions & 1 deletion octopusdeploy_framework/resource_nuget_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
"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 @@ -78,7 +80,9 @@ func (r *nugetFeedTypeResource) Read(ctx context.Context, req resource.ReadReque
client := r.Config.Client
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("unable to load nuget feed", err.Error())
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "nuget feed"); err != nil {
resp.Diagnostics.AddError("unable to load nuget feed", err.Error())
}
return
}

Expand Down
Loading
Loading