Skip to content

Commit abfa16b

Browse files
committed
Address comments
Signed-off-by: graysonwu <wgrayson@vmware.com>
1 parent e6a4c5b commit abfa16b

10 files changed

+84
-74
lines changed

nsxt/policy_common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func getSecurityPolicyAndGatewayRuleSchema(scopeRequired bool, isIds bool, nsxID
191191
"nsx_id": getFlexNsxIDSchema(nsxIDReadOnly),
192192
"display_name": getDisplayNameSchema(),
193193
"description": getDescriptionSchema(),
194+
"path": getPathSchema(),
194195
"revision": getRevisionSchema(),
195196
"destination_groups": {
196197
Type: schema.TypeSet,
@@ -305,7 +306,6 @@ func getSecurityPolicyAndGatewayRuleSchema(scopeRequired bool, isIds bool, nsxID
305306
Required: true,
306307
}
307308
ruleSchema["context"] = getContextSchema()
308-
ruleSchema["path"] = getPathSchema()
309309
} else {
310310
ruleSchema["sequence_number"] = &schema.Schema{
311311
Type: schema.TypeInt,
@@ -408,6 +408,7 @@ func setPolicyRulesInSchema(d *schema.ResourceData, rules []model.Rule) error {
408408
elem := make(map[string]interface{})
409409
elem["display_name"] = rule.DisplayName
410410
elem["description"] = rule.Description
411+
elem["path"] = rule.Path
411412
elem["notes"] = rule.Notes
412413
elem["logged"] = rule.Logged
413414
elem["log_label"] = rule.Tag

nsxt/policy_utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func getDomainFromResourcePath(rPath string) string {
174174
return getResourceIDFromResourcePath(rPath, "domains")
175175
}
176176

177+
func getProjectIDFromResourcePath(rPath string) string {
178+
return getResourceIDFromResourcePath(rPath, "projects")
179+
}
180+
177181
func getResourceIDFromResourcePath(rPath string, rType string) string {
178182
segments := strings.Split(rPath, "/")
179183
for i, seg := range segments {

nsxt/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func Provider() *schema.Provider {
441441
"nsxt_policy_lb_client_ssl_profile": resourceNsxtPolicyLBClientSslProfile(),
442442
"nsxt_policy_lb_http_application_profile": resourceNsxtPolicyLBHttpApplicationProfile(),
443443
"nsxt_policy_security_policy_rule": resourceNsxtPolicySecurityPolicyRule(),
444-
"nsxt_policy_security_policy_no_rule": resourceNsxtPolicySecurityPolicyNoRule(),
444+
"nsxt_policy_parent_security_policy": resourceNsxtPolicyParentSecurityPolicy(),
445445
},
446446

447447
ConfigureFunc: providerConfigure,

nsxt/resource_nsxt_policy_security_policy_no_rule.go renamed to nsxt/resource_nsxt_policy_parent_security_policy.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import (
1313
"github.com/vmware/terraform-provider-nsxt/api/infra/domains"
1414
)
1515

16-
func resourceNsxtPolicySecurityPolicyNoRule() *schema.Resource {
16+
func resourceNsxtPolicyParentSecurityPolicy() *schema.Resource {
1717
return &schema.Resource{
18-
Create: resourceNsxtPolicySecurityPolicyNoRuleCreate,
19-
Read: resourceNsxtPolicySecurityPolicyNoRuleRead,
20-
Update: resourceNsxtPolicySecurityPolicyNoRuleUpdate,
21-
Delete: resourceNsxtPolicySecurityPolicyNoRuleDelete,
18+
Create: resourceNsxtPolicyParentSecurityPolicyCreate,
19+
Read: resourceNsxtPolicyParentSecurityPolicyRead,
20+
Update: resourceNsxtPolicyParentSecurityPolicyUpdate,
21+
Delete: resourceNsxtPolicyParentSecurityPolicyDelete,
2222
Importer: &schema.ResourceImporter{
2323
State: nsxtDomainResourceImporter,
2424
},
2525
Schema: getPolicySecurityPolicySchema(false, true, false),
2626
}
2727
}
2828

29-
func resourceNsxtPolicySecurityPolicyNoRuleCreate(d *schema.ResourceData, m interface{}) error {
29+
func resourceNsxtPolicyParentSecurityPolicyCreate(d *schema.ResourceData, m interface{}) error {
3030
connector := getPolicyConnector(m)
3131

3232
// Initialize resource Id and verify this ID is not yet used
@@ -39,7 +39,7 @@ func resourceNsxtPolicySecurityPolicyNoRuleCreate(d *schema.ResourceData, m inte
3939
domain := d.Get("domain").(string)
4040
client := domains.NewSecurityPoliciesClient(getSessionContext(d, m), connector)
4141

42-
obj := securityPolicySchemaToModelNoRule(d, id)
42+
obj := parentSecurityPolicySchemaToModel(d, id)
4343
err = client.Patch(domain, id, obj)
4444
if err != nil {
4545
return handleCreateError("Security Policy", id, err)
@@ -48,10 +48,10 @@ func resourceNsxtPolicySecurityPolicyNoRuleCreate(d *schema.ResourceData, m inte
4848
d.SetId(id)
4949
d.Set("nsx_id", id)
5050

51-
return resourceNsxtPolicySecurityPolicyNoRuleRead(d, m)
51+
return resourceNsxtPolicyParentSecurityPolicyRead(d, m)
5252
}
5353

54-
func securityPolicySchemaToModelNoRule(d *schema.ResourceData, id string) model.SecurityPolicy {
54+
func parentSecurityPolicySchemaToModel(d *schema.ResourceData, id string) model.SecurityPolicy {
5555
displayName := d.Get("display_name").(string)
5656
description := d.Get("description").(string)
5757
tags := getPolicyTagsFromSchema(d)
@@ -80,12 +80,12 @@ func securityPolicySchemaToModelNoRule(d *schema.ResourceData, id string) model.
8080
}
8181
}
8282

83-
func resourceNsxtPolicySecurityPolicyNoRuleRead(d *schema.ResourceData, m interface{}) error {
84-
_, err := securityPolicyModelToSchemaNoRule(d, m)
83+
func resourceNsxtPolicyParentSecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
84+
_, err := parentSecurityPolicyModelToSchema(d, m)
8585
return err
8686
}
8787

88-
func securityPolicyModelToSchemaNoRule(d *schema.ResourceData, m interface{}) (*model.SecurityPolicy, error) {
88+
func parentSecurityPolicyModelToSchema(d *schema.ResourceData, m interface{}) (*model.SecurityPolicy, error) {
8989
connector := getPolicyConnector(m)
9090
id := d.Id()
9191
domainName := d.Get("domain").(string)
@@ -118,7 +118,7 @@ func securityPolicyModelToSchemaNoRule(d *schema.ResourceData, m interface{}) (*
118118
return &obj, nil
119119
}
120120

121-
func resourceNsxtPolicySecurityPolicyNoRuleUpdate(d *schema.ResourceData, m interface{}) error {
121+
func resourceNsxtPolicyParentSecurityPolicyUpdate(d *schema.ResourceData, m interface{}) error {
122122
connector := getPolicyConnector(m)
123123

124124
id := d.Id()
@@ -134,16 +134,16 @@ func resourceNsxtPolicySecurityPolicyNoRuleUpdate(d *schema.ResourceData, m inte
134134
return handleUpdateError("Security Policy", id, err)
135135
}
136136

137-
obj := securityPolicySchemaToModelNoRule(d, id)
137+
obj := parentSecurityPolicySchemaToModel(d, id)
138138
obj.Rules = remoteObj.Rules
139139
err = client.Patch(domain, id, obj)
140140
if err != nil {
141141
return handleUpdateError("Security Policy", id, err)
142142
}
143143

144-
return resourceNsxtPolicySecurityPolicyNoRuleRead(d, m)
144+
return resourceNsxtPolicyParentSecurityPolicyRead(d, m)
145145
}
146146

147-
func resourceNsxtPolicySecurityPolicyNoRuleDelete(d *schema.ResourceData, m interface{}) error {
147+
func resourceNsxtPolicyParentSecurityPolicyDelete(d *schema.ResourceData, m interface{}) error {
148148
return resourceNsxtPolicySecurityPolicyDelete(d, m)
149149
}

nsxt/resource_nsxt_policy_security_policy_no_rule_test.go renamed to nsxt/resource_nsxt_policy_parent_security_policy_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import (
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1212
)
1313

14-
func TestAccResourceNsxtPolicySecurityPolicyNoRule_basic(t *testing.T) {
15-
testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t, false, func() {
14+
func TestAccResourceNsxtPolicyParentSecurityPolicy_basic(t *testing.T) {
15+
testAccResourceNsxtPolicyParentSecurityPolicyBasic(t, false, func() {
1616
testAccPreCheck(t)
1717
})
1818
}
1919

20-
func TestAccResourceNsxtPolicySecurityPolicyNoRule_multitenancy(t *testing.T) {
21-
testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t, true, func() {
20+
func TestAccResourceNsxtPolicyParentSecurityPolicy_multitenancy(t *testing.T) {
21+
testAccResourceNsxtPolicyParentSecurityPolicyBasic(t, true, func() {
2222
testAccPreCheck(t)
2323
testAccOnlyMultitenancy(t)
2424
})
2525
}
2626

27-
func testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t *testing.T, withContext bool, preCheck func()) {
28-
testResourceName := "nsxt_policy_security_policy_no_rule.test"
27+
func testAccResourceNsxtPolicyParentSecurityPolicyBasic(t *testing.T, withContext bool, preCheck func()) {
28+
testResourceName := "nsxt_policy_parent_security_policy.test"
2929

3030
name := getAccTestResourceName()
3131
updatedName := getAccTestResourceName()
@@ -40,11 +40,11 @@ func testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t *testing.T, withContex
4040
PreCheck: preCheck,
4141
Providers: testAccProviders,
4242
CheckDestroy: func(state *terraform.State) error {
43-
return testAccNsxtPolicySecurityPolicyNoRuleCheckDestroy(state, updatedName)
43+
return testAccNsxtPolicyParentSecurityPolicyCheckDestroy(state, updatedName)
4444
},
4545
Steps: []resource.TestStep{
4646
{
47-
Config: testAccNsxtPolicySecurityPolicyNoRuleTemplate(withContext, name, locked, seqNum, tcpStrict),
47+
Config: testAccNsxtPolicyParentSecurityPolicyTemplate(withContext, name, locked, seqNum, tcpStrict),
4848
Check: resource.ComposeTestCheckFunc(
4949
testAccNsxtPolicySecurityPolicyExists(testResourceName, defaultDomain),
5050
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
@@ -54,7 +54,7 @@ func testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t *testing.T, withContex
5454
),
5555
},
5656
{
57-
Config: testAccNsxtPolicySecurityPolicyNoRuleTemplate(withContext, updatedName, updatedLocked, updatedSeqNum, updatedTCPStrict),
57+
Config: testAccNsxtPolicyParentSecurityPolicyTemplate(withContext, updatedName, updatedLocked, updatedSeqNum, updatedTCPStrict),
5858
Check: resource.ComposeTestCheckFunc(
5959
testAccNsxtPolicySecurityPolicyExists(testResourceName, defaultDomain),
6060
resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName),
@@ -67,19 +67,19 @@ func testAccResourceNsxtPolicySecurityPolicyNoRuleBasic(t *testing.T, withContex
6767
})
6868
}
6969

70-
func TestAccResourceNsxtPolicySecurityPolicyNoRule_importBasic(t *testing.T) {
70+
func TestAccResourceNsxtPolicyParentSecurityPolicy_importBasic(t *testing.T) {
7171
name := getAccTestResourceName()
72-
testResourceName := "nsxt_policy_security_policy_no_rule.test"
72+
testResourceName := "nsxt_policy_parent_security_policy.test"
7373

7474
resource.ParallelTest(t, resource.TestCase{
7575
PreCheck: func() { testAccPreCheck(t) },
7676
Providers: testAccProviders,
7777
CheckDestroy: func(state *terraform.State) error {
78-
return testAccNsxtPolicySecurityPolicyNoRuleCheckDestroy(state, name)
78+
return testAccNsxtPolicyParentSecurityPolicyCheckDestroy(state, name)
7979
},
8080
Steps: []resource.TestStep{
8181
{
82-
Config: testAccNsxtPolicySecurityPolicyNoRuleTemplate(false, name, "true", "1", "true"),
82+
Config: testAccNsxtPolicyParentSecurityPolicyTemplate(false, name, "true", "1", "true"),
8383
},
8484
{
8585
ResourceName: testResourceName,
@@ -91,9 +91,9 @@ func TestAccResourceNsxtPolicySecurityPolicyNoRule_importBasic(t *testing.T) {
9191
})
9292
}
9393

94-
func TestAccResourceNsxtPolicySecurityPolicyNoRule_importBasic_multitenancy(t *testing.T) {
94+
func TestAccResourceNsxtPolicyParentSecurityPolicy_importBasic_multitenancy(t *testing.T) {
9595
name := getAccTestResourceName()
96-
testResourceName := "nsxt_policy_security_policy_no_rule.test"
96+
testResourceName := "nsxt_policy_parent_security_policy.test"
9797

9898
resource.ParallelTest(t, resource.TestCase{
9999
PreCheck: func() {
@@ -106,7 +106,7 @@ func TestAccResourceNsxtPolicySecurityPolicyNoRule_importBasic_multitenancy(t *t
106106
},
107107
Steps: []resource.TestStep{
108108
{
109-
Config: testAccNsxtPolicySecurityPolicyNoRuleTemplate(true, name, "true", "1", "true"),
109+
Config: testAccNsxtPolicyParentSecurityPolicyTemplate(true, name, "true", "1", "true"),
110110
},
111111
{
112112
ResourceName: testResourceName,
@@ -118,11 +118,11 @@ func TestAccResourceNsxtPolicySecurityPolicyNoRule_importBasic_multitenancy(t *t
118118
})
119119
}
120120

121-
func testAccNsxtPolicySecurityPolicyNoRuleCheckDestroy(state *terraform.State, displayName string) error {
121+
func testAccNsxtPolicyParentSecurityPolicyCheckDestroy(state *terraform.State, displayName string) error {
122122
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
123123
for _, rs := range state.RootModule().Resources {
124124

125-
if rs.Type != "nsxt_policy_security_policy_no_rule" {
125+
if rs.Type != "nsxt_policy_parent_security_policy" {
126126
continue
127127
}
128128

@@ -139,13 +139,13 @@ func testAccNsxtPolicySecurityPolicyNoRuleCheckDestroy(state *terraform.State, d
139139
return nil
140140
}
141141

142-
func testAccNsxtPolicySecurityPolicyNoRuleTemplate(withContext bool, name, locked, seqNum, tcpStrict string) string {
142+
func testAccNsxtPolicyParentSecurityPolicyTemplate(withContext bool, name, locked, seqNum, tcpStrict string) string {
143143
context := ""
144144
if withContext {
145145
context = testAccNsxtPolicyMultitenancyContext()
146146
}
147147
return testAccNsxtPolicySecurityPolicyDeps() + fmt.Sprintf(`
148-
resource "nsxt_policy_security_policy_no_rule" "test" {
148+
resource "nsxt_policy_parent_security_policy" "test" {
149149
%s
150150
display_name = "%s"
151151
description = "Acceptance Test"

nsxt/resource_nsxt_policy_security_policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func resourceNsxtPolicySecurityPolicyExistsPartial(domainName string) func(sessi
5757

5858
func policySecurityPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, connector client.Connector, isGlobalManager bool, id string, createFlow bool) error {
5959

60-
obj := securityPolicySchemaToModelNoRule(d, id)
60+
obj := parentSecurityPolicySchemaToModel(d, id)
6161
domain := d.Get("domain").(string)
6262
revision := int64(d.Get("revision").(int))
6363
log.Printf("[INFO] Creating Security Policy with ID %s", id)
@@ -105,7 +105,7 @@ func resourceNsxtPolicySecurityPolicyCreate(d *schema.ResourceData, m interface{
105105
}
106106

107107
func resourceNsxtPolicySecurityPolicyRead(d *schema.ResourceData, m interface{}) error {
108-
obj, err := securityPolicyModelToSchemaNoRule(d, m)
108+
obj, err := parentSecurityPolicyModelToSchema(d, m)
109109
if err != nil {
110110
return err
111111
}

nsxt/resource_nsxt_policy_security_policy_rule.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,26 @@ func resourceNsxtPolicySecurityPolicyRule() *schema.Resource {
3232
func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interface{}) error {
3333
connector := getPolicyConnector(m)
3434

35+
policyPath := d.Get("policy_path").(string)
36+
projectID := getProjectIDFromResourcePath(policyPath)
37+
domain := getDomainFromResourcePath(policyPath)
38+
policyID := getPolicyIDFromPath(policyPath)
39+
40+
if len(d.Get("context").([]interface{})) == 0 {
41+
contexts := make([]interface{}, 1)
42+
ctxMap := make(map[string]interface{})
43+
ctxMap["project_id"] = projectID
44+
contexts[0] = ctxMap
45+
d.Set("context", contexts)
46+
}
47+
3548
// Initialize resource Id and verify this ID is not yet used
3649
id, err := getOrGenerateID2(d, m, resourceNsxtPolicySecurityPolicyRuleExistsPartial(d.Get("policy_path").(string)))
3750
if err != nil {
3851
return err
3952
}
4053

41-
policyPath := d.Get("policy_path").(string)
4254
log.Printf("[INFO] Creating Security Policy Rule with ID %s under policy %s", id, policyPath)
43-
domain := getDomainFromResourcePath(policyPath)
44-
policyID := getPolicyIDFromPath(policyPath)
45-
4655
client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector)
4756
rule := securityPolicyRuleSchemaToModel(d, id)
4857
err = client.Patch(domain, policyID, id, rule)
@@ -134,8 +143,18 @@ func resourceNsxtPolicySecurityPolicyRuleRead(d *schema.ResourceData, m interfac
134143
}
135144

136145
policyPath := d.Get("policy_path").(string)
146+
projectID := getProjectIDFromResourcePath(policyPath)
137147
domain := getDomainFromResourcePath(policyPath)
138148
policyID := getPolicyIDFromPath(policyPath)
149+
150+
if len(d.Get("context").([]interface{})) == 0 {
151+
contexts := make([]interface{}, 1)
152+
ctxMap := make(map[string]interface{})
153+
ctxMap["project_id"] = projectID
154+
contexts[0] = ctxMap
155+
d.Set("context", contexts)
156+
}
157+
139158
client := securitypolicies.NewRulesClient(getSessionContext(d, m), connector)
140159
rule, err := client.Get(domain, policyID, id)
141160
if err != nil {
@@ -218,10 +237,6 @@ func resourceNsxtPolicySecurityPolicyRuleDelete(d *schema.ResourceData, m interf
218237

219238
func nsxtSecurityPolicyRuleImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
220239
importID := d.Id()
221-
// Example of Rule path: /infra/domains/default/security-policies/04e862ad-ddce-434c-8453-229e2740982e/rules/b971bdc3-9e8f-442d-a694-846cbbb46ca5
222-
if strings.Count(importID, "/") != 7 {
223-
return nil, fmt.Errorf("Invalid SecurityPolicyRule path %s", importID)
224-
}
225240
rd, err := nsxtPolicyPathResourceImporterHelper(d, m)
226241
if err != nil {
227242
return rd, err

nsxt/resource_nsxt_policy_security_policy_rule_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func testAccNsxtPolicySecurityPolicyRuleDeps(withContext bool) string {
176176
context = testAccNsxtPolicyMultitenancyContext()
177177
}
178178
return testAccNsxtPolicySecurityPolicyDeps() + fmt.Sprintf(`
179-
resource "nsxt_policy_security_policy_no_rule" "policy1" {
179+
resource "nsxt_policy_parent_security_policy" "policy1" {
180180
%s
181181
display_name = "no-rule-policy"
182182
description = "Acceptance Test"
@@ -205,7 +205,7 @@ func testAccNsxtPolicySecurityPolicyRuleTemplate(withContext bool, name, action,
205205
resource "nsxt_policy_security_policy_rule" "test" {
206206
%s
207207
display_name = "%s"
208-
policy_path = nsxt_policy_security_policy_no_rule.policy1.path
208+
policy_path = nsxt_policy_parent_security_policy.policy1.path
209209
action = "%s"
210210
direction = "%s"
211211
ip_version = "%s"
@@ -217,13 +217,13 @@ resource "nsxt_policy_security_policy_rule" "test" {
217217
tag = "orange"
218218
}
219219
220-
depends_on = [nsxt_policy_security_policy_no_rule.policy1, nsxt_policy_group.group2]
220+
depends_on = [nsxt_policy_parent_security_policy.policy1, nsxt_policy_group.group2]
221221
}
222222
223223
data "nsxt_policy_security_policy_rule" "test" {
224224
%s
225225
display_name = "%s"
226-
policy_path = nsxt_policy_security_policy_no_rule.policy1.path
226+
policy_path = nsxt_policy_parent_security_policy.policy1.path
227227
depends_on = [nsxt_policy_security_policy_rule.test]
228228
}`, context, name, action, direction, ipVersion, seqNum, context, name)
229229
}

0 commit comments

Comments
 (0)