2018-10-16 12:38:32 +00:00
|
|
|
package gonextcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2019-07-22 14:17:11 +00:00
|
|
|
|
|
|
|
req "github.com/levigross/grequests"
|
2018-10-16 12:38:32 +00:00
|
|
|
)
|
|
|
|
|
2019-07-22 14:17:11 +00:00
|
|
|
//appsConfig contains all apps Configuration available actions
|
|
|
|
type appsConfig struct {
|
|
|
|
c *client
|
2018-10-21 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//List lists all the available apps
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) List() (apps []string, err error) {
|
2019-12-20 20:22:13 +00:00
|
|
|
res, err := a.c.baseOcsRequest(http.MethodGet, routes.appsConfig, nil)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r appConfigResponse
|
2018-10-16 12:38:32 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Keys returns the app's config keys
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) Keys(id string) (keys []string, err error) {
|
2019-12-20 20:22:13 +00:00
|
|
|
res, err := a.c.baseOcsRequest(http.MethodGet, routes.appsConfig, nil, id)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r appConfigResponse
|
2018-10-16 12:38:32 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Value get the config value for the given app's key
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) Value(id, key string) (string, error) {
|
2019-12-20 20:22:13 +00:00
|
|
|
res, err := a.c.baseOcsRequest(http.MethodGet, routes.appsConfig, nil, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r appcConfigValueResponse
|
2018-10-16 12:38:32 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//SetValue set the config value for the given app's key
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) SetValue(id, key, value string) error {
|
2018-10-16 12:38:32 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"value": value,
|
|
|
|
},
|
|
|
|
}
|
2019-12-20 20:22:13 +00:00
|
|
|
_, err := a.c.baseOcsRequest(http.MethodPost, routes.appsConfig, ro, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//DeleteValue delete the config value and (!! be careful !!) the key
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) DeleteValue(id, key, value string) error {
|
2019-12-20 20:22:13 +00:00
|
|
|
_, err := a.c.baseOcsRequest(http.MethodDelete, routes.appsConfig, nil, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Get returns all apps AppConfigDetails
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) Get() (map[string]map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-21 12:53:12 +00:00
|
|
|
appsIDs, err := a.List()
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(appsIDs))
|
|
|
|
for i := range appsIDs {
|
|
|
|
go func(id string) {
|
|
|
|
defer wg.Done()
|
2018-10-21 12:53:12 +00:00
|
|
|
d, err := a.Details(id)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[id] = d
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(appsIDs[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Details returns all the config's key, values pair of the app
|
2019-07-22 14:17:11 +00:00
|
|
|
func (a *appsConfig) Details(appID string) (map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-16 12:38:32 +00:00
|
|
|
var err error
|
|
|
|
var ks []string
|
2018-10-21 12:53:12 +00:00
|
|
|
ks, err = a.Keys(appID)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(ks))
|
|
|
|
for i := range ks {
|
|
|
|
go func(key string) {
|
|
|
|
defer wg.Done()
|
2018-10-21 12:53:12 +00:00
|
|
|
v, err := a.Value(appID, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[key] = v
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(ks[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|