2016-03-02 18:42:32 +00:00
|
|
|
package api
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
2016-03-02 18:42:32 +00:00
|
|
|
|
2016-08-08 20:00:31 +00:00
|
|
|
func (c *Sys) CapabilitiesSelf(path string) ([]string, error) {
|
|
|
|
return c.Capabilities(c.c.Token(), path)
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 18:21:07 +00:00
|
|
|
func (c *Sys) Capabilities(token, path string) ([]string, error) {
|
2016-03-02 18:42:32 +00:00
|
|
|
body := map[string]string{
|
|
|
|
"token": token,
|
|
|
|
"path": path,
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:00:31 +00: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 18:42:32 +00:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2016-03-02 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-08-14 18:52:45 +00:00
|
|
|
var result map[string]interface{}
|
|
|
|
err = resp.DecodeJSON(&result)
|
2016-03-17 19:14:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-08 20:00:31 +00:00
|
|
|
|
2018-05-11 15:58:12 +00:00
|
|
|
if result["capabilities"] == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-17 19:14:17 +00:00
|
|
|
var capabilities []string
|
2018-05-11 15:58:12 +00:00
|
|
|
capabilitiesRaw, ok := result["capabilities"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error interpreting returned capabilities")
|
|
|
|
}
|
2016-03-17 19:14:17 +00:00
|
|
|
for _, capability := range capabilitiesRaw {
|
|
|
|
capabilities = append(capabilities, capability.(string))
|
|
|
|
}
|
|
|
|
return capabilities, nil
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|