Skip to content

Commit aa52341

Browse files
committed
Adding a single data source for Tier 0 & Tier 1 gateways. Added the acceptance tests and documentation forthe data source
Signed-off-by: Arun Tony <[email protected]>
1 parent d57278d commit aa52341

7 files changed

+264
-321
lines changed

docs/data-sources/policy_tier0_gateway_interface.md

+27-10
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,49 @@ page_title: "NSXT: policy_tier0_gateway_interface"
44
description: A policy Tier-0 gateway interface data source.
55
---
66

7-
# nsxt_policy_tier0_gateway_interface
7+
# nsxt_policy_gateway_interface
88

9-
This data source provides information about policy Tier-0 gateway interface configured on NSX.
9+
This data source provides information about policy Tier-0 & Tier-1 gateway interface configured on NSX.
1010

1111
This data source is applicable to NSX Policy Manager and NSX Global Manager.
1212

13-
## Example Usage
13+
## Tier0 gateway interface example
1414

1515
```hcl
16-
data "nsxt_policy_tier0_gateway_interface" "tier0_gw_interface" {
17-
display_name = "tier0-gw-interface"
18-
t0_gateway_path = "tier0-gw"
16+
data "nsxt_policy_tier0_gateway" "t0_gw" {
17+
display_name = "t0Gateway"
18+
}
19+
20+
data "nsxt_policy_gateway_interface" "tier0_gw_interface" {
21+
display_name = "gw-interface1"
22+
gateway_path = data.nsxt_policy_tier0_gateway.t0_gw.path
23+
}
24+
```
25+
26+
## Tier1 gateway interface example
27+
28+
```hcl
29+
data "nsxt_policy_tier1_gateway" "t1_gw" {
30+
display_name = "t1Gateway"
31+
}
32+
33+
data "nsxt_policy_gateway_interface" "tier1_gw_interface" {
34+
display_name = "gw-interface2"
35+
gateway_path = data.nsxt_policy_tier1_gateway.t1_gw.path
1936
}
2037
```
2138

2239
## Argument Reference
2340

24-
* `id` - (Optional) The ID of Tier-0 gateway to retrieve.
25-
* `display_name` - (Optional) The Display Name prefix of the Tier-0 gateway interface to retrieve.
26-
* `t0_gateway_path` - (Optional) The Tier-0 gateway to retrieve to which the interface should be linked to.
41+
* `display_name` - (Required) The Display Name prefix of the gateway interface to retrieve.
42+
* `gateway_path` - (Required) The path of the gateway to retrieve which the interface should be linked to.
2743

2844
## Attributes Reference
2945

3046
In addition to arguments listed above, the following attributes are exported:
3147

48+
* `id` - ID of the interface.
3249
* `description` - The description of the resource.
33-
* `edge_cluster_path` - The path of the Edge cluster where this Tier-0 gateway is placed. This attribute is not set for NSX Global Manager, where gateway can span across multiple sites.
50+
* `edge_cluster_path` - The path of the Edge cluster where this gateway is placed. This attribute is not set for NSX Global Manager, where gateway can span across multiple sites. This attribute is set only for Tier0 gateways.
3451
* `path` - The NSX path of the policy resource.
3552
* `segment_path` - Policy path for segment which is connected to this Tier0 Gateway

nsxt/data_source_nsxt_policy_tier0_gateway_interface.go renamed to nsxt/data_source_nsxt_policy_gateway_interface.go

+59-21
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,32 @@ package nsxt
66

77
import (
88
"fmt"
9+
"strings"
910

1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
1213
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1314
)
1415

15-
func dataSourceNsxtPolicyTier0GatewayInterface() *schema.Resource {
16+
func dataSourceNsxtPolicyGatewayInterface() *schema.Resource {
1617
return &schema.Resource{
17-
Read: dataSourceNsxtPolicyTier0GatewayInterfaceRead,
18+
Read: dataSourceNsxtPolicyGatewayInterfaceRead,
1819

1920
Schema: map[string]*schema.Schema{
2021
"id": getDataSourceIDSchema(),
2122
"display_name": getDataSourceDisplayNameSchema(),
2223
"description": getDataSourceDescriptionSchema(),
23-
"t0_gateway_path": {
24+
"gateway_path": {
2425
Type: schema.TypeString,
25-
Description: "The name of the Tier0 gateway where the interface is linked",
26+
Description: "The name of the gateway to which interface is linked",
2627
Required: true,
2728
},
2829
"path": getPathSchema(),
2930
"edge_cluster_path": {
3031
Type: schema.TypeString,
31-
Description: "The path of the edge cluster connected to the Tier0 gateway linked to this interface",
32+
Description: "The path of the edge cluster connected to the gateway linked to this interface. This is exported only for Tier0 gateways",
3233
Optional: true,
33-
Computed: true,
34+
Computed: false,
3435
},
3536
"segment_path": {
3637
Type: schema.TypeString,
@@ -42,48 +43,85 @@ func dataSourceNsxtPolicyTier0GatewayInterface() *schema.Resource {
4243
}
4344
}
4445

45-
func dataSourceNsxtPolicyTier0GatewayInterfaceRead(d *schema.ResourceData, m interface{}) error {
46+
func dataSourceNsxtPolicyGatewayInterfaceRead(d *schema.ResourceData, m interface{}) error {
4647
connector := getPolicyConnector(m)
4748
converter := bindings.NewTypeConverter()
48-
t0Gw := d.Get("t0_gateway_path").(string)
49+
var dataValue interface{}
50+
var path *string
51+
var edgePath *string
52+
var description *string
53+
var segmentPath *string
54+
var errors []error
55+
t0Gw := d.Get("gateway_path").(string)
4956
query := make(map[string]string)
5057
query["parent_path"] = t0Gw + "/locale-services/*"
51-
obj, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), "Tier0Interface", query)
58+
isT0, err := isT0(t0Gw)
59+
if err != nil {
60+
return err
61+
}
62+
searchStr := "Tier1Interface"
63+
if isT0 {
64+
searchStr = "Tier0Interface"
65+
}
66+
obj, err := policyDataSourceResourceRead(d, connector, getSessionContext(d, m), searchStr, query)
5267
if err != nil {
5368
return err
5469
}
55-
dataValue, errors := converter.ConvertToGolang(obj, model.Tier0InterfaceBindingType())
70+
71+
if isT0 {
72+
dataValue, errors = converter.ConvertToGolang(obj, model.Tier0InterfaceBindingType())
73+
currGwInterface := dataValue.(model.Tier0Interface)
74+
path = currGwInterface.Path
75+
edgePath = currGwInterface.EdgePath
76+
description = currGwInterface.Description
77+
segmentPath = currGwInterface.SegmentPath
78+
} else {
79+
dataValue, errors = converter.ConvertToGolang(obj, model.Tier1InterfaceBindingType())
80+
currGwInterface := dataValue.(model.Tier1Interface)
81+
path = currGwInterface.Path
82+
description = currGwInterface.Description
83+
segmentPath = currGwInterface.SegmentPath
84+
}
5685
if len(errors) > 0 {
5786
return errors[0]
5887
}
5988

60-
currGwInterface := dataValue.(model.Tier0Interface)
61-
if currGwInterface.Path != nil {
62-
err := d.Set("path", *currGwInterface.Path)
89+
if path != nil {
90+
err := d.Set("path", *path)
6391
if err != nil {
6492
return fmt.Errorf("Error while setting interface path : %v", err)
6593
}
6694
}
67-
if isPolicyGlobalManager(m) {
68-
d.Set("edge_cluster_path", "")
69-
} else if currGwInterface.EdgePath != nil {
70-
err = d.Set("edge_cluster_path", *currGwInterface.EdgePath)
95+
if isT0 && edgePath != nil {
96+
err = d.Set("edge_cluster_path", *edgePath)
7197
if err != nil {
7298
return fmt.Errorf("Error while setting the interface edge cluster path : %v", err)
7399
}
74100
}
75-
if currGwInterface.Description != nil {
76-
err = d.Set("description", *currGwInterface.Description)
101+
102+
if description != nil {
103+
err = d.Set("description", *description)
77104
if err != nil {
78105
return fmt.Errorf("Error while setting the interface description : %v", err)
79106
}
80107
}
81-
if currGwInterface.SegmentPath != nil {
82-
err = d.Set("segment_path", *currGwInterface.SegmentPath)
108+
if segmentPath != nil {
109+
err = d.Set("segment_path", *segmentPath)
83110
if err != nil {
84111
return fmt.Errorf("Error while setting the segment connected to the interface : %v", err)
85112
}
86113
}
87114
d.SetId(newUUID())
88115
return nil
89116
}
117+
118+
func isT0(t0Gw string) (bool, error) {
119+
segs := strings.Split(t0Gw, "/")
120+
if len(segs) < 3 {
121+
return false, fmt.Errorf("Not a valid gateway path")
122+
}
123+
if segs[len(segs)-2] == "tier-0s" {
124+
return true, nil
125+
}
126+
return false, nil
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package nsxt
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccDataSourceNsxtPolicyGatewayInterface_basic(t *testing.T) {
11+
t0InterfaceName := getAccTestDataSourceName()
12+
t1InterfaceName := getAccTestDataSourceName()
13+
t0GatewayName := "t0testgw"
14+
t1GatewayName := "t1testgw"
15+
t0TestResourceName := "data.nsxt_policy_gateway_interface.test1"
16+
t1TestResourceName := "data.nsxt_policy_gateway_interface.test2"
17+
transportZoneName := getOverlayTransportZoneName()
18+
interfaceDescription := "Acceptance Test"
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
Providers: testAccProviders,
22+
// CheckDestroy: func(state *terraform.State) error {
23+
// return testAccNsxtPolicyTier0InterfaceCheckDestroy(state, t0InterfaceName)
24+
// },
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccNsxtPolicyTier0InterfaceDataSourceTemplate(t0InterfaceName, t1InterfaceName, t0GatewayName, t1GatewayName, transportZoneName, interfaceDescription),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr(t0TestResourceName, "display_name", t0InterfaceName),
30+
resource.TestCheckResourceAttr(t0TestResourceName, "description", interfaceDescription),
31+
resource.TestCheckResourceAttrSet(t0TestResourceName, "path"),
32+
resource.TestCheckResourceAttr(t1TestResourceName, "display_name", t1InterfaceName),
33+
resource.TestCheckResourceAttr(t1TestResourceName, "description", interfaceDescription),
34+
resource.TestCheckResourceAttrSet(t1TestResourceName, "path"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccNsxtPolicyTier0InterfaceDataSourceTemplate(t0InterfaceName string, t1InterfaceName string, t0GatewayName string, t1GatewayName string, transportZoneName string, interfaceDescription string) string {
42+
return CreateT0Gateway(t0GatewayName) + CreateSegment(transportZoneName) + CreateT0GatewayInterface(t0InterfaceName, interfaceDescription) + CreateT1Gateway(t1GatewayName) + CreateT1GatewayInterface(t1InterfaceName, interfaceDescription) + fmt.Sprintf(`
43+
44+
data "nsxt_policy_tier0_gateway" "test" {
45+
display_name = "%s"
46+
depends_on = [nsxt_policy_tier0_gateway_interface.test]
47+
}
48+
49+
data "nsxt_policy_gateway_interface" "test1" {
50+
display_name = "%s"
51+
gateway_path = data.nsxt_policy_tier0_gateway.test.path
52+
depends_on = [nsxt_policy_tier0_gateway_interface.test]
53+
}
54+
55+
data "nsxt_policy_tier1_gateway" "test" {
56+
display_name = "%s"
57+
depends_on = [nsxt_policy_tier1_gateway_interface.test]
58+
}
59+
60+
data "nsxt_policy_gateway_interface" "test2" {
61+
display_name = "%s"
62+
gateway_path = data.nsxt_policy_tier1_gateway.test.path
63+
depends_on = [nsxt_policy_tier1_gateway_interface.test]
64+
}
65+
`, t0GatewayName, t0InterfaceName, t1GatewayName, t1InterfaceName)
66+
}
67+
68+
func CreateT0Gateway(gatewayName string) string {
69+
return fmt.Sprintf(`
70+
resource "nsxt_policy_tier0_gateway" "test" {
71+
display_name = "%s"
72+
ha_mode = "ACTIVE_STANDBY"
73+
edge_cluster_path = data.nsxt_policy_edge_cluster.EC.path
74+
}
75+
76+
data "nsxt_policy_edge_cluster" "EC" {
77+
display_name = "EDGECLUSTER1"
78+
}
79+
`, gatewayName)
80+
}
81+
82+
func CreateSegment(transportZoneName string) string {
83+
return fmt.Sprintf(`
84+
data "nsxt_policy_transport_zone" "overlay_transport_zone" {
85+
display_name = "%s"
86+
}
87+
88+
resource "nsxt_policy_segment" "segment1" {
89+
display_name = "segment-acc-test"
90+
description = "Terraform provisioned Segment"
91+
transport_zone_path = data.nsxt_policy_transport_zone.overlay_transport_zone.path
92+
93+
}
94+
`, transportZoneName)
95+
96+
}
97+
98+
func CreateT0GatewayInterface(interfaceName string, interfaceDescription string) string {
99+
return fmt.Sprintf(`
100+
101+
resource "nsxt_policy_tier0_gateway_interface" "test" {
102+
display_name = "%s"
103+
description = "%s"
104+
type = "SERVICE"
105+
mtu = 1500
106+
gateway_path = nsxt_policy_tier0_gateway.test.path
107+
segment_path = nsxt_policy_segment.segment1.path
108+
subnets = ["1.1.12.2/24"]
109+
110+
tag {
111+
scope = "scope1"
112+
tag = "tag1"
113+
}
114+
depends_on = [nsxt_policy_tier0_gateway.test]
115+
}
116+
`, interfaceName, interfaceDescription)
117+
}
118+
119+
func CreateRealizationT0() string {
120+
return `
121+
data "nsxt_policy_realization_info" "realization_info" {
122+
path = nsxt_policy_tier0_gateway_interface.test.path
123+
depends_on = [nsxt_policy_tier0_gateway_interface.test]
124+
}
125+
126+
data "nsxt_policy_gateway_interface_realization" "gw_realization" {
127+
gateway_path = nsxt_policy_tier0_gateway_interface.test.path
128+
depends_on = [nsxt_policy_tier0_gateway_interface.test]
129+
}
130+
`
131+
}
132+
133+
func CreateT1Gateway(gatewayName string) string {
134+
return fmt.Sprintf(`
135+
resource "nsxt_policy_tier1_gateway" "test" {
136+
display_name = "%s"
137+
ha_mode = "ACTIVE_STANDBY"
138+
edge_cluster_path = data.nsxt_policy_edge_cluster.EC.path
139+
}
140+
141+
142+
`, gatewayName)
143+
}
144+
145+
func CreateT1GatewayInterface(interfaceName string, interfaceDescription string) string {
146+
return fmt.Sprintf(`
147+
148+
resource "nsxt_policy_tier1_gateway_interface" "test" {
149+
display_name = "%s"
150+
description = "%s"
151+
mtu = 1500
152+
gateway_path = nsxt_policy_tier1_gateway.test.path
153+
segment_path = nsxt_policy_segment.segment1.path
154+
subnets = ["1.1.12.2/24"]
155+
156+
tag {
157+
scope = "scope1"
158+
tag = "tag1"
159+
}
160+
depends_on = [nsxt_policy_tier1_gateway.test]
161+
}
162+
`, interfaceName, interfaceDescription)
163+
}
164+
165+
func CreateRealizationT1() string {
166+
return `
167+
data "nsxt_policy_realization_info" "realization_info" {
168+
path = nsxt_policy_tier1_gateway_interface.test.path
169+
depends_on = [nsxt_policy_tier1_gateway_interface.test]
170+
}
171+
172+
data "nsxt_policy_gateway_interface_realization" "gw_realization" {
173+
gateway_path = nsxt_policy_tier1_gateway_interface.test.path
174+
depends_on = [nsxt_policy_tier1_gateway_interface.test]
175+
}
176+
`
177+
}

0 commit comments

Comments
 (0)