Skip to content

Commit

Permalink
Merge branch 'main' into chore-add-proper-release-drafts-#324
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Apr 27, 2022
2 parents d69045c + e4447b5 commit 4a85210
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
48 changes: 48 additions & 0 deletions env0/data_teams.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 dataTeams() *schema.Resource {
return &schema.Resource{
ReadContext: dataTeamsRead,

Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Description: "list of all teams (by name)",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "the team name",
},
},
},
}
}

func dataTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)
teams, err := apiClient.Teams()
if err != nil {
return diag.Errorf("Could not get teams: %v", err)
}

data := []string{}

for _, team := range teams {
data = append(data, team.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_teams_names")

return nil
}
71 changes: 71 additions & 0 deletions env0/data_teams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package env0

import (
"errors"
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestTeamsDataSource(t *testing.T) {
team1 := client.Team{
Id: "id0",
Name: "name1",
Description: "A team's description",
}

team2 := client.Team{
Id: "id1",
Name: "name2",
Description: "A team's description",
}

resourceType := "env0_teams"
resourceName := "test_teams"
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", team1.Name),
resource.TestCheckResourceAttr(accessor, "names.1", team2.Name),
),
},
},
}
}

mockTeams := func(returnValue []client.Team) func(mockFunc *client.MockApiClientInterface) {
return func(mock *client.MockApiClientInterface) {
mock.EXPECT().Teams().AnyTimes().Return(returnValue, nil)
}
}

t.Run("Success", func(t *testing.T) {
runUnitTest(t,
getTestCase(),
mockTeams([]client.Team{team1, team2}),
)
})

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().Teams().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 @@ -61,6 +61,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_gcp_credentials": dataGcpCredentials(),
"env0_azure_credentials": dataAzureCredentials(),
"env0_team": dataTeam(),
"env0_teams": dataTeams(),
"env0_environment": dataEnvironment(),
"env0_workflow_triggers": dataWorkflowTriggers(),
"env0_notification": dataNotification(),
Expand Down
14 changes: 14 additions & 0 deletions examples/data-sources/env0_teams/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "env0_teams" "all_teams" {}

data "env0_team" "teams" {
for_each = toset(data.env0_teams.all_teams.names)
name = each.value
}

output "team1_name" {
value = data.env0_team.teams["team1"].name
}

output "team2_name" {
value = data.env0_team.teams["team2"].name
}
15 changes: 15 additions & 0 deletions tests/integration/021_teams/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
}
4 changes: 4 additions & 0 deletions tests/integration/021_teams/expected_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"team1_name": "team1",
"team2_name": "team2"
}
22 changes: 22 additions & 0 deletions tests/integration/021_teams/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "env0_team" "team_resource1" {
name = "team1"
}

resource "env0_team" "team_resource2" {
name = "team2"
}

data "env0_teams" "all_teams" {}

data "env0_team" "teams" {
for_each = toset(data.env0_teams.all_teams.names)
name = each.value
}

output "team1_name" {
value = var.second_run ? data.env0_team.teams["team1"].name : ""
}

output "team2_name" {
value = var.second_run ? data.env0_team.teams["team2"].name : ""
}

0 comments on commit 4a85210

Please sign in to comment.