Skip to content

Commit ff5c3ee

Browse files
committed
Add OVA-edge support to policy edge resource
Support import-in-create use case for OVA-created edge appliances Signed-off-by: Kobi Samoray <[email protected]>
1 parent 979d81a commit ff5c3ee

File tree

2 files changed

+102
-13
lines changed

2 files changed

+102
-13
lines changed

nsxt/resource_nsxt_policy_edge_transport_node.go

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ func resourceNsxtPolicyEdgeTransportNode() *schema.Resource {
337337
ForceNew: true,
338338
Default: "default",
339339
},
340+
"node_id": {
341+
Type: schema.TypeString,
342+
Optional: true,
343+
Computed: true,
344+
Description: "Unique Id of the fabric node",
345+
ConflictsWith: []string{"advanced_configuration", "appliance_config", "credentials", "form_factor", "management_interface", "vm_deployment_config"},
346+
},
340347
"advanced_configuration": getKeyValuePairListSchema(),
341348
"appliance_config": {
342349
Type: schema.TypeList,
@@ -1180,6 +1187,42 @@ func getVMDeploymentConfigFromSchema(iVmDeploymentCfg interface{}) (*data.Struct
11801187
return nil, nil
11811188
}
11821189

1190+
func policyEdgeTransportNodePredeployedPatch(siteID, epID, etnID string, d *schema.ResourceData, m interface{}) error {
1191+
1192+
connector := getPolicyConnector(m)
1193+
etnClient := enforcement_points.NewEdgeTransportNodesClient(connector)
1194+
1195+
obj, err := etnClient.Get(siteID, epID, etnID)
1196+
if err != nil {
1197+
return err
1198+
}
1199+
1200+
description := d.Get("description").(string)
1201+
obj.Description = &description
1202+
1203+
displayName := d.Get("display_name").(string)
1204+
obj.DisplayName = &displayName
1205+
1206+
obj.Tags = getPolicyTagsFromSchema(d)
1207+
1208+
revision := int64(d.Get("revision").(int))
1209+
obj.Revision = &revision
1210+
1211+
failureDomainPath := d.Get("failure_domain_path").(string)
1212+
obj.FailureDomainPath = &failureDomainPath
1213+
1214+
hostname := d.Get("hostname").(string)
1215+
obj.Hostname = &hostname
1216+
1217+
switchSpec, err := getSwitchFromSchema(d.Get("switch"))
1218+
if err != nil {
1219+
return err
1220+
}
1221+
obj.SwitchSpec = switchSpec
1222+
1223+
return etnClient.Patch(siteID, epID, etnID, obj)
1224+
}
1225+
11831226
func policyEdgeTransportNodePatch(siteID, epID, etnID string, d *schema.ResourceData, m interface{}) error {
11841227

11851228
description := d.Get("description").(string)
@@ -1230,9 +1273,15 @@ func policyEdgeTransportNodePatch(siteID, epID, etnID string, d *schema.Resource
12301273

12311274
func resourceNsxtPolicyEdgeTransportNodeCreate(d *schema.ResourceData, m interface{}) error {
12321275
connector := getPolicyConnector(m)
1276+
12331277
id := d.Get("nsx_id").(string)
1278+
nodeID := d.Get("node_id").(string)
12341279
if id == "" {
1235-
id = newUUID()
1280+
if nodeID != "" {
1281+
id = nodeID
1282+
} else {
1283+
id = newUUID()
1284+
}
12361285
}
12371286
sitePath := d.Get("site_path").(string)
12381287
siteID := getResourceIDFromResourcePath(sitePath, "sites")
@@ -1243,19 +1292,31 @@ func resourceNsxtPolicyEdgeTransportNodeCreate(d *schema.ResourceData, m interfa
12431292
if epID == "" {
12441293
epID = getPolicyEnforcementPoint(m)
12451294
}
1246-
exists, err := resourceNsxtPolicyEdgeTransportNodeExists(siteID, epID, id, connector)
1247-
if err != nil {
1248-
return err
1249-
}
1250-
if exists {
1251-
return fmt.Errorf("resource with ID %s already exists", id)
1252-
}
12531295

1254-
// Create the resource using PATCH
1255-
log.Printf("[INFO] Creating PolicyEdgeTransportNode with ID %s under site %s enforcement point %s", id, siteID, epID)
1256-
err = policyEdgeTransportNodePatch(siteID, epID, id, d, m)
1257-
if err != nil {
1258-
return handleCreateError("EdgeTransportNode", id, err)
1296+
if nodeID == "" {
1297+
exists, err := resourceNsxtPolicyEdgeTransportNodeExists(siteID, epID, id, connector)
1298+
if err != nil {
1299+
return err
1300+
}
1301+
if exists {
1302+
return fmt.Errorf("resource with ID %s already exists", id)
1303+
}
1304+
1305+
// Create the resource using PATCH
1306+
log.Printf("[INFO] Creating PolicyEdgeTransportNode with ID %s under site %s enforcement point %s", id, siteID, epID)
1307+
err = policyEdgeTransportNodePatch(siteID, epID, id, d, m)
1308+
if err != nil {
1309+
return handleCreateError("EdgeTransportNode", id, err)
1310+
}
1311+
} else {
1312+
log.Printf("Adding a pre-existing Edge appliance")
1313+
if id != nodeID {
1314+
return fmt.Errorf("cannot have both nsx_id and node_id attribute set, with different values")
1315+
}
1316+
err := policyEdgeTransportNodePredeployedPatch(siteID, epID, nodeID, d, m)
1317+
if err != nil {
1318+
return err
1319+
}
12591320
}
12601321

12611322
d.SetId(id)

website/docs/r/policy_edge_transport_node.html.markdown

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ data "nsxt_policy_realization_info" "info" {
7575
**NOTE:** `data.vsphere_network`, `data.vsphere_compute_cluster`, `data.vsphere_datastore`, `data.vsphere_host` are
7676
obtained using [hashicorp/vsphere](https://registry.terraform.io/providers/hashicorp/vsphere/) provider.
7777

78+
## Example Usage, with Edge Transport Node created externally and converted into a transport node using Terraform
79+
```hcl
80+
data "nsxt_policy_edge_node" "node1" {
81+
display_name = "tf_edge_node"
82+
}
83+
84+
resource "nsxt_policy_edge_transport_node" "test" {
85+
node_id = data.nsxt_policy_edge_node.node1.id
86+
87+
hostname = "test-edge-12"
88+
switch {
89+
overlay_transport_zone_path = data.nsxt_policy_transport_zone.overlay_tz.path
90+
pnic {
91+
device_name = "fp-eth0"
92+
uplink_name = "uplink1"
93+
}
94+
uplink_host_switch_profile_path = data.nsxt_policy_uplink_host_switch_profile.uplink_host_switch_profile.path
95+
tunnel_endpoint {
96+
ip_assignment {
97+
static_ipv4_pool = data.nsxt_policy_ip_pool.ip_pool1.path
98+
}
99+
}
100+
vlan_transport_zone_paths = [data.nsxt_policy_transport_zone.vlan_tz.path]
101+
}
102+
}
103+
```
104+
78105
## Argument Reference
79106

80107
The following arguments are supported:
@@ -84,6 +111,7 @@ The following arguments are supported:
84111
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
85112
* `site_path` - (Optional) The path of the site which the Edge Transport Node belongs to. `path` field of the existing `nsxt_policy_site` can be used here. Defaults to default site path.
86113
* `enforcement_point` - (Optional) The ID of enforcement point under given `site_path` to manage the Edge Transport Node. Defaults to default enforcement point.
114+
* `node_id` - (Optional) The id of a pre-deployed Edge appliance to be converted into a Policy Edge transport node.
87115
* `advanced_configuration` - (Optional) Array of additional specific properties for advanced or cloud-specific deployments in key-value format.
88116
* `key` - (Required) Key.
89117
* `value` - (Required) Value.

0 commit comments

Comments
 (0)