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

google_project_organization_policy #1226

Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func Provider() terraform.ResourceProvider {
"google_project_iam_member": ResourceIamMemberWithImport(IamProjectSchema, NewProjectIamUpdater, ProjectIdParseFunc),
"google_project_service": resourceGoogleProjectService(),
"google_project_iam_custom_role": resourceGoogleProjectIamCustomRole(),
"google_project_organization_policy": resourceGoogleProjectOrganizationPolicy(),
"google_project_usage_export_bucket": resourceProjectUsageBucket(),
"google_project_services": resourceGoogleProjectServices(),
"google_pubsub_topic": resourcePubsubTopic(),
Expand Down
104 changes: 104 additions & 0 deletions google/resource_google_project_organization_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
)

func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceGoogleProjectOrganizationPolicyCreate,
Read: resourceGoogleProjectOrganizationPolicyRead,
Update: resourceGoogleProjectOrganizationPolicyUpdate,
Delete: resourceGoogleProjectOrganizationPolicyDelete,

Schema: mergeSchemas(
schemaOrganizationPolicy,
map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
),
}
}

func resourceGoogleProjectOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
if err := setProjectOrganizationPolicy(d, meta); err != nil {
return err
}

d.SetId(fmt.Sprintf("%s:%s", d.Get("project"), d.Get("constraint")))

return resourceGoogleProjectOrganizationPolicyRead(d, meta)
}

func resourceGoogleProjectOrganizationPolicyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project := prefixedProject(d.Get("project").(string))

policy, err := config.clientResourceManager.Projects.GetOrgPolicy(project, &cloudresourcemanager.GetOrgPolicyRequest{
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
}).Do()

if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Organization policy for %s", project))
}

d.Set("constraint", policy.Constraint)
d.Set("boolean_policy", flattenBooleanOrganizationPolicy(policy.BooleanPolicy))
d.Set("list_policy", flattenListOrganizationPolicy(policy.ListPolicy))
d.Set("version", policy.Version)
d.Set("etag", policy.Etag)
d.Set("update_time", policy.UpdateTime)

return nil
}

func resourceGoogleProjectOrganizationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
if err := setProjectOrganizationPolicy(d, meta); err != nil {
return err
}

return resourceGoogleProjectOrganizationPolicyRead(d, meta)
}

func resourceGoogleProjectOrganizationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project := prefixedProject(d.Get("project").(string))

_, err := config.clientResourceManager.Projects.ClearOrgPolicy(project, &cloudresourcemanager.ClearOrgPolicyRequest{
Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)),
}).Do()

if err != nil {
return err
}

return nil
}

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
}

_, 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
}
Loading