Skip to content

Commit cfda8ee

Browse files
authored
Merge pull request #460 from Tinyblargon/lxc-pool
Feat: Lxc pool update
2 parents 94a5f15 + 066f346 commit cfda8ee

File tree

7 files changed

+56
-39
lines changed

7 files changed

+56
-39
lines changed

proxmox/config__guest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func GuestReboot(ctx context.Context, vmr *VmRef, client *Client) (err error) {
355355
return
356356
}
357357

358-
func guestSetPoolNoCheck(ctx context.Context, c *Client, guestID GuestID, newPool PoolName, currentPool *PoolName, version Version) (err error) {
358+
func guestSetPoolNoCheck(ctx context.Context, c *Client, guestID GuestID, newPool PoolName, currentPool *PoolName, version EncodedVersion) (err error) {
359359
if newPool == "" {
360360
if currentPool != nil && *currentPool != "" { // leave pool
361361
if err = (*currentPool).removeGuestsNoCheck(ctx, c, []GuestID{guestID}, version); err != nil {
@@ -364,15 +364,15 @@ func guestSetPoolNoCheck(ctx context.Context, c *Client, guestID GuestID, newPoo
364364
}
365365
} else {
366366
if currentPool == nil || *currentPool == "" { // join pool
367-
if version.Encode() < version_8_0_0 {
367+
if version < version_8_0_0 {
368368
if err = newPool.addGuestsNoCheckV7(ctx, c, []GuestID{guestID}); err != nil {
369369
return
370370
}
371371
} else {
372372
newPool.addGuestsNoCheckV8(ctx, c, []GuestID{guestID})
373373
}
374374
} else if newPool != *currentPool { // change pool
375-
if version.Encode() < version_8_0_0 {
375+
if version < version_8_0_0 {
376376
if err = (*currentPool).removeGuestsNoCheck(ctx, c, []GuestID{guestID}, version); err != nil {
377377
return
378378
}

proxmox/config__lxc__new.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ func (config ConfigLXC) Update(ctx context.Context, allowRestart bool, vmr *VmRe
259259
if err := config.Validate(current); err != nil {
260260
return err
261261
}
262-
return config.update_Unsafe(ctx, allowRestart, vmr, current, rawStatus.GetState(), c)
262+
version, err := c.Version(ctx)
263+
if err != nil {
264+
return err
265+
}
266+
return config.update_Unsafe(ctx, allowRestart, vmr, current, rawStatus.GetState(), version.Encode(), c)
263267
}
264268

265269
func (config ConfigLXC) UpdateNoCheck(ctx context.Context, allowRestart bool, vmr *VmRef, c *Client) error {
@@ -277,7 +281,11 @@ func (config ConfigLXC) UpdateNoCheck(ctx context.Context, allowRestart bool, vm
277281
if err != nil {
278282
return err
279283
}
280-
return config.update_Unsafe(ctx, allowRestart, vmr, raw.get(*vmr), rawStatus.GetState(), c)
284+
version, err := c.Version(ctx)
285+
if err != nil {
286+
return err
287+
}
288+
return config.update_Unsafe(ctx, allowRestart, vmr, raw.get(*vmr), rawStatus.GetState(), version.Encode(), c)
281289
}
282290

283291
func (config ConfigLXC) update_Unsafe(
@@ -286,6 +294,7 @@ func (config ConfigLXC) update_Unsafe(
286294
vmr *VmRef,
287295
current *ConfigLXC,
288296
currentState PowerState,
297+
version EncodedVersion,
289298
c *Client) error {
290299

291300
ca := c.new().apiGet()
@@ -313,11 +322,12 @@ func (config ConfigLXC) update_Unsafe(
313322
requiresOffStateForMountActions = markedMounts.offState
314323
}
315324

325+
var err error
316326
if targetState == PowerStateStopped && currentState != PowerStateStopped { // We want the vm to be stopped, better to do this before we start making other api calls
317327
if !allowRestart {
318328
return errors.New("guest has to be stopped before applying changes")
319329
}
320-
if _, err := c.ShutdownVm(ctx, vmr); err != nil {
330+
if _, err = c.ShutdownVm(ctx, vmr); err != nil {
321331
return err
322332
}
323333
currentState = PowerStateStopped // We assume the guest is stopped now
@@ -328,7 +338,7 @@ func (config ConfigLXC) update_Unsafe(
328338
if !allowRestart {
329339
return errors.New("guest has to be stopped before moving disks")
330340
}
331-
if err := GuestShutdown(ctx, vmr, c, true); err != nil { // We have to stop the guest before moving disks
341+
if err = GuestShutdown(ctx, vmr, c, true); err != nil { // We have to stop the guest before moving disks
332342
return err
333343
}
334344
currentState = PowerStateStopped // We assume the guest is stopped now
@@ -338,18 +348,19 @@ func (config ConfigLXC) update_Unsafe(
338348
if len(resize) > 0 || len(move) > 0 {
339349

340350
for i := range move { // Move mounts
341-
if _, err := move[i].move(ctx, true, vmr, c); err != nil {
351+
if _, err = move[i].move(ctx, true, vmr, c); err != nil {
342352
return err
343353
}
344354
}
345355

346356
for i := range resize { // Resize mounts
347-
if _, err := resize[i].resize(ctx, vmr, c); err != nil {
357+
if _, err = resize[i].resize(ctx, vmr, c); err != nil {
348358
return err
349359
}
350360
}
351361

352-
newCurrent, err := c.new().guestGetLxcRawConfig(ctx, vmr) // We have to refetch part of the current config
362+
var newCurrent RawConfigLXC
363+
newCurrent, err = c.new().guestGetLxcRawConfig(ctx, vmr) // We have to refetch part of the current config
353364
if err != nil {
354365
return err
355366
}
@@ -366,11 +377,12 @@ func (config ConfigLXC) update_Unsafe(
366377
}
367378

368379
if params := config.mapToApiUpdate(*current); len(params) > 0 {
369-
if err := c.Put(ctx, params, url+"/config"); err != nil {
380+
if err = c.Put(ctx, params, url+"/config"); err != nil {
370381
return err
371382
}
372383
if currentState == PowerStateRunning || currentState == PowerStateUnknown { // If the guest is running, we have to check if it has pending changes
373-
pendingChanges, err := vmr.pendingChanges(ctx, ca)
384+
var pendingChanges bool
385+
pendingChanges, err = vmr.pendingChanges(ctx, ca)
374386
if err != nil {
375387
return fmt.Errorf("error checking for pending changes: %w", err)
376388
}
@@ -379,19 +391,22 @@ func (config ConfigLXC) update_Unsafe(
379391
// TODO revert pending changes
380392
return errors.New("guest has to be restarted to apply changes")
381393
}
382-
if err := GuestReboot(ctx, vmr, c); err != nil {
394+
if err = GuestReboot(ctx, vmr, c); err != nil {
383395
return fmt.Errorf("error restarting guest: %w", err)
384396
}
385397
currentState = PowerStateRunning // We assume the guest is running now
386398
}
387399
}
388400
}
389401
if currentState != PowerStateRunning && targetState == PowerStateRunning { // We want the guest to be running, so we start it now
390-
if err := GuestStart(ctx, vmr, c); err != nil {
402+
if err = GuestStart(ctx, vmr, c); err != nil {
391403
return err
392404
}
393405
}
394-
return nil
406+
if config.Pool != nil {
407+
err = guestSetPoolNoCheck(ctx, c, vmr.vmId, *config.Pool, current.Pool, version)
408+
}
409+
return err
395410
}
396411

397412
func (config ConfigLXC) Validate(current *ConfigLXC) (err error) {

proxmox/config__pool.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (config ConfigPool) CreateNoCheck(ctx context.Context, c *Client) error {
9090
return err
9191
}
9292
if config.Guests != nil {
93-
return config.Name.addGuestsNoCheck(ctx, c, *config.Guests, nil, version)
93+
return config.Name.addGuestsNoCheck(ctx, c, *config.Guests, nil, version.Encode())
9494
}
9595
return nil
9696
}
@@ -152,7 +152,7 @@ func (config ConfigPool) UpdateNoCheck(ctx context.Context, c *Client) error {
152152
return err
153153
}
154154
if params := config.mapToApi(util.Pointer(current.Get())); len(params) > 0 {
155-
if err = config.Name.put(ctx, c, params, version); err != nil {
155+
if err = config.Name.put(ctx, c, params, version.Encode()); err != nil {
156156
return err
157157
}
158158
}
@@ -180,7 +180,7 @@ const (
180180

181181
var regex_PoolName = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
182182

183-
func (config PoolName) addGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, currentGuests *[]GuestID, version Version) error {
183+
func (config PoolName) addGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, currentGuests *[]GuestID, version EncodedVersion) error {
184184
var guestsToAdd []GuestID
185185
if currentGuests != nil && len(*currentGuests) > 0 {
186186
guestsToAdd = subtractArray(guestIDs, *currentGuests)
@@ -190,7 +190,7 @@ func (config PoolName) addGuestsNoCheck(ctx context.Context, c *Client, guestIDs
190190
if len(guestsToAdd) == 0 {
191191
return nil
192192
}
193-
if version.Encode() >= version_8_0_0 {
193+
if version >= version_8_0_0 {
194194
return config.addGuestsNoCheckV8(ctx, c, guestsToAdd)
195195
}
196196
rawGuests, err := listGuests_Unsafe(ctx, c.new().apiGet())
@@ -240,7 +240,7 @@ func (pool PoolName) AddGuestsNoCheck(ctx context.Context, c *Client, guestIDs [
240240
if err != nil {
241241
return err
242242
}
243-
return pool.addGuestsNoCheck(ctx, c, guestIDs, config.GetGuests(), version)
243+
return pool.addGuestsNoCheck(ctx, c, guestIDs, config.GetGuests(), version.Encode())
244244
}
245245

246246
func (config PoolName) Delete(ctx context.Context, c *Client) error {
@@ -348,8 +348,8 @@ func (PoolName) mapToString(guestIDs []GuestID) (vms string) {
348348
return
349349
}
350350

351-
func (pool PoolName) put(ctx context.Context, c *Client, params map[string]interface{}, version Version) error {
352-
if version.Encode() < version_8_0_0 {
351+
func (pool PoolName) put(ctx context.Context, c *Client, params map[string]any, version EncodedVersion) error {
352+
if version < version_8_0_0 {
353353
return pool.putV7(ctx, c, params)
354354
}
355355
return pool.putV8(ctx, c, params)
@@ -380,18 +380,18 @@ func (pool PoolName) RemoveGuests(ctx context.Context, c *Client, guestIDs []Gue
380380
} else if !exists {
381381
return errors.New(PoolName_Error_NotExists)
382382
}
383-
return pool.removeGuestsNoCheck(ctx, c, guestIDs, version)
383+
return pool.removeGuestsNoCheck(ctx, c, guestIDs, version.Encode())
384384
}
385385

386386
func (pool PoolName) RemoveGuestsNoChecks(ctx context.Context, c *Client, guestIDs []GuestID) error {
387387
version, err := c.GetVersion(ctx)
388388
if err != nil {
389389
return err
390390
}
391-
return pool.removeGuestsNoCheck(ctx, c, guestIDs, version)
391+
return pool.removeGuestsNoCheck(ctx, c, guestIDs, version.Encode())
392392
}
393393

394-
func (pool PoolName) removeGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, version Version) error {
394+
func (pool PoolName) removeGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, version EncodedVersion) error {
395395
return pool.put(ctx, c, map[string]interface{}{
396396
"vms": PoolName("").mapToString(guestIDs),
397397
"delete": "1"},
@@ -423,10 +423,10 @@ func (pool PoolName) SetGuestsNoChecks(ctx context.Context, c *Client, guestID [
423423
if err != nil {
424424
return err
425425
}
426-
return pool.setGuestsNoCheck(ctx, c, guestID, raw.GetGuests(), version)
426+
return pool.setGuestsNoCheck(ctx, c, guestID, raw.GetGuests(), version.Encode())
427427
}
428428

429-
func (pool PoolName) setGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, currentGuests *[]GuestID, version Version) error {
429+
func (pool PoolName) setGuestsNoCheck(ctx context.Context, c *Client, guestIDs []GuestID, currentGuests *[]GuestID, version EncodedVersion) error {
430430
if currentGuests != nil && len(*currentGuests) > 0 {
431431
if err := pool.removeGuestsNoCheck(ctx, c, subtractArray(*currentGuests, guestIDs), version); err != nil {
432432
return err

proxmox/config__qemu.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (config ConfigQemu) Create(ctx context.Context, client *Client) (*VmRef, er
9494
}
9595

9696
var params map[string]interface{}
97-
_, params, err = config.mapToAPI(ConfigQemu{}, version)
97+
_, params, err = config.mapToAPI(ConfigQemu{}, version.Encode())
9898
if err != nil {
9999
return nil, err
100100
}
@@ -181,7 +181,7 @@ func (config *ConfigQemu) defaults() {
181181
}
182182
}
183183

184-
func (config ConfigQemu) mapToAPI(currentConfig ConfigQemu, version Version) (rebootRequired bool, params map[string]interface{}, err error) {
184+
func (config ConfigQemu) mapToAPI(currentConfig ConfigQemu, version EncodedVersion) (rebootRequired bool, params map[string]interface{}, err error) {
185185
// TODO check if cloudInit settings changed, they require a reboot to take effect.
186186
var itemsToDelete string
187187

@@ -571,8 +571,10 @@ func (config ConfigQemu) Update(ctx context.Context, rebootIfNeeded bool, vmr *V
571571
vmr.node = *config.Node
572572
}
573573

574+
versionEncoded := version.Encode()
575+
574576
var params map[string]interface{}
575-
rebootRequired, params, err = config.mapToAPI(*currentConfig, version)
577+
rebootRequired, params, err = config.mapToAPI(*currentConfig, versionEncoded)
576578
if err != nil {
577579
return
578580
}
@@ -594,7 +596,7 @@ func (config ConfigQemu) Update(ctx context.Context, rebootIfNeeded bool, vmr *V
594596
}
595597

596598
if config.Pool != nil { // update pool membership
597-
guestSetPoolNoCheck(ctx, client, vmr.vmId, *config.Pool, currentConfig.Pool, version)
599+
guestSetPoolNoCheck(ctx, client, vmr.vmId, *config.Pool, currentConfig.Pool, versionEncoded)
598600
}
599601

600602
if stopped { // start vm if it was stopped

proxmox/config__qemu__cloudinit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type CloudInit struct {
2121

2222
const CloudInit_Error_UpgradePackagesPre8 = "upgradePackages is only available in version 8 and above"
2323

24-
func (config CloudInit) mapToAPI(current *CloudInit, params map[string]interface{}, version Version) (delete string) {
24+
func (config CloudInit) mapToAPI(current *CloudInit, params map[string]any, version EncodedVersion) (delete string) {
2525
if current != nil { // Update
2626
if config.Custom != nil {
2727
params[qemuApiKeyCloudInitCustom] = config.Custom.mapToAPI(current.Custom)
@@ -75,7 +75,7 @@ func (config CloudInit) mapToAPI(current *CloudInit, params map[string]interface
7575
}
7676
}
7777
// Shared
78-
if config.UpgradePackages != nil && version.Encode() >= version_8_0_0 {
78+
if config.UpgradePackages != nil && version >= version_8_0_0 {
7979
params[qemuApiKeyCloudInitUpgrade] = Btoi(*config.UpgradePackages)
8080
}
8181
if config.UserPassword != nil && *config.UserPassword != "" {

proxmox/config__qemu__cpu.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ func (CpuType) Error(version Version) error {
444444
return errors.New("cpuType can only be one of the following values: " + strings.Join(cpusConverted, ", "))
445445
}
446446

447-
func (cpu CpuType) mapToApi(version Version) string {
447+
func (cpu CpuType) mapToApi(version EncodedVersion) string {
448448
cpus := CpuType("").cpuBase()
449-
if version.Encode() >= version_8_0_0 {
449+
if version >= version_8_0_0 {
450450
cpu.cpuV8(cpus)
451451
}
452452
if v, ok := cpus[CpuType(strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(string(cpu), "_", ""), "-", "")))]; ok {
@@ -456,7 +456,7 @@ func (cpu CpuType) mapToApi(version Version) string {
456456
}
457457

458458
func (cpu CpuType) Validate(version Version) error {
459-
if cpu == "" || cpu.mapToApi(version) != "" {
459+
if cpu == "" || cpu.mapToApi(version.Encode()) != "" {
460460
return nil
461461
}
462462
return CpuType("").Error(version)
@@ -513,7 +513,7 @@ const (
513513
QemuCPU_Error_CoresRequired string = "cores is required"
514514
)
515515

516-
func (cpu QemuCPU) mapToApi(current *QemuCPU, params map[string]interface{}, version Version) (delete string) {
516+
func (cpu QemuCPU) mapToApi(current *QemuCPU, params map[string]any, version EncodedVersion) (delete string) {
517517
if cpu.Affinity != nil {
518518
if len(*cpu.Affinity) != 0 {
519519
params[qemuApiKeyCpuAffinity] = cpu.mapToApiAffinity(*cpu.Affinity)

proxmox/config__qemu_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,15 +4363,15 @@ func Test_ConfigQemu_mapToAPI(t *testing.T) {
43634363
for _, subTest := range append(test.create, test.createUpdate...) {
43644364
name := test.category + "/Create/" + subTest.name
43654365
t.Run(name, func(*testing.T) {
4366-
reboot, tmpParams, _ := subTest.config.mapToAPI(ConfigQemu{}, subTest.version)
4366+
reboot, tmpParams, _ := subTest.config.mapToAPI(ConfigQemu{}, subTest.version.Encode())
43674367
require.Equal(t, subTest.output, tmpParams, name)
43684368
require.Equal(t, false, reboot, name)
43694369
})
43704370
}
43714371
for _, subTest := range append(test.update, test.createUpdate...) {
43724372
name := test.category + "/Update/" + subTest.name
43734373
t.Run(name, func(*testing.T) {
4374-
reboot, tmpParams, _ := subTest.config.mapToAPI(subTest.currentConfig, subTest.version)
4374+
reboot, tmpParams, _ := subTest.config.mapToAPI(subTest.currentConfig, subTest.version.Encode())
43754375
require.Equal(t, subTest.output, tmpParams, name)
43764376
require.Equal(t, subTest.reboot, reboot, name)
43774377
})

0 commit comments

Comments
 (0)