Skip to content
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

Merged
merged 7 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions google/resource_google_folder_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func resourceGoogleFolderOrganizationPolicyRead(d *schema.ResourceData, meta int
d.Set("constraint", policy.Constraint)
d.Set("boolean_policy", flattenBooleanOrganizationPolicy(policy.BooleanPolicy))
d.Set("list_policy", flattenListOrganizationPolicy(policy.ListPolicy))
d.Set("restore_policy", flattenRestoreOrganizationPolicy(policy.RestoreDefault))
d.Set("version", policy.Version)
d.Set("etag", policy.Etag)
d.Set("update_time", policy.UpdateTime)
Expand Down Expand Up @@ -85,18 +86,25 @@ func resourceGoogleFolderOrganizationPolicyDelete(d *schema.ResourceData, meta i
func setFolderOrganizationPolicy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
folder := canonicalFolderId(d.Get("folder").(string))

listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{}))
if err != nil {
return err
}

restore_default, err := expandRestoreOrganizationPolicy(d.Get("restore_policy").([]interface{}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please camelCase your variable names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if err != nil {
return err
}

_, err = config.clientResourceManager.Folders.SetOrgPolicy(folder, &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),
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})),
ListPolicy: listPolicy,
RestoreDefault: restore_default,
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

Expand Down
52 changes: 52 additions & 0 deletions google/resource_google_folder_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("restore", &cloudresourcemanager.RestoreDefault{}),
},
},
})
}

func testAccCheckGoogleFolderOrganizationPolicyDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -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 restore default '%s', instead denied, %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]
Expand Down Expand Up @@ -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" "restore" {
folder = "${google_folder.orgpolicy.name}"
constraint = "serviceuser.services"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: some of these lines use tabs, others use spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to use only spaces


restore_policy {
default = true
}
}
`, folder, "organizations/"+org)
}
66 changes: 59 additions & 7 deletions google/resource_google_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -32,7 +32,7 @@ var schemaOrganizationPolicy = map[string]*schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"boolean_policy"},
ConflictsWith: []string{"boolean_policy", "restore_policy"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allow": {
Expand Down Expand Up @@ -99,6 +99,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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 default is just a construct here- what made you decide to go for a nested object with a boolean that has to be set to true, instead of just top-leveling that boolean (i.e. restore_default = true)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only because in the requirements give me the next structure:
restore_policy {
default = true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morgante, this was your idea I believe. What do you think?

Copy link

@morgante morgante May 24, 2018

Choose a reason for hiding this comment

The 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. boolean_policy, for example, also only has a single boolean value but is also nested. Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -152,6 +166,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", flattenRestoreOrganizationPolicy(policy.RestoreDefault))

return nil
}
Expand Down Expand Up @@ -200,13 +215,19 @@ func setOrganizationPolicy(d *schema.ResourceData, meta interface{}) error {
return err
}

restore_default, err := expandRestoreOrganizationPolicy(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),
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})),
ListPolicy: listPolicy,
RestoreDefault: restore_default,
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

Expand All @@ -227,6 +248,20 @@ func flattenBooleanOrganizationPolicy(policy *cloudresourcemanager.BooleanPolicy
return bPolicies
}

func flattenRestoreOrganizationPolicy(restore_policy *cloudresourcemanager.RestoreDefault) []map[string]interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: camelcase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

bRestore_policy := make([]map[string]interface{}, 0, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the b prefix means here. Maybe just call it rp or something simple like that?

Copy link
Contributor Author

@ortaman ortaman May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to rp (prefix doesnt mean anything)


if restore_policy == nil {
return bRestore_policy
}

bRestore_policy = append(bRestore_policy, map[string]interface{}{
"default": true,
})

return bRestore_policy
}

func expandBooleanOrganizationPolicy(configured []interface{}) *cloudresourcemanager.BooleanPolicy {
if len(configured) == 0 {
return nil
Expand All @@ -238,6 +273,23 @@ func expandBooleanOrganizationPolicy(configured []interface{}) *cloudresourceman
}
}

func expandRestoreOrganizationPolicy(configured []interface{}) (*cloudresourcemanager.RestoreDefault, error) {
if len(configured) == 0 {
return nil, nil
}

if len(configured) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement isn't necessary since we already checked above if it's equal to 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because there are three escenarios there:
1.- when we doesnt have the restore_policy argument.
2.-when we have the restore_policy argument, but the default value is not true.
3.-when we have the restore_policy argument and the default value is true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it could look like this:

	if len(configured) == 0 {
		return nil, nil
	}

	restoreDefaultMap := configured[0].(map[string]interface{})
	default_value := restoreDefaultMap["default"].(bool)
 
	if default_value {
		return &cloudresourcemanager.RestoreDefault{}, nil
	}

	return nil, fmt.Errorf("Invalid value for restore_policy. Expecting default = true")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, one 'if' is not necessary and looks cleaner, good catch (changed).

restoreDefaultMap := configured[0].(map[string]interface{})
default_value := restoreDefaultMap["default"].(bool)

if default_value == true {
return &cloudresourcemanager.RestoreDefault{}, nil
}
}

return nil, fmt.Errorf("Invalid value. Expecting default = true")
}

func flattenListOrganizationPolicy(policy *cloudresourcemanager.ListPolicy) []map[string]interface{} {
lPolicies := make([]map[string]interface{}, 0, 1)

Expand Down
50 changes: 50 additions & 0 deletions google/resource_google_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccOrganizationPolicy(t *testing.T) {
"list_allowSome": testAccOrganizationPolicy_list_allowSome,
"list_denySome": testAccOrganizationPolicy_list_denySome,
"list_update": testAccOrganizationPolicy_list_update,
"restore_policy": testAccOrganizationPolicy_restore_defaultTrue,
}

for name, tc := range testCases {
Expand Down Expand Up @@ -165,6 +166,26 @@ func testAccOrganizationPolicy_list_update(t *testing.T) {
})
}

func testAccOrganizationPolicy_restore_defaultTrue(t *testing.T) {
org := getTestOrgTargetFromEnv(t)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGoogleOrganizationPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccOrganizationPolicyConfig_restore_defaultTrue(org),
Check: testAccCheckGoogleOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
},
{
ResourceName: "google_organization_policy.restore",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckGoogleOrganizationPolicyDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -258,6 +279,22 @@ func testAccCheckGoogleOrganizationListPolicyDeniedValues(n string, values []str
}
}

func testAccCheckGoogleOrganizationRestoreDefaultTrue(n string, policyDefault *cloudresourcemanager.RestoreDefault) resource.TestCheckFunc {
return func(s *terraform.State) error {

policy, err := getGoogleOrganizationPolicyTestResource(s, n)
if err != nil {
return err
}

if !reflect.DeepEqual(policy.RestoreDefault, policyDefault) {
return fmt.Errorf("Expected the restore default '%s', instead denied, %s", policyDefault, policy.RestoreDefault)
}

return nil
}
}

func getGoogleOrganizationPolicyTestResource(s *terraform.State, n string) (*cloudresourcemanager.OrgPolicy, error) {
rn := "google_organization_policy." + n
rs, ok := s.RootModule().Resources[rn]
Expand Down Expand Up @@ -339,3 +376,16 @@ resource "google_organization_policy" "list" {
}
`, org)
}

func testAccOrganizationPolicyConfig_restore_defaultTrue(org string) string {
return fmt.Sprintf(`
resource "google_organization_policy" "restore" {
org_id = "%s"
constraint = "serviceuser.services"

restore_policy {
default = true
}
}
`, org)
}
18 changes: 13 additions & 5 deletions google/resource_google_project_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", flattenRestoreOrganizationPolicy(policy.RestoreDefault))
d.Set("version", policy.Version)
d.Set("etag", policy.Etag)
d.Set("update_time", policy.UpdateTime)
Expand Down Expand Up @@ -85,18 +86,25 @@ func resourceGoogleProjectOrganizationPolicyDelete(d *schema.ResourceData, meta
func setProjectOrganizationPolicy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project := prefixedProject(d.Get("project").(string))

listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{}))
if err != nil {
return err
}

restore_default, err := expandRestoreOrganizationPolicy(d.Get("restore_policy").([]interface{}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This should be camelcase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

if err != nil {
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),
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})),
ListPolicy: listPolicy,
RestoreDefault: restore_default,
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

Expand Down
Loading