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 the option to add a regex filter for list of teams #984

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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 @@ -23,13 +24,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 @@ -38,13 +57,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"))
},
)
})
}
Loading