From 0438162d41ad050cf2aba9dc4c0337c15507c8fa Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Sat, 23 Nov 2024 11:49:32 -0600 Subject: [PATCH] Feat: add the option to add a regex filter for list of teams --- env0/data_teams.go | 26 +++++++++++++++++++++++--- env0/data_teams_test.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/env0/data_teams.go b/env0/data_teams.go index 21d6e822..b2494779 100644 --- a/env0/data_teams.go +++ b/env0/data_teams.go @@ -2,6 +2,7 @@ package env0 import ( "context" + "regexp" "github.com/env0/terraform-provider-env0/client" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -22,6 +23,12 @@ 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: "", + }, }, } } @@ -29,6 +36,18 @@ func dataTeams() *schema.Resource { 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) @@ -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 } diff --git a/env0/data_teams_test.go b/env0/data_teams_test.go index af29386f..df197a03 100644 --- a/env0/data_teams_test.go +++ b/env0/data_teams_test.go @@ -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"), ), }, }, @@ -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{ @@ -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")) + }, + ) + }) }