migrate to integer ID

This commit is contained in:
ston1th 2021-11-28 11:50:56 +01:00
commit 7ff2a92118
4 changed files with 27 additions and 22 deletions

10
node.go
View file

@ -205,7 +205,7 @@ func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerRefList
return
}
func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *ServerRef, err error) {
func (c *NodeClient) FindServer(ctx context.Context, name string, id int) (s *ServerRef, err error) {
nl, err := c.List(ctx)
if err != nil {
return
@ -223,7 +223,7 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
continue
}
for _, s := range sl {
if id != "" && s.ID == id {
if id != -1 && s.ID == id {
return s, nil
}
if name != "" && s.Name == name {
@ -241,11 +241,11 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
}
func (c *NodeClient) FindServerByName(ctx context.Context, name string) (s *ServerRef, err error) {
return c.FindServer(ctx, name, "")
return c.FindServer(ctx, name, -1)
}
func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *ServerRef, err error) {
if id == "" {
func (c *NodeClient) FindServerByID(ctx context.Context, id int) (s *ServerRef, err error) {
if id == -1 {
err = ErrEmptyID
return
}

11
pool.go
View file

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
)
var (
@ -106,7 +105,7 @@ type poolMembers []poolMember
func (pms poolMembers) ServerList() (sl ServerRefList) {
for _, pm := range pms {
sl = append(sl, &ServerRef{
ID: strconv.Itoa(pm.ID),
ID: pm.ID,
Name: pm.Name,
Node: pm.Node,
})
@ -133,7 +132,7 @@ func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerRef
return
}
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id string) (s *ServerRef, err error) {
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername string, id int) (s *ServerRef, err error) {
var pools []string
if poolname != "" {
pools = append(pools, poolname)
@ -149,7 +148,7 @@ func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id st
return nil, err
}
for _, srv := range sl {
if id != "" && srv.ID == id {
if id != -1 && srv.ID == id {
srv.Pool = pool
return srv, nil
}
@ -163,10 +162,10 @@ func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id st
}
func (c *PoolClient) FindServerByName(ctx context.Context, poolname, servername string) (s *ServerRef, err error) {
return c.FindServer(ctx, poolname, servername, "")
return c.FindServer(ctx, poolname, servername, -1)
}
func (c *PoolClient) FindServerByID(ctx context.Context, poolname, id string) (s *ServerRef, err error) {
func (c *PoolClient) FindServerByID(ctx context.Context, poolname string, id int) (s *ServerRef, err error) {
return c.FindServer(ctx, poolname, "", id)
}

View file

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
@ -17,9 +18,10 @@ const (
var (
ErrInvalidPVEURL = errors.New("invalid pve url scheme")
ErrNoID = errors.New("missing id in url")
ErrParsingID = errors.New("error parsing id in url")
)
func ParseURL(s string) (pool, node, id string, err error) {
func ParseURL(s string) (pool, node string, id int, err error) {
u, err := url.Parse(s)
if err != nil {
return
@ -37,7 +39,11 @@ func ParseURL(s string) (pool, node, id string, err error) {
err = ErrNoID
return
}
return u.Host, a[1], a[2], nil
id, err = strconv.Atoi(a[2])
if err != nil {
err = ErrParsingID
}
return u.Host, a[1], id, nil
}
func ServerRefFromURL(s string) (ref *ServerRef, err error) {
@ -53,8 +59,8 @@ func ServerRefFromURL(s string) (ref *ServerRef, err error) {
return
}
func NewURL(pool, node, id string) string {
return fmt.Sprintf("%s%s/%s/%s", PVESchemeURL, pool, node, id)
func NewURL(pool, node string, id int) string {
return fmt.Sprintf("%s%s/%s/%d", PVESchemeURL, pool, node, id)
}
func K8sURL(url string) (string, error) {

View file

@ -13,7 +13,7 @@ import (
var ErrServerNotFound = errors.New("server not found")
type Server struct {
ID string `json:"vmid"`
ID int `json:"vmid"`
Name string `json:"name"`
Node string
Pool string
@ -29,7 +29,7 @@ type Server struct {
}
func (s *Server) UserDataSnippetName() string {
return fmt.Sprintf("%s_userdata", s.ID)
return fmt.Sprintf("%d_userdata", s.ID)
}
func (s *Server) UserDataSnippet(storage string) string {
@ -84,7 +84,7 @@ func (s *Server) Ref() *ServerRef {
type ServerRefList []*ServerRef
type ServerRef struct {
ID string `json:"vmid"`
ID int `json:"vmid"`
Name string `json:"name"`
Node string
Pool string
@ -220,7 +220,7 @@ type ServerClient struct {
client *Client
}
func (c *ServerClient) NextID(ctx context.Context) (id string, err error) {
func (c *ServerClient) NextID(ctx context.Context) (id int, err error) {
req, err := c.client.NewRequest(ctx, "GET", "/cluster/nextid", nil)
if err != nil {
return
@ -285,7 +285,7 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
type ServerTemplateOpts struct {
Name string
TemplateID string
TemplateID int
// TemplateNode is optional
TemplateNode string
Pool string
@ -297,7 +297,7 @@ func (o *ServerTemplateOpts) Validate(ctx context.Context, c *Client) error {
if o.Name == "" {
return errors.New("missing name")
}
if o.TemplateID == "" {
if o.TemplateID <= 0 {
return errors.New("missing template id")
}
if o.TemplateNode == "" {
@ -335,7 +335,7 @@ func (c *ServerClient) CreateFromTemplate(ctx context.Context, opts *ServerTempl
return
}
body := opts.body()
body["newid"] = nextid
body["newid"] = strconv.Itoa(nextid)
node := opts.TemplateNode
if opts.TargetNode != "" && opts.TemplateNode != opts.TargetNode {
node = opts.TargetNode