initial commit
This commit is contained in:
commit
501d579a90
35 changed files with 11357 additions and 0 deletions
96
node.go
Normal file
96
node.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
// Copyright (C) 2020 Marius Schellenberger
|
||||
|
||||
package pve
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ErrServerNotFound = errors.New("server not found")
|
||||
|
||||
type NodeStatus string
|
||||
|
||||
const (
|
||||
NodeStatusOnline NodeStatus = "online"
|
||||
NodeStatusOffline NodeStatus = "offline"
|
||||
NodeStatusUnknown NodeStatus = "unknown"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Name string `json:"node"`
|
||||
Status NodeStatus `json:"status"`
|
||||
}
|
||||
|
||||
type NodeList []*Node
|
||||
|
||||
type ServerList []*Server
|
||||
|
||||
type NodeClient struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (c *NodeClient) List(ctx context.Context) (nl NodeList, err error) {
|
||||
req, err := c.client.NewRequest(ctx, "GET", "/nodes", nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.client.Do(req, &nl)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerList, err error) {
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/nodes/%s/qemu", n.Name), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.client.Do(req, &sl)
|
||||
if err == nil {
|
||||
for i := range sl {
|
||||
sl[i].Node = n.Name
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server, err error) {
|
||||
nl, err := c.List(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, node := range nl {
|
||||
if node.Status != NodeStatusOnline {
|
||||
continue
|
||||
}
|
||||
sl, err := c.ListServers(ctx, node)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, s := range sl {
|
||||
if id != "" && s.ID == id {
|
||||
return s, nil
|
||||
}
|
||||
if name != "" && s.Name == name {
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, ErrServerNotFound
|
||||
}
|
||||
|
||||
func (c *NodeClient) FindServerByName(ctx context.Context, name string) (s *Server, err error) {
|
||||
return c.FindServer(ctx, name, "")
|
||||
}
|
||||
|
||||
func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *Server, err error) {
|
||||
return c.FindServer(ctx, "", id)
|
||||
}
|
||||
|
||||
func (c *NodeClient) FindServerByURL(ctx context.Context, url string) (s *Server, err error) {
|
||||
_, id, err := ParseURL(url)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return c.FindServer(ctx, "", id)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue