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 environment client #166

Merged
merged 16 commits into from
Nov 29, 2021
5 changes: 5 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type ApiClientInterface interface {
TeamProjectAssignmentCreateOrUpdate(payload TeamProjectAssignmentPayload) (TeamProjectAssignment, error)
TeamProjectAssignmentDelete(assignmentId string) error
TeamProjectAssignments(projectId string) ([]TeamProjectAssignment, error)
Environments() ([]Environment, error)
Environment(id string) (Environment, error)
EnvironmentCreate(payload EnvironmentCreate) (Environment, error)
EnvironmentDestroy(id string) (Environment, error)
EnvironmentUpdate(id string, payload EnvironmentUpdate) (Environment, error)
}

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

48 changes: 48 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package client

func (self *ApiClient) Environments() ([]Environment, error) {
var result []Environment
err := self.http.Get("/environments", nil, &result)
if err != nil {
return []Environment{}, err
}
return result, nil
}

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

func (self *ApiClient) EnvironmentCreate(payload EnvironmentCreate) (Environment, error) {
var result Environment

err := self.http.Post("/environments", payload, &result)
if err != nil {
return Environment{}, err
}
return result, nil
}

func (self *ApiClient) EnvironmentDestroy(id string) (Environment, error) {
var result Environment
err := self.http.Post("/environments/"+id+"/destroy", nil, &result)
if err != nil {
return Environment{}, err
}
return result, nil
}

func (self *ApiClient) EnvironmentUpdate(id string, payload EnvironmentUpdate) (Environment, error) {
var result Environment
err := self.http.Put("/environments/"+id, payload, &result)

if err != nil {
return Environment{}, err
}
return result, nil
}
170 changes: 170 additions & 0 deletions client/environment_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"
)

const (
environmentId = "env-id"
)

var _ = Describe("Environment Client", func() {
mockEnvironment := Environment{
Id: environmentId,
Name: "env0",
}

Describe("Environments", func() {
var environments []Environment
mockEnvironments := []Environment{mockEnvironment}
var err error

Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/environments", nil, gomock.Any()).
Do(func(path string, request interface{}, response *[]Environment) {
*response = mockEnvironments
})

environments, err = apiClient.Environments()
})

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

It("Should return the environment", func() {
Expect(environments).To(Equal(mockEnvironments))
})
})

Describe("Failure", func() {
It("On error from server return the error", func() {
expectedErr := errors.New("some error")
httpCall = mockHttpClient.EXPECT().
Get("/environments", nil, gomock.Any()).
Return(expectedErr)

_, err = apiClient.Environments()
Expect(expectedErr).Should(Equal(err))
})
})
})

Describe("Environment", func() {
var environment Environment
var err error

Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/environments/"+mockEnvironment.Id, nil, gomock.Any()).
Do(func(path string, request interface{}, response *Environment) {
*response = mockEnvironment
})

environment, err = apiClient.Environment(mockEnvironment.Id)
})

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

It("Should return environments", func() {
Expect(environment).To(Equal(mockEnvironment))
})
})

Describe("Failure", func() {
It("On error from server return the error", func() {
expectedErr := errors.New("some error")
httpCall = mockHttpClient.EXPECT().
Get("/environments/"+mockEnvironment.Id, nil, gomock.Any()).
Return(expectedErr)

_, err = apiClient.Environment(mockEnvironment.Id)
Expect(expectedErr).Should(Equal(err))
})
})
})

Describe("EnvironmentCreate", func() {
var createdEnvironment Environment
var err error

BeforeEach(func() {
createEnvironmentPayload := EnvironmentCreate{}
copier.Copy(&createEnvironmentPayload, &mockEnvironment)

expectedCreateRequest := createEnvironmentPayload

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

createdEnvironment, err = apiClient.EnvironmentCreate(createEnvironmentPayload)
})

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

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

It("Should return the created environment", func() {
Expect(createdEnvironment).To(Equal(mockEnvironment))
})
})

Describe("EnvironmentDelete", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Post("/environments/"+mockEnvironment.Id+"/destroy", nil, gomock.Any())
apiClient.EnvironmentDestroy(mockEnvironment.Id)
})

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

Describe("EnvironmentUpdate", func() {
Describe("Success", func() {
var updatedEnvironment Environment
var err error

BeforeEach(func() {
updateEnvironmentPayload := EnvironmentUpdate{Name: "updated-name"}

httpCall = mockHttpClient.EXPECT().
Put("/environments/"+mockEnvironment.Id, updateEnvironmentPayload, gomock.Any()).
Do(func(path string, request interface{}, response *Environment) {
*response = mockEnvironment
})

updatedEnvironment, err = apiClient.EnvironmentUpdate(mockEnvironment.Id, updateEnvironmentPayload)
})

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 the environment received from API", func() {
Expect(updatedEnvironment).To(Equal(mockEnvironment))
})
})
})
})
Loading