|
| 1 | +package iam |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 8 | + iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +func iamPolicyCreateBuilder(c *core.Command) *core.Command { |
| 13 | + c.ArgSpecs.GetByName("rules.{index}.permission-set-names.{index}").AutoCompleteFunc = func(ctx context.Context, _ string, _ any) core.AutocompleteSuggestions { |
| 14 | + client := core.ExtractClient(ctx) |
| 15 | + api := iam.NewAPI(client) |
| 16 | + // TODO: store result in a CLI cache |
| 17 | + resp, err := api.ListPermissionSets(&iam.ListPermissionSetsRequest{ |
| 18 | + PageSize: scw.Uint32Ptr(100), |
| 19 | + }, scw.WithAllPages()) |
| 20 | + if err != nil { |
| 21 | + return nil |
| 22 | + } |
| 23 | + suggestions := core.AutocompleteSuggestions{} |
| 24 | + for _, ps := range resp.PermissionSets { |
| 25 | + suggestions = append(suggestions, ps.Name) |
| 26 | + } |
| 27 | + return suggestions |
| 28 | + } |
| 29 | + return c |
| 30 | +} |
| 31 | + |
| 32 | +type PolicyGetInterceptorResponse struct { |
| 33 | + *iam.Policy |
| 34 | + Rules []*iam.Rule |
| 35 | +} |
| 36 | + |
| 37 | +func iamPolicyGetBuilder(c *core.Command) *core.Command { |
| 38 | + c.View = &core.View{ |
| 39 | + Title: "Policy", |
| 40 | + Sections: []*core.ViewSection{ |
| 41 | + { |
| 42 | + Title: "Rules", |
| 43 | + FieldName: "Rules", |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { |
| 48 | + args := argsI.(*iam.GetPolicyRequest) |
| 49 | + api := iam.NewAPI(core.ExtractClient(ctx)) |
| 50 | + |
| 51 | + respI, err := runner(ctx, argsI) |
| 52 | + if err != nil { |
| 53 | + return respI, err |
| 54 | + } |
| 55 | + resp := &PolicyGetInterceptorResponse{ |
| 56 | + Policy: respI.(*iam.Policy), |
| 57 | + } |
| 58 | + |
| 59 | + rules, err := api.ListRules(&iam.ListRulesRequest{ |
| 60 | + PolicyID: args.PolicyID, |
| 61 | + }, scw.WithContext(ctx), scw.WithAllPages()) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to list rules for given policy: %w", err) |
| 64 | + } |
| 65 | + resp.Rules = rules.Rules |
| 66 | + |
| 67 | + return resp, nil |
| 68 | + }) |
| 69 | + |
| 70 | + return c |
| 71 | +} |
0 commit comments