mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-27 03:21:09 +01:00
refactor(proxmox): change VMID type from int to uint64 across Proxmox provider
Updates VMID parameter and field types from int to uint64 throughout the Proxmox provider implementation, including API request structures, provider structs, client methods, and LXC-related functions. Also updates string conversion calls from strconv.Itoa to strconv.FormatUint.
This commit is contained in:
@@ -2,5 +2,5 @@ package proxmoxapi
|
||||
|
||||
type ActionRequest struct {
|
||||
Node string `uri:"node" binding:"required"`
|
||||
VMID int `uri:"vmid" binding:"required"`
|
||||
VMID uint64 `uri:"vmid" binding:"required"`
|
||||
} // @name ProxmoxVMActionRequest
|
||||
|
||||
@@ -11,10 +11,7 @@ import (
|
||||
"github.com/yusing/goutils/http/websocket"
|
||||
)
|
||||
|
||||
type StatsRequest struct {
|
||||
Node string `uri:"node" binding:"required"`
|
||||
VMID int `uri:"vmid" binding:"required"`
|
||||
}
|
||||
type StatsRequest ActionRequest
|
||||
|
||||
// @x-id "nodeStats"
|
||||
// @BasePath /api/v1
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
type ProxmoxProvider struct {
|
||||
*proxmox.Node
|
||||
|
||||
vmid int
|
||||
vmid uint64
|
||||
lxcName string
|
||||
running bool
|
||||
}
|
||||
@@ -27,7 +27,7 @@ const proxmoxStateCheckInterval = 1 * time.Second
|
||||
|
||||
var ErrNodeNotFound = gperr.New("node not found in pool")
|
||||
|
||||
func NewProxmoxProvider(ctx context.Context, nodeName string, vmid int) (idlewatcher.Provider, error) {
|
||||
func NewProxmoxProvider(ctx context.Context, nodeName string, vmid uint64) (idlewatcher.Provider, error) {
|
||||
if nodeName == "" || vmid == 0 {
|
||||
return nil, errors.New("node name and vmid are required")
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func (p *ProxmoxProvider) Watch(ctx context.Context) (<-chan watcher.Event, <-ch
|
||||
|
||||
event := watcher.Event{
|
||||
Type: events.EventTypeDocker,
|
||||
ActorID: strconv.Itoa(p.vmid),
|
||||
ActorID: strconv.FormatUint(p.vmid, 10),
|
||||
ActorName: p.lxcName,
|
||||
}
|
||||
for {
|
||||
|
||||
@@ -125,10 +125,10 @@ func (c *Client) UpdateResources(ctx context.Context) error {
|
||||
// GetResource gets a resource by kind and id.
|
||||
// kind: lxc or qemu
|
||||
// id: <vmid>
|
||||
func (c *Client) GetResource(kind string, id int) (*VMResource, error) {
|
||||
func (c *Client) GetResource(kind string, id uint64) (*VMResource, error) {
|
||||
c.resourcesMu.RLock()
|
||||
defer c.resourcesMu.RUnlock()
|
||||
resource, ok := c.resources[kind+"/"+strconv.Itoa(id)]
|
||||
resource, ok := c.resources[kind+"/"+strconv.FormatUint(id, 10)]
|
||||
if !ok {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ const (
|
||||
proxmoxTaskCheckInterval = 300 * time.Millisecond
|
||||
)
|
||||
|
||||
func (n *Node) LXCAction(ctx context.Context, vmid int, action LXCAction) error {
|
||||
func (n *Node) LXCAction(ctx context.Context, vmid uint64, action LXCAction) error {
|
||||
var upid proxmox.UPID
|
||||
if err := n.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/%s", n.name, vmid, action), nil, &upid); err != nil {
|
||||
return err
|
||||
@@ -82,7 +82,7 @@ func (n *Node) LXCAction(ctx context.Context, vmid int, action LXCAction) error
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) LXCName(ctx context.Context, vmid int) (string, error) {
|
||||
func (n *Node) LXCName(ctx context.Context, vmid uint64) (string, error) {
|
||||
var name nameOnly
|
||||
if err := n.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/current", n.name, vmid), &name); err != nil {
|
||||
return "", err
|
||||
@@ -90,7 +90,7 @@ func (n *Node) LXCName(ctx context.Context, vmid int) (string, error) {
|
||||
return name.Name, nil
|
||||
}
|
||||
|
||||
func (n *Node) LXCStatus(ctx context.Context, vmid int) (LXCStatus, error) {
|
||||
func (n *Node) LXCStatus(ctx context.Context, vmid uint64) (LXCStatus, error) {
|
||||
var status statusOnly
|
||||
if err := n.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/current", n.name, vmid), &status); err != nil {
|
||||
return "", err
|
||||
@@ -98,17 +98,17 @@ func (n *Node) LXCStatus(ctx context.Context, vmid int) (LXCStatus, error) {
|
||||
return status.Status, nil
|
||||
}
|
||||
|
||||
func (n *Node) LXCIsRunning(ctx context.Context, vmid int) (bool, error) {
|
||||
func (n *Node) LXCIsRunning(ctx context.Context, vmid uint64) (bool, error) {
|
||||
status, err := n.LXCStatus(ctx, vmid)
|
||||
return status == LXCStatusRunning, err
|
||||
}
|
||||
|
||||
func (n *Node) LXCIsStopped(ctx context.Context, vmid int) (bool, error) {
|
||||
func (n *Node) LXCIsStopped(ctx context.Context, vmid uint64) (bool, error) {
|
||||
status, err := n.LXCStatus(ctx, vmid)
|
||||
return status == LXCStatusStopped, err
|
||||
}
|
||||
|
||||
func (n *Node) LXCSetShutdownTimeout(ctx context.Context, vmid int, timeout time.Duration) error {
|
||||
func (n *Node) LXCSetShutdownTimeout(ctx context.Context, vmid uint64, timeout time.Duration) error {
|
||||
return n.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/config", n.name, vmid), map[string]interface{}{
|
||||
"startup": fmt.Sprintf("down=%.0f", timeout.Seconds()),
|
||||
}, nil)
|
||||
|
||||
@@ -42,7 +42,7 @@ import (
|
||||
//
|
||||
// - format: "STATUS|CPU%%|MEM USAGE/LIMIT|MEM%%|NET I/O|BLOCK I/O"
|
||||
// - example: running|31.1%|9.6GiB/20GiB|48.87%|4.7GiB/3.3GiB|25GiB/36GiB
|
||||
func (n *Node) LXCStats(ctx context.Context, vmid int, stream bool) (io.ReadCloser, error) {
|
||||
func (n *Node) LXCStats(ctx context.Context, vmid uint64, stream bool) (io.ReadCloser, error) {
|
||||
if !stream {
|
||||
resource, err := n.client.GetResource("lxc", vmid)
|
||||
if err != nil {
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
type NodeConfig struct {
|
||||
Node string `json:"node"`
|
||||
VMID *int `json:"vmid"` // unset: auto discover; explicit 0: node-level route; >0: lxc/qemu resource route
|
||||
VMID *uint64 `json:"vmid"` // unset: auto discover; explicit 0: node-level route; >0: lxc/qemu resource route
|
||||
VMName string `json:"vmname,omitempty"`
|
||||
Services []string `json:"services,omitempty" aliases:"service"`
|
||||
Files []string `json:"files,omitempty" aliases:"file"`
|
||||
|
||||
@@ -212,7 +212,7 @@ func (r *Route) validate() error {
|
||||
for _, p := range proxmoxProviders {
|
||||
// First check if hostname, IP, or alias matches a node (node-level route)
|
||||
if nodeName := p.Client().ReverseLookupNode(hostname, ip, r.Alias); nodeName != "" {
|
||||
zero := 0
|
||||
zero := uint64(0)
|
||||
if r.Proxmox == nil {
|
||||
r.Proxmox = &proxmox.NodeConfig{}
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func (r *Route) validate() error {
|
||||
// Then check if hostname, IP, or alias matches a VM resource
|
||||
resource, _ := p.Client().ReverseLookupResource(ip, hostname, r.Alias)
|
||||
if resource != nil {
|
||||
vmid := int(resource.VMID)
|
||||
vmid := resource.VMID
|
||||
if r.Proxmox == nil {
|
||||
r.Proxmox = &proxmox.NodeConfig{}
|
||||
}
|
||||
@@ -706,7 +706,7 @@ func (r *Route) MarshalZerologObject(e *zerolog.Event) {
|
||||
if r.Proxmox != nil {
|
||||
e.Str("proxmox", r.Proxmox.Node)
|
||||
if r.Proxmox.VMID != nil {
|
||||
e.Int("vmid", *r.Proxmox.VMID)
|
||||
e.Uint64("vmid", *r.Proxmox.VMID)
|
||||
}
|
||||
if r.Proxmox.VMName != "" {
|
||||
e.Str("vmname", r.Proxmox.VMName)
|
||||
|
||||
@@ -45,7 +45,7 @@ type (
|
||||
} // @name IdlewatcherDockerConfig
|
||||
ProxmoxConfig struct {
|
||||
Node string `json:"node" validate:"required"`
|
||||
VMID int `json:"vmid" validate:"required"`
|
||||
VMID uint64 `json:"vmid" validate:"required"`
|
||||
} // @name IdlewatcherProxmoxNodeConfig
|
||||
)
|
||||
|
||||
@@ -69,14 +69,14 @@ func (c *IdlewatcherConfig) Key() string {
|
||||
if c.Docker != nil {
|
||||
return c.Docker.ContainerID
|
||||
}
|
||||
return c.Proxmox.Node + ":" + strconv.Itoa(c.Proxmox.VMID)
|
||||
return c.Proxmox.Node + ":" + strconv.FormatUint(c.Proxmox.VMID, 10)
|
||||
}
|
||||
|
||||
func (c *IdlewatcherConfig) ContainerName() string {
|
||||
if c.Docker != nil {
|
||||
return c.Docker.ContainerName
|
||||
}
|
||||
return "lxc-" + strconv.Itoa(c.Proxmox.VMID)
|
||||
return "lxc-" + strconv.FormatUint(c.Proxmox.VMID, 10)
|
||||
}
|
||||
|
||||
func (c *IdlewatcherConfig) Validate() error {
|
||||
|
||||
Reference in New Issue
Block a user