Skip to content

Commit

Permalink
Feat: able to define is_archived at env0_environment resources (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Feb 6, 2023
1 parent bb71636 commit d4868b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Environment struct {
LifespanEndAt string `json:"lifespanEndAt" tfschema:"ttl"`
LatestDeploymentLogId string `json:"latestDeploymentLogId" tfschema:"deployment_id"`
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
IsArchived bool `json:"isArchived"`
IsArchived bool `json:"isArchived" tfschema:"is_inactive,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
BlueprintId string `json:"blueprintId" tfschema:"-"`
Expand Down Expand Up @@ -178,6 +178,7 @@ type EnvironmentUpdate struct {
PullRequestPlanDeployments *bool `json:"pullRequestPlanDeployments,omitempty" tfschema:"-"`
AutoDeployOnPathChangesOnly *bool `json:"autoDeployOnPathChangesOnly,omitempty" tfschema:"-"`
IsRemoteBackend *bool `json:"isRemoteBackend,omitempty" tfschema:"-"`
IsArchived bool `json:"isArchived" tfschema:"is_inactive"`
}

type EnvironmentDeployResponse struct {
Expand Down
8 changes: 7 additions & 1 deletion env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ func resourceEnvironment() *schema.Resource {
Description: "set an alias for this environment in favor of running VCS commands using PR comments against it. Additional details: https://docs.env0.com/docs/plan-and-apply-from-pr-comments",
Optional: true,
},
"is_inactive": {
Type: schema.TypeBool,
Description: "If 'true', it marks the environment as inactive. It can be re-activated by setting it to 'false' or removing this field.",
Default: false,
Optional: true,
},
"configuration": {
Type: schema.TypeList,
Description: "terraform and environment variables for the environment",
Expand Down Expand Up @@ -603,7 +609,7 @@ func shouldDeploy(d *schema.ResourceData) bool {
}

func shouldUpdate(d *schema.ResourceData) bool {
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob", "auto_deploy_on_path_changes_only", "terragrunt_working_directory", "vcs_commands_alias", "is_remote_backend")
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob", "auto_deploy_on_path_changes_only", "terragrunt_working_directory", "vcs_commands_alias", "is_remote_backend", "is_inactive")
}

func shouldUpdateTTL(d *schema.ResourceData) bool {
Expand Down
5 changes: 5 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: "/terragrunt/directory2/",
VcsCommandsAlias: "alias2",
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: true,
}

template := client.Template{
Expand All @@ -72,6 +73,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"force_destroy": true,
"vcs_commands_alias": environment.VcsCommandsAlias,
"is_remote_backend": *(environment.IsRemoteBackend),
"is_inactive": environment.IsArchived,
})
}

Expand Down Expand Up @@ -111,6 +113,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "revision", updatedEnvironment.LatestDeploymentLog.BlueprintRevision),
resource.TestCheckResourceAttr(accessor, "is_remote_backend", "true"),
resource.TestCheckResourceAttr(accessor, "output", string(updatedEnvironment.LatestDeploymentLog.Output)),
resource.TestCheckResourceAttr(accessor, "is_inactive", "true"),
resource.TestCheckNoResourceAttr(accessor, "deploy_on_push"),
resource.TestCheckNoResourceAttr(accessor, "run_plan_on_pull_requests"),
),
Expand Down Expand Up @@ -138,6 +141,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: updatedEnvironment.IsArchived,
}).Times(1).Return(updatedEnvironment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, updatedEnvironment.Id).Times(3).Return(client.ConfigurationChanges{}, nil)
gomock.InOrder(
Expand Down Expand Up @@ -1158,6 +1162,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: true,
}).Times(1).Return(client.Environment{}, errors.New("error"))
mock.EXPECT().Environment(gomock.Any()).Times(2).Return(environment, nil) // 1 after create, 1 before update
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ resource "env0_environment" "environment-without-template" {
}
}

resource "env0_environment" "inactive" {
depends_on = [env0_template_project_assignment.assignment]
force_destroy = true
name = "environment-${random_string.random.result}-inactive"
project_id = env0_project.test_project.id
template_id = env0_template.template.id
approve_plan_automatically = true
revision = "master"
vcs_commands_alias = "alias"
is_inactive = var.second_run ? "true" : "false"
}

resource "env0_template" "workflow_template" {
name = "Template for workflow environment"
type = "workflow"
Expand Down

0 comments on commit d4868b7

Please sign in to comment.