diff --git a/nsxt/resource_nsxt_upgrade_run.go b/nsxt/resource_nsxt_upgrade_run.go index dcaf81eeb..4dfbc7352 100644 --- a/nsxt/resource_nsxt_upgrade_run.go +++ b/nsxt/resource_nsxt_upgrade_run.go @@ -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{}) @@ -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 { @@ -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) } @@ -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 @@ -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 } @@ -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) @@ -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 } }