Skip to content

Commit

Permalink
Merge branch 'bug/Failed-to-add-servers-to-an-exist-vm-group-in-vsphe…
Browse files Browse the repository at this point in the history
…re-provide' of https://github.com/burnsjared0415/terraform-provider-vsphere into bug/Failed-to-add-servers-to-an-exist-vm-group-in-vsphere-provide
  • Loading branch information
burnsjared0415 committed Sep 16, 2024
2 parents d9e15d2 + 689410b commit 8956103
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
9 changes: 4 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# <!-- markdownlint-disable first-line-h1 no-inline-html -->

## 2.9.2 (not Released)
## 2.9.2 (nNot Released)

BUG FIX:

* `resource/vsphere_compute_cluster_vm_group.go`: Add new code to handle updating existing VM groups. This will need put ran in conjunction with Import.
([#2260]https://github.com/hashicorp/terraform-provider-vsphere/pull/2260)

* `resource/vsphere_compute_cluster_vm_group`: Updates resource to allow for additional virtual
machines to be adding or removed from a VM Group. Must be ran in conjunction with and import.
([#2260]https://github.com/hashicorp/terraform-provider-vsphere/pull/2260)

## 2.9.1 (September 9, 2024)

Expand Down
28 changes: 14 additions & 14 deletions vsphere/resource_vsphere_compute_cluster_vm_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,24 @@ func resourceVSphereComputeClusterVMGroupUpdate(d *schema.ResourceData, meta int
return err
}

// Retrieve the existing VM group information
// Retrieve the existing VM group information.
existingGroup, err := getCurrentVMsInGroup(cluster, name)
if err != nil {
return err
}

// Check if existingGroup is nil
// Check if existingGroup is nil.
if existingGroup == nil {
return fmt.Errorf("VM group %s not found", name)
}

// Expand the new VM group information
// Expand the new VM group information.
newInfo, err := expandClusterVMGroup(d, meta, name)
if err != nil {
return err
}

// Convert existing and new VMs to string slices for diffVMs
// Convert existing and new virtual machines to string slices for diffVmGroup.
existingVMs := make([]string, len(existingGroup.Vm))
for i, vm := range existingGroup.Vm {
existingVMs[i] = vm.Value
Expand All @@ -180,14 +180,10 @@ func resourceVSphereComputeClusterVMGroupUpdate(d *schema.ResourceData, meta int
newVMs[i] = vm.Value
}

// Use diffVMs to find added and removed VMs
addedVMs, removedVMs := diffVMs(existingVMs, newVMs)
// Use diffVmGroup to find added and removed virtual machines from virtual machine group.
addedVMs, removedVMs := diffVmGroup(existingVMs, newVMs)

// Log the added and removed VMs
log.Printf("[DEBUG] Added VMs: %v", addedVMs)
log.Printf("[DEBUG] Removed VMs: %v", removedVMs)

// Convert addedVMs and removedVMs back to ManagedObjectReference slices
// Convert addedVMs and removedVMs back to ManagedObjectReference slices.
addedVMRefs := make([]types.ManagedObjectReference, len(addedVMs))
for i, vm := range addedVMs {
addedVMRefs[i] = types.ManagedObjectReference{
Expand All @@ -204,7 +200,7 @@ func resourceVSphereComputeClusterVMGroupUpdate(d *schema.ResourceData, meta int
}
}

// Merge existing VMs with added VMs and remove duplicates
// Merge existing virtual machines with added virtual machines and remove duplicates.
mergedVMs := append(existingGroup.Vm, addedVMRefs...)
vmMap := make(map[types.ManagedObjectReference]bool)
for _, vm := range mergedVMs {
Expand All @@ -218,7 +214,11 @@ func resourceVSphereComputeClusterVMGroupUpdate(d *schema.ResourceData, meta int
uniqueVMs = append(uniqueVMs, vm)
}

// Update the VM group information with the merged list
if len(uniqueVMs) == 0 {
return fmt.Errorf("the VM group cannot be empty. please ensure that there are VMs in the terraform plan")
}

// Update the VM group information with the merged list.
newInfo.Vm = uniqueVMs

spec := &types.ClusterConfigSpecEx{
Expand Down Expand Up @@ -475,7 +475,7 @@ func resourceVSphereComputeClusterVMGroupClient(meta interface{}) (*govmomi.Clie
return client, nil
}

func diffVMs(oldVMs, newVMs []string) ([]string, []string) {
func diffVmGroup(oldVMs, newVMs []string) ([]string, []string) {
oldVMMap := make(map[string]bool)
for _, vm := range oldVMs {
oldVMMap[vm] = true
Expand Down
5 changes: 4 additions & 1 deletion website/docs/r/compute_cluster_vm_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ resource. Make sure your names are unique across both resources.

[tf-vsphere-cluster-host-group-resource]: /docs/providers/vsphere/r/compute_cluster_host_group.html

** To update a existing group, first import group with imported command in Import section, also note if you import group validate on apply that all groups that are needed to be in the group are added to `virtual_machine_ids`. If you leave any off the list the Virtual Machines will be removed from the group.**
~> **NOTE:** To update a existing VM group, you must first import the group with `import` command in
[Importing](#importing) section. When importing a VM group, validate that all virtual machines that
need to be in the group are included in the `virtual_machine_ids`; otherwise, any virtual machines
that are not in `virtual_machine_ids` the included will be removed from the group.

## Attribute Reference

Expand Down

0 comments on commit 8956103

Please sign in to comment.