Skip to content

Commit 2faae6c

Browse files
ksamorayaruntony005
authored andcommitted
Replace segment_path and segment_port_id with segment_port_path
segment_port_path contains both of the above. Signed-off-by: Kobi Samoray <[email protected]>
1 parent 4953d33 commit 2faae6c

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

nsxt/resource_nsxt_policy_segment_port_binding.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ func resourceNsxtPolicySegmentPortBinding() *schema.Resource {
1616
Delete: func(d *schema.ResourceData, m interface{}) error { return nil },
1717
Schema: map[string]*schema.Schema{
1818
"context": getContextSchema(false, false, false),
19-
"segment_port_id": {
19+
"segment_port_path": {
2020
Type: schema.TypeString,
21-
Description: "ID of the segment port",
22-
Required: true,
23-
ForceNew: true,
24-
},
25-
"segment_path": {
26-
Type: schema.TypeString,
27-
Description: "Path of the segment",
21+
Description: "Policy path of the segment port",
2822
Required: true,
2923
ForceNew: true,
3024
},
@@ -56,8 +50,12 @@ func resourceNsxtPolicySegmentPortBinding() *schema.Resource {
5650
func resourceNsxtPolicySegmentPortBindingCreate(d *schema.ResourceData, m interface{}) error {
5751
connector := getPolicyConnector(m)
5852
context := getSessionContext(d, m)
59-
segmentPortID := d.Get("segment_port_id").(string)
60-
segmentPath := d.Get("segment_path").(string)
53+
segmentPortPath := d.Get("segment_port_path").(string)
54+
segmentPortID := getPolicyIDFromPath(segmentPortPath)
55+
segmentPath, err := getPolicySegmentPathFromPortPath(segmentPortPath)
56+
if err != nil {
57+
return fmt.Errorf("Error parsing Segment Port Path: %v", err)
58+
}
6159

6260
segmentPort, err := getSegmentPort(segmentPath, segmentPortID, context, connector)
6361
if err != nil {
@@ -81,10 +79,14 @@ func resourceNsxtPolicySegmentPortBindingCreate(d *schema.ResourceData, m interf
8179

8280
func resourceNsxtPolicySegmentPortBindingRead(d *schema.ResourceData, m interface{}) error {
8381
connector := getPolicyConnector(m)
84-
segmentPortID := d.Get("segment_port_id").(string)
85-
segmentPath := d.Get("segment_path").(string)
82+
segmentPortPath := d.Get("segment_port_path").(string)
83+
segmentPortID := getPolicyIDFromPath(segmentPortPath)
84+
segmentPath, err := getPolicySegmentPathFromPortPath(segmentPortPath)
85+
if err != nil {
86+
return fmt.Errorf("Error parsing Segment Port Path: %v", err)
87+
}
8688

87-
_, err := getSegmentPort(segmentPath, segmentPortID, getSessionContext(d, m), connector)
89+
_, err = getSegmentPort(segmentPath, segmentPortID, getSessionContext(d, m), connector)
8890
if err != nil {
8991
if isNotFoundError(err) {
9092
d.SetId("")
@@ -105,8 +107,12 @@ func resourceNsxtPolicySegmentPortBindingUpdate(d *schema.ResourceData, m interf
105107
connector := getPolicyConnector(m)
106108
context := getSessionContext(d, m)
107109

108-
segmentPortID := d.Get("segment_port_id").(string)
109-
segmentPath := d.Get("segment_path").(string)
110+
segmentPortPath := d.Get("segment_port_path").(string)
111+
segmentPortID := getPolicyIDFromPath(segmentPortPath)
112+
segmentPath, err := getPolicySegmentPathFromPortPath(segmentPortPath)
113+
if err != nil {
114+
return fmt.Errorf("Error parsing Segment Port Path: %v", err)
115+
}
110116
segmentPort, err := getSegmentPort(segmentPath, segmentPortID, context, connector)
111117
if err != nil {
112118
return fmt.Errorf("Error getting Segment Port: %v", err)
@@ -126,9 +132,13 @@ func resourceNsxtPolicySegmentPortBindingUpdate(d *schema.ResourceData, m interf
126132
}
127133

128134
func policySegmentPortBindingResourceToInfraStruct(segmentPort model.SegmentPort, d *schema.ResourceData, isDestroy bool) (model.Infra, error) {
129-
segmentPath := d.Get("segment_path").(string)
135+
segmentPortPath := d.Get("segment_port_path").(string)
136+
segmentPath, err := getParameterFromPolicyPath("/segments/", "/ports/", segmentPortPath)
137+
if err != nil {
138+
return model.Infra{}, err
139+
}
130140

131-
err := nsxtPolicySegmentPortProfilesSetInStruct(d, &segmentPort)
141+
err = nsxtPolicySegmentPortProfilesSetInStruct(d, &segmentPort)
132142
if err != nil {
133143
return model.Infra{}, err
134144
}

nsxt/resource_nsxt_policy_segment_port_binding_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ func testAccResourceNsxtPolicySegmentPortBinding_basic(t *testing.T, withContext
4949
Config: testAccResourceNsxtPolicySegmentPortBindingTemplate(tzName, segmentName, profilesPrefix, segmentPortName, createResourceTag, withContext),
5050
Check: resource.ComposeTestCheckFunc(
5151
testAccNsxtPolicySegmentPortBindingExists(testResourceName, withContext),
52-
resource.TestCheckResourceAttrSet(testResourceName, "segment_port_id"),
53-
resource.TestCheckResourceAttrSet(testResourceName, "segment_path"),
52+
resource.TestCheckResourceAttrSet(testResourceName, "segment_port_path"),
5453
resource.TestCheckResourceAttr(testResourceName, "discovery_profile.0.ip_discovery_profile_path", mtPrefix+"/infra/ip-discovery-profiles/"+profilesPrefix+"create"),
5554
resource.TestCheckResourceAttr(testResourceName, "discovery_profile.0.mac_discovery_profile_path", mtPrefix+"/infra/mac-discovery-profiles/"+profilesPrefix+"create"),
5655
resource.TestCheckResourceAttr(testResourceName, "security_profile.0.spoofguard_profile_path", mtPrefix+"/infra/spoofguard-profiles/"+profilesPrefix+"create"),
@@ -62,8 +61,7 @@ func testAccResourceNsxtPolicySegmentPortBinding_basic(t *testing.T, withContext
6261
Config: testAccResourceNsxtPolicySegmentPortBindingTemplate(tzName, segmentName, profilesPrefix, segmentPortName, updateResourceTag, withContext),
6362
Check: resource.ComposeTestCheckFunc(
6463
testAccNsxtPolicySegmentPortBindingExists(testResourceName, withContext),
65-
resource.TestCheckResourceAttrSet(testResourceName, "segment_port_id"),
66-
resource.TestCheckResourceAttrSet(testResourceName, "segment_path"),
64+
resource.TestCheckResourceAttrSet(testResourceName, "segment_port_path"),
6765
resource.TestCheckResourceAttr(testResourceName, "discovery_profile.0.ip_discovery_profile_path", mtPrefix+"/infra/ip-discovery-profiles/"+profilesPrefix+"update"),
6866
resource.TestCheckResourceAttr(testResourceName, "discovery_profile.0.mac_discovery_profile_path", mtPrefix+"/infra/mac-discovery-profiles/"+profilesPrefix+"update"),
6967
resource.TestCheckResourceAttr(testResourceName, "security_profile.0.spoofguard_profile_path", mtPrefix+"/infra/spoofguard-profiles/"+profilesPrefix+"update"),
@@ -80,14 +78,20 @@ func testAccNsxtPolicySegmentPortBindingExists(resourceName string, withContext
8078
if !ok {
8179
return fmt.Errorf("Policy Segment Port Binding resource %s not found in resources", resourceName)
8280
}
83-
resourceID := rs.Primary.Attributes["segment_port_id"]
81+
segmentPortPath := rs.Primary.Attributes["segment_port_path"]
82+
segmentPath, err := getPolicySegmentPathFromPortPath(segmentPortPath)
83+
resourceID := getPolicyIDFromPath(segmentPortPath)
8484
if resourceID == "" {
8585
return fmt.Errorf("Policy Segment Port Binding resource ID not set in resources")
8686
}
8787

8888
connector := getPolicyConnector(testAccProvider.Meta())
89-
segmentPath := rs.Primary.Attributes["segment_path"]
90-
_, err := getSegmentPort(segmentPath, resourceID, testAccGetSessionContext(), connector)
89+
90+
if err != nil {
91+
return fmt.Errorf("Error while parsing policy Segment Port Path %s. Error: %v", segmentPortPath, err)
92+
}
93+
94+
_, err = getSegmentPort(segmentPath, resourceID, testAccGetSessionContext(), connector)
9195
if err != nil {
9296
return fmt.Errorf("Error while retrieving policy Segment Port Binding ID %s. Error: %v", resourceID, err)
9397
}
@@ -117,8 +121,7 @@ resource "nsxt_policy_segment_port" "test" {
117121
118122
resource "nsxt_policy_segment_port_binding" "test" {
119123
%s
120-
segment_port_id = nsxt_policy_segment_port.test.id
121-
segment_path = nsxt_policy_segment.test.path
124+
segment_port_path = nsxt_policy_segment_port.test.path
122125
discovery_profile {
123126
ip_discovery_profile_path = nsxt_policy_ip_discovery_profile.%s.path
124127
mac_discovery_profile_path = nsxt_policy_mac_discovery_profile.%s.path

nsxt/segment_port_common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ func getT1IdFromSegPath(segPortPath string) string {
376376
return ""
377377
}
378378

379+
func getPolicySegmentPathFromPortPath(segmentPortPath string) (string, error) {
380+
return getParameterFromPolicyPath("", "/ports/", segmentPortPath)
381+
}
382+
379383
type segmentConfig interface {
380384
nsxtPolicySegmentPortDiscoveryProfileRead(d *schema.ResourceData, m interface{}) error
381385
nsxtPolicySegmentPortQosProfileRead(d *schema.ResourceData, m interface{}) error

0 commit comments

Comments
 (0)