Skip to content

Commit

Permalink
Chore: Get teams using name filter (#521)
Browse files Browse the repository at this point in the history
* getting teams by name

* mock gen

* refactor data_team test

* DRY team api client

* fixing typo
  • Loading branch information
avnerenv0 authored Oct 30, 2022
1 parent 94e53c1 commit 37e9541
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ Afterwards, when cleanup is required, just set `DESTROY_MODE` to `DESTROY_ONLY`
Run from root directory:

```shell
go test ./...
go test -v ./...
```

#### Running a single provider test

```shell
export TEST_PATTERN="TestUnitConfigurationVariableResource/Create" && go test ./env0
export TEST_PATTERN="TestUnitConfigurationVariableResource/Create" && go test -v ./env0
```

#### How to use mocks
Expand Down
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type ApiClientInterface interface {
RemoveCostCredentialsFromProject(projectId string, credentialId string) error
Team(id string) (Team, error)
Teams() ([]Team, error)
TeamsByName(name string) ([]Team, error)
TeamCreate(payload TeamCreatePayload) (Team, error)
TeamUpdate(id string, payload TeamUpdatePayload) (Team, error)
TeamDelete(id string) error
Expand Down
15 changes: 15 additions & 0 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,23 @@ func (client *ApiClient) TeamUpdate(id string, payload TeamUpdatePayload) (Team,
return result, nil
}

func (client *ApiClient) Teams() ([]Team, error) {
func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, err
}
var result []Team
err = client.http.Get("/teams/organizations/"+organizationId, nil, &result)
err = client.http.Get("/teams/organizations/"+organizationId, params, &result)
if err != nil {
return nil, err
}
return result, err
}

func (client *ApiClient) Teams() ([]Team, error) {
return client.GetTeams(nil)
}

func (client *ApiClient) TeamsByName(name string) ([]Team, error) {
return client.GetTeams(map[string]string{"name": name})
}
12 changes: 6 additions & 6 deletions env0/data_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func TestTeamDataSource(t *testing.T) {
}
}

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

Expand All @@ -76,7 +76,7 @@ func TestTeamDataSource(t *testing.T) {
t.Run("By Name", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(teamFieldsByName),
mockListTeamsCall([]client.Team{team, otherTeam}),
mockTeamsByNameCall([]client.Team{team, otherTeam}),
)
})

Expand All @@ -90,21 +90,21 @@ func TestTeamDataSource(t *testing.T) {
t.Run("Throw error when by name and more than one team exists", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(teamFieldsByName, "Found multiple teams for name"),
mockListTeamsCall([]client.Team{team, team, otherTeam}),
mockTeamsByNameCall([]client.Team{team, team, otherTeam}),
)
})

t.Run("Throw error when by name and no projects found at all", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(teamFieldsByName, "Could not find an env0 team with name"),
mockListTeamsCall([]client.Team{}),
mockTeamsByNameCall([]client.Team{}),
)
})

t.Run("Throw error when by name and no teams found with that name", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(teamFieldsByName, "Could not find an env0 team with name"),
mockListTeamsCall([]client.Team{otherTeam}),
mockTeamsByNameCall([]client.Team{otherTeam}),
)
})
}
4 changes: 2 additions & 2 deletions env0/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func resourceTeamImport(ctx context.Context, d *schema.ResourceData, meta interf
}
}

func getTeamByName(name interface{}, meta interface{}) (client.Team, diag.Diagnostics) {
func getTeamByName(name string, meta interface{}) (client.Team, diag.Diagnostics) {
apiClient := meta.(client.ApiClientInterface)
teams, err := apiClient.Teams()
teams, err := apiClient.TeamsByName(name)
if err != nil {
return client.Team{}, diag.Errorf("Could not get teams: %v", err)
}
Expand Down

0 comments on commit 37e9541

Please sign in to comment.