Skip to content

Commit

Permalink
Feat: Add HCL variable type
Browse files Browse the repository at this point in the history
  • Loading branch information
liranfarage89 committed Dec 22, 2021
1 parent 5515923 commit 9e180a8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 95 deletions.
31 changes: 29 additions & 2 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/rodaine/hclencoder"
)

func resourceConfigurationVariable() *schema.Resource {
Expand All @@ -33,10 +34,25 @@ func resourceConfigurationVariable() *schema.Resource {
Optional: true,
},
"value": {
Type: schema.TypeString,
Description: "value for the configuration variable",
Type: schema.TypeString,
Description: "value for the configuration variable",
ConflictsWith: []string{"hcl_value"},
Optional: true,
},
"hcl_value": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeMap,
//Computed: false,
Elem: &schema.Schema{
Type: schema.TypeString,
//Computed: false,
},
},
Description: "HCL value for the configuration variable",
Required: true,
Sensitive: true,
//Computed: false,
},
"is_sensitive": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -111,7 +127,14 @@ func resourceConfigurationVariableCreate(ctx context.Context, d *schema.Resource
scope, scopeId := whichScope(d)
name := d.Get("name").(string)
description := d.Get("description").(string)

value := d.Get("value").(string)

if hclValue, ok := d.GetOk("hcl_value"); ok {
encode, _ := hclencoder.Encode(hclValue)
hclValue = strings.ReplaceAll(strings.ReplaceAll(string(encode), " ", ""), "\n", "")
}

isSensitive := d.Get("is_sensitive").(bool)
typeAsString := d.Get("type").(string)
var type_ client.ConfigurationVariableType
Expand All @@ -128,6 +151,7 @@ func resourceConfigurationVariableCreate(ctx context.Context, d *schema.Resource
return getEnumErr
}

//TODO: add hcl_value
configurationVariable, err := apiClient.ConfigurationVariableCreate(name, value, isSensitive, scope, scopeId, type_, actualEnumValues, description)
if err != nil {
return diag.Errorf("could not create configurationVariable: %v", err)
Expand Down Expand Up @@ -170,7 +194,10 @@ func resourceConfigurationVariableRead(ctx context.Context, d *schema.ResourceDa
if variable.Id == id {
d.Set("name", variable.Name)
d.Set("description", variable.Description)

d.Set("value", variable.Value)
//d.Set("hcl_value", variable.Value)

d.Set("is_sensitive", variable.IsSensitive)
if variable.Type != nil && *variable.Type == client.ConfigurationVariableTypeTerraform {
d.Set("type", "terraform")
Expand Down
45 changes: 45 additions & 0 deletions env0/resource_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"regexp"
"strings"
"testing"
)

Expand Down Expand Up @@ -49,6 +50,50 @@ func TestUnitConfigurationVariableResource(t *testing.T) {
mock.EXPECT().ConfigurationVariableDelete(configVar.Id).Times(1).Return(nil)
})
})

accessor2 := resourceAccessor(resourceType, resourceName+"2")

configVar2 := client.ConfigurationVariable{
Id: "id0",
Name: "name1",
Description: "desc1",
Value: "[{foo=\"bar\"},]",
}

stepConfig2 := resourceConfigCreate(resourceType, resourceName+"2", map[string]interface{}{
"name": configVar2.Name,
"description": configVar2.Description,
//"hcl_value": "[{foo=\"bar\"},]",
})
println(stepConfig2)
stepConfig2 = strings.Replace(stepConfig2, "}", "", 1)
//stepConfig2 += fmt.Sprintf("\n\t%s = %s \n }", "hcl_value", "[{ foo = \"bar\" }]")
stepConfig2 += fmt.Sprintf("\n\t%s = %s \n }", "hcl_value", "[{ foo = \"bar\" }]")
println(stepConfig2)
t.Run("Create2", func(t *testing.T) {

createTestCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: stepConfig2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor2, "id", configVar2.Id),
resource.TestCheckResourceAttr(accessor2, "name", configVar2.Name),
resource.TestCheckResourceAttr(accessor2, "description", configVar2.Description),
resource.TestCheckResourceAttr(accessor2, "hcl_value", "[{foo=\"bar\"},]"),
),
},
},
}

runUnitTest(t, createTestCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariableCreate(configVar2.Name, configVar2.Value, false, client.ScopeGlobal, "", client.ConfigurationVariableTypeEnvironment,
nil, configVar2.Description).Times(1).Return(configVar, nil)
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").Times(1).Return([]client.ConfigurationVariable{configVar2}, nil)
mock.EXPECT().ConfigurationVariableDelete(configVar2.Id).Times(1).Return(nil)
})
})

t.Run("Create Enum", func(t *testing.T) {
schema := client.ConfigurationVariableSchema{
Type: "string",
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ require (
github.com/google/uuid v1.2.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.4
github.com/imdario/mergo v0.3.11 // indirect
github.com/jarcoal/httpmock v1.0.8
github.com/jinzhu/copier v0.3.2
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/onsi/ginkgo v1.16.2
github.com/onsi/gomega v1.12.0
github.com/rodaine/hclencoder v0.0.1
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
)
Loading

0 comments on commit 9e180a8

Please sign in to comment.