Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into fix-handle_drift_withIsDeleted-#225
  • Loading branch information
samuel-br committed Mar 7, 2022
2 parents d388c31 + 48a9479 commit 4649799
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ jobs:
go-version: ${{ secrets.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
- name: Generate mocks
run: |
go install github.com/golang/mock/mockgen@v1.6.0
go generate client/api_client.go
- name: Go fmt
run: |
! go fmt ./... | read
! go fmt ./... | read
- name: Go Test
run: go test -v ./...
3 changes: 3 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type ApiClientInterface interface {
EnvironmentSchedulingDelete(environmentId string) error
WorkflowTrigger(environmentId string) ([]WorkflowTrigger, error)
WorkflowTriggerUpsert(environmentId string, request WorkflowTriggerUpsertPayload) ([]WorkflowTrigger, error)
EnvironmentDriftDetection(environmentId string) (EnvironmentSchedulingExpression, error)
EnvironmentUpdateDriftDetection(environmentId string, payload EnvironmentSchedulingExpression) (EnvironmentSchedulingExpression, error)
EnvironmentStopDriftDetection(environmentId string) error
}

func NewApiClient(client http.HttpClientInterface) ApiClientInterface {
Expand Down
50 changes: 44 additions & 6 deletions client/api_client_mock.go

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

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

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

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

func (self *ApiClient) EnvironmentUpdateDriftDetection(environmentId string, payload EnvironmentSchedulingExpression) (EnvironmentSchedulingExpression, error) {
var result EnvironmentSchedulingExpression

err := self.http.Patch("/scheduling/drift-detection/environments/"+environmentId, payload, &result)
if err != nil {
return EnvironmentSchedulingExpression{}, err
}

return result, nil
}

func (self *ApiClient) EnvironmentStopDriftDetection(environmentId string) error {
err := self.http.Patch("/scheduling/drift-detection/environments/"+environmentId, EnvironmentSchedulingExpression{Enabled: false}, &EnvironmentScheduling{})
if err != nil {
return err
}

return nil
}
114 changes: 114 additions & 0 deletions client/environment_drift_detection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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("EnvironmentDriftDetection", func() {
environmentId := "env"
path := "/scheduling/drift-detection/environments/" + environmentId
mockError := errors.New("I don't like milk")
schedulingExpression := EnvironmentSchedulingExpression{Cron: "0 * * * *", Enabled: true}
var driftResponse EnvironmentSchedulingExpression

Describe("Get", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get(path, nil, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentSchedulingExpression) {
*response = schedulingExpression
})
driftResponse, _ = apiClient.EnvironmentDriftDetection(environmentId)
})

It("Should return the drift scheduling response", func() {
Expect(driftResponse).To(Equal(schedulingExpression))
})
})

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

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get(path, gomock.Any(), gomock.Any()).
Return(mockError)

_, err = apiClient.EnvironmentDriftDetection(environmentId)

})

It("Should fail if API call fails", func() {
Expect(err).To(Equal(mockError))
})
})
})
Describe("Update", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Patch(path, schedulingExpression, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentSchedulingExpression) {
*response = schedulingExpression
})
driftResponse, _ = apiClient.EnvironmentUpdateDriftDetection(environmentId, schedulingExpression)
})

It("Should return the drift scheduling response", func() {
Expect(driftResponse).To(Equal(schedulingExpression))
})
})

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

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Patch(path, schedulingExpression, gomock.Any()).
Return(mockError)

_, err = apiClient.EnvironmentUpdateDriftDetection(environmentId, schedulingExpression)

})

It("Should fail if API call fails", func() {
Expect(err).To(Equal(mockError))
})
})
})
Describe("Delete", func() {
Describe("Success", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Patch(path, EnvironmentSchedulingExpression{Enabled: false}, gomock.Any()).
Do(func(path string, request interface{}, response *EnvironmentSchedulingExpression) {
*response = schedulingExpression
})
_ = apiClient.EnvironmentStopDriftDetection(environmentId)
})
})

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

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Patch(path, EnvironmentSchedulingExpression{Enabled: false}, gomock.Any()).
Return(mockError)

err = apiClient.EnvironmentStopDriftDetection(environmentId)

})

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

})
2 changes: 1 addition & 1 deletion client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ type PolicyUpdatePayload struct {
}

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

Expand Down
12 changes: 6 additions & 6 deletions env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariablesById(configurationVariable.Id).AnyTimes().
Return(configurationVariable, nil)
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeGlobal, "").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
})
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariablesById(configurationVariable.Id).AnyTimes().
Return(configurationVariable, nil)
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeGlobal, "").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
})
Expand All @@ -128,7 +128,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariables(client.ScopeTemplate, "template_id").AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeTemplate, "template_id").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
})
Expand All @@ -144,7 +144,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariables(client.ScopeEnvironment, configurationVariable.Id).AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, configurationVariable.Id).AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
})
Expand All @@ -160,7 +160,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeGlobal, "").AnyTimes().
Return(nil, errors.New("not found"))
})
})
Expand All @@ -176,7 +176,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeGlobal, "").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
})
Expand Down
Loading

0 comments on commit 4649799

Please sign in to comment.