-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add API calls for configuration set (#853)
* Feat: add API calls for configuration set * Fixes based on PR comments
- Loading branch information
1 parent
4fb6335
commit 7314bd3
Showing
5 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters