Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: resource env0_environment - add support to change template_id #998

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -176,7 +175,7 @@ func resourceEnvironment() *schema.Resource {
},
"template_id": {
Type: schema.TypeString,
Description: "the template id the environment is to be created from.\nImportant note: the template must first be assigned to the same project as the environment (project_id). Use 'env0_template_project_assignment' to assign the template to the project. In addition, be sure to leverage 'depends_on' if applicable.\nImportant note: After the environment is created, this field cannot be modified.",
Description: "the template id the environment is to be created from.\nImportant note: the template must first be assigned to the same project as the environment (project_id). Use 'env0_template_project_assignment' to assign the template to the project. In addition, be sure to leverage 'depends_on' if applicable",
TomerHeber marked this conversation as resolved.
Show resolved Hide resolved
Optional: true,
ExactlyOneOf: []string{"without_template_settings", "template_id"},
},
Expand Down Expand Up @@ -382,13 +381,6 @@ func resourceEnvironment() *schema.Resource {
Optional: true,
},
},
CustomizeDiff: customdiff.ValidateChange("template_id", func(ctx context.Context, oldValue, newValue, meta interface{}) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this blocked changing template_id...

if oldValue != "" && oldValue != newValue {
return errors.New("template_id may not be modified, create a new environment instead")
}

return nil
}),
}
}

Expand Down Expand Up @@ -736,7 +728,7 @@ func shouldDeploy(d *schema.ResourceData) bool {
}
}

return d.HasChanges("revision", "configuration", "sub_environment_configuration", "variable_sets")
return d.HasChanges("revision", "configuration", "sub_environment_configuration", "variable_sets", "template_id")
Copy link
Collaborator Author

@TomerHeber TomerHeber Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will call redeploy if template_id has changed.

}

func shouldUpdate(d *schema.ResourceData) bool {
Expand Down
29 changes: 26 additions & 3 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
})

t.Run("avoid modifying template id", func(t *testing.T) {
t.Run("redeploy on change in template id", func(t *testing.T) {
templateId := "template-id"
newTemplateId := "new-template-id"

Expand All @@ -581,6 +581,19 @@ func TestUnitEnvironmentResource(t *testing.T) {
},
}

updatedEnvironment := client.Environment{
Id: environment.Id,
Name: environment.Name,
ProjectId: environment.ProjectId,
LatestDeploymentLog: client.DeploymentLog{
BlueprintId: newTemplateId,
},
}

deployRequest := client.DeployRequest{
BlueprintId: newTemplateId,
}

testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Expand All @@ -604,8 +617,12 @@ func TestUnitEnvironmentResource(t *testing.T) {
"template_id": newTemplateId,
"force_destroy": true,
}),
PlanOnly: true,
ExpectError: regexp.MustCompile("template_id may not be modified, create a new environment instead"),
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", newTemplateId),
),
},
},
}
Expand All @@ -626,6 +643,12 @@ func TestUnitEnvironmentResource(t *testing.T) {
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, 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().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(2).Return(nil, nil),
mock.EXPECT().EnvironmentDeploy(environment.Id, deployRequest).Times(1).Return(client.EnvironmentDeployResponse{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important - it shows that redeploy is called.

Id: "deployment-id",
}, nil),
mock.EXPECT().Environment(environment.Id).Times(1).Return(updatedEnvironment, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1),
)
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ resource "env0_template" "template" {
terraform_version = "0.15.1"
}

resource "env0_template" "template2" {
repository = data.env0_template.github_template_for_environment.repository
github_installation_id = data.env0_template.github_template_for_environment.github_installation_id
name = "Template for environment resource 2-${random_string.random.result}"
type = "terraform"
path = "misc/null-resource"
terraform_version = "0.16.1"
}

resource "env0_template_project_assignment" "assignment" {
template_id = env0_template.template.id
project_id = env0_project.test_project.id
Expand Down Expand Up @@ -77,6 +86,16 @@ resource "env0_environment" "move_environment" {
prevent_auto_deploy = true
}

resource "env0_environment" "modify_template" {
depends_on = [env0_template_project_assignment.assignment]
force_destroy = true
name = "environment-modify-template-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = var.second_run ? env0_template.template2.id : env0_template.template.id
prevent_auto_deploy = true
}


resource "env0_custom_role" "custom_role1" {
name = "custom-role-${random_string.random.result}"
permissions = [
Expand Down
Loading