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 HCL configuration variable type #195

Merged
merged 14 commits into from
Dec 28, 2021
Merged
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
9 changes: 9 additions & 0 deletions env0/data_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func dataConfigurationVariable() *schema.Resource {
Description: "the configuration variable option",
},
},
"hcl": {
Type: schema.TypeBool,
Description: "set to true if the value is in HCL format",
Default: false,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -127,6 +133,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.Hcl {
d.Set("hcl", true)
}
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, "hcl", strconv.FormatBool(configurationVariable.Schema.Format == client.Hcl)),
)

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