Skip to content

Commit

Permalink
Feat: support new apply-all environment property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Jul 10, 2024
1 parent 5196058 commit a440af9
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 55 deletions.
3 changes: 3 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Environment struct {
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
VcsPrCommentsEnabled bool `json:"vcsPrCommentsEnabled" tfschema:"-"`
BlueprintId string `json:"blueprintId" tfschema:"-"`
IsRemoteBackend *bool `json:"isRemoteBackend" tfschema:"-"`
IsArchived *bool `json:"isArchived" tfschema:"-"`
Expand All @@ -144,6 +145,7 @@ type EnvironmentCreate struct {
TTL *TTL `json:"ttl,omitempty" tfschema:"-"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
VcsPrCommentsEnabled bool `json:"vcsPrCommentsEnabled"`
IsRemoteBackend *bool `json:"isRemoteBackend,omitempty" tfschema:"-"`
Type string `json:"type,omitempty"`
DriftDetectionRequest *DriftDetectionRequest `json:"driftDetectionRequest,omitempty" tfschema:"-"`
Expand Down Expand Up @@ -193,6 +195,7 @@ type EnvironmentUpdate struct {
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias,omitempty"`
VcsPrCommentsEnabled bool `json:"vcsPrCommentsEnabled"`
RequiresApproval *bool `json:"requiresApproval,omitempty" tfschema:"-"`
ContinuousDeployment *bool `json:"continuousDeployment,omitempty" tfschema:"-"`
PullRequestPlanDeployments *bool `json:"pullRequestPlanDeployments,omitempty" tfschema:"-"`
Expand Down
2 changes: 2 additions & 0 deletions client/project_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Policy struct {
ForceRemoteBackend bool `json:"forceRemoteBackend"`
DriftDetectionCron string `json:"driftDetectionCron"`
DriftDetectionEnabled bool `json:"driftDetectionEnabled"`
VcsPrCommentsEnabledDefault bool `json:"vcsPrCommentsEnabledDefault"`
}

type PolicyUpdatePayload struct {
Expand All @@ -36,6 +37,7 @@ type PolicyUpdatePayload struct {
ForceRemoteBackend bool `json:"forceRemoteBackend"`
DriftDetectionCron string `json:"driftDetectionCron"`
DriftDetectionEnabled bool `json:"driftDetectionEnabled"`
VcsPrCommentsEnabledDefault bool `json:"vcsPrCommentsEnabledDefault"`
}

// Policy retrieves a policy from the API
Expand Down
24 changes: 24 additions & 0 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,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,
},
"vcs_pr_comments_enabled": {
Type: schema.TypeBool,
Description: "set to 'true' to enable running VCS PR commands using PR comments. This can be set to 'true' (enabled) without setting alias in 'vcs_commands_alias'.",
Optional: true,
Default: false,
},
"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.",
Expand Down Expand Up @@ -380,6 +386,12 @@ func setEnvironmentSchema(ctx context.Context, d *schema.ResourceData, environme
return fmt.Errorf("schema resource data serialization failed: %v", err)
}

if val := d.Get("vcs_pr_comments_enabled").(bool); !val && environment.VcsPrCommentsEnabled {
// VcsPrCommentsEnabled may have been "forced" to be 'true', ignore the drift.
} else {
d.Set("vcs_pr_comments_enabled", environment.VcsPrCommentsEnabled)
}

if !isTemplateless(d) {
if environment.LatestDeploymentLog.BlueprintId != "" {
d.Set("template_id", environment.LatestDeploymentLog.BlueprintId)
Expand Down Expand Up @@ -882,6 +894,11 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
return client.EnvironmentCreate{}, diag.Errorf("schema resource data deserialization failed: %v", err)
}

if val := d.Get("vcs_commands_alias").(string); len(val) > 0 {
// if alias is set - VcsPrCommentsEnabled must be true.
payload.VcsPrCommentsEnabled = true
}

//lint:ignore SA1019 reason: https://github.com/hashicorp/terraform-plugin-sdk/issues/817
if val, exists := d.GetOkExists("deploy_on_push"); exists {
payload.ContinuousDeployment = boolPtr(val.(bool))
Expand Down Expand Up @@ -1002,6 +1019,11 @@ func getUpdatePayload(d *schema.ResourceData) (client.EnvironmentUpdate, diag.Di
return client.EnvironmentUpdate{}, diag.Errorf("schema resource data deserialization failed: %v", err)
}

if val := d.Get("vcs_commands_alias").(string); len(val) > 0 {
// if alias is set - VcsPrCommentsEnabled must be true.
payload.VcsPrCommentsEnabled = true
}

if d.HasChange("approve_plan_automatically") {
payload.RequiresApproval = boolPtr(!d.Get("approve_plan_automatically").(bool))
}
Expand Down Expand Up @@ -1366,6 +1388,8 @@ func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, me
d.Set("force_destroy", false)
d.Set("removal_strategy", "destroy")

d.Set("vcs_pr_comments_enabled", environment.VcsCommandsAlias != "" || environment.VcsPrCommentsEnabled)

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
} else {
Expand Down
39 changes: 31 additions & 8 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
TerragruntWorkingDirectory: "/terragrunt/directory/",
VcsCommandsAlias: "alias",
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: "namespace",
}
Expand All @@ -58,6 +59,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
TerragruntWorkingDirectory: "/terragrunt/directory/",
VcsCommandsAlias: "alias2",
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: boolPtr(true),
K8sNamespace: "namespace",
Expand Down Expand Up @@ -171,8 +173,9 @@ func TestUnitEnvironmentResource(t *testing.T) {
DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
VcsPrCommentsEnabled: true,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
Expand All @@ -181,6 +184,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: updatedEnvironment.IsArchived,
VcsPrCommentsEnabled: true,
}).Times(1).Return(updatedEnvironment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, updatedEnvironment.Id).Times(3).Return(client.ConfigurationChanges{}, nil)
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", updatedEnvironment.Id).Times(3).Return(nil, nil)
Expand Down Expand Up @@ -609,7 +613,11 @@ func TestUnitEnvironmentResource(t *testing.T) {
ImportState: true,
ImportStateId: environment.Id,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
ImportStateVerifyIgnore: []string{"force_destroy", "vcs_pr_comments_enabled"},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "vcs_pr_comments_enabled", "true"),
resource.TestCheckResourceAttr(accessor, "force_destroy", "false"),
),
},
},
}
Expand All @@ -626,8 +634,9 @@ func TestUnitEnvironmentResource(t *testing.T) {
DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
VcsPrCommentsEnabled: true,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(environment.Id).Times(3).Return(environment, nil)
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down Expand Up @@ -696,13 +705,15 @@ func TestUnitEnvironmentResource(t *testing.T) {
Enabled: true,
Cron: driftDetectionCron,
},
K8sNamespace: environment.K8sNamespace,
K8sNamespace: environment.K8sNamespace,
VcsPrCommentsEnabled: true,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: updatedEnvironment.IsArchived,
}).Times(1).Return(updatedEnvironment, nil)
Expand Down Expand Up @@ -778,13 +789,15 @@ func TestUnitEnvironmentResource(t *testing.T) {
Enabled: true,
Cron: driftDetectionCron,
},
K8sNamespace: environment.K8sNamespace,
K8sNamespace: environment.K8sNamespace,
VcsPrCommentsEnabled: true,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: updatedEnvironment.IsArchived,
}).Times(1).Return(updatedEnvironment, nil)
Expand Down Expand Up @@ -1941,6 +1954,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(client.Environment{}, errors.New("error"))
Expand Down Expand Up @@ -1973,6 +1987,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
Expand All @@ -1983,6 +1998,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: boolPtr(true),
}).Times(1).Return(client.Environment{}, errors.New("error"))
Expand Down Expand Up @@ -2061,6 +2077,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
Expand Down Expand Up @@ -2116,6 +2133,7 @@ func TestUnitEnvironmentWithoutTemplateResource(t *testing.T) {
WorkspaceName: "workspace-name",
TerragruntWorkingDirectory: "/terragrunt/directory/",
VcsCommandsAlias: "alias",
VcsPrCommentsEnabled: true,
LatestDeploymentLog: client.DeploymentLog{
BlueprintId: "id-template-0",
},
Expand Down Expand Up @@ -2182,6 +2200,7 @@ func TestUnitEnvironmentWithoutTemplateResource(t *testing.T) {
PullRequestPlanDeployments: environment.PullRequestPlanDeployments,
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
VcsPrCommentsEnabled: true,
}

templateCreatePayload := client.TemplateCreatePayload{
Expand Down Expand Up @@ -2431,7 +2450,11 @@ func TestUnitEnvironmentWithoutTemplateResource(t *testing.T) {
ImportState: true,
ImportStateId: environment.Id,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
ImportStateVerifyIgnore: []string{"force_destroy", "vcs_pr_comments_enabled"},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "vcs_pr_comments_enabled", "true"),
resource.TestCheckResourceAttr(accessor, "force_destroy", "false"),
),
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions env0/resource_project_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func resourceProjectPolicy() *schema.Resource {
Optional: true,
ValidateDiagFunc: ValidateCronExpression,
},
"vcs_pr_comments_enabled_default": {
Type: schema.TypeBool,
Description: "if 'true' all environments created in this project via the UI will be created with an 'enabled' running VCS PR commands using PR comments. Note: in the provider when creating an environment this is ignored, and must be configured explicitly by setting 'vcs_pr_comments_enabled' to 'true' or setting an alias in 'vcs_commands_alias'.",
Optional: true,
Default: false,
},
},
}
}
Expand Down
Loading

0 comments on commit a440af9

Please sign in to comment.