Skip to content

Commit

Permalink
add prevent_auto_deploy flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-landesman committed Mar 12, 2024
1 parent 8abc98d commit 206f055
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ func resourceEnvironment() *schema.Resource {
Description: "should use remote backend",
Optional: true,
},
"prevent_auto_deploy": {
Type: schema.TypeBool,
Description: "prevent auto deploy",
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",
Expand Down Expand Up @@ -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))
Expand Down
66 changes: 66 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,72 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
})

t.Run("prevent auto deploy", func(t *testing.T) {
templateId := "template-id"
newTemplateId := "new-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"),
),
},
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": environment.Name,
"project_id": environment.ProjectId,
"template_id": newTemplateId,
"force_destroy": true,
"prevent_auto_deploy": true,
}),
ExpectError: regexp.MustCompile("template_id may not be modified, create a new environment instead"),
},
},
}

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().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"
Expand Down

0 comments on commit 206f055

Please sign in to comment.