Skip to content

Commit

Permalink
Add team API calls (#136)
Browse files Browse the repository at this point in the history
* Add team API calls + tests
  • Loading branch information
RLRabinowitz committed Aug 30, 2021
1 parent 013f6ea commit 4af7b89
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 4 deletions.
5 changes: 5 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type ApiClientInterface interface {
AssignCloudCredentialsToProject(projectId string, credentialId string) (CloudCredentialsProjectAssignment, error)
RemoveCloudCredentialsFromProject(projectId string, credentialId string) error
CloudCredentialIdsInProject(projectId string) ([]string, error)
Team(id string) (Team, error)
Teams() ([]Team, error)
TeamCreate(payload TeamCreatePayload) (Team, error)
TeamUpdate(id string, payload TeamUpdatePayload) (Team, error)
TeamDelete(id string) error
}

func NewApiClient(client http.HttpClientInterface) ApiClientInterface {
Expand Down
74 changes: 74 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.

18 changes: 18 additions & 0 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,21 @@ type ApiKey struct {
OrganizationId string `json:"organizationId"`
Type string `json:"type"`
}

type TeamCreatePayload struct {
Name string `json:"name"`
Description string `json:"description"`
OrganizationId string `json:"organizationId"`
}

type TeamUpdatePayload struct {
Name string `json:"name"`
Description string `json:"description"`
}

type Team struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
OrganizationId string `json:"organizationId"`
}
63 changes: 63 additions & 0 deletions client/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package client

import "errors"

func (self *ApiClient) TeamCreate(payload TeamCreatePayload) (Team, error) {
if payload.Name == "" {
return Team{}, errors.New("Must specify team name on creation")
}
if payload.OrganizationId != "" {
return Team{}, errors.New("Must not specify organizationId")
}
organizationId, err := self.organizationId()
if err != nil {
return Team{}, err
}
payload.OrganizationId = organizationId

var result Team
err = self.http.Post("/teams", payload, &result)
if err != nil {
return Team{}, err
}
return result, nil
}

func (self *ApiClient) Team(id string) (Team, error) {
var result Team
err := self.http.Get("/teams/"+id, nil, &result)
if err != nil {
return Team{}, err
}
return result, nil
}

func (self *ApiClient) TeamDelete(id string) error {
return self.http.Delete("/teams/" + id)
}

func (self *ApiClient) TeamUpdate(id string, payload TeamUpdatePayload) (Team, error) {
if payload.Name == "" {
return Team{}, errors.New("Must specify team name on update")
}

var result Team
err := self.http.Put("/teams/"+id, payload, &result)
if err != nil {
return Team{}, err
}
return result, nil
}

func (self *ApiClient) Teams() ([]Team, error) {
organizationId, err := self.organizationId()
if err != nil {
return nil, err
}
var result []Team
err = self.http.Get("/teams/organizations/"+organizationId, nil, &result)
if err != nil {
return nil, err
}
return result, err
}
170 changes: 170 additions & 0 deletions client/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package client_test

import (
"errors"
. "github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
"github.com/jinzhu/copier"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Teams Client", func() {
mockTeam := Team{
Id: "team-id",
Name: "team-name",
}

Describe("Get Single Team", func() {
var returnedTeam Team

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/teams/"+mockTeam.Id, gomock.Nil(), gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})
returnedTeam, _ = apiClient.Team(mockTeam.Id)
})

It("Should send GET request", func() {
httpCall.Times(1)
})

It("Should return team", func() {
Expect(returnedTeam).To(Equal(mockTeam))
})
})

Describe("Get All Teams", func() {
var returnedTeams []Team
mockTeams := []Team{mockTeam}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
httpCall = mockHttpClient.EXPECT().
Get("/teams/organizations/"+organizationId, nil, gomock.Any()).
Do(func(path string, request interface{}, response *[]Team) {
*response = mockTeams
})
returnedTeams, _ = apiClient.Teams()
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

It("Should send GET request", func() {
httpCall.Times(1)
})

It("Should return teams", func() {
Expect(returnedTeams).To(Equal(mockTeams))
})
})

