Skip to content

Commit

Permalink
Feat: support removal_strategy on destroy environment
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Mar 21, 2024
1 parent d0ab3b0 commit 0c6d784
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type ApiClientInterface interface {
EnvironmentCreate(payload EnvironmentCreate) (Environment, error)
EnvironmentCreateWithoutTemplate(payload EnvironmentCreateWithoutTemplate) (Environment, error)
EnvironmentDestroy(id string) (Environment, error)
EnvironmentMarkAsArchived(id string) error
EnvironmentUpdate(id string, payload EnvironmentUpdate) (Environment, error)
EnvironmentDeploy(id string, payload DeployRequest) (EnvironmentDeployResponse, error)
EnvironmentUpdateTTL(id string, payload TTL) (Environment, error)
Expand Down
14 changes: 14 additions & 0 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ func (client *ApiClient) EnvironmentUpdate(id string, payload EnvironmentUpdate)
return result, nil
}

func (client *ApiClient) EnvironmentMarkAsArchived(id string) error {
payload := struct {
IsArchived bool `json:"isArchived"`
}{true}

return client.http.Put("/environments/"+id, &payload, nil)
}

func (client *ApiClient) EnvironmentUpdateTTL(id string, payload TTL) (Environment, error) {
var result Environment
err := client.http.Put("/environments/"+id+"/ttl", payload, &result)
Expand Down
20 changes: 20 additions & 0 deletions client/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ var _ = Describe("Environment Client", func() {
})
})

Describe("EnvironmentMarkAsArchived", func() {
payload := struct {
IsArchived bool `json:"isArchived"`
}{true}

var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Put("/environments/"+mockEnvironment.Id, &payload, nil).Return(nil).Times(1)
err = apiClient.EnvironmentMarkAsArchived(mockEnvironment.Id)
})

It("Should send an archive put request", func() {})

It("Should not return error", func() {
Expect(err).To(BeNil())
})

})

Describe("EnvironmentUpdate", func() {
Describe("Success", func() {
var updatedEnvironment Environment
Expand Down
22 changes: 20 additions & 2 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ func resourceEnvironment() *schema.Resource {
Optional: true,
Default: false,
},
"removal_strategy": {
Type: schema.TypeString,
Description: "by default when removing an environment, it gets destroyed. Setting this value to 'mark_as_archived' will force the environment to be archived instead of tying to destroy it ('Mark as inactive' in the UI)",
Optional: true,
Default: "destroy",
ValidateDiagFunc: NewStringInValidator([]string{"destroy", "mark_as_archived"}),
},
},
CustomizeDiff: customdiff.ValidateChange("template_id", func(ctx context.Context, oldValue, newValue, meta interface{}) error {
if oldValue != "" && oldValue != newValue {
Expand Down Expand Up @@ -768,14 +775,24 @@ func updateTTL(d *schema.ResourceData, apiClient client.ApiClientInterface) diag
}

func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

markAsArchived := d.Get("removal_strategy").(string) == "mark_as_archived"

if markAsArchived {
if err := apiClient.EnvironmentMarkAsArchived(d.Id()); err != nil {
return diag.Errorf("could not archive the environment: %v", err)
}

return nil
}

canDestroy := d.Get("force_destroy")

if canDestroy != true {
return diag.Errorf(`must enable "force_destroy" safeguard in order to destroy`)
}

apiClient := meta.(client.ApiClientInterface)

_, err := apiClient.EnvironmentDestroy(d.Id())
if err != nil {
if frerr, ok := err.(*http.FailedResponseError); ok && frerr.BadRequest() {
Expand Down Expand Up @@ -1180,6 +1197,7 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta
}

d.Set("force_destroy", false)
d.Set("removal_strategy", "destroy")

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
Expand Down
48 changes: 48 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,54 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
})

t.Run("Mark as archived", func(t *testing.T) {
environment := client.Environment{
Id: uuid.New().String(),
Name: "name",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
BlueprintId: templateId,
},
}

testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": environment.Name,
"project_id": environment.ProjectId,
"template_id": templateId,
"force_destroy": false,
"removal_strategy": "mark_as_archived",
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", environment.Id),
resource.TestCheckResourceAttr(accessor, "name", environment.Name),
resource.TestCheckResourceAttr(accessor, "project_id", environment.ProjectId),
resource.TestCheckResourceAttr(accessor, "template_id", templateId),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(1).Return(template, nil),
mock.EXPECT().EnvironmentCreate(client.EnvironmentCreate{
Name: environment.Name,
ProjectId: environment.ProjectId,

DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
}).Times(1).Return(environment, nil),
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().EnvironmentMarkAsArchived(environment.Id).Times(1).Return(nil),
)
})
})

t.Run("Import By Id", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,11 @@ resource "env0_environment" "workflow-environment" {
}
}
}

resource "env0_environment" "mark_as_archived" {
depends_on = [env0_template_project_assignment.assignment]
name = "environment-mark-as-archived-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = env0_template.template.id
removal_strategy = "mark_as_archived"
}

0 comments on commit 0c6d784

Please sign in to comment.