Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enable switch rename #239

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading