Skip to content

Commit

Permalink
Add the option to remove NSX during deletion
Browse files Browse the repository at this point in the history
While removing a host transport node collection, allow cleaning up the
NSX bits from the hypervisors.
Also set revision attribute on update to allow updating.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Feb 13, 2024
1 parent 9e55291 commit d2f66fa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions nsxt/resource_nsxt_policy_host_transport_node_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ package nsxt
import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/fabric/compute_collections"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

const removeOnDestroyDefault = false

func resourceNsxtPolicyHostTransportNodeCollection() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyHostTransportNodeCollectionCreate,
Expand Down Expand Up @@ -89,6 +94,12 @@ func resourceNsxtPolicyHostTransportNodeCollection() *schema.Resource {
Optional: true,
Description: "Transport Node Profile Path",
},
"remove_nsx_on_destroy": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate whether NSX service should be removed from hypervisors during resource deletion",
Default: removeOnDestroyDefault,
},
},
}
}
Expand Down Expand Up @@ -263,6 +274,30 @@ func resourceNsxtPolicyHostTransportNodeCollectionUpdate(d *schema.ResourceData,
return resourceNsxtPolicyHostTransportNodeCollectionRead(d, m)
}

func getComputeCollectionMemberStateConf(connector client.Connector, id string) *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"notyet"},
Target: []string{"success", "failed"},
Refresh: func() (interface{}, string, error) {
client := compute_collections.NewMemberStatusClient(connector)
statuses, err := client.List(id)
if err != nil {
log.Printf("[DEBUG]: NSX Failed to retrieve compute collection member statuses: %v", err)
return nil, "failed", err
}

// When NSX bits are successfully, no member statuses will remain in the results list
if len(statuses.Results) > 0 {
return nil, "notyet", nil
}
return "success", "success", nil
},
Delay: time.Duration(5) * time.Second,
Timeout: time.Duration(1200) * time.Second,
PollInterval: time.Duration(5) * time.Second,
}
}

func resourceNsxtPolicyHostTransportNodeCollectionDelete(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := enforcement_points.NewTransportNodeCollectionsClient(connector)
Expand All @@ -271,6 +306,22 @@ func resourceNsxtPolicyHostTransportNodeCollectionDelete(d *schema.ResourceData,
return err
}

removeNsxOnDestroy := d.Get("remove_nsx_on_destroy").(bool)
if removeNsxOnDestroy {
log.Printf("[INFO] Removing NSX from hosts associated with HostTransportNodeCollection with ID %s", id)
err = client.Removensx(siteID, epID, id)
if err != nil {
return handleDeleteError("HostTransportNodeCollection", id, err)
}

// Busy-wait until removal is complete
ccID := d.Get("compute_collection_id").(string)
stateConf := getComputeCollectionMemberStateConf(connector, ccID)
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("failed to remove NSX bits from hosts: %v", err)
}
}
log.Printf("[INFO] Deleting HostTransportNodeCollection with ID %s", id)
err = client.Delete(siteID, epID, id)
if err != nil {
Expand All @@ -297,5 +348,7 @@ func resourceNsxtPolicyHostTransportNodeCollectionImporter(d *schema.ResourceDat
return rd, err
}
d.Set("site_path", sitePath)
d.Set("remove_nsx_on_destroy", removeOnDestroyDefault)

return rd, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The following arguments are supported:
* `transport_node_profile_sub_config_name` - (Required) Name of the TransportNodeProfile sub configuration to be used.
* `sub_cluster_id` - (Required) sub-cluster ID.
* `transport_node_profile_path` - (Optional) Transport Node Profile Path.
* `remove_nsx_on_destroy` - (Optional) Upon deletion, uninstall NSX from Transport Node Collection member hosts. Default is false.

## Attributes Reference

Expand All @@ -61,3 +62,4 @@ An existing policy Host Transport Node Collection can be [imported][docs-import]
terraform import nsxt_policy_host_transport_node_collection.test POLICY_PATH
```
The above command imports Policy Host Transport Node Collection named test with NSX policy path POLICY_PATH.
Note: `remove_nsx_on_destroy` will be set to default value upon import. To enforce the intent value, reapply the plan.

0 comments on commit d2f66fa

Please sign in to comment.