Skip to content

Commit

Permalink
Fix env0 configuration variable import #126 (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-br authored Feb 1, 2022
1 parent 5160c76 commit 706d5cd
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 22 deletions.
28 changes: 17 additions & 11 deletions client/api_client_mock.go

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

5 changes: 3 additions & 2 deletions env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package env0
import (
"errors"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"regexp"
"strconv"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestUnitConfigurationVariableData(t *testing.T) {
Expand Down
15 changes: 10 additions & 5 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func resourceConfigurationVariable() *schema.Resource {
}
}

const templateScope = "TEMPLATE"

func whichScope(d *schema.ResourceData) (client.Scope, string) {
scope := client.ScopeGlobal
scopeId := ""
Expand All @@ -115,10 +117,6 @@ func whichScope(d *schema.ResourceData) (client.Scope, string) {
scope = client.ScopeTemplate
scopeId = templateId.(string)
}
if templateId, ok := d.GetOk("blueprint_id"); ok {
scope = client.ScopeTemplate
scopeId = templateId.(string)
}
if environmentId, ok := d.GetOk("environment_id"); ok {
scope = client.ScopeEnvironment
scopeId = environmentId.(string)
Expand Down Expand Up @@ -302,7 +300,14 @@ func resourceConfigurationVariableImport(ctx context.Context, d *schema.Resource
return nil, errors.New(getErr[0].Summary)
} else {
d.SetId(variable.Id)
scopeName := strings.ToLower(fmt.Sprintf("%s_id", variable.Scope))

var scopeName string

if variable.Scope == client.ScopeTemplate {
scopeName = strings.ToLower(fmt.Sprintf("%s_id", templateScope))
} else {
scopeName = strings.ToLower(fmt.Sprintf("%s_id", variable.Scope))
}

d.Set(scopeName, configurationParams.ScopeId)

Expand Down
84 changes: 84 additions & 0 deletions env0/resource_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,88 @@ resource "%s" "test" {
mock.EXPECT().ConfigurationVariableDelete(configVar.Id).Times(1).Return(nil)
})
})

importStateId_id := `{ "Scope": "BLUEPRINT", "ScopeId": "id0", "Id": "id1", "name": "name0"}`
importStateId_name := `{ "Scope": "BLUEPRINT", "ScopeId": "id0", "name": "name0"}`
ResourceNameImport := "env0_configuration_variable.test"
configVarImport := client.ConfigurationVariable{
Id: "id1",
Name: "name0",
Description: "desc0",
Value: "Variable",
IsReadonly: &isReadonly,
IsRequired: &isRequired,
Scope: "BLUEPRINT",
}
stepConfirImport := resourceConfigCreate(resourceType, resourceName, map[string]interface{}{

"name": configVarImport.Name,
"description": configVarImport.Description,
"value": configVarImport.Value,
"is_read_only": strconv.FormatBool(*configVar.IsReadonly),
"is_required": strconv.FormatBool(*configVar.IsRequired),
"template_id": "id0",
})

configurationVariableCreateParamsImport := client.ConfigurationVariableCreateParams{
Name: configVarImport.Name,
Value: configVarImport.Value,
IsSensitive: false,
Scope: client.ScopeTemplate,
ScopeId: "id0",
Type: client.ConfigurationVariableTypeEnvironment,
EnumValues: nil,
Description: configVarImport.Description,
Format: client.Text,
IsRequired: *configVarImport.IsRequired,
IsReadonly: *configVarImport.IsReadonly,
}
t.Run("import by name", func(t *testing.T) {

createTestCaseForImport := resource.TestCase{
Steps: []resource.TestStep{
{
Config: stepConfirImport,
},
{
ResourceName: ResourceNameImport,
ImportState: true,
ImportStateId: importStateId_name,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_required", "is_read_only"},
},
},
}

runUnitTest(t, createTestCaseForImport, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariableCreate(configurationVariableCreateParamsImport).Times(1).Return(configVarImport, nil)
mock.EXPECT().ConfigurationVariablesById(configVarImport.Id).Times(2).Return(configVarImport, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeTemplate, configurationVariableCreateParamsImport.ScopeId).AnyTimes().Return([]client.ConfigurationVariable{configVarImport}, nil)
mock.EXPECT().ConfigurationVariableDelete(configVarImport.Id).Times(1).Return(nil)
})
})

t.Run("import by id", func(t *testing.T) {

createTestCaseForImport := resource.TestCase{
Steps: []resource.TestStep{
{
Config: stepConfirImport,
},
{
ResourceName: ResourceNameImport,
ImportState: true,
ImportStateId: importStateId_id,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_required", "is_read_only"},
},
},
}

runUnitTest(t, createTestCaseForImport, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariableCreate(configurationVariableCreateParamsImport).Times(1).Return(configVarImport, nil)
mock.EXPECT().ConfigurationVariablesById(configVarImport.Id).Times(3).Return(configVarImport, nil)
mock.EXPECT().ConfigurationVariableDelete(configVarImport.Id).Times(1).Return(nil)
})
})
}
5 changes: 3 additions & 2 deletions env0/resource_project_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package env0

import (
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"regexp"
"testing"
)

func TestUnitProjectResource(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions examples/resources/env0_environment_scheduling/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ data "env0_environment" "example" {

resource "env0_environment_schedling" "example" {
environment_id = data.env0_environment.example.id
deploy_cron = "5 * * * *"
destroy_cron = "10 * * * *"
deploy_cron = "5 * * * *"
destroy_cron = "10 * * * *"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/adhocore/gronx v0.2.6
github.com/go-resty/resty/v2 v2.6.0
github.com/golang/mock v1.4.3
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.2.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-exec v0.15.0 // indirect
Expand Down

0 comments on commit 706d5cd

Please sign in to comment.