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: add data source for templates #345

Merged
merged 3 commits into from
Apr 28, 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
48 changes: 48 additions & 0 deletions env0/data_templates.go
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
}
69 changes: 69 additions & 0 deletions env0/data_templates_test.go
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"))
},
)
})
}
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_project_policy": dataPolicy(),
"env0_configuration_variable": dataConfigurationVariable(),
"env0_template": dataTemplate(),
"env0_templates": dataTemplates(),
"env0_ssh_key": dataSshKey(),
"env0_aws_cost_credentials": dataCostCredentials(string(client.AwsCostCredentialsType)),
"env0_azure_cost_credentials": dataCostCredentials(string(client.AzureCostCredentialsType)),
Expand Down
14 changes: 14 additions & 0 deletions examples/data-sources/env0_templates/data-source.tf
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
}
15 changes: 15 additions & 0 deletions tests/integration/022_templates/conf.tf
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
}
1 change: 1 addition & 0 deletions tests/integration/022_templates/expected_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
44 changes: 44 additions & 0 deletions tests/integration/022_templates/main.tf
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
}