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

Fix: data env0_templates includes deleted templates #389

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion env0/data_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func dataTemplatesRead(ctx context.Context, d *schema.ResourceData, meta interfa
data := []string{}

for _, template := range templates {
data = append(data, template.Name)
if !template.IsDeleted {
data = append(data, template.Name)
}
}

d.Set("names", data)
Expand Down
9 changes: 8 additions & 1 deletion env0/data_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func TestTemplatesDataSource(t *testing.T) {
Name: "name1",
}

template3 := client.Template{
Id: "id2",
Name: "name2",
IsDeleted: true,
}

resourceType := "env0_templates"
resourceName := "test_templates"
accessor := dataSourceAccessor(resourceType, resourceName)
Expand All @@ -32,6 +38,7 @@ func TestTemplatesDataSource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "names.0", template1.Name),
resource.TestCheckResourceAttr(accessor, "names.1", template2.Name),
resource.TestCheckNoResourceAttr(accessor, "names.2"),
),
},
},
Expand All @@ -47,7 +54,7 @@ func TestTemplatesDataSource(t *testing.T) {
t.Run("Success", func(t *testing.T) {
runUnitTest(t,
getTestCase(),
mockTemplates([]client.Template{template1, template2}),
mockTemplates([]client.Template{template1, template2, template3}),
)
})

Expand Down
18 changes: 13 additions & 5 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
provider "random" {}

resource "random_string" "random" {
length = 8
special = false
min_lower = 8
}

resource "env0_project" "test_project" {
name = "Test-Project-for-environment"
name = "Test-Project-for-environment-${random_string.random.result}"
force_destroy = true
}

resource "env0_template" "template" {
name = "Template for environment resource"
name = "Template for environment resource-${random_string.random.result}"
type = "terraform"
repository = "https://github.com/env0/templates"
path = "misc/null-resource"
Expand All @@ -13,7 +21,7 @@ resource "env0_template" "template" {

resource "env0_environment" "example" {
force_destroy = true
name = "environment"
name = "environment-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = env0_template.template.id
wait_for = "FULLY_DEPLOYED"
Expand All @@ -31,7 +39,7 @@ data "env0_configuration_variable" "env_config_variable" {
}

resource "env0_template" "terragrunt_template" {
name = "Terragrunt template for environment resource"
name = "Terragrunt template for environment resource-${random_string.random.result}"
type = "terragrunt"
repository = "https://github.com/env0/templates"
path = "misc/null-resource"
Expand All @@ -41,7 +49,7 @@ resource "env0_template" "terragrunt_template" {

resource "env0_environment" "terragrunt_environment" {
force_destroy = true
name = "environment"
name = "environment-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = env0_template.terragrunt_template.id
approve_plan_automatically = true
Expand Down
9 changes: 4 additions & 5 deletions tests/integration/022_templates/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ resource "env0_template" "github_template2" {

data "env0_templates" "all_templates" {}

# This is removed temporarily until https://github.com/env0/terraform-provider-env0/issues/350 is fixed
#data "env0_template" "templates" {
# for_each = toset(data.env0_templates.all_templates.names)
# name = each.value
#}
data "env0_template" "templates" {
for_each = toset(data.env0_templates.all_templates.names)
name = each.value
}