diff --git a/api/hyperv-winrm/vm_switch.go b/api/hyperv-winrm/vm_switch.go index 69fd979..40a6dc6 100644 --- a/api/hyperv-winrm/vm_switch.go +++ b/api/hyperv-winrm/vm_switch.go @@ -177,22 +177,29 @@ func (c *ClientConfig) GetVMSwitch(ctx context.Context, name string) (result api } type updateVMSwitchArgs struct { + OldName string VmSwitchJson string } var updateVMSwitchTemplate = template.Must(template.New("UpdateVMSwitch").Parse(` $ErrorActionPreference = 'Stop' Import-Module Hyper-V +$oldName = '{{.OldName}}' $vmSwitch = '{{.VmSwitchJson}}' | ConvertFrom-Json $minimumBandwidthMode = [Microsoft.HyperV.PowerShell.VMSwitchBandwidthMode]$vmSwitch.BandwidthReservationMode $switchType = [Microsoft.HyperV.PowerShell.VMSwitchType]$vmSwitch.SwitchType $NetAdapterNames = @($vmSwitch.NetAdapterNames) + #when EnablePacketDirect=true it seems to throw an exception if EnableIov=true or EnableEmbeddedTeaming=true -$switchObject = Get-VMSwitch -Name "$($vmSwitch.Name)*" | ?{$_.Name -eq $vmSwitch.Name} +$switchObject = Get-VMSwitch -Name "$($oldName)*" | ?{$_.Name -eq $oldName} if (!$switchObject){ - throw "Switch does not exist - $($vmSwitch.Name)" + throw "Switch does not exist - $($oldName)" +} + +if ($oldName -ne $vmSwitch.Name) { + Rename-VMSwitch -Name $oldName -NewName $vmSwitch.Name } $SetVmSwitchArgs = @{} @@ -233,6 +240,7 @@ Set-VMSwitch @SetVmSwitchArgs func (c *ClientConfig) UpdateVMSwitch( ctx context.Context, + oldName string, name string, notes string, allowManagementOS bool, @@ -270,6 +278,7 @@ func (c *ClientConfig) UpdateVMSwitch( } err = c.WinRmClient.RunFireAndForgetScript(ctx, updateVMSwitchTemplate, updateVMSwitchArgs{ + OldName: oldName, VmSwitchJson: string(vmSwitchJson), }) diff --git a/api/vm_switch.go b/api/vm_switch.go index 6303d86..ada356f 100644 --- a/api/vm_switch.go +++ b/api/vm_switch.go @@ -166,6 +166,7 @@ type HypervVmSwitchClient interface { GetVMSwitch(ctx context.Context, name string) (result VmSwitch, err error) UpdateVMSwitch( ctx context.Context, + oldName string, name string, notes string, allowManagementOS bool, diff --git a/internal/provider/resource_hyperv_network_switch.go b/internal/provider/resource_hyperv_network_switch.go index 3fa9f23..48b6f84 100644 --- a/internal/provider/resource_hyperv_network_switch.go +++ b/internal/provider/resource_hyperv_network_switch.go @@ -373,7 +373,9 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa log.Printf("[INFO][hyperv][update] updating hyperv switch: %#v", d) c := meta.(api.Client) - switchName := d.Id() + id := d.Id() + newName := d.Get("name").(string) + notes := (d.Get("notes")).(string) allowManagementOS := (d.Get("allow_management_os")).(bool) // embeddedTeamingEnabled := (d.Get("enable_embedded_teaming")).(bool) @@ -444,12 +446,14 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa return diag.Errorf("[ERROR][hyperv][update] defaultQueueVmmqQueuePairs must be greater then 0") } - err := c.UpdateVMSwitch(ctx, switchName, notes, allowManagementOS, switchType, netAdapterNames, defaultFlowMinimumBandwidthAbsolute, defaultFlowMinimumBandwidthWeight, defaultQueueVmmqEnabled, defaultQueueVmmqQueuePairs, defaultQueueVrssEnabled) + err := c.UpdateVMSwitch(ctx, id, newName, notes, allowManagementOS, switchType, netAdapterNames, defaultFlowMinimumBandwidthAbsolute, defaultFlowMinimumBandwidthWeight, defaultQueueVmmqEnabled, defaultQueueVmmqQueuePairs, defaultQueueVrssEnabled) if err != nil { return diag.FromErr(err) } + d.SetId(newName) + log.Printf("[INFO][hyperv][update] updated hyperv switch: %#v", d) return resourceHyperVNetworkSwitchRead(ctx, d, meta)