diff --git a/client/environment.go b/client/environment.go index 6c0bd56f..7d24d1c1 100644 --- a/client/environment.go +++ b/client/environment.go @@ -138,6 +138,7 @@ type EnvironmentCreate struct { IsRemoteBackend *bool `json:"isRemoteBackend,omitempty" tfschema:"-"` Type string `json:"type,omitempty"` DriftDetectionRequest *DriftDetectionRequest `json:"driftDetectionRequest,omitempty" tfschema:"-"` + PreventAutoDeploy *bool `json:"preventAutoDeploy,omitempty" tfschema:"-"` } // When converted to JSON needs to be flattened. See custom MarshalJSON below. diff --git a/env0/resource_environment.go b/env0/resource_environment.go index 2226b923..4c9769f9 100644 --- a/env0/resource_environment.go +++ b/env0/resource_environment.go @@ -249,6 +249,11 @@ func resourceEnvironment() *schema.Resource { Description: "should use remote backend", Optional: true, }, + "prevent_auto_deploy": { + Type: schema.TypeBool, + Description: "use this flag to prevent auto deploy on environment creation", + Optional: true, + }, "terragrunt_working_directory": { Type: schema.TypeString, Description: "The working directory path to be used by a Terragrunt template. If left empty '/' is used. Note: modifying this field destroys the current environment and creates a new one", @@ -799,6 +804,11 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac payload.PullRequestPlanDeployments = boolPtr(val.(bool)) } + //lint:ignore SA1019 reason: https://github.com/hashicorp/terraform-plugin-sdk/issues/817 + if val, exists := d.GetOkExists("prevent_auto_deploy"); exists { + payload.PreventAutoDeploy = boolPtr(val.(bool)) + } + //lint:ignore SA1019 reason: https://github.com/hashicorp/terraform-plugin-sdk/issues/817 if val, exists := d.GetOkExists("auto_deploy_on_path_changes_only"); exists { payload.AutoDeployOnPathChangesOnly = boolPtr(val.(bool)) diff --git a/env0/resource_environment_test.go b/env0/resource_environment_test.go index a3f4664c..a47e7b3f 100644 --- a/env0/resource_environment_test.go +++ b/env0/resource_environment_test.go @@ -185,6 +185,59 @@ func TestUnitEnvironmentResource(t *testing.T) { }) }) + t.Run("prevent auto deploy", func(t *testing.T) { + templateId := "template-id" + truthyFruity := true + + environment := client.Environment{ + Id: uuid.New().String(), + Name: "name", + ProjectId: "project-id", + LatestDeploymentLog: client.DeploymentLog{ + BlueprintId: templateId, + }, + } + + testCase := resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ + "name": environment.Name, + "project_id": environment.ProjectId, + "template_id": templateId, + "force_destroy": true, + "prevent_auto_deploy": true, + }), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(accessor, "id", environment.Id), + resource.TestCheckResourceAttr(accessor, "name", environment.Name), + resource.TestCheckResourceAttr(accessor, "project_id", environment.ProjectId), + resource.TestCheckResourceAttr(accessor, "template_id", templateId), + resource.TestCheckResourceAttr(accessor, "prevent_auto_deploy", "true"), + ), + }, + }, + } + + runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + gomock.InOrder( + mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(1).Return(template, nil), + mock.EXPECT().EnvironmentCreate(client.EnvironmentCreate{ + Name: environment.Name, + ProjectId: environment.ProjectId, + DeployRequest: &client.DeployRequest{ + BlueprintId: templateId, + }, + + PreventAutoDeploy: &truthyFruity, + }).Times(1).Return(environment, nil), + mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil), + mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil), + mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1), + ) + }) + }) + t.Run("avoid modifying template id", func(t *testing.T) { templateId := "template-id" newTemplateId := "new-template-id"