Skip to content

Commit

Permalink
Feat: add the option to add a regex filter for list of teams
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Nov 23, 2024
1 parent c43e444 commit 0438162
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
26 changes: 23 additions & 3 deletions env0/data_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"context"
"regexp"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -22,13 +23,31 @@ func dataTeams() *schema.Resource {
Description: "the team name",
},
},
"filter": {
Type: schema.TypeString,
Description: "add an optional regex filter, only names matching the filter will be added to 'names'. Note: The regex filter is Golang flavor",
Optional: true,
Default: "",
},
},
}
}

func dataTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

var regex *regexp.Regexp

var err error

filter := d.Get("filter").(string)
if filter != "" {
regex, err = regexp.Compile(filter)
if err != nil {
return diag.Errorf("Invalid filter: %v", err)
}
}

teams, err := apiClient.Teams()
if err != nil {
return diag.Errorf("Could not get teams: %v", err)
Expand All @@ -37,13 +56,14 @@ func dataTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}
data := []string{}

for _, team := range teams {
data = append(data, team.Name)
if regex == nil || regex.MatchString(team.Name) {
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")
d.SetId("all_teams_names" + filter)

return nil
}
40 changes: 37 additions & 3 deletions env0/data_teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@ func TestTeamsDataSource(t *testing.T) {
Description: "A team's description",
}

team3 := client.Team{
Id: "id2",
Name: "name3",
Description: "A team's description",
}

resourceType := "env0_teams"
resourceName := "test_teams"
accessor := dataSourceAccessor(resourceType, resourceName)

getTestCase := func() resource.TestCase {
getTestCase := func(params map[string]interface{}) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}),
Config: dataSourceConfigCreate(resourceType, resourceName, params),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "names.0", team1.Name),
resource.TestCheckResourceAttr(accessor, "names.1", team2.Name),
resource.TestCheckNoResourceAttr(accessor, "names.2"),
),
},
},
Expand All @@ -48,11 +55,20 @@ func TestTeamsDataSource(t *testing.T) {

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

t.Run("Success with regex filter", func(t *testing.T) {
runUnitTest(t,
getTestCase(map[string]interface{}{
"filter": "name(?:1|2)",
}),
mockTeams([]client.Team{team1, team2, team3}),
)
})

t.Run("API Call Error", func(t *testing.T) {
runUnitTest(t,
resource.TestCase{
Expand All @@ -68,4 +84,22 @@ func TestTeamsDataSource(t *testing.T) {
},
)
})

t.Run("invalid regex filter", func(t *testing.T) {
runUnitTest(t,
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"filter": "(ab",
}),
ExpectError: regexp.MustCompile("Invalid filter:.+"),
},
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Teams().AnyTimes().Return(nil, errors.New("error"))
},
)
})
}

0 comments on commit 0438162

Please sign in to comment.