|
| 1 | +package rdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +func userListBuilder(c *core.Command) *core.Command { |
| 12 | + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { |
| 13 | + type customUser struct { |
| 14 | + Name string `json:"name"` |
| 15 | + IsAdmin bool `json:"is_admin"` |
| 16 | + ReadOnly []string `json:"readonly"` |
| 17 | + ReadWrite []string `json:"readwrite"` |
| 18 | + All []string `json:"all"` |
| 19 | + Custom []string `json:"custom"` |
| 20 | + None []string `json:"none"` |
| 21 | + } |
| 22 | + |
| 23 | + resI, err := runner(ctx, argsI) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + index := map[string]*customUser{} |
| 29 | + var res []*customUser |
| 30 | + listUserRequest := argsI.(*rdb.ListUsersRequest) |
| 31 | + listUserResponse := resI.([]*rdb.User) |
| 32 | + for _, user := range listUserResponse { |
| 33 | + user := &customUser{ |
| 34 | + Name: user.Name, |
| 35 | + IsAdmin: user.IsAdmin, |
| 36 | + ReadOnly: []string{}, |
| 37 | + ReadWrite: []string{}, |
| 38 | + All: []string{}, |
| 39 | + Custom: []string{}, |
| 40 | + None: []string{}, |
| 41 | + } |
| 42 | + res = append(res, user) |
| 43 | + index[user.Name] = user |
| 44 | + } |
| 45 | + |
| 46 | + api := rdb.NewAPI(core.ExtractClient(ctx)) |
| 47 | + |
| 48 | + listPrivileges, err := api.ListPrivileges( |
| 49 | + &rdb.ListPrivilegesRequest{InstanceID: listUserRequest.InstanceID}, |
| 50 | + scw.WithAllPages(), |
| 51 | + ) |
| 52 | + if err != nil { |
| 53 | + return resI, err |
| 54 | + } |
| 55 | + |
| 56 | + for _, privilege := range listPrivileges.Privileges { |
| 57 | + switch privilege.Permission { |
| 58 | + case rdb.PermissionAll: |
| 59 | + index[privilege.UserName].All = append(index[privilege.UserName].All, privilege.DatabaseName) |
| 60 | + case rdb.PermissionReadonly: |
| 61 | + index[privilege.UserName].ReadOnly = append(index[privilege.UserName].ReadOnly, privilege.DatabaseName) |
| 62 | + case rdb.PermissionCustom: |
| 63 | + index[privilege.UserName].Custom = append(index[privilege.UserName].Custom, privilege.DatabaseName) |
| 64 | + case rdb.PermissionNone: |
| 65 | + index[privilege.UserName].None = append(index[privilege.UserName].None, privilege.DatabaseName) |
| 66 | + case rdb.PermissionReadwrite: |
| 67 | + index[privilege.UserName].ReadWrite = append(index[privilege.UserName].ReadWrite, privilege.DatabaseName) |
| 68 | + } |
| 69 | + } |
| 70 | + return res, nil |
| 71 | + } |
| 72 | + |
| 73 | + return c |
| 74 | +} |
0 commit comments