Skip to content

Commit

Permalink
Feat: env0_environment resource support for PR plan / apply and Alias… (
Browse files Browse the repository at this point in the history
#392)

* Feat: env0_environment resource support for PR plan / apply and Alias options

* updated integration tests
  • Loading branch information
TomerHeber committed May 24, 2022
1 parent 888dd94 commit bc149ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Environment struct {
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
IsArchived bool `json:"isArchived"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
}

type EnvironmentCreate struct {
Expand All @@ -106,6 +107,7 @@ type EnvironmentCreate struct {
ConfigurationChanges *ConfigurationChanges `json:"configurationChanges,omitempty"`
TTL *TTL `json:"ttl,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
}

type EnvironmentUpdate struct {
Expand All @@ -117,6 +119,7 @@ type EnvironmentUpdate struct {
AutoDeployOnPathChangesOnly *bool `json:"autoDeployOnPathChangesOnly,omitempty"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias,omitempty"`
}

type EnvironmentDeployResponse struct {
Expand Down
13 changes: 12 additions & 1 deletion env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func resourceEnvironment() *schema.Resource {
Description: "The working directory path to be used by a Terragrunt template. If left empty '/' is used.",
Optional: true,
},
"vcs_commands_alias": {
Type: schema.TypeString,
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,
},
"configuration": {
Type: schema.TypeList,
Description: "terraform and environment variables for the environment",
Expand Down Expand Up @@ -206,6 +211,7 @@ func setEnvironmentSchema(d *schema.ResourceData, environment client.Environment
safeSet(d, "auto_deploy_by_custom_glob", environment.AutoDeployByCustomGlob)
safeSet(d, "ttl", environment.LifespanEndAt)
safeSet(d, "terragrunt_working_directory", environment.TerragruntWorkingDirectory)
safeSet(d, "vcs_commands_alias", environment.VcsCommandsAlias)

if environment.LatestDeploymentLog != (client.DeploymentLog{}) {
d.Set("template_id", environment.LatestDeploymentLog.BlueprintId)
Expand Down Expand Up @@ -337,7 +343,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")
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")
}

func shouldUpdateTTL(d *schema.ResourceData) bool {
Expand Down Expand Up @@ -427,6 +433,10 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
payload.TerragruntWorkingDirectory = terragruntWorkingDirectory.(string)
}

if vcsCommandsAlias, ok := d.GetOk("vcs_commands_alias"); ok {
payload.VcsCommandsAlias = vcsCommandsAlias.(string)
}

continuousDeployment := d.Get("deploy_on_push").(bool)
if d.HasChange("deploy_on_push") {
payload.ContinuousDeployment = &continuousDeployment
Expand Down Expand Up @@ -502,6 +512,7 @@ func getUpdatePayload(d *schema.ResourceData) (client.EnvironmentUpdate, diag.Di
payload.AutoDeployOnPathChangesOnly = &autoDeployOnPathChangesOnly
payload.AutoDeployByCustomGlob = d.Get("auto_deploy_by_custom_glob").(string)
payload.TerragruntWorkingDirectory = d.Get("terragrunt_working_directory").(string)
payload.VcsCommandsAlias = d.Get("vcs_commands_alias").(string)

err := assertDeploymentTriggers(payload.AutoDeployByCustomGlob, continuousDeployment, pullRequestPlanDeployments, autoDeployOnPathChangesOnly)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
TerragruntWorkingDirectory: "/terragrunt/directory/",
VcsCommandsAlias: "alias",
}

updatedEnvironment := client.Environment{
Expand All @@ -41,6 +42,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
TerragruntWorkingDirectory: "/terragrunt/directory2/",
VcsCommandsAlias: "alias2",
}

createEnvironmentResourceConfig := func(environment client.Environment) string {
Expand All @@ -51,6 +53,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"workspace": environment.WorkspaceName,
"terragrunt_working_directory": environment.TerragruntWorkingDirectory,
"force_destroy": true,
"vcs_commands_alias": environment.VcsCommandsAlias,
})
}
autoDeployOnPathChangesOnlyDefault := true
Expand All @@ -69,6 +72,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "template_id", templateId),
resource.TestCheckResourceAttr(accessor, "workspace", environment.WorkspaceName),
resource.TestCheckResourceAttr(accessor, "terragrunt_working_directory", environment.TerragruntWorkingDirectory),
resource.TestCheckResourceAttr(accessor, "vcs_commands_alias", environment.VcsCommandsAlias),
),
},
{
Expand All @@ -80,6 +84,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "template_id", templateId),
resource.TestCheckResourceAttr(accessor, "workspace", environment.WorkspaceName),
resource.TestCheckResourceAttr(accessor, "terragrunt_working_directory", updatedEnvironment.TerragruntWorkingDirectory),
resource.TestCheckResourceAttr(accessor, "vcs_commands_alias", updatedEnvironment.VcsCommandsAlias),
),
},
},
Expand All @@ -93,6 +98,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
AutoDeployOnPathChangesOnly: &autoDeployOnPathChangesOnlyDefault,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
Expand All @@ -102,6 +108,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
AutoDeployOnPathChangesOnly: &autoDeployOnPathChangesOnlyDefault,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
}).Times(1).Return(updatedEnvironment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, updatedEnvironment.Id).Times(3).Return(client.ConfigurationChanges{}, nil)
gomock.InOrder(
Expand Down Expand Up @@ -809,6 +816,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
}).Times(1).Return(client.Environment{}, errors.New("error"))
})

Expand Down Expand Up @@ -838,13 +846,15 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
}).Times(1).Return(environment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(2).Return(client.ConfigurationChanges{}, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
AutoDeployOnPathChangesOnly: &autoDeployOnPathChangesOnlyDefault,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
}).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 Expand Up @@ -876,6 +886,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"revision": updatedEnvironment.LatestDeploymentLog.BlueprintRevision,
"force_destroy": true,
"terragrunt_working_directory": environment.TerragruntWorkingDirectory,
"vcs_commands_alias": environment.VcsCommandsAlias,
}),
ExpectError: regexp.MustCompile("failed deploying environment: error"),
},
Expand Down Expand Up @@ -916,6 +927,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(gomock.Any()).Return(client.Environment{}, errors.New("error"))
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ resource "env0_environment" "example" {
}
approve_plan_automatically = true
revision = "master"
vcs_commands_alias = "alias"
}

data "env0_configuration_variable" "env_config_variable" {
Expand Down

0 comments on commit bc149ea

Please sign in to comment.