-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for restoring default organization policies #1477
Changes from 2 commits
2ac78fc
b49b6ec
4722e12
deeaac5
0ee0f59
11d5f19
00e2b6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,24 @@ func TestAccFolderOrganizationPolicy_list_update(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccFolderOrganizationPolicy_restore_defaultTrue(t *testing.T) { | ||
t.Parallel() | ||
|
||
folder := acctest.RandomWithPrefix("tf-test") | ||
org := getTestOrgFromEnv(t) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckGoogleOrganizationPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFolderOrganizationPolicy_restore_defaultTrue(org, folder), | ||
Check: getGoogleFolderOrganizationRestoreDefaultTrue("list", &cloudresourcemanager.RestoreDefault{}), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleFolderOrganizationPolicyDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
|
@@ -222,6 +240,22 @@ func testAccCheckGoogleFolderOrganizationListPolicyDeniedValues(n string, values | |
} | ||
} | ||
|
||
func getGoogleFolderOrganizationRestoreDefaultTrue(n string, policyDefault *cloudresourcemanager.RestoreDefault) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
policy, err := getGoogleFolderOrganizationPolicyTestResource(s, n) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !reflect.DeepEqual(policy.RestoreDefault, policyDefault) { | ||
return fmt.Errorf("Expected the list policy to '%s', restore default got, %s", policyDefault, policy.RestoreDefault) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func getGoogleFolderOrganizationPolicyTestResource(s *terraform.State, n string) (*cloudresourcemanager.OrgPolicy, error) { | ||
rn := "google_folder_organization_policy." + n | ||
rs, ok := s.RootModule().Resources[rn] | ||
|
@@ -322,3 +356,21 @@ resource "google_folder_organization_policy" "list" { | |
} | ||
`, folder, "organizations/"+org) | ||
} | ||
|
||
func testAccFolderOrganizationPolicy_restore_defaultTrue(org, folder string) string { | ||
return fmt.Sprintf(` | ||
resource "google_folder" "orgpolicy" { | ||
display_name = "%s" | ||
parent = "%s" | ||
} | ||
|
||
resource "google_folder_organization_policy" "list" { | ||
folder = "${google_folder.orgpolicy.name}" | ||
constraint = "serviceuser.services" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: some of these lines use tabs, others use spaces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed to use only spaces |
||
|
||
restore_policy { | ||
default = true | ||
} | ||
} | ||
`, folder, "organizations/"+org) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ var schemaOrganizationPolicy = map[string]*schema.Schema{ | |
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
ConflictsWith: []string{"list_policy"}, | ||
ConflictsWith: []string{"list_policy", "restore_policy"}, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"enforced": { | ||
|
@@ -32,7 +32,8 @@ var schemaOrganizationPolicy = map[string]*schema.Schema{ | |
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
ConflictsWith: []string{"boolean_policy"}, | ||
ConflictsWith: []string{"boolean_policy", "restore_policy"}, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allow": { | ||
|
@@ -99,6 +100,20 @@ var schemaOrganizationPolicy = map[string]*schema.Schema{ | |
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"restore_policy": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
ConflictsWith: []string{"boolean_policy", "list_policy"}, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"default": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused about the structure of this. The API requires an empty object, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only because in the requirements give me the next structure: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @morgante, this was your idea I believe. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I specified it that way was for equivalence with the other constraint types, which use a nested structure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it makes sense. It's a little different in this case because the others are mirroring the API exactly as it is, whereas this one is adding a field into an object, even though that field doesn't exist. I do think it's probably less confusing than just requiring an empty object though, and a bit more future proof anyway, so sure. I'm down. |
||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func resourceGoogleOrganizationPolicy() *schema.Resource { | ||
|
@@ -152,6 +167,7 @@ func resourceGoogleOrganizationPolicyRead(d *schema.ResourceData, meta interface | |
d.Set("version", policy.Version) | ||
d.Set("etag", policy.Etag) | ||
d.Set("update_time", policy.UpdateTime) | ||
d.Set("restore_policy", policy.RestoreDefault) | ||
|
||
return nil | ||
} | ||
|
@@ -195,20 +211,37 @@ func setOrganizationPolicy(d *schema.ResourceData, meta interface{}) error { | |
config := meta.(*Config) | ||
org := "organizations/" + d.Get("org_id").(string) | ||
|
||
listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{})) | ||
restore_default, err := checkRestoreDefault(d.Get("restore_policy").([]interface{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), | ||
ListPolicy: listPolicy, | ||
Version: int64(d.Get("version").(int)), | ||
Etag: d.Get("etag").(string), | ||
}, | ||
}).Do() | ||
if restore_default != nil { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored code |
||
_, err = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
RestoreDefault: restore_default, | ||
}, | ||
}).Do() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored code |
||
} else { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored code |
||
listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), | ||
ListPolicy: listPolicy, | ||
Version: int64(d.Get("version").(int)), | ||
Etag: d.Get("etag").(string), | ||
}, | ||
}).Do() | ||
} | ||
|
||
return err | ||
} | ||
|
@@ -316,6 +349,20 @@ func expandListOrganizationPolicy(configured []interface{}) (*cloudresourcemanag | |
}, nil | ||
} | ||
|
||
func checkRestoreDefault(configured []interface{}) (*cloudresourcemanager.RestoreDefault, error) { | ||
if len(configured) > 0 { | ||
restoreDefaultMap := configured[0].(map[string]interface{}) | ||
default_value := restoreDefaultMap["default"].(bool) | ||
|
||
if default_value { | ||
restore_default := &cloudresourcemanager.RestoreDefault{} | ||
return restore_default, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func canonicalOrgPolicyConstraint(constraint string) string { | ||
if strings.HasPrefix(constraint, "constraints/") { | ||
return constraint | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ func resourceGoogleProjectOrganizationPolicyRead(d *schema.ResourceData, meta in | |
d.Set("constraint", policy.Constraint) | ||
d.Set("boolean_policy", flattenBooleanOrganizationPolicy(policy.BooleanPolicy)) | ||
d.Set("list_policy", flattenListOrganizationPolicy(policy.ListPolicy)) | ||
d.Set("restore_policy", policy.RestoreDefault) | ||
d.Set("version", policy.Version) | ||
d.Set("etag", policy.Etag) | ||
d.Set("update_time", policy.UpdateTime) | ||
|
@@ -90,15 +91,32 @@ func setProjectOrganizationPolicy(d *schema.ResourceData, meta interface{}) erro | |
return err | ||
} | ||
|
||
_, err = config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), | ||
ListPolicy: listPolicy, | ||
Version: int64(d.Get("version").(int)), | ||
Etag: d.Get("etag").(string), | ||
}, | ||
}).Do() | ||
restore_default, err := checkRestoreDefault(d.Get("restore_policy").([]interface{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if restore_default != nil { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored code |
||
_, err = config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
RestoreDefault: restore_default, | ||
}, | ||
}).Do() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code refactored |
||
} else { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored code |
||
_, err = config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{ | ||
Policy: &cloudresourcemanager.OrgPolicy{ | ||
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), | ||
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), | ||
ListPolicy: listPolicy, | ||
Version: int64(d.Get("version").(int)), | ||
Etag: d.Get("etag").(string), | ||
}, | ||
}).Do() | ||
} | ||
|
||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - remove line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored code