first compiling version
This commit is contained in:
parent
60fb40d61a
commit
3d54199faa
27 changed files with 908 additions and 243 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
|
||||
|
|
@ -15,7 +16,8 @@ import (
|
|||
|
||||
var (
|
||||
ErrClientIsNil = errors.New("client is nil")
|
||||
base = schema.LBPath
|
||||
keyPath = schema.KeyPath
|
||||
reqPath = schema.ReqPath
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
|
@ -29,42 +31,66 @@ func NewClient(c *apiclient.Client) (*Client, error) {
|
|||
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
|
||||
if cluster != "" {
|
||||
path += "/" + cluster
|
||||
}
|
||||
func (c *Client) GetApprovals(ctx context.Context) (approvals schema.Approvals, err error) {
|
||||
path := reqPath
|
||||
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)
|
||||
_, err = c.c.Do(req, &approvals)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb *schema.LoadBalancer, err error) {
|
||||
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
|
||||
func (c *Client) RejectApproval(ctx context.Context, id string) error {
|
||||
return c.sendApproval(ctx, id, false)
|
||||
}
|
||||
|
||||
func (c *Client) AcceptApproval(ctx context.Context, id string) error {
|
||||
return c.sendApproval(ctx, id, true)
|
||||
}
|
||||
|
||||
func (c *Client) sendApproval(ctx context.Context, id string, approve bool) (err error) {
|
||||
path := fmt.Sprintf("%s/%s", reqPath, id)
|
||||
method := "DELETE"
|
||||
if approve {
|
||||
method = "PUT"
|
||||
}
|
||||
req, err := c.c.NewRequest(ctx, method, path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.Do(req, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) GetKeys(ctx context.Context) (keys schema.Keys, err error) {
|
||||
path := keyPath
|
||||
req, err := c.c.NewRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.Do(req, &lb)
|
||||
_, err = c.c.Do(req, &keys)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb *schema.LoadBalancer) (err error) {
|
||||
func (c *Client) GetKey(ctx context.Context, id string) (key schema.Key, err error) {
|
||||
path := fmt.Sprintf("%s/%s", keyPath, id)
|
||||
req, err := c.c.NewRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.c.FollowRedirect(req, &key, time.Second*2)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) CreateKey(ctx context.Context, key schema.NewKey) (err error) {
|
||||
buf := new(bytes.Buffer)
|
||||
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
|
||||
err = lb.ValidateClient()
|
||||
path := keyPath
|
||||
err = key.Validate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = json.NewEncoder(buf).Encode(lb)
|
||||
err = json.NewEncoder(buf).Encode(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -76,21 +102,8 @@ func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, l
|
|||
return
|
||||
}
|
||||
|
||||
func (c *Client) DeleteLoadBalancer(ctx context.Context, cluster, name string) (err error) {
|
||||
path := fmt.Sprintf("%s/%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
|
||||
}
|
||||
|
||||
func (c *Client) DeleteLoadBalancersWithCluster(ctx context.Context, cluster string) (err error) {
|
||||
if cluster == "" {
|
||||
return errors.New("cluster value can not be empty")
|
||||
}
|
||||
path := base + "/" + cluster
|
||||
func (c *Client) DeleteKey(ctx context.Context, id string) (err error) {
|
||||
path := fmt.Sprintf("%s/%s", keyPath, id)
|
||||
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue