Skip to content

Commit 38269aa

Browse files
modular-magicianchrisst
authored andcommitted
Document that a policy must be defined. (hashicorp#700)
Signed-off-by: Modular Magician <[email protected]>
1 parent 75c3e19 commit 38269aa

7 files changed

+86
-9
lines changed

google-beta/resource_google_folder_organization_policy.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ func resourceFolderOrgPolicyImporter(d *schema.ResourceData, meta interface{}) (
5151
}
5252

5353
func resourceGoogleFolderOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
54+
d.SetId(fmt.Sprintf("%s:%s", d.Get("folder"), d.Get("constraint")))
55+
56+
if isOrganizationPolicyUnset(d) {
57+
return resourceGoogleFolderOrganizationPolicyDelete(d, meta)
58+
}
59+
5460
if err := setFolderOrganizationPolicy(d, meta); err != nil {
5561
return err
5662
}
5763

58-
d.SetId(fmt.Sprintf("%s:%s", d.Get("folder"), d.Get("constraint")))
59-
6064
return resourceGoogleFolderOrganizationPolicyRead(d, meta)
6165
}
6266

@@ -84,6 +88,10 @@ func resourceGoogleFolderOrganizationPolicyRead(d *schema.ResourceData, meta int
8488
}
8589

8690
func resourceGoogleFolderOrganizationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
91+
if isOrganizationPolicyUnset(d) {
92+
return resourceGoogleFolderOrganizationPolicyDelete(d, meta)
93+
}
94+
8795
if err := setFolderOrganizationPolicy(d, meta); err != nil {
8896
return err
8997
}

google-beta/resource_google_organization_policy.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@ func resourceGoogleOrganizationPolicy() *schema.Resource {
144144
}
145145

146146
func resourceGoogleOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
147+
d.SetId(fmt.Sprintf("%s:%s", d.Get("org_id"), d.Get("constraint").(string)))
148+
149+
if isOrganizationPolicyUnset(d) {
150+
return resourceGoogleOrganizationPolicyDelete(d, meta)
151+
}
152+
147153
if err := setOrganizationPolicy(d, meta); err != nil {
148154
return err
149155
}
150156

151-
d.SetId(fmt.Sprintf("%s:%s", d.Get("org_id"), d.Get("constraint").(string)))
152-
153157
return resourceGoogleOrganizationPolicyRead(d, meta)
154158
}
155159

@@ -177,6 +181,10 @@ func resourceGoogleOrganizationPolicyRead(d *schema.ResourceData, meta interface
177181
}
178182

179183
func resourceGoogleOrganizationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
184+
if isOrganizationPolicyUnset(d) {
185+
return resourceGoogleOrganizationPolicyDelete(d, meta)
186+
}
187+
180188
if err := setOrganizationPolicy(d, meta); err != nil {
181189
return err
182190
}
@@ -211,6 +219,19 @@ func resourceGoogleOrganizationPolicyImportState(d *schema.ResourceData, meta in
211219
return []*schema.ResourceData{d}, nil
212220
}
213221

222+
// Organization policies can be "inherited from parent" the UI, and this is the default
223+
// state of the resource without any policy set. In order to revert to this state the current
224+
// resource cannot be updated it must instead be Deleted. This allows Terraform to assert that
225+
// no policy has been set even if previously one had.
226+
// See https://github.com/terraform-providers/terraform-provider-google/issues/3607
227+
func isOrganizationPolicyUnset(d *schema.ResourceData) bool {
228+
listPolicy := d.Get("list_policy").([]interface{})
229+
booleanPolicy := d.Get("boolean_policy").([]interface{})
230+
restorePolicy := d.Get("restore_policy").([]interface{})
231+
232+
return len(listPolicy)+len(booleanPolicy)+len(restorePolicy) == 0
233+
}
234+
214235
func setOrganizationPolicy(d *schema.ResourceData, meta interface{}) error {
215236
config := meta.(*Config)
216237
org := "organizations/" + d.Get("org_id").(string)

google-beta/resource_google_project_organization_policy.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@ func resourceProjectOrgPolicyImporter(d *schema.ResourceData, meta interface{})
5050
}
5151

5252
func resourceGoogleProjectOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
53+
d.SetId(fmt.Sprintf("%s:%s", d.Get("project"), d.Get("constraint")))
54+
55+
if isOrganizationPolicyUnset(d) {
56+
return resourceGoogleProjectOrganizationPolicyDelete(d, meta)
57+
}
58+
5359
if err := setProjectOrganizationPolicy(d, meta); err != nil {
5460
return err
5561
}
5662

57-
d.SetId(fmt.Sprintf("%s:%s", d.Get("project"), d.Get("constraint")))
58-
5963
return resourceGoogleProjectOrganizationPolicyRead(d, meta)
6064
}
6165

