extended URL scheme

This commit is contained in:
ston1th 2020-06-03 23:45:54 +02:00
commit 0994baf5b0
4 changed files with 39 additions and 18 deletions

View file

@ -11,6 +11,7 @@ import (
var (
ErrNodesOffline = errors.New("one or more nodes are offline")
ErrNodesNotSearched = errors.New("one or more nodes could not be searched")
ErrEmptyID = errors.New("id is empty")
)
type NodeStatus string
@ -95,11 +96,15 @@ func (c *NodeClient) FindServerByName(ctx context.Context, name string) (s *Serv
}
func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *Server, err error) {
if id == "" {
err = ErrEmptyID
return
}
return c.FindServer(ctx, "", id)
}
func (c *NodeClient) FindServerByURL(ctx context.Context, url string) (s *Server, err error) {
_, id, err := ParseURL(url)
_, _, id, err := ParseURL(url)
if err != nil {
return
}

View file

@ -12,6 +12,7 @@ import (
var (
ErrPoolExists = errors.New("pool already exists")
ErrPoolNotExists = errors.New("pool does not exist")
ErrEmptyPoolName = errors.New("pool name is empty")
)
func MakePoolName(namespace, name string) string {
@ -78,6 +79,10 @@ type pool struct {
}
func (c *PoolClient) List(ctx context.Context, name string) (sl ServerList, err error) {
if name == "" {
err = ErrEmptyPoolName
return
}
var p pool
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/pools/%s", name), nil)
if err != nil {
@ -113,7 +118,7 @@ func (c *PoolClient) FindServerByID(ctx context.Context, poolname, id string) (s
}
func (c *PoolClient) FindServerByURL(ctx context.Context, url string) (s *Server, err error) {
pool, id, err := ParseURL(url)
pool, _, id, err := ParseURL(url)
if err != nil {
return
}

View file

@ -11,9 +11,12 @@ import (
const PVEScheme = "pve"
var ErrInvalidPVEURL = errors.New("invalid pve url scheme")
var (
ErrInvalidPVEURL = errors.New("invalid pve url scheme")
ErrNoID = errors.New("missing id in url")
)
func ParseURL(s string) (pool, id string, err error) {
func ParseURL(s string) (pool, node, id string, err error) {
u, err := url.Parse(s)
if err != nil {
return
@ -22,9 +25,18 @@ func ParseURL(s string) (pool, id string, err error) {
err = ErrInvalidPVEURL
return
}
return u.Host, strings.TrimPrefix(u.Path, "/"), nil
a := strings.Split(u.Path, "/")
if len(a) != 3 {
err = ErrInvalidPVEURL
return
}
if a[2] == "" {
err = ErrNoID
return
}
return u.Host, a[1], a[2], nil
}
func NewURL(pool, id string) string {
return fmt.Sprintf("%s://%s/%s", PVEScheme, pool, id)
func NewURL(pool, node, id string) string {
return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id)
}

View file

@ -184,26 +184,25 @@ func (c *ServerClient) getServer(ctx context.Context, node, id string) (s *Serve
}
func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err error) {
pool, id, err := ParseURL(url)
pool, node, id, err := ParseURL(url)
if err != nil {
return
}
var srv *Server
if pool != "" {
if node == "" && pool != "" {
srv, err = c.client.Pool.FindServerByID(ctx, pool, id)
if err == ErrServerNotFound {
return
}
node = srv.Node
} else if node == "" && pool == "" {
srv, err = c.client.Node.FindServerByID(ctx, id)
if err != nil {
return
}
node = srv.Node
}
srv, err = c.client.Node.FindServerByID(ctx, id)
if err != nil {
return
}
if srv == nil {
err = ErrServerNotFound
return
}
s, cfg, err := c.getServer(ctx, srv.Node, srv.ID)
s, cfg, err := c.getServer(ctx, node, id)
if err != nil {
return
}