Skip to content

Commit

Permalink
Feat: Add HCL configuration variable type (#195)
Browse files Browse the repository at this point in the history
* Feat: Add HCL variable type

* Revert "Feat: Add HCL variable type"

This reverts commit 9e180a8.

* Feat: Add HCL variable type

* set the hclVariable in plan step

* added format to the client

* refactored params object

* added format to the client

* added format to configuration variable schema

* fixed configuration_variable client

* added interpolation unit test with hcl type

* added hcl to data

* fixed empty format bad request error + refactoring

* 1. added  format to environment configuration variable.
2. refactor format.HCL
3. refactor hcl=true to format.

* fixed the data_configuration_variable.go
  • Loading branch information
liranfarage89 authored Dec 28, 2021
1 parent 5515923 commit 2ad99bb
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 102 deletions.
4 changes: 2 additions & 2 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type ApiClient struct {

type ApiClientInterface interface {
ConfigurationVariables(scope Scope, scopeId string) ([]ConfigurationVariable, error)
ConfigurationVariableCreate(name string, value string, isSensitive bool, scope Scope, scopeId string, type_ ConfigurationVariableType, enumValues []string, description string) (ConfigurationVariable, error)
ConfigurationVariableUpdate(id string, name string, value string, isSensitive bool, scope Scope, scopeId string, type_ ConfigurationVariableType, enumValues []string, description string) (ConfigurationVariable, error)
ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error)
ConfigurationVariableUpdate(params ConfigurationVariableUpdateParams) (ConfigurationVariable, error)
ConfigurationVariableDelete(id string) error
Organization() (Organization, error)
organizationId() (string, error)
Expand Down
16 changes: 8 additions & 8 deletions client/api_client_mock.go

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

74 changes: 41 additions & 33 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (self *ApiClient) ConfigurationVariables(scope Scope, scopeId string) ([]Co
return result, nil
}

func (self *ApiClient) ConfigurationVariableCreate(name string, value string, isSensitive bool, scope Scope, scopeId string, type_ ConfigurationVariableType, enumValues []string, description string) (ConfigurationVariable, error) {
if scope == ScopeDeploymentLog || scope == ScopeDeployment {
func (self *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error) {
if params.Scope == ScopeDeploymentLog || params.Scope == ScopeDeployment {
return ConfigurationVariable{}, errors.New("Must not create variable on scope deployment / deploymentLog")
}
organizationId, err := self.organizationId()
Expand All @@ -41,23 +41,20 @@ func (self *ApiClient) ConfigurationVariableCreate(name string, value string, is
}
var result []ConfigurationVariable
request := map[string]interface{}{
"name": name,
"description": description,
"value": value,
"isSensitive": isSensitive,
"scope": scope,
"type": type_,
"name": params.Name,
"description": params.Description,
"value": params.Value,
"isSensitive": params.IsSensitive,
"scope": params.Scope,
"type": params.Type,
"organizationId": organizationId,
}
if scope != ScopeGlobal {
request["scopeId"] = scopeId
}
if enumValues != nil {
request["schema"] = map[string]interface{}{
"type": "string",
"enum": enumValues,
}
if params.Scope != ScopeGlobal {
request["scopeId"] = params.ScopeId
}

request["schema"] = getSchema(params)

requestInArray := []map[string]interface{}{request}
err = self.http.Post("configuration", requestInArray, &result)
if err != nil {
Expand All @@ -66,12 +63,26 @@ func (self *ApiClient) ConfigurationVariableCreate(name string, value string, is
return result[0], nil
}

func getSchema(params ConfigurationVariableCreateParams) map[string]interface{} {
schema := map[string]interface{}{
"type": "string",
}
if params.EnumValues != nil {
schema["enum"] = params.EnumValues
}
if params.Format != Text {
schema["format"] = params.Format
}
return schema
}

func (self *ApiClient) ConfigurationVariableDelete(id string) error {
return self.http.Delete("configuration/" + id)
}

func (self *ApiClient) ConfigurationVariableUpdate(id string, name string, value string, isSensitive bool, scope Scope, scopeId string, type_ ConfigurationVariableType, enumValues []string, description string) (ConfigurationVariable, error) {
if scope == ScopeDeploymentLog || scope == ScopeDeployment {
func (self *ApiClient) ConfigurationVariableUpdate(updateParams ConfigurationVariableUpdateParams) (ConfigurationVariable, error) {
commonParams := updateParams.CommonParams
if commonParams.Scope == ScopeDeploymentLog || commonParams.Scope == ScopeDeployment {
return ConfigurationVariable{}, errors.New("Must not create variable on scope deployment / deploymentLog")
}
organizationId, err := self.organizationId()
Expand All @@ -80,24 +91,21 @@ func (self *ApiClient) ConfigurationVariableUpdate(id string, name string, value
}
var result []ConfigurationVariable
request := map[string]interface{}{
"id": id,
"name": name,
"description": description,
"value": value,
"isSensitive": isSensitive,
"scope": scope,
"type": type_,
"id": updateParams.Id,
"name": commonParams.Name,
"description": commonParams.Description,
"value": commonParams.Value,
"isSensitive": commonParams.IsSensitive,
"scope": commonParams.Scope,
"type": commonParams.Type,
"organizationId": organizationId,
}
if scope != ScopeGlobal {
request["scopeId"] = scopeId
}
if enumValues != nil {
request["schema"] = map[string]interface{}{
"type": "string",
"enum": enumValues,
}
if commonParams.Scope != ScopeGlobal {
request["scopeId"] = commonParams.ScopeId
}

request["schema"] = getSchema(updateParams.CommonParams)

requestInArray := []map[string]interface{}{request}
err = self.http.Post("/configuration", requestInArray, &result)
if err != nil {
Expand Down
53 changes: 36 additions & 17 deletions client/configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
var _ = Describe("Configuration Variable", func() {
isSensitive := true
varType := ConfigurationVariableTypeEnvironment
schema := ConfigurationVariableSchema{
Type: "string",
Format: HCL,
}
mockConfigurationVariable := ConfigurationVariable{
Id: "config-var-id-789",
Name: "configName",
Expand All @@ -22,6 +26,7 @@ var _ = Describe("Configuration Variable", func() {
Type: &varType,
ScopeId: "project-123",
UserId: "user|123",
Schema: &schema,
}

Describe("ConfigurationVariableCreate", func() {
Expand All @@ -39,6 +44,10 @@ var _ = Describe("Configuration Variable", func() {
"scopeId": mockConfigurationVariable.ScopeId,
"scope": mockConfigurationVariable.Scope,
"type": *mockConfigurationVariable.Type,
"schema": map[string]interface{}{
"type": mockConfigurationVariable.Schema.Type,
"format": mockConfigurationVariable.Schema.Format,
},
}}

httpCall = mockHttpClient.EXPECT().
Expand All @@ -48,14 +57,17 @@ var _ = Describe("Configuration Variable", func() {
})

createdConfigurationVariable, _ = apiClient.ConfigurationVariableCreate(
mockConfigurationVariable.Name,
mockConfigurationVariable.Value,
*mockConfigurationVariable.IsSensitive,
mockConfigurationVariable.Scope,
mockConfigurationVariable.ScopeId,
*mockConfigurationVariable.Type,
nil,
mockConfigurationVariable.Description,
ConfigurationVariableCreateParams{
Name: mockConfigurationVariable.Name,
Value: mockConfigurationVariable.Value,
Description: mockConfigurationVariable.Description,
IsSensitive: *mockConfigurationVariable.IsSensitive,
Scope: mockConfigurationVariable.Scope,
ScopeId: mockConfigurationVariable.ScopeId,
Type: *mockConfigurationVariable.Type,
EnumValues: nil,
Format: mockConfigurationVariable.Schema.Format,
},
)
})

Expand Down Expand Up @@ -103,6 +115,9 @@ var _ = Describe("Configuration Variable", func() {
"scopeId": mockConfigurationVariable.ScopeId,
"scope": mockConfigurationVariable.Scope,
"type": *mockConfigurationVariable.Type,
"schema": map[string]interface{}{
"type": mockConfigurationVariable.Schema.Type,
},
}}

httpCall = mockHttpClient.EXPECT().
Expand All @@ -112,15 +127,19 @@ var _ = Describe("Configuration Variable", func() {
})

updatedConfigurationVariable, _ = apiClient.ConfigurationVariableUpdate(
mockConfigurationVariable.Id,
newName,
newValue,
*mockConfigurationVariable.IsSensitive,
mockConfigurationVariable.Scope,
mockConfigurationVariable.ScopeId,
*mockConfigurationVariable.Type,
nil,
newDescription,
ConfigurationVariableUpdateParams{
Id: mockConfigurationVariable.Id,
CommonParams: ConfigurationVariableCreateParams{
Name: newName,
Value: newValue,
Description: newDescription,
IsSensitive: *mockConfigurationVariable.IsSensitive,
Scope: mockConfigurationVariable.Scope,
ScopeId: mockConfigurationVariable.ScopeId,
Type: *mockConfigurationVariable.Type,
EnumValues: nil,
},
},
)
})

Expand Down
29 changes: 27 additions & 2 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ type EnvironmentDeployResponse struct {
}

type ConfigurationVariableSchema struct {
Type string `json:"type"`
Enum []string `json:"enum"`
Type string `json:"type"`
Enum []string `json:"enum"`
Format Format `json:"format,omitempty"`
}

type ConfigurationVariable struct {
Expand All @@ -117,6 +118,30 @@ type ConfigurationVariable struct {
ToDelete *bool `json:"toDelete,omitempty"`
}

type ConfigurationVariableCreateParams struct {
Name string
Value string
IsSensitive bool
Scope Scope
ScopeId string
Type ConfigurationVariableType
EnumValues []string
Description string
Format Format
}

type ConfigurationVariableUpdateParams struct {
CommonParams ConfigurationVariableCreateParams
Id string
}

type Format string

const (
Text Format = ""
HCL Format = "HCL"
)

type Scope string

const (
Expand Down
8 changes: 8 additions & 0 deletions env0/data_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func dataConfigurationVariable() *schema.Resource {
Description: "the configuration variable option",
},
},
"format": {
Type: schema.TypeString,
Description: "specifies the format of the configuration value (for example: HCL)",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -127,6 +132,9 @@ func dataConfigurationVariableRead(ctx context.Context, d *schema.ResourceData,
d.Set("is_sensitive", variable.IsSensitive)
d.Set("scope", variable.Scope)
d.Set("enum", variable.Schema.Enum)
if variable.Schema.Format != client.Text {
d.Set("format", string(variable.Schema.Format))
}
if *variable.Type == client.ConfigurationVariableTypeEnvironment {
d.Set("type", "environment")
} else if *variable.Type == client.ConfigurationVariableTypeTerraform {
Expand Down
3 changes: 2 additions & 1 deletion env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
IsSensitive: &isSensitive,
Scope: client.ScopeEnvironment,
Type: &variableType,
Schema: &client.ConfigurationVariableSchema{Type: "string"},
Schema: &client.ConfigurationVariableSchema{Type: "string", Format: client.HCL},
}

checkResources := resource.ComposeAggregateTestCheckFunc(
Expand All @@ -38,6 +38,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "value", configurationVariable.Value),
resource.TestCheckResourceAttr(accessor, "scope", string(configurationVariable.Scope)),
resource.TestCheckResourceAttr(accessor, "is_sensitive", strconv.FormatBool(*configurationVariable.IsSensitive)),
resource.TestCheckResourceAttr(accessor, "format", string(configurationVariable.Schema.Format)),
)

t.Run("ScopeGlobal", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 2ad99bb

Please sign in to comment.