Skip to content

Commit

Permalink
Feat: add API calls for configuration set (#853)
Browse files Browse the repository at this point in the history
* Feat: add API calls for configuration set

* Fixes based on PR comments
  • Loading branch information
TomerHeber committed May 21, 2024
1 parent 4fb6335 commit 7314bd3
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
74 changes: 74 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.

75 changes: 75 additions & 0 deletions client/configuration_set.go
Original file line number Diff line number Diff line change
@@ -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
}
154 changes: 154 additions & 0 deletions client/configuration_set_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
1 change: 1 addition & 0 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
ScopeDeployment Scope = "DEPLOYMENT"
ScopeDeploymentLog Scope = "DEPLOYMENT_LOG"
ScopeWorkflow Scope = "WORKFLOW"
ScopeSet Scope = "SET"
)

type Format string
Expand Down

0 comments on commit 7314bd3

Please sign in to comment.