Skip to content

Commit

Permalink
Fix: env0_environment import missing some fields (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Jul 23, 2023
1 parent 9db74b0 commit 9a65c08
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type Environment struct {
Id string `json:"id"`
Name string `json:"name"`
ProjectId string `json:"projectId"`
WorkspaceName string `json:"workspaceName,omitempty"`
WorkspaceName string `json:"workspaceName,omitempty" tfschema:"workspace"`
RequiresApproval *bool `json:"requiresApproval,omitempty" tfschema:"-"`
ContinuousDeployment *bool `json:"continuousDeployment,omitempty" tfschema:"deploy_on_push,omitempty"`
PullRequestPlanDeployments *bool `json:"pullRequestPlanDeployments,omitempty" tfschema:"run_plan_on_pull_requests,omitempty"`
Expand Down
19 changes: 18 additions & 1 deletion env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta

d.SetId(environment.Id)
d.Set("deployment_id", environment.LatestDeploymentLogId)
d.Set("auto_deploy_on_path_changes_only", environment.AutoDeployOnPathChangesOnly)
if environment.AutoDeployOnPathChangesOnly != nil {
d.Set("auto_deploy_on_path_changes_only", *environment.AutoDeployOnPathChangesOnly)
}

setEnvironmentSchema(d, environment, environmentConfigurationVariables)

Expand Down Expand Up @@ -1113,6 +1115,21 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta
d.Set("deployment_id", environment.LatestDeploymentLogId)
setEnvironmentSchema(d, environment, environmentConfigurationVariables)

if environment.IsRemoteBackend != nil {
d.Set("is_remote_backend", *environment.IsRemoteBackend)
}

if environment.AutoDeployOnPathChangesOnly != nil {
d.Set("auto_deploy_on_path_changes_only", *environment.AutoDeployOnPathChangesOnly)
}

d.Set("is_inactive", false) // default is false.
if environment.IsArchived != nil {
d.Set("is_inactive", *environment.IsArchived)
}

d.Set("force_destroy", false)

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
} else {
Expand Down
60 changes: 49 additions & 11 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (

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

func TestUnitEnvironmentResource(t *testing.T) {
resourceType := "env0_environment"
resourceName := "test"
resourceNameImport := resourceType + "." + resourceName
accessor := resourceAccessor(resourceType, resourceName)
templateId := "template-id"
deploymentLogId := "deploymentLogId0"
Expand All @@ -26,7 +28,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
updatedDriftDetectionCron := "*/10 1 * * *"

environment := client.Environment{
Id: "id0",
Id: uuid.New().String(),
Name: "my-environment",
ProjectId: "project-id",
WorkspaceName: "workspace-name",
Expand Down Expand Up @@ -182,6 +184,42 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
})

t.Run("Import By Id", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: createEnvironmentResourceConfig(environment),
},
{
ResourceName: resourceNameImport,
ImportState: true,
ImportStateId: environment.Id,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(1).Return(template, nil)
mock.EXPECT().EnvironmentCreate(client.EnvironmentCreate{
Name: environment.Name,
ProjectId: environment.ProjectId,
WorkspaceName: environment.WorkspaceName,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(environment.Id).Times(3).Return(environment, nil)
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(3).Return(client.ConfigurationChanges{}, nil)
})
})

t.Run("Success create and remove drift cron", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
Expand Down Expand Up @@ -344,7 +382,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Success in create and deploy with variables update", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
Expand Down Expand Up @@ -546,7 +584,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Create configuration variables - default values", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
Expand Down Expand Up @@ -616,7 +654,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Create and redeploy configuration variables - sensitive values", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
Expand Down Expand Up @@ -762,7 +800,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Create configuration variables - schema type string", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
Expand Down Expand Up @@ -834,7 +872,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
// Tests use-cases where the response returned by the backend varies from the order of the state.
t.Run("Create unordered configuration variables", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
Expand Down Expand Up @@ -914,7 +952,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Update to: revision, configuration should trigger a deployment", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
Expand Down Expand Up @@ -1035,7 +1073,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
testTTL := func() {
t.Run("TTL update", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LifespanEndAt: "2021-12-08T11:45:11Z",
Expand Down Expand Up @@ -1109,7 +1147,7 @@ func TestUnitEnvironmentResource(t *testing.T) {

t.Run("Deleting TTL from environment should update ttl to infinite", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LifespanEndAt: "2021-12-08T11:45:11Z",
Expand Down Expand Up @@ -1184,7 +1222,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
falsey := false
truthyFruity := true
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
LatestDeploymentLog: client.DeploymentLog{
Expand Down Expand Up @@ -1286,7 +1324,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
testForceDestroy := func() {
t.Run("should only allow destroy when force destroy is enabled", func(t *testing.T) {
environment := client.Environment{
Id: "id0",
Id: environment.Id,
Name: "my-environment",
ProjectId: "project-id",
WorkspaceName: "workspace-name",
Expand Down

0 comments on commit 9a65c08

Please sign in to comment.