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 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) nl, err := c.List(ctx)
if err != nil { if err != nil {
return return
@ -223,7 +223,7 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
continue continue
} }
for _, s := range sl { for _, s := range sl {
if id != "" && s.ID == id { if id != -1 && s.ID == id {
return s, nil return s, nil
} }
if name != "" && s.Name == name { 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) { 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) { func (c *NodeClient) FindServerByID(ctx context.Context, id int) (s *ServerRef, err error) {
if id == "" { if id == -1 {
err = ErrEmptyID err = ErrEmptyID
return return
} }

11
pool.go
View file

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

View file

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

View file

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