From 7314bd31b7fef60256f24dd10ebf972f13ca8821 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Tue, 21 May 2024 09:55:31 -0500 Subject: [PATCH] Feat: add API calls for configuration set (#853) * Feat: add API calls for configuration set * Fixes based on PR comments --- client/api_client.go | 5 + client/api_client_mock.go | 74 +++++++++++++++ client/configuration_set.go | 75 +++++++++++++++ client/configuration_set_test.go | 154 +++++++++++++++++++++++++++++++ client/configuration_variable.go | 1 + 5 files changed, 309 insertions(+) create mode 100644 client/configuration_set.go create mode 100644 client/configuration_set_test.go diff --git a/client/api_client.go b/client/api_client.go index 212d8cc0..a6d15fa7 100644 --- a/client/api_client.go +++ b/client/api_client.go @@ -152,6 +152,11 @@ type ApiClientInterface interface { TeamRoleAssignments(payload *TeamRoleAssignmentListPayload) ([]TeamRoleAssignmentPayload, error) KubernetesCredentialsCreate(payload *KubernetesCredentialsCreatePayload) (*Credentials, error) KubernetesCredentialsUpdate(id string, payload *KubernetesCredentialsUpdatePayload) (*Credentials, error) + ConfigurationSetCreate(payload *CreateConfigurationSetPayload) (*ConfigurationSet, error) + ConfigurationSetUpdate(id string, payload *UpdateConfigurationSetPayload) (*ConfigurationSet, error) + ConfigurationSet(id string) (*ConfigurationSet, error) + ConfigurationSetDelete(id string) error + ConfigurationVariablesBySetId(setId string) ([]ConfigurationVariable, error) } func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface { diff --git a/client/api_client_mock.go b/client/api_client_mock.go index 67e928f3..17c5c279 100644 --- a/client/api_client_mock.go +++ b/client/api_client_mock.go @@ -349,6 +349,65 @@ func (mr *MockApiClientInterfaceMockRecorder) CloudCredentialsList() *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloudCredentialsList", reflect.TypeOf((*MockApiClientInterface)(nil).CloudCredentialsList)) } +// ConfigurationSet mocks base method. +func (m *MockApiClientInterface) ConfigurationSet(arg0 string) (*ConfigurationSet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConfigurationSet", arg0) + ret0, _ := ret[0].(*ConfigurationSet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ConfigurationSet indicates an expected call of ConfigurationSet. +func (mr *MockApiClientInterfaceMockRecorder) ConfigurationSet(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationSet", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationSet), arg0) +} + +// ConfigurationSetCreate mocks base method. +func (m *MockApiClientInterface) ConfigurationSetCreate(arg0 *CreateConfigurationSetPayload) (*ConfigurationSet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConfigurationSetCreate", arg0) + ret0, _ := ret[0].(*ConfigurationSet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ConfigurationSetCreate indicates an expected call of ConfigurationSetCreate. +func (mr *MockApiClientInterfaceMockRecorder) ConfigurationSetCreate(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationSetCreate", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationSetCreate), arg0) +} + +// ConfigurationSetDelete mocks base method. +func (m *MockApiClientInterface) ConfigurationSetDelete(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConfigurationSetDelete", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// ConfigurationSetDelete indicates an expected call of ConfigurationSetDelete. +func (mr *MockApiClientInterfaceMockRecorder) ConfigurationSetDelete(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationSetDelete", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationSetDelete), arg0) +} + +// ConfigurationSetUpdate mocks base method. +func (m *MockApiClientInterface) ConfigurationSetUpdate(arg0 string, arg1 *UpdateConfigurationSetPayload) (*ConfigurationSet, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConfigurationSetUpdate", arg0, arg1) + ret0, _ := ret[0].(*ConfigurationSet) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ConfigurationSetUpdate indicates an expected call of ConfigurationSetUpdate. +func (mr *MockApiClientInterfaceMockRecorder) ConfigurationSetUpdate(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationSetUpdate", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationSetUpdate), arg0, arg1) +} + // ConfigurationVariableCreate mocks base method. func (m *MockApiClientInterface) ConfigurationVariableCreate(arg0 ConfigurationVariableCreateParams) (ConfigurationVariable, error) { m.ctrl.T.Helper() @@ -423,6 +482,21 @@ func (mr *MockApiClientInterfaceMockRecorder) ConfigurationVariablesByScope(arg0 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationVariablesByScope", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationVariablesByScope), arg0, arg1) } +// ConfigurationVariablesBySetId mocks base method. +func (m *MockApiClientInterface) ConfigurationVariablesBySetId(arg0 string) ([]ConfigurationVariable, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConfigurationVariablesBySetId", arg0) + ret0, _ := ret[0].([]ConfigurationVariable) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ConfigurationVariablesBySetId indicates an expected call of ConfigurationVariablesBySetId. +func (mr *MockApiClientInterfaceMockRecorder) ConfigurationVariablesBySetId(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfigurationVariablesBySetId", reflect.TypeOf((*MockApiClientInterface)(nil).ConfigurationVariablesBySetId), arg0) +} + // CostCredentialIdsInProject mocks base method. func (m *MockApiClientInterface) CostCredentialIdsInProject(arg0 string) ([]CostCredentialProjectAssignment, error) { m.ctrl.T.Helper() diff --git a/client/configuration_set.go b/client/configuration_set.go new file mode 100644 index 00000000..242ddcbe --- /dev/null +++ b/client/configuration_set.go @@ -0,0 +1,75 @@ +package client + +type CreateConfigurationSetPayload struct { + Name string `json:"name"` + Description string `json:"description"` + // if Scope is "organization", scopeId will be calculated in the functions. + Scope string `json:"scope"` // "project" or "organization". + ScopeId string `json:"scopeId"` // project id or organization id. + ConfigurationProperties []ConfigurationVariable `json:"configurationProperties"` +} + +type UpdateConfigurationSetPayload struct { + Name string `json:"name"` + Description string `json:"description"` + ConfigurationPropertiesChanges []ConfigurationVariable `json:"configurationPropertiesChanges"` // delta changes. +} + +type ConfigurationSet struct { + Id string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` +} + +func (client *ApiClient) ConfigurationSetCreate(payload *CreateConfigurationSetPayload) (*ConfigurationSet, error) { + var result ConfigurationSet + var err error + + if payload.Scope == "organization" { + payload.ScopeId, err = client.OrganizationId() + if err != nil { + return nil, err + } + } + + if err := client.http.Post("/configuration-sets", payload, &result); err != nil { + return nil, err + } + + return &result, nil +} + +func (client *ApiClient) ConfigurationSetUpdate(id string, payload *UpdateConfigurationSetPayload) (*ConfigurationSet, error) { + var result ConfigurationSet + + if err := client.http.Put("/configuration-sets/"+id, payload, &result); err != nil { + return nil, err + } + + return &result, nil +} + +func (client *ApiClient) ConfigurationSet(id string) (*ConfigurationSet, error) { + var result ConfigurationSet + + if err := client.http.Get("/configuration-sets/"+id, nil, &result); err != nil { + return nil, err + } + + return &result, nil +} + +func (client *ApiClient) ConfigurationSetDelete(id string) error { + return client.http.Delete("/configuration-sets/"+id, nil) +} + +func (client *ApiClient) ConfigurationVariablesBySetId(setId string) ([]ConfigurationVariable, error) { + var result []ConfigurationVariable + + if err := client.http.Get("/configuration", map[string]string{ + "setId": setId, + }, &result); err != nil { + return nil, err + } + return result, nil +} diff --git a/client/configuration_set_test.go b/client/configuration_set_test.go new file mode 100644 index 00000000..f994b508 --- /dev/null +++ b/client/configuration_set_test.go @@ -0,0 +1,154 @@ +package client_test + +import ( + . "github.com/env0/terraform-provider-env0/client" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "go.uber.org/mock/gomock" +) + +var _ = Describe("Configuration Set", func() { + id := "id12345" + projectId := "projectId123" + + mockConfigurationSet := ConfigurationSet{ + Id: id, + Name: "name", + Description: "description", + } + + var configurationSet *ConfigurationSet + + Describe("create organization configuration set", func() { + BeforeEach(func() { + mockOrganizationIdCall(organizationId).Times(1) + + createPayload := CreateConfigurationSetPayload{ + Name: "name1", + Description: "des1", + Scope: "organization", + } + + createPayloadWithScopeId := CreateConfigurationSetPayload{ + Name: "name1", + Description: "des1", + Scope: "organization", + ScopeId: organizationId, + } + + httpCall = mockHttpClient.EXPECT(). + Post("/configuration-sets", &createPayloadWithScopeId, gomock.Any()). + Do(func(path string, request interface{}, response *ConfigurationSet) { + *response = mockConfigurationSet + }).Times(1) + + configurationSet, _ = apiClient.ConfigurationSetCreate(&createPayload) + }) + + It("Should return configuration set", func() { + Expect(*configurationSet).To(Equal(mockConfigurationSet)) + }) + }) + + Describe("create project configuration set", func() { + BeforeEach(func() { + createPayload := CreateConfigurationSetPayload{ + Name: "name1", + Description: "des1", + Scope: "project", + ScopeId: projectId, + } + + httpCall = mockHttpClient.EXPECT(). + Post("/configuration-sets", &createPayload, gomock.Any()). + Do(func(path string, request interface{}, response *ConfigurationSet) { + *response = mockConfigurationSet + }).Times(1) + + configurationSet, _ = apiClient.ConfigurationSetCreate(&createPayload) + }) + + It("Should return configuration set", func() { + Expect(*configurationSet).To(Equal(mockConfigurationSet)) + }) + }) + + Describe("update configuration set", func() { + BeforeEach(func() { + updatePayload := UpdateConfigurationSetPayload{ + Name: "name2", + Description: "des2", + } + + httpCall = mockHttpClient.EXPECT(). + Put("/configuration-sets/"+id, &updatePayload, gomock.Any()). + Do(func(path string, request interface{}, response *ConfigurationSet) { + *response = mockConfigurationSet + }).Times(1) + + configurationSet, _ = apiClient.ConfigurationSetUpdate(id, &updatePayload) + }) + + It("Should return configuration set", func() { + Expect(*configurationSet).To(Equal(mockConfigurationSet)) + }) + }) + + Describe("get configuration set by id", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/configuration-sets/"+id, nil, gomock.Any()). + Do(func(path string, request interface{}, response *ConfigurationSet) { + *response = mockConfigurationSet + }).Times(1) + + configurationSet, _ = apiClient.ConfigurationSet(id) + }) + + It("Should return configuration set", func() { + Expect(*configurationSet).To(Equal(mockConfigurationSet)) + }) + }) + + Describe("delete configuration set", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Delete("/configuration-sets/"+id, nil). + Do(func(path string, request interface{}) {}). + Times(1) + + apiClient.ConfigurationSetDelete(id) + }) + + It("Should call delete once", func() {}) + }) + + Describe("get configuration variables by set id", func() { + mockVariables := []ConfigurationVariable{ + { + ScopeId: "a", + Value: "b", + Scope: "c", + Id: "d", + }, + } + + var variables []ConfigurationVariable + + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/configuration", map[string]string{ + "setId": id, + }, gomock.Any()). + Do(func(path string, request interface{}, response *[]ConfigurationVariable) { + *response = mockVariables + }).Times(1) + + variables, _ = apiClient.ConfigurationVariablesBySetId(id) + }) + + It("Should return configuration variables", func() { + Expect(variables).To(Equal(mockVariables)) + }) + }) +}) diff --git a/client/configuration_variable.go b/client/configuration_variable.go index 8225c988..48313d7e 100644 --- a/client/configuration_variable.go +++ b/client/configuration_variable.go @@ -14,6 +14,7 @@ const ( ScopeDeployment Scope = "DEPLOYMENT" ScopeDeploymentLog Scope = "DEPLOYMENT_LOG" ScopeWorkflow Scope = "WORKFLOW" + ScopeSet Scope = "SET" ) type Format string