added pool finder

This commit is contained in:
ston1th 2020-06-04 21:37:23 +02:00
commit 59ad9d8ec5
3 changed files with 53 additions and 11 deletions

53
pool.go
View file

@ -74,11 +74,31 @@ func (c *PoolClient) Delete(ctx context.Context, name string) (err error) {
return return
} }
type pools struct {
poolid string `json:"poolid"`
}
func (c *PoolClient) ListPools(ctx context.Context) (list []string, err error) {
var ps []pools
req, err := c.client.NewRequest(ctx, "GET", "/pools", nil)
if err != nil {
return
}
_, err = c.client.Do(req, &ps)
if err != nil {
return
}
for _, p := range ps {
list = append(list, p.poolid)
}
return
}
type pool struct { type pool struct {
members ServerList `json:"members"` members ServerList `json:"members"`
} }
func (c *PoolClient) List(ctx context.Context, name string) (sl ServerList, err error) { func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerList, err error) {
if name == "" { if name == "" {
err = ErrEmptyPoolName err = ErrEmptyPoolName
return return
@ -94,16 +114,29 @@ func (c *PoolClient) List(ctx context.Context, name string) (sl ServerList, err
} }
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id string) (s *Server, err error) { func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id string) (s *Server, err error) {
sl, err := c.List(ctx, poolname) var pools []string
if err != nil { if poolname != "" {
return pools = append(pools, poolname)
} } else {
for _, srv := range sl { pools, err = c.ListPools(ctx)
if id != "" && srv.ID == id { if err != nil {
return srv, nil return
} }
if servername != "" && srv.Name == servername { }
return srv, nil for _, pool := range pools {
sl, err := c.ListMembers(ctx, pool)
if err != nil {
return
}
for _, srv := range sl {
if id != "" && srv.ID == id {
srv.Pool = pool
return srv, nil
}
if servername != "" && srv.Name == servername {
srv.Pool = pool
return srv, nil
}
} }
} }
return nil, ErrServerNotFound return nil, ErrServerNotFound

View file

@ -40,3 +40,11 @@ func ParseURL(s string) (pool, node, id string, err error) {
func NewURL(pool, node, id string) string { func NewURL(pool, node, id string) string {
return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id) return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id)
} }
func K8sURL(url string) (string, error) {
pool, _, id, err := ParseURL(url)
if err != nil {
return "", nil
}
return NewURL(pool, "", id), nil
}

View file

@ -18,6 +18,7 @@ type Server struct {
ID string `json:"vmid"` ID string `json:"vmid"`
Name string `json:"name"` Name string `json:"name"`
Node string Node string
Pool string
Status ServerStatus `json:"status"` Status ServerStatus `json:"status"`
Resources Resources Resources Resources
NetDevices NetworkDevices NetDevices NetworkDevices
@ -58,8 +59,8 @@ func (s *Server) InstanceID() string {
return NewURL("", s.Node, s.ID) return NewURL("", s.Node, s.ID)
} }
func (s *Server) K8sID(pool string) string { func (s *Server) K8sID() string {
return NewURL(pool, "", s.ID) return NewURL(s.Pool, "", s.ID)
} }
type Resources struct { type Resources struct {