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: support new vcs_connection resource #999

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export PATH=$PATH:$(go env GOPATH) # if not
2. Install mockgen

```
go install go.uber.org/mock/mockgen@v0.3.0
go install go.uber.org/mock/mockgen@v0.5.0
```

3. Make sure to add this line in files that include the interface you'd wish to mock:
Expand Down
5 changes: 5 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ type ApiClientInterface interface {
CloudAccount(id string) (*CloudAccount, error)
CloudAccounts() ([]CloudAccount, error)
VcsToken(vcsType string, repository string) (*VscToken, error)
VcsConnection(id string) (*VcsConnection, error)
VcsConnectionCreate(payload VcsConnectionCreatePayload) (*VcsConnection, error)
VcsConnectionUpdate(id string, payload VcsConnectionUpdatePayload) (*VcsConnection, error)
VcsConnectionDelete(id string) error
VcsConnections() ([]VcsConnection, error)
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
Expand Down
1,155 changes: 615 additions & 540 deletions client/api_client_mock.go

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions client/http/client_mock.go

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

81 changes: 81 additions & 0 deletions client/vcs_connection.go
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"`
Copy link
Contributor

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...

Copy link
Collaborator Author

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?

Copy link
Contributor

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack!

Copy link
Collaborator Author

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.

}

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already use Put

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
171 changes: 171 additions & 0 deletions client/vcs_connection_test.go
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())
})
})
})
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_variable_set_assignment": resourceVariableSetAssignment(),
"env0_environment_output_configuration_variable": resourceEnvironmentOutputConfigurationVariable(),
"env0_aws_cloud_configuration": resourceAwsCloudConfiguration(),
"env0_vcs_connection": resourceVcsConnection(),
},
}

Expand Down
Loading
Loading