-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add data source for templates (#345)
* Feat: add data source for templates * updated integration test to generate random names
- Loading branch information
1 parent
06b5f6e
commit 12008eb
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataTemplates() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataTemplatesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"names": { | ||
Type: schema.TypeList, | ||
Description: "list of all templates (by name)", | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "the template name", | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataTemplatesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
templates, err := apiClient.Templates() | ||
if err != nil { | ||
return diag.Errorf("Could not get templates: %v", err) | ||
} | ||
|
||
data := []string{} | ||
|
||
for _, template := range templates { | ||
data = append(data, template.Name) | ||
} | ||
|
||
d.Set("names", data) | ||
|
||
// Not really needed. But required by Terraform SDK - https://github.com/hashicorp/terraform-plugin-sdk/issues/541 | ||
d.SetId("all_templates_names") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestTemplatesDataSource(t *testing.T) { | ||
template1 := client.Template{ | ||
Id: "id0", | ||
Name: "name0", | ||
} | ||
|
||
template2 := client.Template{ | ||
Id: "id1", | ||
Name: "name1", | ||
} | ||
|
||
resourceType := "env0_templates" | ||
resourceName := "test_templates" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getTestCase := func() resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "names.0", template1.Name), | ||
resource.TestCheckResourceAttr(accessor, "names.1", template2.Name), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
mockTemplates := func(returnValue []client.Template) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Templates().AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
runUnitTest(t, | ||
getTestCase(), | ||
mockTemplates([]client.Template{template1, template2}), | ||
) | ||
}) | ||
|
||
t.Run("API Call Error", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
ExpectError: regexp.MustCompile("error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Templates().AnyTimes().Return(nil, errors.New("error")) | ||
}, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
data "env0_templates" "all_templates" {} | ||
|
||
data "env0_template" "templates" { | ||
for_each = toset(data.env0_templates.all_templates.names) | ||
name = each.value | ||
} | ||
|
||
output "template1_name" { | ||
value = data.env0_template.templates["Github Test-111"].name | ||
} | ||
|
||
output "template2_name" { | ||
value = data.env0_template.templates["Github Test-222"].name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
terraform { | ||
backend "local" { | ||
} | ||
required_providers { | ||
env0 = { | ||
source = "terraform-registry.env0.com/env0/env0" | ||
} | ||
} | ||
} | ||
|
||
provider "env0" {} | ||
|
||
variable "second_run" { | ||
default = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
provider "random" {} | ||
|
||
resource "random_string" "random" { | ||
length = 8 | ||
special = false | ||
min_lower = 8 | ||
} | ||
|
||
data "env0_template" "github_template" { | ||
name = "Github Integrated Template" | ||
} | ||
|
||
resource "env0_template" "github_template1" { | ||
name = "Github Test ${random_string.random.result}-1" | ||
description = "Template description - GitHub" | ||
type = "terraform" | ||
repository = data.env0_template.github_template.repository | ||
github_installation_id = data.env0_template.github_template.github_installation_id | ||
path = "misc/null-resource" | ||
retries_on_deploy = 3 | ||
retry_on_deploy_only_when_matches_regex = "abc" | ||
retries_on_destroy = 1 | ||
terraform_version = "0.15.1" | ||
} | ||
|
||
resource "env0_template" "github_template2" { | ||
name = "Github Test ${random_string.random.result}-2" | ||
description = "Template description - GitHub" | ||
type = "terraform" | ||
repository = data.env0_template.github_template.repository | ||
github_installation_id = data.env0_template.github_template.github_installation_id | ||
path = "misc/null-resource" | ||
retries_on_deploy = 3 | ||
retry_on_deploy_only_when_matches_regex = "abc" | ||
retries_on_destroy = 1 | ||
terraform_version = "0.15.1" | ||
} | ||
|
||
data "env0_templates" "all_templates" {} | ||
|
||
data "env0_template" "templates" { | ||
for_each = toset(data.env0_templates.all_templates.names) | ||
name = each.value | ||
} |