first working version
This commit is contained in:
parent
372915e24d
commit
6da132106f
18 changed files with 519 additions and 96 deletions
85
pkg/api/v1/client/client.go
Normal file
85
pkg/api/v1/client/client.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
apiclient "git.giftfish.de/ston1th/haproxy-lb/pkg/api/client"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrClientIsNil = errors.New("client is nil")
|
||||
base = "/" + schema.Version
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
c *apiclient.Client
|
||||
}
|
||||
|
||||
func NewClient(c *apiclient.Client) (*Client, error) {
|
||||
if c == nil {
|
||||
return nil, ErrClientIsNil
|
||||
}
|
||||
return &Client{c}, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetLoadBalancers(ctx context.Context) (lbs map[string]schema.LoadBalancer, err error) {
|
||||
return c.GetLoadBalancersWithCluster(ctx, "")
|
||||
}
|
||||
|
||||
func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string) (lbs map[string]schema.LoadBalancer, err error) {
|
||||
path := base + "/lb"
|
||||
if cluster != "" {
|
||||
path += "/" + cluster
|
||||
}
|
||||
req, err := c.c.NewRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
lbs = make(map[string]schema.LoadBalancer)
|
||||
_, err = c.c.Do(req, &lbs)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb schema.LoadBalancer, err error) {
|
||||
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
|
||||
req, err := c.c.NewRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.Do(req, &lb)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb schema.LoadBalancer) (err error) {
|
||||
buf := new(bytes.Buffer)
|
||||
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
|
||||
err = lb.ValidateClient()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = json.NewEncoder(buf).Encode(lb)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req, err := c.c.NewRequest(ctx, "POST", path, buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.Do(req, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) DeleteLoadBalancer(ctx context.Context, cluster, name string) (err error) {
|
||||
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
|
||||
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.Do(req, nil)
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue