Skip to content

Commit f24e71f

Browse files
authored
Fix: env0_environment should return an error when template_id update… (#1044)
* Feat: env0_environment should return an error when template_id update is not applied on an environment that is "Waiting for approval" * modify * approve-plan
1 parent 2d76c80 commit f24e71f

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

env0/resource_environment.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,19 @@ func deploy(d *schema.ResourceData, apiClient client.ApiClientInterface) diag.Di
837837
return diag.FromErr(err)
838838
}
839839

840+
// See: https://github.com/env0/terraform-provider-env0/issues/1005
841+
// If the user requires approval for deployment, we cannot update the template_id.
842+
if d.HasChange("template_id") {
843+
environment, err := apiClient.Environment(d.Id())
844+
if err != nil {
845+
return diag.Errorf("could not get environment: %v", err)
846+
}
847+
848+
if environment.RequiresApproval != nil && *environment.RequiresApproval {
849+
return diag.Errorf("cannot update template_id when user requires approval for deployment. Please set 'approve_plan_automatically' to 'true' or avoid updating the 'template_id' field")
850+
}
851+
}
852+
840853
subEnvironments, err := getSubEnvironments(d)
841854
if err != nil {
842855
return diag.Errorf("failed to extract subenvrionments from resourcedata: %v", err)

env0/resource_environment_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
644644
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
645645
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
646646
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(2).Return(nil, nil),
647+
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
647648
mock.EXPECT().EnvironmentDeploy(environment.Id, deployRequest).Times(1).Return(client.EnvironmentDeployResponse{
648649
Id: "deployment-id",
649650
}, nil),

tests/integration/012_environment/main.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ resource "env0_environment" "move_environment" {
8787
}
8888

8989
resource "env0_environment" "modify_template" {
90-
depends_on = [env0_template_project_assignment.assignment]
91-
force_destroy = true
92-
name = "environment-modify-template-${random_string.random.result}"
93-
project_id = env0_project.test_project.id
94-
template_id = var.second_run ? env0_template.template2.id : env0_template.template.id
95-
prevent_auto_deploy = true
90+
depends_on = [env0_template_project_assignment.assignment]
91+
force_destroy = true
92+
name = "environment-modify-template-${random_string.random.result}"
93+
project_id = env0_project.test_project.id
94+
template_id = var.second_run ? env0_template.template2.id : env0_template.template.id
95+
prevent_auto_deploy = true
96+
approve_plan_automatically = true
9697
}
9798

9899

0 commit comments

Comments
 (0)