2017-02-17 19:37:00 +00:00
|
|
|
package enterprise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
2017-02-19 19:47:19 +00:00
|
|
|
// Add creates a new User in Influx Enterprise
|
2017-02-17 19:37:00 +00:00
|
|
|
func (c *Client) Add(ctx context.Context, u *chronograf.User) (*chronograf.User, error) {
|
|
|
|
if err := c.Ctrl.CreateUser(ctx, u.Name, u.Passwd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the User from Influx Enterprise
|
|
|
|
func (c *Client) Delete(ctx context.Context, u *chronograf.User) error {
|
|
|
|
return c.Ctrl.DeleteUser(ctx, u.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a user if name exists.
|
|
|
|
func (c *Client) Get(ctx context.Context, name string) (*chronograf.User, error) {
|
|
|
|
u, err := c.Ctrl.User(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &chronograf.User{
|
2017-02-17 22:35:56 +00:00
|
|
|
Name: u.Name,
|
|
|
|
Permissions: toChronograf(u.Permissions),
|
2017-02-17 19:37:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the user's permissions or roles
|
|
|
|
func (c *Client) Update(ctx context.Context, u *chronograf.User) error {
|
2017-02-17 22:35:56 +00:00
|
|
|
// Only allow one type of change at a time. If it is a password
|
|
|
|
// change then do it and return without any changes to permissions
|
|
|
|
if u.Passwd != "" {
|
|
|
|
return c.Ctrl.ChangePassword(ctx, u.Name, u.Passwd)
|
|
|
|
}
|
|
|
|
perms := toEnterprise(u.Permissions)
|
|
|
|
return c.Ctrl.SetUserPerms(ctx, u.Name, perms)
|
2017-02-17 19:37:00 +00:00
|
|
|
}
|
2017-02-17 21:13:51 +00:00
|
|
|
|
|
|
|
// All is all users in influx
|
|
|
|
func (c *Client) All(ctx context.Context) ([]chronograf.User, error) {
|
|
|
|
all, err := c.Ctrl.Users(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]chronograf.User, len(all.Users))
|
|
|
|
for i, user := range all.Users {
|
|
|
|
res[i] = chronograf.User{
|
2017-02-17 22:35:56 +00:00
|
|
|
Name: user.Name,
|
|
|
|
Permissions: toChronograf(user.Permissions),
|
2017-02-17 21:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2017-02-17 22:35:56 +00:00
|
|
|
|
|
|
|
func toEnterprise(perms chronograf.Permissions) Permissions {
|
|
|
|
res := Permissions{}
|
|
|
|
for _, perm := range perms {
|
|
|
|
if perm.Scope == chronograf.AllScope {
|
|
|
|
// Enterprise uses empty string as the key for all databases
|
|
|
|
res[""] = perm.Allowed
|
|
|
|
} else {
|
|
|
|
res[perm.Name] = perm.Allowed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func toChronograf(perms Permissions) chronograf.Permissions {
|
|
|
|
res := chronograf.Permissions{}
|
|
|
|
for db, perm := range perms {
|
|
|
|
// Enterprise uses empty string as the key for all databases
|
|
|
|
if db == "" {
|
|
|
|
res = append(res, chronograf.Permission{
|
|
|
|
Scope: chronograf.AllScope,
|
|
|
|
Allowed: perm,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res = append(res, chronograf.Permission{
|
|
|
|
Scope: chronograf.DBScope,
|
|
|
|
Name: db,
|
|
|
|
Allowed: perm,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|