Skip to content

Commit

Permalink
Add import support for organization_policies
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
chrisst authored and modular-magician committed Mar 12, 2019
1 parent 7a91ac0 commit 0dbb6bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
16 changes: 16 additions & 0 deletions google/resource_google_folder_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
Expand All @@ -14,6 +15,10 @@ func resourceGoogleFolderOrganizationPolicy() *schema.Resource {
Update: resourceGoogleFolderOrganizationPolicyUpdate,
Delete: resourceGoogleFolderOrganizationPolicyDelete,

Importer: &schema.ResourceImporter{
State: resourceFolderOrgPolicyImporter,
},

Schema: mergeSchemas(
schemaOrganizationPolicy,
map[string]*schema.Schema{
Expand All @@ -27,6 +32,17 @@ func resourceGoogleFolderOrganizationPolicy() *schema.Resource {
}
}

func resourceFolderOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), ":")
if len(parts) != 2 {
return nil, fmt.Errorf("ID must be in the format folders/<folderId>:constraints/<constraint>")
}
d.Set("folder", parts[0])
d.Set("constraint", parts[1])

return []*schema.ResourceData{d}, nil
}

func resourceGoogleFolderOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
if err := setFolderOrganizationPolicy(d, meta); err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions google/resource_google_folder_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func TestAccFolderOrganizationPolicy_list_allowAll(t *testing.T) {
Config: testAccFolderOrganizationPolicy_list_allowAll(org, folder),
Check: testAccCheckGoogleFolderOrganizationListPolicyAll("list", "ALLOW"),
},
{
ResourceName: "google_folder_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -85,6 +90,11 @@ func TestAccFolderOrganizationPolicy_list_allowSome(t *testing.T) {
Config: testAccFolderOrganizationPolicy_list_allowSome(org, folder, project),
Check: testAccCheckGoogleFolderOrganizationListPolicyAllowedValues("list", []string{"projects/" + project}),
},
{
ResourceName: "google_folder_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -103,6 +113,11 @@ func TestAccFolderOrganizationPolicy_list_denySome(t *testing.T) {
Config: testAccFolderOrganizationPolicy_list_denySome(org, folder),
Check: testAccCheckGoogleFolderOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
},
{
ResourceName: "google_folder_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -125,6 +140,11 @@ func TestAccFolderOrganizationPolicy_list_update(t *testing.T) {
Config: testAccFolderOrganizationPolicy_list_denySome(org, folder),
Check: testAccCheckGoogleFolderOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
},
{
ResourceName: "google_folder_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -143,6 +163,11 @@ func TestAccFolderOrganizationPolicy_restore_defaultTrue(t *testing.T) {
Config: testAccFolderOrganizationPolicy_restore_defaultTrue(org, folder),
Check: getGoogleFolderOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
},
{
ResourceName: "google_folder_organization_policy.restore",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
16 changes: 16 additions & 0 deletions google/resource_google_project_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
Expand All @@ -14,6 +15,10 @@ func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
Update: resourceGoogleProjectOrganizationPolicyUpdate,
Delete: resourceGoogleProjectOrganizationPolicyDelete,

Importer: &schema.ResourceImporter{
State: resourceProjectOrgPolicyImporter,
},

Schema: mergeSchemas(
schemaOrganizationPolicy,
map[string]*schema.Schema{
Expand All @@ -27,6 +32,17 @@ func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
}
}

func resourceProjectOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), ":")
if len(parts) != 2 {
return nil, fmt.Errorf("ID must be in the format <projectId>:constraints/<constraint>")
}
d.Set("project", parts[0])
d.Set("constraint", parts[1])

return []*schema.ResourceData{d}, nil
}

func resourceGoogleProjectOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
if err := setProjectOrganizationPolicy(d, meta); err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions google/resource_google_project_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func testAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
Config: testAccProjectOrganizationPolicyConfig_list_allowAll(projectId),
Check: testAccCheckGoogleProjectOrganizationListPolicyAll("list", "ALLOW"),
},
{
ResourceName: "google_project_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -100,6 +105,11 @@ func testAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
Config: testAccProjectOrganizationPolicyConfig_list_allowSome(project),
Check: testAccCheckGoogleProjectOrganizationListPolicyAllowedValues("list", []string{canonicalProject}),
},
{
ResourceName: "google_project_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -115,6 +125,11 @@ func testAccProjectOrganizationPolicy_list_denySome(t *testing.T) {
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
},
{
ResourceName: "google_project_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -134,6 +149,11 @@ func testAccProjectOrganizationPolicy_list_update(t *testing.T) {
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
},
{
ResourceName: "google_project_organization_policy.list",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -150,6 +170,11 @@ func testAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
Config: testAccProjectOrganizationPolicyConfig_restore_defaultTrue(projectId),
Check: getGoogleProjectOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
},
{
ResourceName: "google_project_organization_policy.restore",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down

0 comments on commit 0dbb6bf

Please sign in to comment.