|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver" |
| 10 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" |
| 11 | +) |
| 12 | + |
| 13 | +// ListCommand creates the 'object-storage user-policy list' command |
| 14 | +func ListCommand() commands.Command { |
| 15 | + return &listCommand{ |
| 16 | + BaseCommand: commands.New( |
| 17 | + "list", |
| 18 | + "List policies attached to a user in managed object storage service", |
| 19 | + "upctl object-storage user-policy list <service-uuid> --username myuser", |
| 20 | + "upctl object-storage user-policy list my-service --username myuser", |
| 21 | + ), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type listCommand struct { |
| 26 | + *commands.BaseCommand |
| 27 | + completion.ObjectStorage |
| 28 | + resolver.CachingObjectStorage |
| 29 | + params request.GetManagedObjectStorageUserPoliciesRequest |
| 30 | +} |
| 31 | + |
| 32 | +// InitCommand implements Command.InitCommand |
| 33 | +func (s *listCommand) InitCommand() { |
| 34 | + fs := s.Cobra().Flags() |
| 35 | + fs.StringVar(&s.params.Username, "username", "", "The username to list policies for.") |
| 36 | + commands.Must(s.Cobra().MarkFlagRequired("username")) |
| 37 | +} |
| 38 | + |
| 39 | +// Execute implements Command.Execute |
| 40 | +func (s *listCommand) Execute(exec commands.Executor, serviceUUID string) (output.Output, error) { |
| 41 | + s.params.ServiceUUID = serviceUUID |
| 42 | + svc := exec.All() |
| 43 | + |
| 44 | + msg := fmt.Sprintf("Listing policies for user %s in service %s", s.params.Username, serviceUUID) |
| 45 | + exec.PushProgressStarted(msg) |
| 46 | + |
| 47 | + res, err := svc.GetManagedObjectStorageUserPolicies(exec.Context(), &s.params) |
| 48 | + if err != nil { |
| 49 | + return commands.HandleError(exec, msg, err) |
| 50 | + } |
| 51 | + |
| 52 | + exec.PushProgressSuccess(msg) |
| 53 | + |
| 54 | + rows := []output.TableRow{} |
| 55 | + for _, policy := range res { |
| 56 | + rows = append(rows, output.TableRow{ |
| 57 | + policy.Name, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + return output.MarshaledWithHumanOutput{ |
| 62 | + Value: res, |
| 63 | + Output: output.Table{ |
| 64 | + Columns: []output.TableColumn{ |
| 65 | + {Key: "name", Header: "Policy Name"}, |
| 66 | + }, |
| 67 | + Rows: rows, |
| 68 | + }, |
| 69 | + }, nil |
| 70 | +} |
0 commit comments