-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add project_policy resource and data source (#151)
* feat: add policy resource * feat: create resource schema * feat: stub resource handlers * feat: resource (wip) * feat: policy resource update * feat: policy data source * fix: policy data source * feat: policy provider (wip) * fix: policy data source (wip) * test: data policy (broken) * fix: id required * test: fix broken data source test * test: integration (wip) * test: number of environments * test: num environments * fix: add project_id to resource * fix: minor * fix: complete models * test: scaffold unit test for policy resource * test: failing unit test for policy resource * fix: policy resource * fix: handle zeroes for number of environments * test: fix case on error * fix: support null number of environments * refactor: remove unreachable error * refactor: remove redundant code * refactor: clean up errors * refactor: remove unnecessary validation * test: all policy attributes * refactor: policy > project_policy * test: fix expected error * test: fix resource name in integration test * fix: review comments * test: fix resource spec test example * fix: disallow zero for number of environments * fix: handling of creating policy * fix: validate project id is not empty
- Loading branch information
1 parent
81f8649
commit 75ab01d
Showing
11 changed files
with
596 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataPolicyRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "id of the policy", | ||
Computed: true, | ||
}, | ||
"project_id": { | ||
Type: schema.TypeString, | ||
Description: "id of the project", | ||
Required: true, | ||
}, | ||
"number_of_environments": { | ||
Type: schema.TypeInt, | ||
Description: "number of environments per project", | ||
Computed: true, | ||
}, | ||
"number_of_environments_total": { | ||
Type: schema.TypeInt, | ||
Description: "number of environments total", | ||
Computed: true, | ||
}, | ||
"requires_approval_default": { | ||
Type: schema.TypeBool, | ||
Description: "requires approval", | ||
Computed: true, | ||
}, | ||
"include_cost_estimation": { | ||
Type: schema.TypeBool, | ||
Description: "include cost estimation", | ||
Computed: true, | ||
}, | ||
"skip_apply_when_plan_is_empty": { | ||
Type: schema.TypeBool, | ||
Description: "skip apply when plan is empty", | ||
Computed: true, | ||
}, | ||
"disable_destroy_environments": { | ||
Type: schema.TypeBool, | ||
Description: "disable destroy environments", | ||
Computed: true, | ||
}, | ||
"skip_redundant_deployments": { | ||
Type: schema.TypeBool, | ||
Description: "skip redundant deployments", | ||
Computed: true, | ||
}, | ||
"updated_by": { | ||
Type: schema.TypeString, | ||
Description: "updated by", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var err diag.Diagnostics | ||
var policy client.Policy | ||
|
||
projectId, ok := d.GetOk("project_id") | ||
if ok { | ||
policy, err = getPolicyByProjectId(projectId.(string), meta) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
d.SetId(policy.Id) | ||
setPolicySchema(d, policy) | ||
return nil | ||
} | ||
|
||
func getPolicyByProjectId(projectId string, meta interface{}) (client.Policy, diag.Diagnostics) { | ||
apiClient := meta.(client.ApiClientInterface) | ||
policy, err := apiClient.Policy(projectId) | ||
if err != nil { | ||
return client.Policy{}, diag.Errorf("Could not query policy: %v", err) | ||
} | ||
return policy, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package env0 | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestPolicyDataSource(t *testing.T) { | ||
policy := client.Policy{ | ||
Id: "id0", | ||
ProjectId: "project0", | ||
NumberOfEnvironments: 1, | ||
NumberOfEnvironmentsTotal: 2, | ||
RequiresApprovalDefault: true, | ||
IncludeCostEstimation: true, | ||
SkipApplyWhenPlanIsEmpty: true, | ||
DisableDestroyEnvironments: true, | ||
} | ||
|
||
resourceType := "env0_project_policy" | ||
resourceName := "test_policy" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getValidTestCase := func(input map[string]interface{}) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"project_id": policy.ProjectId, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", policy.Id), | ||
resource.TestCheckResourceAttr(accessor, "project_id", policy.ProjectId), | ||
resource.TestCheckResourceAttr(accessor, "number_of_environments", strconv.Itoa(policy.NumberOfEnvironments)), | ||
resource.TestCheckResourceAttr(accessor, "number_of_environments_total", strconv.Itoa(policy.NumberOfEnvironmentsTotal)), | ||
resource.TestCheckResourceAttr(accessor, "requires_approval_default", strconv.FormatBool(policy.RequiresApprovalDefault)), | ||
resource.TestCheckResourceAttr(accessor, "include_cost_estimation", strconv.FormatBool(policy.IncludeCostEstimation)), | ||
resource.TestCheckResourceAttr(accessor, "skip_apply_when_plan_is_empty", strconv.FormatBool(policy.SkipApplyWhenPlanIsEmpty)), | ||
resource.TestCheckResourceAttr(accessor, "disable_destroy_environments", strconv.FormatBool(policy.DisableDestroyEnvironments)), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
t.Run("valid", func(t *testing.T) { | ||
runUnitTest(t, getValidTestCase(map[string]interface{}{}), func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Policy(policy.ProjectId).AnyTimes().Return(policy, nil) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.