Skip to content

Commit 4fc5fc5

Browse files
authored
feat: add variable validation in org policy v2 (#149)
1 parent dfe81a0 commit 4fc5fc5

File tree

10 files changed

+37
-163
lines changed

10 files changed

+37
-163
lines changed

examples/basic_org_policies/versions.tf

-24
This file was deleted.

examples/boolean_org_exclude/versions.tf

-24
This file was deleted.

examples/boolean_project_allow/versions.tf

-24
This file was deleted.

examples/list_folder_deny/versions.tf

-24
This file was deleted.

examples/list_org_exclude/versions.tf

-24
This file was deleted.

examples/v2_boolean_org_enforce/main.tf

-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ module "gcp_org_policy_v2" {
2525
policy_root_id = var.org_id
2626
rules = [{
2727
enforcement = true
28-
allow = []
29-
deny = []
30-
conditions = []
3128
}]
3229
constraint = "compute.requireOsLogin"
3330
policy_type = "boolean"

examples/v2_boolean_org_enforce/versions.tf

-26
This file was deleted.

modules/org_policy_v2/README.md

+28-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ Organization Policies are of two types `boolean` and `list`.
99
## Usage
1010
Example usage is included in the [examples](./examples/org_policy_v2) folder, but simple usage is as follows:
1111

12+
- Bool organization policy
13+
1214
```hcl
13-
module "gcp_org_policy_v2" {
15+
module "gcp_org_policy_v2_bool" {
1416
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2"
1517
version = "~> 5.2.0"
1618
@@ -25,15 +27,10 @@ module "gcp_org_policy_v2" {
2527
# Rule 1
2628
{
2729
enforcement = true
28-
allow = []
29-
deny = []
30-
conditions = []
3130
},
3231
# Rule 2
3332
{
3433
enforcement = true
35-
allow = []
36-
deny = []
3734
conditions = [{
3835
description = "description of the condition"
3936
expression = "resource.matchTagId('tagKeys/123456789', 'tagValues/123456789') && resource.matchTag('123456789/1234', 'abcd')"
@@ -45,6 +42,28 @@ module "gcp_org_policy_v2" {
4542
}
4643
```
4744

45+
- List organization policy
46+
47+
```hcl
48+
module "gcp_org_policy_v2_list" {
49+
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2"
50+
version = "~> 5.0"
51+
52+
policy_root = "organization"
53+
policy_root_id = var.org_id
54+
constraint = "gcp.resourceLocations"
55+
policy_type = "list"
56+
57+
rules = [
58+
# Rule 1
59+
{
60+
enforcement = true
61+
allow = ["in:us-locations"]
62+
}
63+
]
64+
}
65+
```
66+
4867
### Variables
4968
To control module's behavior, change variables' values regarding the following:
5069

@@ -99,7 +118,7 @@ To control module's behavior, change variables' values regarding the following:
99118
| policy\_root | Resource hierarchy node to apply the policy to: can be one of `organization`, `folder`, or `project`. | `string` | `"organization"` | no |
100119
| policy\_root\_id | The policy root id, either of organization\_id, folder\_id or project\_id | `string` | `null` | no |
101120
| policy\_type | The constraint type to work with (either 'boolean' or 'list') | `string` | `"list"` | no |
102-
| rules | List of rules per policy. Up to 10. | <pre>list(object(<br> {<br> enforcement = bool<br> allow = list(string)<br> deny = list(string)<br> conditions = list(object(<br> {<br> description = string<br> expression = string<br> title = string<br> location = string<br> }<br> ))<br> }<br> ))</pre> | n/a | yes |
121+
| rules | List of rules per policy. Up to 10. | <pre>list(object(<br> {<br> enforcement = bool<br> allow = optional(list(string), [])<br> deny = optional(list(string), [])<br> conditions = optional(list(object(<br> {<br> description = string<br> expression = string<br> title = string<br> location = string<br> }<br> )), [])<br> }<br> ))</pre> | n/a | yes |
103122

104123
## Outputs
105124

@@ -114,15 +133,15 @@ To control module's behavior, change variables' values regarding the following:
114133
---
115134

116135
## Compatibility
117-
This module is meant for use with Terraform 0.13+ and tested using Terraform 1.0+. If you find incompatibilities using Terraform >=0.13, please open an issue.
136+
This module is meant for use with Terraform 1.3+ and tested using Terraform 1.0+. If you find incompatibilities using Terraform >=1.3, please open an issue.
118137
If you haven't
119138
[upgraded](https://www.terraform.io/upgrade-guides/0-13.html) and need a Terraform
120139
0.12.x-compatible version of this module, the last released version
121140
intended for Terraform 0.12.x is [v4.0.0](https://registry.terraform.io/modules/terraform-google-modules/-org-policy/google/v4.0.0).
122141

123142
## Requirements
124143
### Terraform plugins
125-
- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.0
144+
- [Terraform](https://www.terraform.io/downloads.html) >= 1.3.0
126145
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) >= v2.5.0
127146

128147
### Permissions

modules/org_policy_v2/variables.tf

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ variable "policy_root" {
1818
description = "Resource hierarchy node to apply the policy to: can be one of `organization`, `folder`, or `project`."
1919
type = string
2020
default = "organization"
21+
validation {
22+
condition = contains(["organization", "folder", "project"], var.policy_root)
23+
error_message = "policy_root should be one of organization, folder, or project"
24+
}
2125
}
2226

2327
variable "policy_root_id" {
@@ -60,16 +64,16 @@ variable "rules" {
6064
type = list(object(
6165
{
6266
enforcement = bool
63-
allow = list(string)
64-
deny = list(string)
65-
conditions = list(object(
67+
allow = optional(list(string), [])
68+
deny = optional(list(string), [])
69+
conditions = optional(list(object(
6670
{
6771
description = string
6872
expression = string
6973
title = string
7074
location = string
7175
}
72-
))
76+
)), [])
7377
}
7478
))
7579
}

modules/org_policy_v2/versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
terraform {
18-
required_version = ">= 0.13"
18+
required_version = ">= 1.3"
1919
required_providers {
2020

2121
google = {

0 commit comments

Comments
 (0)