Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
josemanuelt committed May 15, 2018
1 parent b49b6ec commit 739251a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 100 deletions.
39 changes: 12 additions & 27 deletions google/resource_google_folder_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +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", policy.RestoreDefault)
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 @@ -86,37 +86,22 @@ 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 := checkRestoreDefault(d.Get("restore_policy").([]interface{}))
listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{}))
if err != nil {
return err
}

if restore_default != nil {

_, err = config.clientResourceManager.Folders.SetOrgPolicy(folder, &cloudresourcemanager.SetOrgPolicyRequest{
Policy: &cloudresourcemanager.OrgPolicy{
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
RestoreDefault: restore_default,
},
}).Do()

} else {

_, 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),
},
}).Do()
}
_, 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,
RestoreDefault: expandRestoreOrganizationPolicy(d.Get("restore_policy").([]interface{})),
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

return err
}
82 changes: 39 additions & 43 deletions google/resource_google_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var schemaOrganizationPolicy = map[string]*schema.Schema{
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"boolean_policy", "restore_policy"},

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allow": {
Expand Down Expand Up @@ -167,7 +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", policy.RestoreDefault)
d.Set("restore_policy", flattenRestoreOrganizationPolicy(policy.RestoreDefault))

return nil
}
Expand Down Expand Up @@ -211,37 +210,21 @@ func setOrganizationPolicy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
org := "organizations/" + d.Get("org_id").(string)

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

if restore_default != nil {

_, err = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{
Policy: &cloudresourcemanager.OrgPolicy{
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
RestoreDefault: restore_default,
},
}).Do()

} else {

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()
}
_, 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,
RestoreDefault: expandRestoreOrganizationPolicy(d.Get("restore_policy").([]interface{})),
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

return err
}
Expand All @@ -260,6 +243,20 @@ func flattenBooleanOrganizationPolicy(policy *cloudresourcemanager.BooleanPolicy
return bPolicies
}

func flattenRestoreOrganizationPolicy(restore_policy *cloudresourcemanager.RestoreDefault) []map[string]interface{} {
bRestore_policy := make([]map[string]interface{}, 0, 1)

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 @@ -271,6 +268,19 @@ func expandBooleanOrganizationPolicy(configured []interface{}) *cloudresourceman
}
}

func expandRestoreOrganizationPolicy(configured []interface{}) *cloudresourcemanager.RestoreDefault {
if len(configured) > 0 {
restoreDefaultMap := configured[0].(map[string]interface{})
default_value := restoreDefaultMap["default"].(bool)

if default_value {
return &cloudresourcemanager.RestoreDefault{}
}
}

return nil
}

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

Expand Down Expand Up @@ -349,20 +359,6 @@ 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
Expand Down
8 changes: 5 additions & 3 deletions google/resource_google_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestAccOrganizationPolicy(t *testing.T) {
"list_allowAll": testAccOrganizationPolicy_list_allowAll,
"list_allowSome": testAccOrganizationPolicy_list_allowSome,
"list_denySome": testAccOrganizationPolicy_list_denySome,
"list_restore": testAccOrganizationPolicy_restore_defaultTrue,
"list_update": testAccOrganizationPolicy_list_update,
"restore_policy": testAccOrganizationPolicy_restore_defaultTrue,
}

for name, tc := range testCases {
Expand Down Expand Up @@ -177,8 +178,9 @@ func testAccOrganizationPolicy_restore_defaultTrue(t *testing.T) {
Check: testAccCheckGoogleOrganizationRestoreDefaultTrue("list", &cloudresourcemanager.RestoreDefault{}),
},
{
ResourceName: "google_organization_policy.list",
ImportState: true,
ResourceName: "google_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down
39 changes: 12 additions & 27 deletions google/resource_google_project_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +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("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 @@ -86,37 +86,22 @@ 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 := checkRestoreDefault(d.Get("restore_policy").([]interface{}))
listPolicy, err := expandListOrganizationPolicy(d.Get("list_policy").([]interface{}))
if err != nil {
return err
}

if restore_default != nil {

_, err = config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{
Policy: &cloudresourcemanager.OrgPolicy{
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
RestoreDefault: restore_default,
},
}).Do()

} else {

_, 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()
}
_, 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,
RestoreDefault: expandRestoreOrganizationPolicy(d.Get("restore_policy").([]interface{})),
Version: int64(d.Get("version").(int)),
Etag: d.Get("etag").(string),
},
}).Do()

return err
}

0 comments on commit 739251a

Please sign in to comment.