Skip to content

Commit

Permalink
Merge pull request #239 from taliesins/bug-switch-rename
Browse files Browse the repository at this point in the history
fix: Enable switch rename
  • Loading branch information
taliesins authored Jan 15, 2024
2 parents 82c1c1f + 3d7fd31 commit 7107093
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions api/hyperv-winrm/vm_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @{}
Expand Down Expand Up @@ -233,6 +240,7 @@ Set-VMSwitch @SetVmSwitchArgs

func (c *ClientConfig) UpdateVMSwitch(
ctx context.Context,
oldName string,
name string,
notes string,
allowManagementOS bool,
Expand Down Expand Up @@ -270,6 +278,7 @@ func (c *ClientConfig) UpdateVMSwitch(
}

err = c.WinRmClient.RunFireAndForgetScript(ctx, updateVMSwitchTemplate, updateVMSwitchArgs{
OldName: oldName,
VmSwitchJson: string(vmSwitchJson),
})

Expand Down
1 change: 1 addition & 0 deletions api/vm_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions internal/provider/resource_hyperv_network_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7107093

Please sign in to comment.