-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
334 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters