influxdb/chronograf/enterprise/roles.go

114 lines
2.8 KiB
Go
Raw Normal View History

2017-02-22 15:29:39 +00:00
package enterprise
2017-02-23 22:02:53 +00:00
import (
"context"
"github.com/influxdata/influxdb/v2/chronograf"
2017-02-23 22:02:53 +00:00
)
// RolesStore uses a control client operate on Influx Enterprise roles. Roles are
// groups of permissions applied to groups of users
type RolesStore struct {
Ctrl
Logger chronograf.Logger
}
2017-02-22 15:29:39 +00:00
// Add creates a new Role in Influx Enterprise
2017-02-23 22:02:53 +00:00
// This must be done in three smaller steps: creating, setting permissions, setting users.
func (c *RolesStore) Add(ctx context.Context, u *chronograf.Role) (*chronograf.Role, error) {
if err := c.Ctrl.CreateRole(ctx, u.Name); err != nil {
return nil, err
}
if err := c.Ctrl.SetRolePerms(ctx, u.Name, ToEnterprise(u.Permissions)); err != nil {
return nil, err
}
users := make([]string, len(u.Users))
for i, u := range u.Users {
users[i] = u.Name
}
if err := c.Ctrl.SetRoleUsers(ctx, u.Name, users); err != nil {
2017-02-22 15:29:39 +00:00
return nil, err
}
return u, nil
}
// Delete the Role from Influx Enterprise
2017-02-23 22:02:53 +00:00
func (c *RolesStore) Delete(ctx context.Context, u *chronograf.Role) error {
2017-02-22 15:29:39 +00:00
return c.Ctrl.DeleteRole(ctx, u.Name)
}
// Get retrieves a Role if name exists.
2017-02-23 22:02:53 +00:00
func (c *RolesStore) Get(ctx context.Context, name string) (*chronograf.Role, error) {
role, err := c.Ctrl.Role(ctx, name)
2017-02-22 15:29:39 +00:00
if err != nil {
return nil, err
}
2017-02-23 22:02:53 +00:00
// Hydrate all the users to gather their permissions and their roles.
users := make([]chronograf.User, len(role.Users))
for i, u := range role.Users {
user, err := c.Ctrl.User(ctx, u)
if err != nil {
return nil, err
}
users[i] = chronograf.User{
Name: user.Name,
Permissions: ToChronograf(user.Permissions),
}
}
2017-02-22 15:29:39 +00:00
return &chronograf.Role{
2017-02-23 22:02:53 +00:00
Name: role.Name,
Permissions: ToChronograf(role.Permissions),
Users: users,
2017-02-22 15:29:39 +00:00
}, nil
}
2017-02-23 22:02:53 +00:00
// Update the Role's permissions and roles
func (c *RolesStore) Update(ctx context.Context, u *chronograf.Role) error {
if u.Permissions != nil {
perms := ToEnterprise(u.Permissions)
if err := c.Ctrl.SetRolePerms(ctx, u.Name, perms); err != nil {
return err
}
2017-02-23 22:02:53 +00:00
}
if u.Users != nil {
users := make([]string, len(u.Users))
for i, u := range u.Users {
users[i] = u.Name
}
return c.Ctrl.SetRoleUsers(ctx, u.Name, users)
2017-02-23 22:02:53 +00:00
}
return nil
2017-02-22 15:29:39 +00:00
}
// All is all Roles in influx
2017-02-23 22:02:53 +00:00
func (c *RolesStore) All(ctx context.Context) ([]chronograf.Role, error) {
2017-02-22 15:29:39 +00:00
all, err := c.Ctrl.Roles(ctx, nil)
if err != nil {
return nil, err
}
return all.ToChronograf(), nil
}
2017-02-23 22:02:53 +00:00
// ToChronograf converts enterprise roles to chronograf
func (r *Roles) ToChronograf() []chronograf.Role {
res := make([]chronograf.Role, len(r.Roles))
for i, role := range r.Roles {
2017-02-23 22:02:53 +00:00
users := make([]chronograf.User, len(role.Users))
for i, user := range role.Users {
users[i] = chronograf.User{
Name: user,
}
}
2017-02-22 15:29:39 +00:00
res[i] = chronograf.Role{
2017-02-23 22:02:53 +00:00
Name: role.Name,
Permissions: ToChronograf(role.Permissions),
Users: users,
2017-02-22 15:29:39 +00:00
}
}
return res
2017-02-23 22:02:53 +00:00
}