added config
This commit is contained in:
parent
271c91eecb
commit
928086aea9
2 changed files with 68 additions and 19 deletions
80
client.go
80
client.go
|
|
@ -38,10 +38,10 @@ func ExponentialBackoff(b float64, d time.Duration) BackoffFunc {
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
endpoint string
|
endpoints []string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
session session
|
session session
|
||||||
//pollInterval time.Duration
|
//pollInterval time.Duration
|
||||||
//backoffFunc BackoffFunc
|
//backoffFunc BackoffFunc
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
|
|
@ -66,7 +66,15 @@ type ClientOption func(*Client)
|
||||||
|
|
||||||
func WithEndpoint(endpoint string) ClientOption {
|
func WithEndpoint(endpoint string) ClientOption {
|
||||||
return func(client *Client) {
|
return func(client *Client) {
|
||||||
client.endpoint = strings.TrimRight(endpoint, "/")
|
client.endpoints = append(client.endpoints, strings.TrimRight(endpoint, "/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithEndpoints(endpoints []string) ClientOption {
|
||||||
|
return func(client *Client) {
|
||||||
|
for _, e := range endpoints {
|
||||||
|
client.endpoints = append(client.endpoints, strings.TrimRight(e, "/"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,33 +118,65 @@ func WithCredentials(username, password string) ClientOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrMissingAPIEnv = errors.New("missing environment variable PVE_API")
|
ErrMissingEndpointsEnv = errors.New("missing environment variable PVE_ENDPOINTS")
|
||||||
ErrMissingUserEnv = errors.New("missing environment variable PVE_USER")
|
ErrMissingUserEnv = errors.New("missing environment variable PVE_USER")
|
||||||
ErrMissingPasswordEnv = errors.New("missing environment variable PVE_PASSWORD")
|
ErrMissingPasswordEnv = errors.New("missing environment variable PVE_PASSWORD")
|
||||||
|
|
||||||
|
ErrMissingEndpoints = errors.New("missing Endpoints in config")
|
||||||
|
ErrMissingUser = errors.New("missing User in config")
|
||||||
|
ErrMissingPassword = errors.New("missing Password in config")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Endpoints []string `json:"endpoints"`
|
||||||
|
User string `json:"user"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Insecure bool `json:"insecure"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClientOptionsFromConfig(cfg Config) (options []ClientOption, err error) {
|
||||||
|
if len(cfg.Endpoints) == 0 {
|
||||||
|
err = ErrMissingEndpoints
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cfg.User == "" {
|
||||||
|
err = ErrMissingUser
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cfg.Password == "" {
|
||||||
|
err = ErrMissingPassword
|
||||||
|
return
|
||||||
|
}
|
||||||
|
options = []ClientOption{
|
||||||
|
WithEndpoints(cfg.Endpoints),
|
||||||
|
WithCredentials(cfg.User, cfg.Password),
|
||||||
|
WithInsecureClient(cfg.Insecure),
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func ClientOptionsFromEnv() (options []ClientOption, err error) {
|
func ClientOptionsFromEnv() (options []ClientOption, err error) {
|
||||||
api := os.Getenv("PVE_API")
|
endpoints := os.Getenv("PVE_ENDPOINTS")
|
||||||
if api == "" {
|
if endpoints == "" {
|
||||||
err = ErrMissingAPIEnv
|
err = ErrMissingEndpointsEnv
|
||||||
//return
|
return
|
||||||
}
|
}
|
||||||
user := os.Getenv("PVE_USER")
|
user := os.Getenv("PVE_USER")
|
||||||
if user == "" {
|
if user == "" {
|
||||||
err = ErrMissingUserEnv
|
err = ErrMissingUserEnv
|
||||||
//return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pass := os.Getenv("PVE_PASSWORD")
|
pass := os.Getenv("PVE_PASSWORD")
|
||||||
if pass == "" {
|
if pass == "" {
|
||||||
err = ErrMissingPasswordEnv
|
err = ErrMissingPasswordEnv
|
||||||
//return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
insecure, _ := strconv.ParseBool(os.Getenv("PVE_INSECURE"))
|
insecure, _ := strconv.ParseBool(os.Getenv("PVE_INSECURE"))
|
||||||
|
|
||||||
options = []ClientOption{
|
options = []ClientOption{
|
||||||
WithEndpoint(api),
|
WithEndpoints(strings.Split(endpoints, ",")),
|
||||||
WithCredentials(user, pass),
|
WithCredentials(user, pass),
|
||||||
WithInsecureClient(insecure),
|
WithInsecureClient(insecure),
|
||||||
}
|
}
|
||||||
|
|
@ -174,6 +214,12 @@ func NewClient(options ...ClientOption) *Client {
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
func (c *Client) getEndpoint() string {
|
||||||
|
if len(c.endpoints) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return c.endpoints[0]
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Auth() (err error) {
|
func (c *Client) Auth() (err error) {
|
||||||
if !time.Now().After(c.session.Time) {
|
if !time.Now().After(c.session.Time) {
|
||||||
|
|
@ -184,7 +230,7 @@ func (c *Client) Auth() (err error) {
|
||||||
if !time.Now().After(c.session.Time) {
|
if !time.Now().After(c.session.Time) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u := c.endpoint + "/access/ticket"
|
u := c.getEndpoint() + "/access/ticket"
|
||||||
body := httpbody{"username": c.username, "password": c.password}
|
body := httpbody{"username": c.username, "password": c.password}
|
||||||
r, err := http.NewRequest("POST", u, body.Reader())
|
r, err := http.NewRequest("POST", u, body.Reader())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -210,7 +256,7 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
url := c.endpoint + path
|
url := c.getEndpoint() + path
|
||||||
req, err := http.NewRequest(method, url, body)
|
req, err := http.NewRequest(method, url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PVEScheme = "pve"
|
const (
|
||||||
|
PVEScheme = "pve"
|
||||||
|
PVESchemeURL = PVEScheme + "://"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidPVEURL = errors.New("invalid pve url scheme")
|
ErrInvalidPVEURL = errors.New("invalid pve url scheme")
|
||||||
|
|
@ -51,7 +54,7 @@ func ServerRefFromURL(s string) (ref *ServerRef, 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", PVESchemeURL, pool, node, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func K8sURL(url string) (string, error) {
|
func K8sURL(url string) (string, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue