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
}
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 {
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 == "" {
err = ErrEmptyPoolName
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) {
sl, err := c.List(ctx, poolname)
if err != nil {
return
}
for _, srv := range sl {
if id != "" && srv.ID == id {
return srv, nil
var pools []string
if poolname != "" {
pools = append(pools, poolname)
} else {
pools, err = c.ListPools(ctx)
if err != 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

View file

@ -40,3 +40,11 @@ func ParseURL(s string) (pool, node, id string, err error) {
func NewURL(pool, node, id string) string {
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"`
Name string `json:"name"`
Node string
Pool string
Status ServerStatus `json:"status"`
Resources Resources
NetDevices NetworkDevices
@ -58,8 +59,8 @@ func (s *Server) InstanceID() string {
return NewURL("", s.Node, s.ID)
}
func (s *Server) K8sID(pool string) string {
return NewURL(pool, "", s.ID)
func (s *Server) K8sID() string {
return NewURL(s.Pool, "", s.ID)
}
type Resources struct {