66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
// Copyright (C) 2020 Marius Schellenberger
|
|
|
|
package pve
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
PVEScheme = "pve"
|
|
PVESchemeURL = PVEScheme + "://"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidPVEURL = errors.New("invalid pve url scheme")
|
|
ErrNoID = errors.New("missing id in url")
|
|
)
|
|
|
|
func ParseURL(s string) (pool, node, id string, err error) {
|
|
u, err := url.Parse(s)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if u.Scheme != PVEScheme {
|
|
err = ErrInvalidPVEURL
|
|
return
|
|
}
|
|
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 ServerRefFromURL(s string) (ref *ServerRef, err error) {
|
|
pool, node, id, err := ParseURL(s)
|
|
if err != nil {
|
|
return
|
|
}
|
|
ref = &ServerRef{
|
|
ID: id,
|
|
Node: node,
|
|
Pool: pool,
|
|
}
|
|
return
|
|
}
|
|
|
|
func NewURL(pool, node, id string) string {
|
|
return fmt.Sprintf("%s%s/%s/%s", PVESchemeURL, pool, node, id)
|
|
}
|
|
|
|
func K8sURL(url string) (string, error) {
|
|
pool, _, id, err := ParseURL(url)
|
|
if err != nil {
|
|
return "", nil
|
|
}
|
|
return NewURL(pool, "", id), nil
|
|
}
|