@@ -83,6 +87,10 @@ func resourceGoogleProjectOrganizationPolicyRead(d *schema.ResourceData, meta in
8387
}
8488

8589
func resourceGoogleProjectOrganizationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
90+
if isOrganizationPolicyUnset(d) {
91+
return resourceGoogleProjectOrganizationPolicyDelete(d, meta)
92+
}
93+
8694
if err := setProjectOrganizationPolicy(d, meta); err != nil {
8795
return err
8896
}

google-beta/resource_google_project_organization_policy_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestAccProjectOrganizationPolicy(t *testing.T) {
2222
"list_denySome": testAccProjectOrganizationPolicy_list_denySome,
2323
"list_update": testAccProjectOrganizationPolicy_list_update,
2424
"restore_policy": testAccProjectOrganizationPolicy_restore_defaultTrue,
25+
"empty_policy": testAccProjectOrganizationPolicy_none,
2526
}
2627

2728
for name, tc := range testCases {
@@ -179,6 +180,27 @@ func testAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
179180
})
180181
}
181182

183+
func testAccProjectOrganizationPolicy_none(t *testing.T) {
184+
projectId := getTestProjectFromEnv()
185+
186+
resource.Test(t, resource.TestCase{
187+
PreCheck: func() { testAccPreCheck(t) },
188+
Providers: testAccProviders,
189+
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
190+
Steps: []resource.TestStep{
191+
{
192+
Config: testAccProjectOrganizationPolicyConfig_none(projectId),
193+
Check: testAccCheckGoogleProjectOrganizationPolicyDestroy,
194+
},
195+
{
196+
ResourceName: "google_project_organization_policy.none",
197+
ImportState: true,
198+
ImportStateVerify: true,
199+
},
200+
},
201+
})
202+
}
203+
182204
func testAccCheckGoogleProjectOrganizationPolicyDestroy(s *terraform.State) error {
183205
config := testAccProvider.Meta().(*Config)
184206

@@ -387,6 +409,15 @@ resource "google_project_organization_policy" "restore" {
387409
`, pid)
388410
}
389411

412+
func testAccProjectOrganizationPolicyConfig_none(pid string) string {
413+
return fmt.Sprintf(`
414+
resource "google_project_organization_policy" "none" {
415+
project = "%s"
416+
constraint = "constraints/serviceuser.services"
417+
}
418+
`, pid)
419+
}
420+
390421
func canonicalProjectId(project string) string {
391422
if strings.HasPrefix(project, "projects/") {
392423
return project

website/docs/r/google_folder_organization_policy.html.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ can also be used to allow or deny all values. Structure is documented below.
9494

9595
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
9696

97+
~> **Note:** If none of [`boolean_policy`, `list_policy`, `restore_policy`] are defined the policy for a given constraint will
98+
effectively be unset. This is represented in the UI as the constraint being 'Inherited'.
99+
97100
- - -
98101

99102
The `boolean_policy` block supports:

website/docs/r/google_organization_policy.html.markdown

+6-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ The following arguments are supported:
8686

8787
* `version` - (Optional) Version of the Policy. Default version is 0.
8888

89-
* `boolean_policy` - (Optional) A boolean policy is a constraint that is either enforced or not. Structure is documented below.
89+
* `boolean_policy` - (Optional) A boolean policy is a constraint that is either enforced or not. Structure is documented below.
9090

9191
* `list_policy` - (Optional) A policy that can define specific values that are allowed or denied for the given constraint. It can also be used to allow or deny all values. Structure is documented below.
9292

93-
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
93+
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
94+
95+
~> **Note:** If none of [`boolean_policy`, `list_policy`, `restore_policy`] are defined the policy for a given constraint will
96+
effectively be unset. This is represented in the UI as the constraint being 'Inherited'.
9497

9598
- - -
9699

@@ -122,7 +125,7 @@ The `restore_policy` block supports:
122125
In addition to the arguments listed above, the following computed attributes are
123126
exported:
124127

125-
* `etag` - (Computed) The etag of the organization policy. `etag` is used for optimistic concurrency control as a way to help prevent simultaneous updates of a policy from overwriting each other.
128+
* `etag` - (Computed) The etag of the organization policy. `etag` is used for optimistic concurrency control as a way to help prevent simultaneous updates of a policy from overwriting each other.
126129

127130
* `update_time` - (Computed) The timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds, representing when the variable was last updated. Example: "2016-10-09T12:33:37.578138407Z".
128131

website/docs/r/google_project_organization_policy.html.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ The following arguments are supported:
9393

9494
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
9595

96+
~> **Note:** If none of [`boolean_policy`, `list_policy`, `restore_policy`] are defined the policy for a given constraint will
97+
effectively be unset. This is represented in the UI as the constraint being 'Inherited'.
98+
9699
- - -
97100

98101
The `boolean_policy` block supports:

0 commit comments

Comments
 (0)