Skip to content

Commit 0401afb

Browse files
Place security policy under folder for easier cleanup (#5174) (#3601)
Signed-off-by: Modular Magician <[email protected]>
1 parent 3c56207 commit 0401afb

10 files changed

+38
-45
lines changed

.changelog/5174.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google-beta/resource_compute_organization_security_policy_association.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ func resourceComputeOrganizationSecurityPolicyAssociationDelete(d *schema.Resour
239239
func resourceComputeOrganizationSecurityPolicyAssociationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
240240
config := meta.(*Config)
241241
if err := parseImportId([]string{
242-
"(?P<policy_id>[^/]+)/association/(?P<name>[^/]+)",
243-
"(?P<policy_id>[^/]+)/(?P<name>[^/]+)",
242+
"(?P<policy_id>.+)/association/(?P<name>[^/]+)",
244243
}, d, config); err != nil {
245244
return nil, err
246245
}

google-beta/resource_compute_organization_security_policy_association_generated_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,20 @@ func TestAccComputeOrganizationSecurityPolicyAssociation_organizationSecurityPol
5151

5252
func testAccComputeOrganizationSecurityPolicyAssociation_organizationSecurityPolicyAssociationBasicExample(context map[string]interface{}) string {
5353
return Nprintf(`
54+
resource "google_folder" "security_policy_target" {
55+
provider = google-beta
56+
display_name = "tf-test-secpol-%{random_suffix}"
57+
parent = "organizations/%{org_id}"
58+
}
59+
5460
resource "google_compute_organization_security_policy" "policy" {
5561
provider = google-beta
56-
5762
display_name = "tf-test%{random_suffix}"
58-
parent = "organizations/%{org_id}"
63+
parent = google_folder.security_policy_target.name
5964
}
6065
6166
resource "google_compute_organization_security_policy_rule" "policy" {
6267
provider = google-beta
63-
6468
policy_id = google_compute_organization_security_policy.policy.id
6569
action = "allow"
6670
@@ -83,7 +87,6 @@ resource "google_compute_organization_security_policy_rule" "policy" {
8387
8488
resource "google_compute_organization_security_policy_association" "policy" {
8589
provider = google-beta
86-
8790
name = "tf-test%{random_suffix}"
8891
attachment_id = google_compute_organization_security_policy.policy.parent
8992
policy_id = google_compute_organization_security_policy.policy.id

google-beta/resource_compute_organization_security_policy_generated_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func testAccComputeOrganizationSecurityPolicy_organizationSecurityPolicyBasicExa
5252
return Nprintf(`
5353
resource "google_compute_organization_security_policy" "policy" {
5454
provider = google-beta
55-
5655
display_name = "tf-test%{random_suffix}"
5756
parent = "organizations/%{org_id}"
5857
}

google-beta/resource_compute_organization_security_policy_rule.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"log"
2020
"reflect"
2121
"strconv"
22-
"strings"
2322
"time"
2423

2524
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -540,33 +539,18 @@ func resourceComputeOrganizationSecurityPolicyRuleDelete(d *schema.ResourceData,
540539

541540
func resourceComputeOrganizationSecurityPolicyRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
542541
config := meta.(*Config)
543-
544-
// current import_formats can't import fields with forward slashes in their value
545-
if err := parseImportId([]string{"(?P<policy_id>.+)"}, d, config); err != nil {
542+
if err := parseImportId([]string{
543+
"(?P<policy_id>.+)/priority/(?P<priority>[^/]+)",
544+
}, d, config); err != nil {
546545
return nil, err
547546
}
548547

549-
nameParts := strings.Split(d.Get("policy_id").(string), "/")
550-
if len(nameParts) != 6 {
551-
return nil, fmt.Errorf(
552-
"Saw %s when the import ID is expected to have shape %s",
553-
d.Get("policy_id").(string),
554-
"locations/global/securityPolicies/{{policy_id}}/priority/{{priority}}",
555-
)
556-
}
557-
if err := d.Set("policy_id", fmt.Sprintf("locations/global/securityPolicies/%s", nameParts[3])); err != nil {
558-
return nil, fmt.Errorf("Error setting policy_id: %s", err)
559-
}
560-
561-
if prio, err := strconv.ParseInt(nameParts[5], 10, 64); err != nil {
562-
return nil, fmt.Errorf(
563-
"Priority %s cannot be converted to integer", nameParts[5],
564-
)
565-
} else {
566-
if err := d.Set("priority", prio); err != nil {
567-
return nil, fmt.Errorf("Error setting priority: %s", err)
568-
}
548+
// Replace import id for the resource id
549+
id, err := replaceVars(d, config, "{{policy_id}}/priority/{{priority}}")
550+
if err != nil {
551+
return nil, fmt.Errorf("Error constructing id: %s", err)
569552
}
553+
d.SetId(id)
570554

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

google-beta/resource_compute_organization_security_policy_rule_generated_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func testAccComputeOrganizationSecurityPolicyRule_organizationSecurityPolicyRule
5353
return Nprintf(`
5454
resource "google_compute_organization_security_policy" "policy" {
5555
provider = google-beta
56-
5756
display_name = "tf-test%{random_suffix}"
5857
parent = "organizations/%{org_id}"
5958
}

google-beta/resource_compute_organization_security_policy_rule_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ func TestAccComputeOrganizationSecurityPolicyRule_organizationSecurityPolicyRule
4141

4242
func testAccComputeOrganizationSecurityPolicyRule_organizationSecurityPolicyRulePreUpdateExample(context map[string]interface{}) string {
4343
return Nprintf(`
44-
resource "google_compute_organization_security_policy" "policy" {
45-
display_name = "tf-test%{random_suffix}"
44+
resource "google_folder" "security_policy_target" {
45+
display_name = "tf-test-secpol-%{random_suffix}"
4646
parent = "organizations/%{org_id}"
4747
}
4848
49+
resource "google_compute_organization_security_policy" "policy" {
50+
display_name = "tf-test%{random_suffix}"
51+
parent = google_folder.security_policy_target.name
52+
}
4953
resource "google_compute_organization_security_policy_rule" "policy" {
50-
5154
policy_id = google_compute_organization_security_policy.policy.id
5255
action = "allow"
5356
@@ -72,13 +75,17 @@ resource "google_compute_organization_security_policy_rule" "policy" {
7275

7376
func testAccComputeOrganizationSecurityPolicyRule_organizationSecurityPolicyRulePostUpdateExample(context map[string]interface{}) string {
7477
return Nprintf(`
78+
resource "google_folder" "security_policy_target" {
79+
display_name = "tf-test-secpol-%{random_suffix}"
80+
parent = "organizations/%{org_id}"
81+
}
82+
7583
resource "google_compute_organization_security_policy" "policy" {
7684
display_name = "tf-test%{random_suffix}"
77-
parent = "organizations/%{org_id}"
85+
parent = google_folder.security_policy_target.name
7886
}
7987
8088
resource "google_compute_organization_security_policy_rule" "policy" {
81-
8289
policy_id = google_compute_organization_security_policy.policy.id
8390
action = "deny"
8491

website/docs/r/compute_organization_security_policy.html.markdown

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ To get more information about OrganizationSecurityPolicy, see:
3939
```hcl
4040
resource "google_compute_organization_security_policy" "policy" {
4141
provider = google-beta
42-
4342
display_name = "tf-test%{random_suffix}"
4443
parent = "organizations/123456789"
4544
}

website/docs/r/compute_organization_security_policy_association.html.markdown

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ To get more information about OrganizationSecurityPolicyAssociation, see:
3737

3838

3939
```hcl
40+
resource "google_folder" "security_policy_target" {
41+
provider = google-beta
42+
display_name = "tf-test-secpol-%{random_suffix}"
43+
parent = "organizations/123456789"
44+
}
45+
4046
resource "google_compute_organization_security_policy" "policy" {
4147
provider = google-beta
42-
4348
display_name = "tf-test%{random_suffix}"
44-
parent = "organizations/123456789"
49+
parent = google_folder.security_policy_target.name
4550
}
4651
4752
resource "google_compute_organization_security_policy_rule" "policy" {
4853
provider = google-beta
49-
5054
policy_id = google_compute_organization_security_policy.policy.id
5155
action = "allow"
5256
@@ -69,7 +73,6 @@ resource "google_compute_organization_security_policy_rule" "policy" {
6973
7074
resource "google_compute_organization_security_policy_association" "policy" {
7175
provider = google-beta
72-
7376
name = "tf-test%{random_suffix}"
7477
attachment_id = google_compute_organization_security_policy.policy.parent
7578
policy_id = google_compute_organization_security_policy.policy.id
@@ -123,5 +126,4 @@ OrganizationSecurityPolicyAssociation can be imported using any of these accepte
123126

124127
```
125128
$ terraform import google_compute_organization_security_policy_association.default {{policy_id}}/association/{{name}}
126-
$ terraform import google_compute_organization_security_policy_association.default {{policy_id}}/{{name}}
127129
```

website/docs/r/compute_organization_security_policy_rule.html.markdown

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ To get more information about OrganizationSecurityPolicyRule, see:
3939
```hcl
4040
resource "google_compute_organization_security_policy" "policy" {
4141
provider = google-beta
42-
4342
display_name = "tf-test%{random_suffix}"
4443
parent = "organizations/123456789"
4544
}
@@ -208,5 +207,4 @@ OrganizationSecurityPolicyRule can be imported using any of these accepted forma
208207

209208
```
210209
$ terraform import google_compute_organization_security_policy_rule.default {{policy_id}}/priority/{{priority}}
211-
$ terraform import google_compute_organization_security_policy_rule.default {{policy_id}}/{{priority}}
212210
```

0 commit comments

Comments
 (0)