Describe("TeamCreate", func() {
Describe("Success", func() {
var createdTeam Team
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

createTeamPayload := TeamCreatePayload{}
copier.Copy(&createTeamPayload, &mockTeam)

expectedCreateRequest := createTeamPayload
expectedCreateRequest.OrganizationId = organizationId

httpCall = mockHttpClient.EXPECT().
Post("/teams", expectedCreateRequest, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})

createdTeam, err = apiClient.TeamCreate(createTeamPayload)
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

It("Should send POST request with params", func() {
httpCall.Times(1)
})

It("Should not return error", func() {
Expect(err).To(BeNil())
})

It("Should return created team", func() {
Expect(createdTeam).To(Equal(mockTeam))
})
})

Describe("Failure", func() {
It("Should fail when team has no name", func() {
teamWithoutNamePayload := TeamCreatePayload{Description: "team-without-name"}
_, err := apiClient.TeamCreate(teamWithoutNamePayload)
Expect(err).To(BeEquivalentTo(errors.New("Must specify team name on creation")))
})

It("Should fail if request includes organizationId (should be inferred automatically)", func() {
payloadWithOrgId := TeamCreatePayload{Name: "team-name", OrganizationId: "org-id"}
_, err := apiClient.TeamCreate(payloadWithOrgId)
Expect(err).To(BeEquivalentTo(errors.New("Must not specify organizationId")))
})
})
})

Describe("TeamDelete", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete("/teams/" + mockTeam.Id)
apiClient.TeamDelete(mockTeam.Id)
})

It("Should send DELETE request with team id", func() {
httpCall.Times(1)
})
})

Describe("TeamUpdate", func() {
Describe("Success", func() {
var updatedTeam Team
var err error

BeforeEach(func() {
updateTeamPayload := TeamUpdatePayload{Name: "updated-name"}

httpCall = mockHttpClient.EXPECT().
Put("/teams/"+mockTeam.Id, updateTeamPayload, gomock.Any()).
Do(func(path string, request interface{}, response *Team) {
*response = mockTeam
})

updatedTeam, err = apiClient.TeamUpdate(mockTeam.Id, updateTeamPayload)
})

It("Should send Put request with expected payload", func() {
httpCall.Times(1)
})

It("Should not return an error", func() {
Expect(err).To(BeNil())
})

It("Should return team received from API", func() {
Expect(updatedTeam).To(Equal(mockTeam))
})
})

Describe("Failure", func() {
It("Should fail if team has no name", func() {
payloadWithNoName := TeamUpdatePayload{Description: "team-without-name"}
_, err := apiClient.TeamUpdate(mockTeam.Id, payloadWithNoName)
Expect(err).To(BeEquivalentTo(errors.New("Must specify team name on update")))
})
})
})
})
4 changes: 2 additions & 2 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
provider "env0" {
api_key = var.env0_api_key # or use ENV0_API_KEY
api_secret = var.env0_api_secret # or use ENV0_API_SECRET
api_key = var.env0_api_key # or use ENV0_API_KEY
api_secret = var.env0_api_secret # or use ENV0_API_SECRET
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ data "env0_project" "project" {

resource "env0_cloud_credentials_project_assignment" "example" {
credential_id = env0_aws_credentials.credentials.id
project_id = data.env0_project.project.id
project_id = data.env0_project.project.id
}
2 changes: 1 addition & 1 deletion examples/resources/env0_configuration_variable/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource "env0_configuration_variable" "example" {
resource "env0_configuration_variable" "drop_down" {
name = "ENVIRONMENT_VARIABLE_DROP_DOWN"
value = "first option"
enum = [
enum = [
"first option",
"second option"
]
Expand Down

0 comments on commit 4af7b89

Please sign in to comment.