Skip to content

Commit 0dbb6bf

Browse files
chrisstmodular-magician
authored andcommitted
Add import support for organization_policies
Signed-off-by: Modular Magician <[email protected]>
1 parent 7a91ac0 commit 0dbb6bf

4 files changed

+82
-0
lines changed

google/resource_google_folder_organization_policy.go

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"strings"
56

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

18+
Importer: &schema.ResourceImporter{
19+
State: resourceFolderOrgPolicyImporter,
20+
},
21+
1722
Schema: mergeSchemas(
1823
schemaOrganizationPolicy,
1924
map[string]*schema.Schema{
@@ -27,6 +32,17 @@ func resourceGoogleFolderOrganizationPolicy() *schema.Resource {
2732
}
2833
}
2934

35+
func resourceFolderOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
36+
parts := strings.Split(d.Id(), ":")
37+
if len(parts) != 2 {
38+
return nil, fmt.Errorf("ID must be in the format folders/<folderId>:constraints/<constraint>")
39+
}
40+
d.Set("folder", parts[0])
41+
d.Set("constraint", parts[1])
42+
43+
return []*schema.ResourceData{d}, nil
44+
}
45+
3046
func resourceGoogleFolderOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
3147
if err := setFolderOrganizationPolicy(d, meta); err != nil {
3248
return err

google/resource_google_folder_organization_policy_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func TestAccFolderOrganizationPolicy_list_allowAll(t *testing.T) {
6666
Config: testAccFolderOrganizationPolicy_list_allowAll(org, folder),
6767
Check: testAccCheckGoogleFolderOrganizationListPolicyAll("list", "ALLOW"),
6868
},
69+
{
70+
ResourceName: "google_folder_organization_policy.list",
71+
ImportState: true,
72+
ImportStateVerify: true,
73+
},
6974
},
7075
})
7176
}
@@ -85,6 +90,11 @@ func TestAccFolderOrganizationPolicy_list_allowSome(t *testing.T) {
8590
Config: testAccFolderOrganizationPolicy_list_allowSome(org, folder, project),
8691
Check: testAccCheckGoogleFolderOrganizationListPolicyAllowedValues("list", []string{"projects/" + project}),
8792
},
93+
{
94+
ResourceName: "google_folder_organization_policy.list",
95+
ImportState: true,
96+
ImportStateVerify: true,
97+
},
8898
},
8999
})
90100
}
@@ -103,6 +113,11 @@ func TestAccFolderOrganizationPolicy_list_denySome(t *testing.T) {
103113
Config: testAccFolderOrganizationPolicy_list_denySome(org, folder),
104114
Check: testAccCheckGoogleFolderOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
105115
},
116+
{
117+
ResourceName: "google_folder_organization_policy.list",
118+
ImportState: true,
119+
ImportStateVerify: true,
120+
},
106121
},
107122
})
108123
}
@@ -125,6 +140,11 @@ func TestAccFolderOrganizationPolicy_list_update(t *testing.T) {
125140
Config: testAccFolderOrganizationPolicy_list_denySome(org, folder),
126141
Check: testAccCheckGoogleFolderOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
127142
},
143+
{
144+
ResourceName: "google_folder_organization_policy.list",
145+
ImportState: true,
146+
ImportStateVerify: true,
147+
},
128148
},
129149
})
130150
}
@@ -143,6 +163,11 @@ func TestAccFolderOrganizationPolicy_restore_defaultTrue(t *testing.T) {
143163
Config: testAccFolderOrganizationPolicy_restore_defaultTrue(org, folder),
144164
Check: getGoogleFolderOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
145165
},
166+
{
167+
ResourceName: "google_folder_organization_policy.restore",
168+
ImportState: true,
169+
ImportStateVerify: true,
170+
},
146171
},
147172
})
148173
}

google/resource_google_project_organization_policy.go

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"strings"
56

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

18+
Importer: &schema.ResourceImporter{
19+
State: resourceProjectOrgPolicyImporter,
20+
},
21+
1722
Schema: mergeSchemas(
1823
schemaOrganizationPolicy,
1924
map[string]*schema.Schema{
@@ -27,6 +32,17 @@ func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
2732
}
2833
}
2934

35+
func resourceProjectOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
36+
parts := strings.Split(d.Id(), ":")
37+
if len(parts) != 2 {
38+
return nil, fmt.Errorf("ID must be in the format <projectId>:constraints/<constraint>")
39+
}
40+
d.Set("project", parts[0])
41+
d.Set("constraint", parts[1])
42+
43+
return []*schema.ResourceData{d}, nil
44+
}
45+
3046
func resourceGoogleProjectOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
3147
if err := setProjectOrganizationPolicy(d, meta); err != nil {
3248
return err

google/resource_google_project_organization_policy_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func testAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
8484
Config: testAccProjectOrganizationPolicyConfig_list_allowAll(projectId),
8585
Check: testAccCheckGoogleProjectOrganizationListPolicyAll("list", "ALLOW"),
8686
},
87+
{
88+
ResourceName: "google_project_organization_policy.list",
89+
ImportState: true,
90+
ImportStateVerify: true,
91+
},
8792
},
8893
})
8994
}
@@ -100,6 +105,11 @@ func testAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
100105
Config: testAccProjectOrganizationPolicyConfig_list_allowSome(project),
101106
Check: testAccCheckGoogleProjectOrganizationListPolicyAllowedValues("list", []string{canonicalProject}),
102107
},
108+
{
109+
ResourceName: "google_project_organization_policy.list",
110+
ImportState: true,
111+
ImportStateVerify: true,
112+
},
103113
},
104114
})
105115
}
@@ -115,6 +125,11 @@ func testAccProjectOrganizationPolicy_list_denySome(t *testing.T) {
115125
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
116126
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
117127
},
128+
{
129+
ResourceName: "google_project_organization_policy.list",
130+
ImportState: true,
131+
ImportStateVerify: true,
132+
},
118133
},
119134
})
120135
}
@@ -134,6 +149,11 @@ func testAccProjectOrganizationPolicy_list_update(t *testing.T) {
134149
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
135150
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
136151
},
152+
{
153+
ResourceName: "google_project_organization_policy.list",
154+
ImportState: true,
155+
ImportStateVerify: true,
156+
},
137157
},
138158
})
139159
}
@@ -150,6 +170,11 @@ func testAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
150170
Config: testAccProjectOrganizationPolicyConfig_restore_defaultTrue(projectId),
151171
Check: getGoogleProjectOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
152172
},
173+
{
174+
ResourceName: "google_project_organization_policy.restore",
175+
ImportState: true,
176+
ImportStateVerify: true,
177+
},
153178
},
154179
})
155180
}

0 commit comments

Comments
 (0)