Skip to content

Commit 22bc639

Browse files
committed
Implement policy edge transport node RTEP
Signed-off-by: Kobi Samoray <[email protected]>
1 parent cc7dbf2 commit 22bc639

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

Diff for: nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ func Provider() *schema.Provider {
542542
"nsxt_policy_edge_transport_node": resourceNsxtPolicyEdgeTransportNode(),
543543
"nsxt_policy_edge_high_availability_profile": resourceNsxtPolicyEdgeHighAvailabilityProfile(),
544544
"nsxt_policy_edge_cluster": resourceNsxtPolicyEdgeCluster(),
545+
"nsxt_policy_edge_transport_node_rtep": resourceNsxtPolicyEdgeTransportNodeRTEP(),
545546
},
546547

547548
ConfigureFunc: providerConfigure,
+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
"github.com/vmware/vsphere-automation-sdk-go/lib/vapi/std/errors"
14+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
15+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
16+
)
17+
18+
var rtepAssignments = []string{
19+
"static_ipv4_list",
20+
"static_ipv4_pool",
21+
}
22+
23+
func resourceNsxtPolicyEdgeTransportNodeRTEP() *schema.Resource {
24+
return &schema.Resource{
25+
Create: resourceNsxtPolicyEdgeTransportNodeRTEPCreate,
26+
Read: resourceNsxtPolicyEdgeTransportNodeRTEPRead,
27+
Update: resourceNsxtPolicyEdgeTransportNodeRTEPUpdate,
28+
Delete: resourceNsxtPolicyEdgeTransportNodeRTEPDelete,
29+
Importer: &schema.ResourceImporter{
30+
State: resourceNsxtPolicyEdgeTransportNodeRTEPImporter,
31+
},
32+
Schema: map[string]*schema.Schema{
33+
"edge_transport_node_path": {
34+
Type: schema.TypeString,
35+
Description: "Policy path of Edge transport node to associate with remote tunnel endpoint.",
36+
Required: true,
37+
ForceNew: true,
38+
},
39+
"host_switch_name": {
40+
Type: schema.TypeString,
41+
Description: "The host switch name to be used for the remote tunnel endpoint",
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
"ip_assignment": getPolicyIPAssignmentSchema(false, 1, 1, rtepAssignments),
46+
"named_teaming_policy": {
47+
Type: schema.TypeString,
48+
Description: "The named teaming policy to be used by the remote tunnel endpoint",
49+
Optional: true,
50+
},
51+
"vlan": {
52+
Type: schema.TypeInt,
53+
Description: "VLAN id for remote tunnel endpoint",
54+
Required: true,
55+
ValidateFunc: validation.IntBetween(0, 4094),
56+
},
57+
},
58+
}
59+
}
60+
61+
func getEdgeTransportNodeKeysFromPath(path string) (string, string, string) {
62+
siteID := getResourceIDFromResourcePath(path, "sites")
63+
epID := getResourceIDFromResourcePath(path, "enforcement-points")
64+
id := getResourceIDFromResourcePath(path, "edge-transport-nodes")
65+
66+
return siteID, epID, id
67+
}
68+
69+
func setNsxtPolicyEdgeTransportNodeRTEP(d *schema.ResourceData, m interface{}, op string) error {
70+
connector := getPolicyConnector(m)
71+
72+
tnPath := d.Get("edge_transport_node_path").(string)
73+
client := enforcement_points.NewEdgeTransportNodesClient(connector)
74+
75+
siteID, epID, edgeID := getEdgeTransportNodeKeysFromPath(tnPath)
76+
obj, err := client.Get(siteID, epID, edgeID)
77+
if err != nil {
78+
return err
79+
}
80+
81+
hswName := d.Get("host_switch_name").(string)
82+
found := false
83+
84+
for i, hsw := range obj.SwitchSpec.Switches {
85+
if *hsw.SwitchName == hswName {
86+
{
87+
found = true
88+
if len(hsw.RemoteTunnelEndpoint) > 0 && op == "create" {
89+
return fmt.Errorf("remote tunnel endpoint for Edge transport node %s already exists", tnPath)
90+
}
91+
92+
var rteps []model.PolicyEdgeTransportNodeRtepConfig
93+
94+
if op != "delete" {
95+
ipAssignments, err := getPolicyIPAssignmentsFromSchema(d.Get("ip_assignment"))
96+
if err != nil {
97+
return err
98+
}
99+
namedTeamingPolicy := d.Get("named_teaming_policy").(string)
100+
vlan := int64(d.Get("vlan").(int))
101+
rteps = []model.PolicyEdgeTransportNodeRtepConfig{
102+
{
103+
IpAssignmentSpecs: ipAssignments,
104+
NamedTeamingPolicy: &namedTeamingPolicy,
105+
Vlan: &vlan,
106+
},
107+
}
108+
}
109+
110+
obj.SwitchSpec.Switches[i].RemoteTunnelEndpoint = rteps
111+
112+
err = client.Patch(siteID, epID, edgeID, obj)
113+
if err != nil {
114+
return err
115+
}
116+
117+
if op == "create" {
118+
d.SetId(fmt.Sprintf("%s:%s", tnPath, hswName))
119+
}
120+
}
121+
}
122+
}
123+
124+
if !found {
125+
return fmt.Errorf("switch %s not found for Edge transport node %s", hswName, tnPath)
126+
}
127+
128+
return resourceNsxtPolicyEdgeTransportNodeRTEPRead(d, m)
129+
}
130+
131+
func resourceNsxtPolicyEdgeTransportNodeRTEPCreate(d *schema.ResourceData, m interface{}) error {
132+
return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "create")
133+
}
134+
135+
func resourceNsxtPolicyEdgeTransportNodeRTEPRead(d *schema.ResourceData, m interface{}) error {
136+
connector := getPolicyConnector(m)
137+
138+
tnPath := d.Get("edge_transport_node_path").(string)
139+
client := enforcement_points.NewEdgeTransportNodesClient(connector)
140+
141+
siteID, epID, edgeID := getEdgeTransportNodeKeysFromPath(tnPath)
142+
obj, err := client.Get(siteID, epID, edgeID)
143+
if err != nil {
144+
return err
145+
}
146+
147+
hswName := d.Get("host_switch_name").(string)
148+
149+
for _, hsw := range obj.SwitchSpec.Switches {
150+
if *hsw.SwitchName == hswName {
151+
{
152+
if len(hsw.RemoteTunnelEndpoint) > 0 {
153+
return errors.NotFound{}
154+
}
155+
156+
ipAssignment, err := setPolicyIPAssignmentsInSchema(obj.ManagementInterface.IpAssignmentSpecs)
157+
if err != nil {
158+
return err
159+
}
160+
161+
// Only one RTEP is supported
162+
d.Set("ip_assignment", ipAssignment)
163+
d.Set("named_teaming_policy", hsw.RemoteTunnelEndpoint[0].NamedTeamingPolicy)
164+
d.Set("vlan", hsw.RemoteTunnelEndpoint[0].Vlan)
165+
166+
return nil
167+
}
168+
}
169+
}
170+
171+
return errors.NotFound{}
172+
}
173+
174+
func resourceNsxtPolicyEdgeTransportNodeRTEPUpdate(d *schema.ResourceData, m interface{}) error {
175+
return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "update")
176+
}
177+
178+
func resourceNsxtPolicyEdgeTransportNodeRTEPDelete(d *schema.ResourceData, m interface{}) error {
179+
return setNsxtPolicyEdgeTransportNodeRTEP(d, m, "delete")
180+
}
181+
182+
func resourceNsxtPolicyEdgeTransportNodeRTEPImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
183+
importID := d.Id()
184+
segs := strings.Split(importID, ":")
185+
if len(segs) != 2 {
186+
return []*schema.ResourceData{d}, fmt.Errorf("import parameter %s should have a policy path to PolicyEdgeTransportNode and a switch_name, separated by ':'", importID)
187+
}
188+
tnPath := segs[0]
189+
190+
err := validateImportPolicyPath(tnPath)
191+
if err != nil {
192+
return []*schema.ResourceData{d}, err
193+
}
194+
195+
hswName := segs[1]
196+
197+
d.Set("edge_transport_node_path", tnPath)
198+
d.Set("host_switch_name", hswName)
199+
200+
d.SetId(fmt.Sprintf("%s:%s", tnPath, hswName))
201+
202+
return []*schema.ResourceData{d}, nil
203+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
subcategory: "Beta"
3+
layout: "nsxt"
4+
page_title: "NSXT: nsxt_policy_edge_transport_node_rtep"
5+
description: A resource to configure an Policy Edge Transport Node RTEP (remote tunnel endpoint).
6+
---
7+
8+
# nsxt_policy_edge_transport_node_rtep
9+
10+
This resource provides a method for the management of a Policy Edge Transport Node RTEP (remote tunnel endpoint).
11+
This resource is supported with NSX 9.0.0 onwards.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "nsxt_policy_node" "test_node" {
17+
display_name = "edgenode1"
18+
}
19+
20+
data "nsxt_policy_ip_pool" "ip_pool1" {
21+
display_name = "ip-pool1"
22+
}
23+
24+
resource "nsxt_policy_edge_transport_node_rtep" "test_rtep" {
25+
edge_transport_node_path = data.nsxt_edge_node.test_node.path
26+
host_switch_name = "someSwitch"
27+
ip_assignment {
28+
static_ipv4_pool = data.nsxt_policy_ip_pool.ip_pool1.path
29+
}
30+
named_teaming_policy = "tp123"
31+
vlan = 500
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `edge_transport_node_path` - (Required) Policy path for Policy Edge Transport Node to associate with remote tunnel endpoint.
40+
* `host_switch_name` - (Required) The host switch name to be used for the remote tunnel endpoint.
41+
* `ip_assignment` - (Required) - Specification for IPs to be used with host switch virtual tunnel endpoints. Should contain exatly one of the below:
42+
* `static_ipv4_list` - (Optional) IP assignment specification value for Static IPv4 List.
43+
* `default_gateway` - (Required) Gateway IP.
44+
* `ip_addresses` - (Required) List of IPV4 addresses for edge transport node host switch virtual tunnel endpoints.
45+
* `subnet_mask` - (Required) Subnet mask.
46+
* `static_ipv4_pool` - (Optional) IP assignment specification for Static IPv4 Pool. Input can be MP ip pool UUID or policy path of IP pool.
47+
* `vlan` - (Required) VLAN id for remote tunnel endpoint.
48+
* `named_teaming_policy` - (Optional) The named teaming policy to be used by the remote tunnel endpoint.
49+
50+
## Importing
51+
52+
An existing Edge Transport Node RTEP can be [imported][docs-import] into this resource, via the following command:
53+
54+
[docs-import]: https://www.terraform.io/cli/import
55+
56+
```
57+
terraform import nsxt_policy_edge_transport_node_rtep.test POLICY_PATH:SWITCH_ID
58+
```
59+
The above command imports Policy Edge Transport Node RTEP named `test` with the NSX Policy Transport Node path `POLICY_PATH` and host_switch_name `SWITCH_ID`.
60+
61+
**NOTE:** The Policy Edge Transport Node path and host_switch_name are separated by a colon.

0 commit comments

Comments
 (0)