Skip to content

Commit 17ede6a

Browse files
committed
added lxc update endpoint impl
1 parent fbad1fd commit 17ede6a

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
#### Added
2020
- `POST /custom-api/v1/lxc/{id}/exec-async` implementation as `PVE.LXC.ExecAsync`.
2121
- `GET /custom-api/v1/cmd/{id}` implementation as `PVE.LXC.GetCMDResult`.
22+
- `GET /nodes/{node}/lxc/{id}/config` implementation as `PVE.LXC.Update`.
2223

2324
## [v0.6.1]
2425
### PVE API client
@@ -43,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4344
- `GET /nodes/{node}/firewall/rules` implementation as `PVE.Node.Firewall.GetRule`.
4445
- `POST /nodes/{node}/firewall/rules` implementation as `PVE.Node.Firewall.NewRule`.
4546
- `DELETE /nodes/{node}/firewall/rules/{pos}` implementation as `PVE.Node.Firewall.DeleteRule` and `PVE.Node.Firewall.DeleteRuleByPos`.
46-
- `GET /nodes/{node}/lxc/{id}/interfaces` implementation as `PVE.LXC.GetInterfaces` and `PVE.LXC.GetInterface` .
47+
- `GET /nodes/{node}/lxc/{id}/interfaces` implementation as `PVE.LXC.GetInterfaces` and `PVE.LXC.GetInterface`.
4748
- `GET /nodes/{node}/lxc/{id}/status/current` implementation as `PVE.LXC.GetStatus`.
4849
- `POST /nodes/{node}/lxc/{id}/status/reboot` implementation as `PVE.LXC.Reboot`.
4950
- `POST /nodes/{node}/lxc/{id}/status/resume` implementation as `PVE.LXC.Resume`.

