Skip to content

Commit

Permalink
Host upgrade group cleanup when not in schema
Browse files Browse the repository at this point in the history
When a host upgrade group is deleted, clean it from NSX.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jun 3, 2024
1 parent d235730 commit 24658ee
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions nsxt/resource_nsxt_upgrade_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ func updateUpgradeUnitGroups(upgradeClientSet *upgradeClientSet, d *schema.Resou
}
}

groupList, err := upgradeClientSet.GroupClient.List(nil, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return err
}
groupIDMap := make(map[string]bool)
groupNameMap := make(map[string]bool)

preUpgradeGroupID := ""
for _, groupI := range d.Get(componentToGroupKey[component]).([]interface{}) {
group := groupI.(map[string]interface{})
Expand All @@ -531,11 +538,8 @@ func updateUpgradeUnitGroups(upgradeClientSet *upgradeClientSet, d *schema.Resou
if groupName == "" {
return fmt.Errorf("couldn't find upgrade unit group without id or display_name")
}
groupNameMap[groupName] = true
// This is a custom group, try to find it by name
groupList, err := upgradeClientSet.GroupClient.List(nil, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return err
}
for i, group := range groupList.Results {
if *group.DisplayName == groupName {
if groupGet == nil {
Expand Down Expand Up @@ -568,7 +572,7 @@ func updateUpgradeUnitGroups(upgradeClientSet *upgradeClientSet, d *schema.Resou
if group["hosts"] != nil {
schemaUnits = interface2StringList(group["hosts"].([]interface{}))
}
getHostDefaultUpgradeGroup, err := getHostDefaultUpgradeGroupGetter(m)
getHostDefaultUpgradeGroup, err := getHostDefaultUpgradeGroupGetter(m, groupList)
if err != nil {
return fmt.Errorf("failed to retrieve host upgrade groups, error is %v", err)
}
Expand All @@ -595,6 +599,7 @@ func updateUpgradeUnitGroups(upgradeClientSet *upgradeClientSet, d *schema.Resou
groupGet.UpgradeUnits = groupMembers
}
} else {
groupIDMap[groupID] = true
group, err := upgradeClientSet.GroupClient.Get(groupID, nil)
if err != nil {
return err
Expand Down Expand Up @@ -661,6 +666,24 @@ func updateUpgradeUnitGroups(upgradeClientSet *upgradeClientSet, d *schema.Resou
}
preUpgradeGroupID = groupID
}

// After group update is complete, check for empty custom host groups which aren't defined in the schema, and have no members - these can be deleted.
componentType := "HOST"
groupList, err = upgradeClientSet.GroupClient.List(&componentType, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return err
}
for _, group := range groupList.Results {
// Non-empty groups cannot be deleted anyway so skip
if *group.UpgradeUnitCount == 0 {
if !groupIDMap[*group.Id] && !groupNameMap[*group.DisplayName] {
err = upgradeClientSet.GroupClient.Delete(*group.Id)
if err != nil {
return err
}
}
}
}
return nil
}

Expand Down Expand Up @@ -820,15 +843,9 @@ func resourceNsxtUpgradeRunDelete(d *schema.ResourceData, m interface{}) error {
return nil
}

func getHostDefaultUpgradeGroupGetter(m interface{}) (func(string) (string, error), error) {
func getHostDefaultUpgradeGroupGetter(m interface{}, groupList model.UpgradeUnitGroupListResult) (func(string) (string, error), error) {
connector := getPolicyConnector(m)
hostClient := nsx.NewTransportNodesClient(connector)
groupClient := upgrade.NewUpgradeUnitGroupsClient(connector)
componentType := "HOST"
hostGroups, err := groupClient.List(&componentType, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return nil, err
}

return func(hostID string) (string, error) {
host, err := hostClient.Get(hostID)
Expand All @@ -847,9 +864,9 @@ func getHostDefaultUpgradeGroupGetter(m interface{}) (func(string) (string, erro
}
// This host is not a part of a compute cluster:
// it should be assigned to the 'Group 1 for ESXI' group (this value is hardcoded in NSX)
if hostGroups.Results != nil {
for _, group := range hostGroups.Results {
if group.DisplayName != nil && *group.DisplayName == hostUpgradeUnitDefaultGroup {
if groupList.Results != nil {
for _, group := range groupList.Results {
if group.DisplayName != nil && *group.DisplayName == hostUpgradeUnitDefaultGroup && *group.Type_ == "HOST" {
return *group.Id, nil
}
}
Expand Down

0 comments on commit 24658ee

Please sign in to comment.