-
Notifications
You must be signed in to change notification settings - Fork 14
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 teams (#343)
* Feat: add data source for teams * Update env0/data_teams.go Co-authored-by: GiliFaroEnv0 <92937594+GiliFaroEnv0@users.noreply.github.com> Co-authored-by: GiliFaroEnv0 <92937594+GiliFaroEnv0@users.noreply.github.com>
- Loading branch information
1 parent
bd37e0c
commit e4447b5
Showing
7 changed files
with
175 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 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 | ||
} |
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,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")) | ||
}, | ||
) | ||
}) | ||
} |
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_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 | ||
} |
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,4 @@ | ||
{ | ||
"team1_name": "team1", | ||
"team2_name": "team2" | ||
} |
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,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 : "" | ||
} |