Skip to content

Commit 1c1fcbb

Browse files
nehalk-tfniharika-98
authored andcommitted
Add parameters to google_org_policy_policy (GoogleCloudPlatform#12008)
1 parent f619be5 commit 1c1fcbb

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

mmv1/products/orgpolicy/Policy.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ examples:
5454
- name: 'org_policy_policy_dry_run_spec'
5555
primary_resource_id: 'primary'
5656
exclude_test: true
57+
- name: 'org_policy_policy_parameters_enforce'
58+
primary_resource_id: 'primary'
59+
exclude_test: true
5760
parameters:
5861
- name: 'parent'
5962
type: String
@@ -121,6 +124,13 @@ properties:
121124
send_empty_value: true
122125
custom_flatten: 'templates/terraform/custom_flatten/enum_bool.go.tmpl'
123126
custom_expand: 'templates/terraform/custom_expand/enum_bool.go.tmpl'
127+
- name: 'parameters'
128+
description: 'Optional. Required for Managed Constraints if parameters defined in constraints. Pass parameter values when policy enforcement is enabled. Ensure that parameter value types match those defined in the constraint definition. For example: { \"allowedLocations\" : [\"us-east1\", \"us-west1\"], \"allowAll\" : true }'
129+
custom_flatten: 'templates/terraform/custom_flatten/json_schema.tmpl'
130+
custom_expand: 'templates/terraform/custom_expand/json_schema.tmpl'
131+
state_func: 'func(v interface{}) string { s, _ := structure.NormalizeJsonString(v); return s }'
132+
validation:
133+
function: 'validation.StringIsJSON'
124134
- name: 'condition'
125135
type: NestedObject
126136
description: 'A condition which determines whether this rule is used in the evaluation of the policy. When set, the `expression` field in the `Expr'' must include from 1 to 10 subexpressions, joined by the "||" or "&&" operators. Each subexpression must be of the form "resource.matchTag(''/tag_key_short_name, ''tag_value_short_name'')". or "resource.matchTagId(''tagKeys/key_id'', ''tagValues/value_id'')". where key_name and value_name are the resource names for Label Keys and Values. These names are available from the Tag Manager Service. An example expression is: "resource.matchTag(''123456789/environment, ''prod'')". or "resource.matchTagId(''tagKeys/123'', ''tagValues/456'')".'
@@ -197,6 +207,13 @@ properties:
197207
send_empty_value: true
198208
custom_flatten: 'templates/terraform/custom_flatten/enum_bool.go.tmpl'
199209
custom_expand: 'templates/terraform/custom_expand/enum_bool.go.tmpl'
210+
- name: 'parameters'
211+
description: 'Optional. Required for Managed Constraints if parameters defined in constraints. Pass parameter values when policy enforcement is enabled. Ensure that parameter value types match those defined in the constraint definition. For example: { \"allowedLocations\" : [\"us-east1\", \"us-west1\"], \"allowAll\" : true }'
212+
custom_flatten: 'templates/terraform/custom_flatten/json_schema.tmpl'
213+
custom_expand: 'templates/terraform/custom_expand/json_schema.tmpl'
214+
state_func: 'func(v interface{}) string { s, _ := structure.NormalizeJsonString(v); return s }'
215+
validation:
216+
function: 'validation.StringIsJSON'
200217
- name: 'condition'
201218
type: NestedObject
202219
description: 'A condition which determines whether this rule is used in the evaluation of the policy. When set, the `expression` field in the `Expr'' must include from 1 to 10 subexpressions, joined by the "||" or "&&" operators. Each subexpression must be of the form "resource.matchTag(''/tag_key_short_name, ''tag_value_short_name'')". or "resource.matchTagId(''tagKeys/key_id'', ''tagValues/value_id'')". where key_name and value_name are the resource names for Label Keys and Values. These names are available from the Tag Manager Service. An example expression is: "resource.matchTag(''123456789/environment, ''prod'')". or "resource.matchTagId(''tagKeys/123'', ''tagValues/456'')".'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "google_org_policy_policy" "primary" {
2+
name = "projects/${google_project.basic.name}/policies/compute.managed.restrictDiskCreation"
3+
parent = "projects/${google_project.basic.name}"
4+
5+
spec {
6+
rules {
7+
enforce = "TRUE"
8+
parameters = jsonencode({"isSizeLimitCheck" : true, "allowedDiskTypes" : ["pd-ssd", "pd-standard"]})
9+
}
10+
}
11+
}
12+
13+
resource "google_project" "basic" {
14+
project_id = "id"
15+
name = "id"
16+
org_id = "123456789"
17+
deletion_policy = "DELETE"
18+
}

mmv1/third_party/terraform/services/orgpolicy/resource_org_policy_policy_test.go

+103
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,106 @@ func testAccCheckOrgPolicyPolicyDestroyProducer(t *testing.T) func(s *terraform.
458458
return nil
459459
}
460460
}
461+
func TestAccOrgPolicyPolicy_EnforceParameterizedMCPolicy(t *testing.T) {
462+
// Skip this test as no constraints yet launched in production, verified functionality with manual testing.
463+
t.Skip()
464+
t.Parallel()
465+
466+
context := map[string]interface{}{
467+
"org_id": envvar.GetTestOrgFromEnv(t),
468+
"random_suffix": acctest.RandString(t, 10),
469+
}
470+
471+
acctest.VcrTest(t, resource.TestCase{
472+
PreCheck: func() { acctest.AccTestPreCheck(t) },
473+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
474+
CheckDestroy: testAccCheckOrgPolicyPolicyDestroyProducer(t),
475+
Steps: []resource.TestStep{
476+
{
477+
Config: testAccOrgPolicyPolicy_EnforceParameterizedMCPolicy(context),
478+
},
479+
{
480+
ResourceName: "google_org_policy_policy.primary",
481+
ImportState: true,
482+
ImportStateVerify: true,
483+
ImportStateVerifyIgnore: []string{"name", "spec.0.rules.0.condition.0.expression"},
484+
},
485+
},
486+
})
487+
}
488+
func testAccOrgPolicyPolicy_EnforceParameterizedMCPolicy(context map[string]interface{}) string {
489+
return acctest.Nprintf(`
490+
resource "google_org_policy_policy" "primary" {
491+
name = "projects/${google_project.basic.name}/policies/essentialcontacts.managed.allowedContactDomains"
492+
parent = "projects/${google_project.basic.name}"
493+
494+
spec {
495+
rules {
496+
enforce = "TRUE"
497+
parameters = "{\"allowedDomains\": [\"@google.com\"]}"
498+
}
499+
}
500+
}
501+
502+
resource "google_project" "basic" {
503+
project_id = "tf-test-id%{random_suffix}"
504+
name = "tf-test-id%{random_suffix}"
505+
org_id = "%{org_id}"
506+
deletion_policy = "DELETE"
507+
}
508+
509+
510+
`, context)
511+
}
512+
513+
func TestAccOrgPolicyPolicy_EnforceParameterizedMCDryRunPolicy(t *testing.T) {
514+
// Skip this test as no constraints yet launched in production, verified functionality with manual testing.
515+
t.Skip()
516+
t.Parallel()
517+
518+
context := map[string]interface{}{
519+
"org_id": envvar.GetTestOrgFromEnv(t),
520+
"random_suffix": acctest.RandString(t, 10),
521+
}
522+
523+
acctest.VcrTest(t, resource.TestCase{
524+
PreCheck: func() { acctest.AccTestPreCheck(t) },
525+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
526+
CheckDestroy: testAccCheckOrgPolicyPolicyDestroyProducer(t),
527+
Steps: []resource.TestStep{
528+
{
529+
Config: testAccOrgPolicyPolicy_EnforceParameterizedMCDryRunPolicy(context),
530+
},
531+
{
532+
ResourceName: "google_org_policy_policy.primary",
533+
ImportState: true,
534+
ImportStateVerify: true,
535+
ImportStateVerifyIgnore: []string{"name", "spec.0.rules.0.condition.0.expression"},
536+
},
537+
},
538+
})
539+
}
540+
func testAccOrgPolicyPolicy_EnforceParameterizedMCDryRunPolicy(context map[string]interface{}) string {
541+
return acctest.Nprintf(`
542+
resource "google_org_policy_policy" "primary" {
543+
name = "projects/${google_project.basic.name}/policies/essentialcontacts.managed.allowedContactDomains"
544+
parent = "projects/${google_project.basic.name}"
545+
546+
dry_run_spec {
547+
rules {
548+
enforce = "TRUE"
549+
parameters = "{\"allowedDomains\": [\"@google.com\"]}"
550+
}
551+
}
552+
}
553+
554+
resource "google_project" "basic" {
555+
project_id = "tf-test-id%{random_suffix}"
556+
name = "tf-test-id%{random_suffix}"
557+
org_id = "%{org_id}"
558+
deletion_policy = "DELETE"
559+
}
560+
561+
562+
`, context)
563+
}

0 commit comments

Comments
 (0)