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: environment scheduling api client #207

Merged
merged 9 commits into from
Jan 18, 2022
3 changes: 3 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type ApiClientInterface interface {
EnvironmentUpdate(id string, payload EnvironmentUpdate) (Environment, error)
EnvironmentDeploy(id string, payload DeployRequest) (EnvironmentDeployResponse, error)
EnvironmentUpdateTTL(id string, payload TTL) (Environment, error)
EnvironmentScheduling(environmentId string) (EnvironmentScheduling, error)
EnvironmentSchedulingUpdate(environmentId string, payload EnvironmentScheduling) (EnvironmentScheduling, error)
EnvironmentSchedulingDelete(environmentId string) error
}

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

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

import "errors"

func (self *ApiClient) EnvironmentScheduling(environmentId string) (EnvironmentScheduling, error) {
var result EnvironmentScheduling

err := self.http.Get("/scheduling/environments/"+environmentId, nil, &result)
if err != nil {
return result, err
}
return result, nil
}

func (self *ApiClient) EnvironmentSchedulingUpdate(environmentId string, payload EnvironmentScheduling) (EnvironmentScheduling, error) {
var result EnvironmentScheduling

if payload.Deploy.Cron == payload.Destroy.Cron {
return EnvironmentScheduling{}, errors.New("deploy and destroy cron expressions must not be the same")
}

err := self.http.Put("/scheduling/environments/"+environmentId, payload, &result)
if err != nil {
return EnvironmentScheduling{}, err
}

return result, nil
}

func (self *ApiClient) EnvironmentSchedulingDelete(environmentId string) error {
err := self.http.Put("/scheduling/environments/"+environmentId, EnvironmentScheduling{}, nil)
if err != nil {
return err
}

return nil
}
143 changes: 143 additions & 0 deletions client/environment_scheduling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package client_test

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

var _ = Describe("EnvironmentScheduling", func() {
mockEnvironmentId := "env0"
mockError := errors.New("very cool error")

mockDeployCronPayload := EnvironmentSchedulingExpression{Cron: "1 * * * *", Enabled: true}
mockDestroyCronPayload := EnvironmentSchedulingExpression{Cron: "0 * * * *", Enabled: true}

mockEnvironmentSchedulingPayload := EnvironmentScheduling{
Deploy: mockDeployCronPayload,
Destroy: mockDestroyCronPayload,
}

var environmentSchedulingResponse EnvironmentScheduling

Describe("Update", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Put("/scheduling/environments/"+mockEnvironmentId, mockEnvironmentSchedulingPayload, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentScheduling) {
*response = mockEnvironmentSchedulingPayload
}).Times(1)
environmentSchedulingResponse, _ = apiClient.EnvironmentSchedulingUpdate(mockEnvironmentId, mockEnvironmentSchedulingPayload)

})

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

It("Should return environment scheduling response", func() {
Expect(environmentSchedulingResponse).To(Equal(mockEnvironmentSchedulingPayload))
})
})

Describe("Failure", func() {
It("Should fail if cron expressions are the same", func() {
mockFailedEnvironmentSchedulingPayload := EnvironmentScheduling{
Deploy: mockDeployCronPayload,
Destroy: mockDeployCronPayload,
}
_, err := apiClient.EnvironmentSchedulingUpdate(mockEnvironmentId, mockFailedEnvironmentSchedulingPayload)
Expect(err).To(BeEquivalentTo(errors.New("deploy and destroy cron expressions must not be the same")))
})

It("Should fail if API call fails", func() {
httpCall = mockHttpClient.EXPECT().
Put("/scheduling/environments/"+mockEnvironmentId, gomock.Any(), gomock.Any()).
Return(mockError).
Times(1)

_, err := apiClient.EnvironmentSchedulingUpdate(mockEnvironmentId, mockEnvironmentSchedulingPayload)

Expect(err).To(BeEquivalentTo(mockError))
})

})

})

Describe("Get", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/scheduling/environments/"+mockEnvironmentId, nil, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentScheduling) {
*response = mockEnvironmentSchedulingPayload
})
environmentSchedulingResponse, _ = apiClient.EnvironmentScheduling(mockEnvironmentId)
})

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

It("Should return the environment scheduling response", func() {
Expect(environmentSchedulingResponse).To(Equal(mockEnvironmentSchedulingPayload))
})
})

Describe("Fail", func() {
var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/scheduling/environments/"+mockEnvironmentId, gomock.Any(), gomock.Any()).
Return(mockError).
Times(1)

_, err = apiClient.EnvironmentScheduling(mockEnvironmentId)

})

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

It("Should fail if API call fails", func() {
Expect(err).To(Equal(mockError))
})
})

})

Describe("Delete", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Put("/scheduling/environments/"+mockEnvironmentId, EnvironmentScheduling{}, nil)
apiClient.EnvironmentSchedulingDelete(mockEnvironmentId)
})

It("Should send PUT request with empty environment scheduling object", func() {
httpCall.Times(1)
})
})

Describe("Fail", func() {
var err error
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Put("/scheduling/environments/"+mockEnvironmentId, gomock.Any(), gomock.Any()).
Return(mockError).
Times(1)
err = apiClient.EnvironmentSchedulingDelete(mockEnvironmentId)
})

It("Should return error", func() {
Expect(err).To(BeEquivalentTo(mockError))
})
})
})

})
8 changes: 4 additions & 4 deletions client/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
. "github.com/onsi/gomega"
)

const (
environmentId = "env-id"
)

var _ = Describe("Environment Client", func() {
const (
environmentId = "env-id"
)

mockEnvironment := Environment{
Id: environmentId,
Name: "env0",
Expand Down
10 changes: 10 additions & 0 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ type PolicyUpdatePayload struct {
ContinuousDeploymentDefault bool `json:"continuousDeploymentDefault"`
}

type EnvironmentSchedulingExpression struct {
Cron string `json:"cron"`
Enabled bool `json:"enabled"`
}

type EnvironmentScheduling struct {
Deploy EnvironmentSchedulingExpression `json:"deploy,omitempty"`
Destroy EnvironmentSchedulingExpression `json:"destroy,omitempty"`
}

func (p PolicyUpdatePayload) MarshalJSON() ([]byte, error) {
type serial struct {
ProjectId string `json:"projectId"`
Expand Down