2016-03-02 21:42:32 +03:00
|
|
|
package api
|
|
|
|
|
2018-07-25 01:49:55 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2018-08-14 22:29:22 +03:00
|
|
|
"errors"
|
2018-07-25 01:49:55 +03:00
|
|
|
"fmt"
|
2018-08-14 22:29:22 +03:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-07-25 01:49:55 +03:00
|
|
|
)
|
2016-03-02 21:42:32 +03:00
|
|
|
|
2016-08-08 23:00:31 +03:00
|
|
|
func (c *Sys) CapabilitiesSelf(path string) ([]string, error) {
|
|
|
|
return c.Capabilities(c.c.Token(), path)
|
2016-03-02 21:42:32 +03:00
|
|
|
}
|
|
|
|
|
2016-03-04 21:21:07 +03:00
|
|
|
func (c *Sys) Capabilities(token, path string) ([]string, error) {
|
2016-03-02 21:42:32 +03:00
|
|
|
body := map[string]string{
|
|
|
|
"token": token,
|
|
|
|
"path": path,
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:00:31 +03:00
|
|
|
reqPath := "/v1/sys/capabilities"
|
|
|
|
if token == c.c.Token() {
|
|
|
|
reqPath = fmt.Sprintf("%s-self", reqPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := c.c.NewRequest("POST", reqPath)
|
2016-03-02 21:42:32 +03:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-25 01:49:55 +03:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2016-03-02 21:42:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-03-17 22:14:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
2018-05-11 18:58:12 +03:00
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
|
|
|
|
var res []string
|
|
|
|
err = mapstructure.Decode(secret.Data[path], &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-17 22:14:17 +03:00
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
|
2018-09-19 20:06:37 +03:00
|
|
|
if len(res) == 0 {
|
|
|
|
_, ok := secret.Data["capabilities"]
|
|
|
|
if ok {
|
|
|
|
err = mapstructure.Decode(secret.Data["capabilities"], &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
return res, nil
|
2016-03-02 21:42:32 +03:00
|
|
|
}
|