Skip to content

Commit

Permalink
Merge branch 'main' into fix-typos
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Dec 22, 2024
2 parents e037d50 + b666191 commit 1151100
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
3 changes: 1 addition & 2 deletions docs/resources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ If true must specify one of the following - 'github_installation_id' if using Gi
If true must specify one of the following - 'github_installation_id' if using GitHub, 'gitlab_project_id' and 'token_id' if using GitLab, or 'bitbucket_client_key' if using BitBucket.
- `sub_environment_configuration` (Block List) the subenvironments for a workflow environment. Template type must be 'workflow'. Must match the configuration as defined in 'env0.workflow.yml' (see [below for nested schema](#nestedblock--sub_environment_configuration))
- `template_id` (String) the template id the environment is to be created from.
Important 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.
Important note: After the environment is created, this field cannot be modified.
Important 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. Please note that changing this attribute will require environment redeploy
- `terragrunt_working_directory` (String) The working directory path to be used by a Terragrunt template. If left empty '/' is used. Note: modifying this field destroys the current environment and creates a new one
- `ttl` (String) the date the environment should be destroyed at (iso format). omitting this attribute will result in infinite ttl.
- `variable_sets` (List of String) a list of IDs of variable sets to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'
Expand Down
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. Please note that changing this attribute will require environment redeploy",
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 {
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")
}

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{
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

0 comments on commit 1151100

Please sign in to comment.