-
Notifications
You must be signed in to change notification settings - Fork 14
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: support new vcs_connection resource #999
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package client | ||
|
||
type VcsConnection struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Url string `json:"url"` | ||
VcsAgentKey string `json:"vcsAgentKey"` | ||
} | ||
|
||
type VcsConnectionCreatePayload struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Url string `json:"url"` | ||
VcsAgentKey string `json:"vcsAgentKey,omitempty"` | ||
} | ||
|
||
type VcsConnectionUpdatePayload struct { | ||
Name string `json:"name"` | ||
VcsAgentKey string `json:"vcsAgentKey,omitempty"` | ||
} | ||
|
||
func (client *ApiClient) VcsConnection(id string) (*VcsConnection, error) { | ||
var result VcsConnection | ||
|
||
if err := client.http.Get("/vcs/connections/"+id, nil, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (client *ApiClient) VcsConnectionCreate(payload VcsConnectionCreatePayload) (*VcsConnection, error) { | ||
organizationId, err := client.OrganizationId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
payloadWithOrg := struct { | ||
VcsConnectionCreatePayload | ||
OrganizationId string `json:"organizationId"` | ||
}{ | ||
VcsConnectionCreatePayload: payload, | ||
OrganizationId: organizationId, | ||
} | ||
|
||
var result VcsConnection | ||
if err := client.http.Post("/vcs/connections", payloadWithOrg, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (client *ApiClient) VcsConnectionUpdate(id string, payload VcsConnectionUpdatePayload) (*VcsConnection, error) { | ||
var result VcsConnection | ||
|
||
if err := client.http.Put("/vcs/connections/"+id, payload, &result); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you already use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah - was just asking to be on the safe side... |
||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (client *ApiClient) VcsConnectionDelete(id string) error { | ||
return client.http.Delete("/vcs/connections/"+id, nil) | ||
} | ||
|
||
func (client *ApiClient) VcsConnections() ([]VcsConnection, error) { | ||
organizationId, err := client.OrganizationId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result []VcsConnection | ||
if err := client.http.Get("/vcs/connections", map[string]string{"organizationId": organizationId}, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package client_test | ||
|
||
import ( | ||
. "github.com/env0/terraform-provider-env0/client" | ||
"github.com/jinzhu/copier" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
var _ = Describe("VcsConnection Client", func() { | ||
mockVcsConnection := VcsConnection{ | ||
Id: "id0", | ||
Name: "test-connection", | ||
Type: "GitHubEnterprise", | ||
Url: "https://github.example.com", | ||
VcsAgentKey: "ENV0_DEFAULT", | ||
} | ||
|
||
Describe("Get VcsConnection", func() { | ||
var returnedVcsConnection *VcsConnection | ||
var err error | ||
|
||
BeforeEach(func() { | ||
httpCall = mockHttpClient.EXPECT(). | ||
Get("/vcs/connections/"+mockVcsConnection.Id, nil, gomock.Any()). | ||
Do(func(path string, request interface{}, response *VcsConnection) { | ||
*response = mockVcsConnection | ||
}) | ||
returnedVcsConnection, err = apiClient.VcsConnection(mockVcsConnection.Id) | ||
}) | ||
|
||
It("Should send GET request", func() { | ||
httpCall.Times(1) | ||
}) | ||
|
||
It("Should return vcs connection", func() { | ||
Expect(*returnedVcsConnection).To(Equal(mockVcsConnection)) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("Create VcsConnection", func() { | ||
var createdVcsConnection *VcsConnection | ||
var err error | ||
|
||
BeforeEach(func() { | ||
mockOrganizationIdCall() | ||
|
||
createPayload := VcsConnectionCreatePayload{} | ||
_ = copier.Copy(&createPayload, &mockVcsConnection) | ||
|
||
expectedCreateRequest := struct { | ||
VcsConnectionCreatePayload | ||
OrganizationId string `json:"organizationId"` | ||
}{ | ||
VcsConnectionCreatePayload: createPayload, | ||
OrganizationId: organizationId, | ||
} | ||
|
||
httpCall = mockHttpClient.EXPECT(). | ||
Post("/vcs/connections", expectedCreateRequest, gomock.Any()). | ||
Do(func(path string, request interface{}, response *VcsConnection) { | ||
*response = mockVcsConnection | ||
}) | ||
|
||
createdVcsConnection, err = apiClient.VcsConnectionCreate(createPayload) | ||
}) | ||
|
||
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 vcs connection", func() { | ||
Expect(*createdVcsConnection).To(Equal(mockVcsConnection)) | ||
}) | ||
}) | ||
|
||
Describe("Update VcsConnection", func() { | ||
var updatedVcsConnection *VcsConnection | ||
var err error | ||
|
||
BeforeEach(func() { | ||
updatePayload := VcsConnectionUpdatePayload{ | ||
Name: mockVcsConnection.Name, | ||
VcsAgentKey: mockVcsConnection.VcsAgentKey, | ||
} | ||
|
||
httpCall = mockHttpClient.EXPECT(). | ||
Put("/vcs/connections/"+mockVcsConnection.Id, updatePayload, gomock.Any()). | ||
Do(func(path string, request interface{}, response *VcsConnection) { | ||
*response = mockVcsConnection | ||
}) | ||
|
||
updatedVcsConnection, err = apiClient.VcsConnectionUpdate(mockVcsConnection.Id, updatePayload) | ||
}) | ||
|
||
It("Should send PUT request with params", func() { | ||
httpCall.Times(1) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("Should return updated vcs connection", func() { | ||
Expect(*updatedVcsConnection).To(Equal(mockVcsConnection)) | ||
}) | ||
}) | ||
|
||
Describe("Delete VcsConnection", func() { | ||
var err error | ||
|
||
BeforeEach(func() { | ||
httpCall = mockHttpClient.EXPECT().Delete("/vcs/connections/"+mockVcsConnection.Id, nil) | ||
err = apiClient.VcsConnectionDelete(mockVcsConnection.Id) | ||
}) | ||
|
||
It("Should send DELETE request", func() { | ||
httpCall.Times(1) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("List VcsConnections", func() { | ||
var returnedVcsConnections []VcsConnection | ||
var err error | ||
mockVcsConnections := []VcsConnection{mockVcsConnection} | ||
|
||
BeforeEach(func() { | ||
mockOrganizationIdCall() | ||
|
||
httpCall = mockHttpClient.EXPECT(). | ||
Get("/vcs/connections", map[string]string{"organizationId": organizationId}, gomock.Any()). | ||
Do(func(path string, request interface{}, response *[]VcsConnection) { | ||
*response = mockVcsConnections | ||
}) | ||
returnedVcsConnections, err = apiClient.VcsConnections() | ||
}) | ||
|
||
It("Should get organization id", func() { | ||
organizationIdCall.Times(1) | ||
}) | ||
|
||
It("Should send GET request", func() { | ||
httpCall.Times(1) | ||
}) | ||
|
||
It("Should return vcs connections", func() { | ||
Expect(returnedVcsConnections).To(Equal(mockVcsConnections)) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
during update I don't think VcsAgentKey can be empty...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it's a PUT and not a PATCH?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's a PUT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discussed internally. This field can be empty (null or undefined).
Therefore, omitempty is needed.