pkg/pve/pve_lxc.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,132 @@ func (s *PVELxcService) GetInterfaceByName(node string, id int, name string) (re
716716
}
717717
return res, fmt.Errorf("vmid '%d' or interface '%s' not found", id, name)
718718
}
719+
720+
type pveUpdateLxcRequest struct {
721+
Node string `in:"nonzero;path=node"`
722+
VMID int `in:"nonzero;path=vmid"`
723+
Arch string `in:"omitempty;form=arch"`
724+
CMode string `in:"omitempty;form=cmode"`
725+
Console int `in:"omitempty;form=console"` // bool
726+
Cores int `in:"omitempty;form=cores"`
727+
CPULimit int `in:"omitempty;form=cpulimit"`
728+
CPUUnits int `in:"omitempty;form=cpuunits"`
729+
Debug int `in:"omitempty;form=debug"` // bool
730+
Desc string `in:"omitempty;form=description"`
731+
// dev[n] string Device to pass through to the container
732+
Features string `in:"omitempty;form=features"`
733+
Hookscript string `in:"omitempty;form=hookscript"`
734+
Hostname string `in:"omitempty;form=hostname"`
735+
Lock string `in:"omitempty;form=lock"`
736+
Memory int `in:"omitempty;form=memory"`
737+
//mp Use volume as container mount point. Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume.
738+
Nameserver string `in:"omitempty;form=nameserver"`
739+
OnBoot int `in:"omitempty;form=onboot"` // bool
740+
OSType string `in:"omitempty;form=ostype"`
741+
Protection int `in:"omitempty;form=protection"` // bool
742+
RootFS string `in:"omitempty;form=rootfs"`
743+
Searchdomain string `in:"omitempty;form=searchdomain"`
744+
Startup string `in:"omitempty;form=startup"`
745+
Swap int `in:"omitempty;form=swap"`
746+
Tags string `in:"omitempty;form=tags"`
747+
Template int `in:"omitempty;form=template"` // bool
748+
Timezone string `in:"omitempty;form=timezone"`
749+
TTY int `in:"omitempty;form=tty"`
750+
Unprivileged int `in:"omitempty;form=unprivileged"` // bool
751+
// unused[n] // Reference to unused volumes. This is used internally, and should not be modified manually.
752+
}
753+
754+
type UpdateLxcRequest struct {
755+
Node string // The cluster node name.
756+
VMID int // The (unique) ID of the VM.
757+
Arch LxcArch // OS architecture type.
758+
CMode LxcConsoleMode // Console mode. By default, the console command tries to open a connection to one of the available tty devices. By setting cmode to 'console' it tries to attach to /dev/console instead. If you set cmode to 'shell', it simply invokes a shell inside the container (no login).
759+
Console bool // Attach a console device (/dev/console) to the container.
760+
Cores int // The number of cores assigned to the container. A container can use all available cores by default.
761+
CPULimit int // Limit of CPU usage. NOTE: If the computer has 2 CPUs, it has a total of '2' CPU time. Value '0' indicates no CPU limit.
762+
CPUUnits int // CPU weight for a container. Argument is used in the kernel fair scheduler. The larger the number is, the more CPU time this container gets. Number is relative to the weights of all the other running guests.
763+
Debug bool // Try to be more verbose. For now this only enables debug log-level on start.
764+
Desc string // Description for the Container. Shown in the web-interface CT's summary. This is saved as comment inside the configuration file.
765+
Features LXCFeatures // Allow containers access to advanced features.
766+
Hookscript string // Script that will be exectued during various steps in the containers lifetime.
767+
Hostname string // Set a host name for the container.
768+
Lock LxcLock // Lock/unlock the container.
769+
Memory int // Amount of RAM for the container in MB.
770+
Nameserver string // Sets DNS server IP address for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.
771+
Net []LxcNet // Specifies network interfaces for the container.
772+
OnBoot bool // Specifies whether a container will be started during system bootup.
773+
OSType string // OS type. This is used to setup configuration inside the container, and corresponds to lxc setup scripts in /usr/share/lxc/config/<ostype>.common.conf. Value 'unmanaged' can be used to skip and OS specific setup. debian | devuan | ubuntu | centos | fedora | opensuse | archlinux | alpine | gentoo | nixos | unmanaged
774+
Protection bool // Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.
775+
RootFS string // Use volume as container root (in format "{STORAGE_ID}:{SIZE_IN_GIGS}", i.e. "local-lvm:8", if value not specified it defaults to "local-lvm:8", TODO: make this a struct).
776+
Searchdomain string // Sets DNS search domains for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.
777+
Startup string // make this a struct Startup and shutdown behavior. Order is a non-negative number defining the general startup order. Shutdown in done with reverse ordering. Additionally you can set the 'up' or 'down' delay in seconds, which specifies a delay to wait before the next VM is started or stopped.
778+
Swap int // Amount of SWAP for the container in MB.
779+
Tags string // Tags of the Container. This is only meta information.
780+
Template bool // Enable/disable Template.
781+
Timezone string // Time zone to use in the container. If option isn't set, then nothing will be done. Can be set to 'host' to match the host time zone, or an arbitrary time zone option from /usr/share/zoneinfo/zone.tab
782+
TTY int // Specify the number of tty available to the container.
783+
Unprivileged bool // Makes the container run as unprivileged user. (Should not be modified manually.)
784+
// unused[n] // Reference to unused volumes. This is used internally, and should not be modified manually.
785+
// dev[n] string Device to pass through to the container
786+
//mp Use volume as container mount point. Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume.
787+
}
788+
789+
// Update updates an existing LXC container.
790+
//
791+
// PUT /nodes/{node}/lxc/{vmid}/config requires VM.Config.Disk, VM.Config.CPU, VM.Config.Memory, VM.Config.Network, VM.Config.Options permissions.
792+
func (s *PVELxcService) Update(req UpdateLxcRequest) (err error) {
793+
method := http.MethodPut
794+
path := "/nodes/{node}/lxc/{vmid}/config"
795+
796+
// convert bool to int
797+
console := helpers.BoolToInt(req.Console)
798+
debug := helpers.BoolToInt(req.Debug)
799+
onBoot := helpers.BoolToInt(req.OnBoot)
800+
protection := helpers.BoolToInt(req.Protection)
801+
template := helpers.BoolToInt(req.Template)
802+
unprivileged := helpers.BoolToInt(req.Unprivileged)
803+
804+
payload := pveUpdateLxcRequest{
805+
Node: req.Node,
806+
VMID: req.VMID,
807+
Arch: string(req.Arch),
808+
CMode: string(req.CMode),
809+
Console: console,
810+
Cores: req.Cores,
811+
CPULimit: req.CPULimit,
812+
CPUUnits: req.CPUUnits,
813+
Debug: debug,
814+
Desc: req.Desc,
815+
Features: req.Features.String(),
816+
Hookscript: req.Hookscript,
817+
Hostname: req.Hostname,
818+
Lock: string(req.Lock),
819+
Memory: req.Memory,
820+
Nameserver: req.Nameserver,
821+
OnBoot: onBoot,
822+
OSType: req.OSType,
823+
Protection: protection,
824+
RootFS: req.RootFS,
825+
Searchdomain: req.Searchdomain,
826+
Startup: req.Startup,
827+
Swap: req.Swap,
828+
Tags: req.Tags,
829+
Template: template,
830+
Timezone: req.Timezone,
831+
TTY: req.TTY,
832+
Unprivileged: unprivileged,
833+
}
834+
835+
netValues := map[string]string{}
836+
for i, net := range req.Net {
837+
netValues[fmt.Sprintf("net%d", i)] = net.String()
838+
}
839+
840+
err = s.api.client.sendReq3(method, path, &payload, netValues, nil)
841+
842+
if err != nil {
843+
return err
844+
}
845+
846+
return nil
847+
}

0 commit comments

Comments
 (0)