Skip to content

Commit

Permalink
Merge branch 'main' into fix-integration-test-25-uses-data-from-other…
Browse files Browse the repository at this point in the history
…-tests
  • Loading branch information
TomerHeber authored Oct 25, 2023
2 parents 0c08f76 + bb5f991 commit 7a9e3f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
4 changes: 4 additions & 0 deletions client/project_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Policy struct {
MaxTtl *string `json:"maxTtl,omitempty" tfschema:",omitempty"`
DefaultTtl *string `json:"defaultTtl,omitempty" tfschema:",omitempty"`
ForceRemoteBackend bool `json:"forceRemoteBackend"`
DriftDetectionCron string `json:"driftDetectionCron"`
DriftDetectionEnabled bool `json:"driftDetectionEnabled"`
}

type PolicyUpdatePayload struct {
Expand All @@ -32,6 +34,8 @@ type PolicyUpdatePayload struct {
MaxTtl string `json:"maxTtl,omitempty"`
DefaultTtl string `json:"defaultTtl,omitempty"`
ForceRemoteBackend bool `json:"forceRemoteBackend"`
DriftDetectionCron string `json:"driftDetectionCron"`
DriftDetectionEnabled bool `json:"driftDetectionEnabled"`
}

// Policy retrieves a policy from the API
Expand Down
1 change: 1 addition & 0 deletions docs/resources/project_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ resource "env0_project_policy" "example" {
- `continuous_deployment_default` (Boolean) Redeploy on every push to the git branch default value
- `default_ttl` (String) the default environment time-to-live allowed on deploy time. Format is <number>-<M/w/d/h> (Examples: 12-h, 3-d, 1-w, 1-M). Default value is 'inherit' which inherits the organization policy. must be equal or shorter than max_ttl
- `disable_destroy_environments` (Boolean) Disallow destroying environment in the project
- `drift_detection_cron` (String) default cron expression for new environments
- `force_remote_backend` (Boolean) if 'true' all environments created in this project will be forced to use env0 remote backend. Default is 'false'
- `include_cost_estimation` (Boolean) Enable cost estimation for the project
- `max_ttl` (String) the maximum environment time-to-live allowed on deploy time. Format is <number>-<M/w/d/h> (Examples: 12-h, 3-d, 1-w, 1-M). Default value is 'inherit' which inherits the organization policy. must be equal or longer than default_ttl
Expand Down
2 changes: 1 addition & 1 deletion env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Provider(version string) plugin.ProviderFunc {
},
ResourcesMap: map[string]*schema.Resource{
"env0_project": resourceProject(),
"env0_project_policy": resourcePolicy(),
"env0_project_policy": resourceProjectPolicy(),
"env0_configuration_variable": resourceConfigurationVariable(),
"env0_template": resourceTemplate(),
"env0_ssh_key": resourceSshKey(),
Expand Down
34 changes: 22 additions & 12 deletions env0/resource_project_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourcePolicy() *schema.Resource {
func resourceProjectPolicy() *schema.Resource {
return &schema.Resource{
CreateContext: resourcePolicyCreate,
ReadContext: resourcePolicyRead,
UpdateContext: resourcePolicyUpdate,
DeleteContext: resourcePolicyDelete,
CreateContext: resourceProjectPolicyCreate,
ReadContext: resourceProjectPolicyRead,
UpdateContext: resourceProjectPolicyUpdate,
DeleteContext: resourceProjectPolicyDelete,

Importer: &schema.ResourceImporter{StateContext: resourcePolicyImport},
Importer: &schema.ResourceImporter{StateContext: resourceProjectPolicyImport},

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -105,12 +105,18 @@ func resourcePolicy() *schema.Resource {
Optional: true,
Default: false,
},
"drift_detection_cron": {
Type: schema.TypeString,
Description: "default cron expression for new environments",
Optional: true,
ValidateDiagFunc: ValidateCronExpression,
},
},
}
}

func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := resourcePolicyUpdate(ctx, d, meta); err != nil {
func resourceProjectPolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := resourceProjectPolicyUpdate(ctx, d, meta); err != nil {
return err
}

Expand All @@ -120,7 +126,7 @@ func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta inte
return nil
}

func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceProjectPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

projectId := d.Id()
Expand All @@ -139,7 +145,7 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interf
return nil
}

func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceProjectPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

payload := client.PolicyUpdatePayload{}
Expand All @@ -165,14 +171,18 @@ func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta inte
payload.MaxTtl = ""
}

if payload.DriftDetectionCron != "" {
payload.DriftDetectionEnabled = true
}

if _, err := apiClient.PolicyUpdate(payload); err != nil {
return diag.Errorf("could not update policy: %v", err)
}

return nil
}

func resourcePolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceProjectPolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

projectId := d.Id()
Expand All @@ -195,7 +205,7 @@ func resourcePolicyDelete(ctx context.Context, d *schema.ResourceData, meta inte
return nil
}

func resourcePolicyImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
func resourceProjectPolicyImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
projectId := d.Id()

policy, err := getPolicyByProjectId(projectId, meta)
Expand Down
9 changes: 8 additions & 1 deletion env0/resource_project_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.uber.org/mock/gomock"
)

func TestUnitPolicyResource(t *testing.T) {
func TestUnitProjectPolicyResource(t *testing.T) {
resourceType := "env0_project_policy"
resourceName := "test"
accessor := resourceAccessor(resourceType, resourceName)
Expand All @@ -28,6 +28,8 @@ func TestUnitPolicyResource(t *testing.T) {
MaxTtl: stringPtr("inherit"),
DefaultTtl: stringPtr("inherit"),
ForceRemoteBackend: true,
DriftDetectionCron: "0 4 * * *",
DriftDetectionEnabled: true,
}

updatedPolicy := client.Policy{
Expand Down Expand Up @@ -67,6 +69,7 @@ func TestUnitPolicyResource(t *testing.T) {
"run_pull_request_plan_default": policy.RunPullRequestPlanDefault,
"continuous_deployment_default": policy.ContinuousDeploymentDefault,
"force_remote_backend": policy.ForceRemoteBackend,
"drift_detection_cron": policy.DriftDetectionCron,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "project_id", policy.ProjectId),
Expand All @@ -82,6 +85,7 @@ func TestUnitPolicyResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "max_ttl", "inherit"),
resource.TestCheckResourceAttr(accessor, "default_ttl", "inherit"),
resource.TestCheckResourceAttr(accessor, "force_remote_backend", strconv.FormatBool(policy.ForceRemoteBackend)),
resource.TestCheckResourceAttr(accessor, "drift_detection_cron", policy.DriftDetectionCron),
),
},
{
Expand All @@ -107,6 +111,7 @@ func TestUnitPolicyResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "max_ttl", "Infinite"),
resource.TestCheckResourceAttr(accessor, "default_ttl", *updatedPolicy.DefaultTtl),
resource.TestCheckResourceAttr(accessor, "force_remote_backend", strconv.FormatBool(updatedPolicy.ForceRemoteBackend)),
resource.TestCheckResourceAttr(accessor, "drift_detection_cron", updatedPolicy.DriftDetectionCron),
),
},
},
Expand All @@ -127,6 +132,8 @@ func TestUnitPolicyResource(t *testing.T) {
MaxTtl: "inherit",
DefaultTtl: "inherit",
ForceRemoteBackend: true,
DriftDetectionEnabled: true,
DriftDetectionCron: policy.DriftDetectionCron,
}).Times(1).Return(policy, nil),
mock.EXPECT().Policy(gomock.Any()).Times(2).Return(policy, nil), // 1 after create, 1 before update
// Update
Expand Down
1 change: 1 addition & 0 deletions tests/integration/011_policy/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ resource "env0_project_policy" "test_policy" {
skip_apply_when_plan_is_empty = false
disable_destroy_environments = false
skip_redundant_deployments = false
drift_detection_cron = var.second_run ? "0 4 * * *" : "0 3 * * *"
}

resource "env0_project_policy" "test_policy_2" {
Expand Down

0 comments on commit 7a9e3f6

Please sign in to comment.