Skip to content

Commit 4c7763c

Browse files
modular-magicianchrisst
authored andcommitted
Add import support for organization_policies (#3218)
<!-- This change is generated by MagicModules. --> /cc @chrisst
1 parent a99e032 commit 4c7763c

6 files changed

+119
-5
lines changed

google/resource_google_folder_organization_policy.go

+23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func resourceGoogleFolderOrganizationPolicy() *schema.Resource {
1414
Update: resourceGoogleFolderOrganizationPolicyUpdate,
1515
Delete: resourceGoogleFolderOrganizationPolicyDelete,
1616

17+
Importer: &schema.ResourceImporter{
18+
State: resourceFolderOrgPolicyImporter,
19+
},
20+
1721
Schema: mergeSchemas(
1822
schemaOrganizationPolicy,
1923
map[string]*schema.Schema{
@@ -27,6 +31,25 @@ func resourceGoogleFolderOrganizationPolicy() *schema.Resource {
2731
}
2832
}
2933

34+
func resourceFolderOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
35+
config := meta.(*Config)
36+
37+
if err := parseImportId([]string{
38+
"folders/(?P<folder>[^/]+):constraints/(?P<constraint>[^/]+)",
39+
"(?P<folder>[^/]+):(?P<constraint>[^/]+)"},
40+
d, config); err != nil {
41+
return nil, err
42+
}
43+
44+
if d.Get("folder") == "" || d.Get("constraint") == "" {
45+
return nil, fmt.Errorf("unable to parse folder or constraint. Check import formats")
46+
}
47+
48+
d.Set("folder", "folders/"+d.Get("folder").(string))
49+
50+
return []*schema.ResourceData{d}, nil
51+
}
52+
3053
func resourceGoogleFolderOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
3154
if err := setFolderOrganizationPolicy(d, meta); err != nil {
3255
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

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
1414
Update: resourceGoogleProjectOrganizationPolicyUpdate,
1515
Delete: resourceGoogleProjectOrganizationPolicyDelete,
1616

17+
Importer: &schema.ResourceImporter{
18+
State: resourceProjectOrgPolicyImporter,
19+
},
20+
1721
Schema: mergeSchemas(
1822
schemaOrganizationPolicy,
1923
map[string]*schema.Schema{
@@ -27,6 +31,24 @@ func resourceGoogleProjectOrganizationPolicy() *schema.Resource {
2731
}
2832
}
2933

34+
func resourceProjectOrgPolicyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
35+
config := meta.(*Config)
36+
37+
if err := parseImportId([]string{
38+
"projects/(?P<project>[^/]+):constraints/(?P<constraint>[^/]+)",
39+
"(?P<project>[^/]+):constraints/(?P<constraint>[^/]+)",
40+
"(?P<project>[^/]+):(?P<constraint>[^/]+)"},
41+
d, config); err != nil {
42+
return nil, err
43+
}
44+
45+
if d.Get("project") == "" || d.Get("constraint") == "" {
46+
return nil, fmt.Errorf("unable to parse project or constraint. Check import formats")
47+
}
48+
49+
return []*schema.ResourceData{d}, nil
50+
}
51+
3052
func resourceGoogleProjectOrganizationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
3153
if err := setProjectOrganizationPolicy(d, meta); err != nil {
3254
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
}

website/docs/r/google_folder_organization_policy.html.markdown

+13-4
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ The following arguments are supported:
8787

8888
* `version` - (Optional) Version of the Policy. Default version is 0.
8989

90-
* `boolean_policy` - (Optional) A boolean policy is a constraint that is either enforced or not. Structure is documented below.
90+
* `boolean_policy` - (Optional) A boolean policy is a constraint that is either enforced or not. Structure is documented below.
9191

92-
* `list_policy` - (Optional) A policy that can define specific values that are allowed or denied for the given constraint. It
92+
* `list_policy` - (Optional) A policy that can define specific values that are allowed or denied for the given constraint. It
9393
can also be used to allow or deny all values. Structure is documented below.
9494

95-
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
95+
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
9696

9797
- - -
9898

@@ -124,6 +124,15 @@ The `restore_policy` block supports:
124124
In addition to the arguments listed above, the following computed attributes are
125125
exported:
126126

127-
* `etag` - (Computed) The etag of the organization policy. `etag` is used for optimistic concurrency control as a way to help prevent simultaneous updates of a policy from overwriting each other.
127+
* `etag` - (Computed) The etag of the organization policy. `etag` is used for optimistic concurrency control as a way to help prevent simultaneous updates of a policy from overwriting each other.
128128

129129
* `update_time` - (Computed) The timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds, representing when the variable was last updated. Example: "2016-10-09T12:33:37.578138407Z".
130+
131+
## Import
132+
133+
Folder organization policies can be imported using any of the follow formats:
134+
135+
```
136+
$ terraform import google_folder_organization_policy.policy folders/folder-1234:constraints/serviceuser.services
137+
$ terraform import google_folder_organization_policy.policy folder-1234:serviceuser.services
138+
```

website/docs/r/google_project_organization_policy.html.markdown

+11-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ The following arguments are supported:
9191

9292
* `list_policy` - (Optional) A policy that can define specific values that are allowed or denied for the given constraint. It can also be used to allow or deny all values. Structure is documented below.
9393

94-
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
94+
* `restore_policy` - (Optional) A restore policy is a constraint to restore the default policy. Structure is documented below.
9595

9696
- - -
9797

@@ -126,3 +126,13 @@ exported:
126126
* `etag` - (Computed) The etag of the organization policy. `etag` is used for optimistic concurrency control as a way to help prevent simultaneous updates of a policy from overwriting each other.
127127

128128
* `update_time` - (Computed) The timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds, representing when the variable was last updated. Example: "2016-10-09T12:33:37.578138407Z".
129+
130+
## Import
131+
132+
Project organization policies can be imported using any of the follow formats:
133+
134+
```
135+
$ terraform import google_project_organization_policy.policy projects/test-project:constraints/serviceuser.services
136+
$ terraform import google_project_organization_policy.policy test-project:constraints/serviceuser.services
137+
$ terraform import google_project_organization_policy.policy test-project:serviceuser.services
138+
```

0 commit comments

Comments
 (0)