|
| 1 | +package iam |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + iamv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/iam/v1" |
| 10 | + |
| 11 | + "github.com/qdrant/qcloud-cli/internal/cmd/output" |
| 12 | + "github.com/qdrant/qcloud-cli/internal/cmd/util" |
| 13 | + "github.com/qdrant/qcloud-cli/internal/qcloudapi" |
| 14 | + "github.com/qdrant/qcloud-cli/internal/state" |
| 15 | +) |
| 16 | + |
| 17 | +// resolveUser looks up a user by UUID or email from the account's user list. |
| 18 | +func resolveUser(cmd *cobra.Command, client *qcloudapi.Client, accountID, idOrEmail string) (*iamv1.User, error) { |
| 19 | + ctx := cmd.Context() |
| 20 | + resp, err := client.IAM().ListUsers(ctx, &iamv1.ListUsersRequest{AccountId: accountID}) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("failed to list users: %w", err) |
| 23 | + } |
| 24 | + for _, u := range resp.GetItems() { |
| 25 | + if util.IsUUID(idOrEmail) { |
| 26 | + if u.GetId() == idOrEmail { |
| 27 | + return u, nil |
| 28 | + } |
| 29 | + } else { |
| 30 | + if u.GetEmail() == idOrEmail { |
| 31 | + return u, nil |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return nil, fmt.Errorf("user %s not found", idOrEmail) |
| 36 | +} |
| 37 | + |
| 38 | +// resolveRoleIDs converts a slice of role names or UUIDs to UUIDs. |
| 39 | +// Values that already look like UUIDs are passed through unchanged. |
| 40 | +// Non-UUID values are resolved by name via ListRoles. |
| 41 | +func resolveRoleIDs(ctx context.Context, client *qcloudapi.Client, accountID string, namesOrIDs []string) ([]string, error) { |
| 42 | + if len(namesOrIDs) == 0 { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + |
| 46 | + // Check whether any name resolution is needed. |
| 47 | + var needsLookup bool |
| 48 | + for _, v := range namesOrIDs { |
| 49 | + if !util.IsUUID(v) { |
| 50 | + needsLookup = true |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + var rolesByName map[string]string |
| 56 | + if needsLookup { |
| 57 | + resp, err := client.IAM().ListRoles(ctx, &iamv1.ListRolesRequest{AccountId: accountID}) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed to list roles: %w", err) |
| 60 | + } |
| 61 | + rolesByName = make(map[string]string, len(resp.GetItems())) |
| 62 | + for _, r := range resp.GetItems() { |
| 63 | + rolesByName[r.GetName()] = r.GetId() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + ids := make([]string, 0, len(namesOrIDs)) |
| 68 | + for _, v := range namesOrIDs { |
| 69 | + if util.IsUUID(v) { |
| 70 | + ids = append(ids, v) |
| 71 | + } else { |
| 72 | + id, ok := rolesByName[v] |
| 73 | + if !ok { |
| 74 | + return nil, fmt.Errorf("role %q not found", v) |
| 75 | + } |
| 76 | + ids = append(ids, id) |
| 77 | + } |
| 78 | + } |
| 79 | + return ids, nil |
| 80 | +} |
| 81 | + |
| 82 | +// modifyUserRoles calls AssignUserRoles with the given add/delete IDs, then |
| 83 | +// fetches and prints the resulting role list. |
| 84 | +func modifyUserRoles(s *state.State, cmd *cobra.Command, client *qcloudapi.Client, accountID string, user *iamv1.User, addIDs, removeIDs []string) error { |
| 85 | + ctx := cmd.Context() |
| 86 | + |
| 87 | + _, err := client.IAM().AssignUserRoles(ctx, &iamv1.AssignUserRolesRequest{ |
| 88 | + AccountId: accountID, |
| 89 | + UserId: user.GetId(), |
| 90 | + RoleIdsToAdd: addIDs, |
| 91 | + RoleIdsToDelete: removeIDs, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to modify roles: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + rolesResp, err := client.IAM().ListUserRoles(ctx, &iamv1.ListUserRolesRequest{ |
| 98 | + AccountId: accountID, |
| 99 | + UserId: user.GetId(), |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("failed to list user roles: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + if s.Config.JSONOutput() { |
| 106 | + return output.PrintJSON(cmd.OutOrStdout(), rolesResp) |
| 107 | + } |
| 108 | + |
| 109 | + w := cmd.OutOrStdout() |
| 110 | + fmt.Fprintf(w, "Roles for %s:\n", user.GetEmail()) |
| 111 | + printRoles(w, rolesResp.GetRoles()) |
| 112 | + return nil |
| 113 | +} |
0 commit comments