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: add support for agent settings #323

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions client/agent_project_assignment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package client

// Key is project id.
// Value is agent id.
type AssignProjectsAgentsAssignmentsPayload map[string]interface{}

type ProjectsAgentsAssignments struct {
DefaultAgent string `json:"defaultAgent"`
ProjectsAgents map[string]interface{} `json:"ProjectsAgents"`
}

func (client *ApiClient) AssignAgentsToProjects(payload AssignProjectsAgentsAssignmentsPayload) (*ProjectsAgentsAssignments, error) {
organizationId, err := client.organizationId()
if err != nil {
return nil, err
}

var result ProjectsAgentsAssignments
if err := client.http.Post("/agents/projects-assignments?organizationId="+organizationId, payload, &result); err != nil {
return nil, err
}

return &result, nil
}

func (client *ApiClient) ProjectsAgentsAssignments() (*ProjectsAgentsAssignments, error) {
organizationId, err := client.organizationId()
if err != nil {
return nil, err
}

var result ProjectsAgentsAssignments
err = client.http.Get("/agents/projects-assignments", map[string]string{"organizationId": organizationId}, &result)
if err != nil {
return nil, err
}

return &result, nil
}

/*

*/
141 changes: 141 additions & 0 deletions client/agent_project_assignment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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"
)

// mockOrganizationIdCall(organizationId)

var _ = Describe("Agent Project Assignment", func() {
psas := map[string]interface{}{
"pid1": "aid1",
"pid2": "aid2",
}

expectedResponse := ProjectsAgentsAssignments{
ProjectsAgents: psas,
}

errorMock := errors.New("error")

Describe("AssignAgentsToProjects", func() {

Describe("Successful", func() {
var actualResult *ProjectsAgentsAssignments
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Post("/agents/projects-assignments?organizationId="+organizationId, gomock.Any(), gomock.Any()).
Do(func(path string, request interface{}, response *ProjectsAgentsAssignments) {
*response = expectedResponse
}).Times(1)
actualResult, err = apiClient.AssignAgentsToProjects(psas)

})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

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

It("should return the POST result", func() {
Expect(*actualResult).To(Equal(expectedResponse))
})

It("Should not return error", func() {
Expect(err).To(BeNil())
})
})

Describe("Failure", func() {
var actualResult *ProjectsAgentsAssignments
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Post("/agents/projects-assignments?organizationId="+organizationId, gomock.Any(), gomock.Any()).
Return(errorMock)

actualResult, err = apiClient.AssignAgentsToProjects(psas)
})

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

It("Should not return results", func() {
Expect(actualResult).To(BeNil())
})
})
})

Describe("ProjectsAgentsAssignments", func() {
Describe("Successful", func() {
var actualResult *ProjectsAgentsAssignments
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Get("/agents/projects-assignments", gomock.Any(), gomock.Any()).
Do(func(path string, request interface{}, response *ProjectsAgentsAssignments) {
*response = expectedResponse
})
actualResult, err = apiClient.ProjectsAgentsAssignments()
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

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

It("Should return the GET result", func() {
Expect(*actualResult).To(Equal(expectedResponse))
})

It("Should not return error", func() {
Expect(err).To(BeNil())
})
})

Describe("Failure", func() {
var actualResult *ProjectsAgentsAssignments
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Get("/agents/projects-assignments", gomock.Any(), gomock.Any()).
Return(errorMock)

actualResult, err = apiClient.ProjectsAgentsAssignments()
})

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

It("Should not return results", func() {
Expect(actualResult).To(BeNil())
})
})
})
})
2 changes: 2 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type ApiClientInterface interface {
ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error)
ApiKeyDelete(id string) error
ApiKeys() ([]ApiKey, error)
AssignAgentsToProjects(payload AssignProjectsAgentsAssignmentsPayload) (*ProjectsAgentsAssignments, error)
ProjectsAgentsAssignments() (*ProjectsAgentsAssignments, error)
}

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