-
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.
Feat: Add Project Policies Support to client (#150)
Co-authored-by: Yaron Yarimi <yaronya@users.noreply.github.com>
- Loading branch information
1 parent
165b3fa
commit f1e32fb
Showing
7 changed files
with
216 additions
and
2 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
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,26 @@ | ||
package client | ||
|
||
// Policy retrieves a policy from the API | ||
func (self *ApiClient) Policy(projectId string) (Policy, error) { | ||
u, err := newQueryURL("/policies", parameter{"projectId", projectId}) | ||
if err != nil { | ||
return Policy{}, err | ||
} | ||
|
||
var result Policy | ||
err = self.http.Get(u.String(), nil, &result) | ||
if err != nil { | ||
return Policy{}, err | ||
} | ||
return result, nil | ||
} | ||
|
||
// PolicyUpdate updates a policy through the API | ||
func (self *ApiClient) PolicyUpdate(payload PolicyUpdatePayload) (Policy, error) { | ||
var result Policy | ||
err := self.http.Put("/policies", payload, &result) | ||
if err != nil { | ||
return Policy{}, err | ||
} | ||
return result, nil | ||
} |
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,107 @@ | ||
package client_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
. "github.com/env0/terraform-provider-env0/client" | ||
gomock "github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const ( | ||
policyId = "policy0" | ||
) | ||
|
||
var _ = Describe("Policy", func() { | ||
mockPolicy := Policy{ | ||
Id: policyId, | ||
} | ||
|
||
Describe("Policy", func() { | ||
var policy Policy | ||
var err error | ||
|
||
path := fmt.Sprintf("/policies?projectId=%s", mockPolicy.ProjectId) | ||
|
||
Describe("Success", func() { | ||
BeforeEach(func() { | ||
policiesResult := mockPolicy | ||
httpCall = mockHttpClient.EXPECT(). | ||
Get(path, nil, gomock.Any()). | ||
Do(func(path string, request interface{}, response *Policy) { | ||
*response = policiesResult | ||
}) | ||
|
||
policy, err = apiClient.Policy(mockPolicy.ProjectId) | ||
}) | ||
|
||
It("Should send GET request once", func() { | ||
httpCall.Times(1) | ||
}) | ||
|
||
It("Should return policy", func() { | ||
Expect(policy).Should(Equal(mockPolicy)) | ||
}) | ||
|
||
It("Should not return an error", func() { | ||
Expect(err).Should(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("Failure", func() { | ||
It("On error from server return the error", func() { | ||
expectedErr := errors.New("some error") | ||
httpCall = mockHttpClient.EXPECT(). | ||
Get(path, nil, gomock.Any()). | ||
Return(expectedErr) | ||
|
||
_, err = apiClient.Policy(mockPolicy.ProjectId) | ||
Expect(expectedErr).Should(Equal(err)) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("PolicyUpdate", func() { | ||
updatePolicyPayload := PolicyUpdatePayload{ProjectId: "project0"} | ||
Describe("Success", func() { | ||
var updatedPolicy Policy | ||
var err error | ||
|
||
BeforeEach(func() { | ||
httpCall = mockHttpClient.EXPECT(). | ||
Put("/policies", updatePolicyPayload, gomock.Any()). | ||
Do(func(path string, request interface{}, response *Policy) { | ||
*response = mockPolicy | ||
}) | ||
|
||
updatedPolicy, err = apiClient.PolicyUpdate(updatePolicyPayload) | ||
}) | ||
|
||
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(updatedPolicy).To(Equal(mockPolicy)) | ||
}) | ||
}) | ||
|
||
Describe("Failure", func() { | ||
It("On error from server return the error", func() { | ||
expectedErr := errors.New("some error") | ||
httpCall = mockHttpClient.EXPECT(). | ||
Put("/policies", updatePolicyPayload, gomock.Any()). | ||
Return(expectedErr) | ||
|
||
_, err := apiClient.PolicyUpdate(updatePolicyPayload) | ||
Expect(expectedErr).Should(Equal(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,25 @@ | ||
package client | ||
|
||
import "net/url" | ||
|
||
type parameter struct { | ||
key string | ||
value string | ||
} | ||
|
||
func newQueryURL(path string, params ...parameter) (*url.URL, error) { | ||
u, err := url.Parse(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
q := u.Query() | ||
|
||
for _, param := range params { | ||
q.Add(param.key, param.value) | ||
} | ||
|
||
u.RawQuery = q.Encode() | ||
|
||
return u, nil | ||
} |