|
| 1 | +package edge_services |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "github.com/scaleway/scaleway-cli/v2/core" |
| 9 | + "github.com/scaleway/scaleway-cli/v2/internal/editor" |
| 10 | + edgeservices "github.com/scaleway/scaleway-sdk-go/api/edge_services/v1beta1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +var edgeServicesRouteRulesEditYamlExample = `route_rules: |
| 15 | +- rule_http_match: |
| 16 | + method_filters: |
| 17 | + - get |
| 18 | + - post |
| 19 | + path_filter: |
| 20 | + path_filter_type: regex |
| 21 | + value: ^/api/.* |
| 22 | + backend_stage_id: 11111111-1111-1111-1111-111111111111 |
| 23 | +- rule_http_match: |
| 24 | + method_filters: |
| 25 | + - get |
| 26 | + path_filter: |
| 27 | + path_filter_type: regex |
| 28 | + value: ^/static/.* |
| 29 | + backend_stage_id: 11111111-1111-1111-1111-111111111111 |
| 30 | +` |
| 31 | + |
| 32 | +var edgeServicesRouteRulesEditYamlExampleSimple = `route_rules: |
| 33 | +- rule_http_match: |
| 34 | + method_filters: |
| 35 | + - get |
| 36 | + - post |
| 37 | + path_filter: |
| 38 | + path_filter_type: regex |
| 39 | + value: ^/api/.* |
| 40 | +- rule_http_match: |
| 41 | + method_filters: |
| 42 | + - get |
| 43 | + path_filter: |
| 44 | + path_filter_type: regex |
| 45 | + value: ^/static/.* |
| 46 | +` |
| 47 | + |
| 48 | +type edgeServicesRouteRulesEditArgs struct { |
| 49 | + RouteStageID string |
| 50 | + BackendStageID *string |
| 51 | + Mode editor.MarshalMode |
| 52 | +} |
| 53 | + |
| 54 | +func edgeServicesRouteRulesEditCommand() *core.Command { |
| 55 | + return &core.Command{ |
| 56 | + Short: "Edit all route rules of a route stage", |
| 57 | + Long: `Edit all route rules of a route stage. |
| 58 | +
|
| 59 | +If backend-stage-id is provided, the editor will only show rule_http_match fields and the specified backend will be applied to all rules automatically. |
| 60 | +Otherwise, opens the editor with full rules including backend_stage_id for each rule.`, |
| 61 | + Namespace: "edge-services", |
| 62 | + Resource: "route-rules", |
| 63 | + Verb: "edit", |
| 64 | + ArgsType: reflect.TypeOf(edgeServicesRouteRulesEditArgs{}), |
| 65 | + ArgSpecs: core.ArgSpecs{ |
| 66 | + { |
| 67 | + Name: "route-stage-id", |
| 68 | + Short: "ID of the route stage to edit", |
| 69 | + Required: true, |
| 70 | + Positional: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: "backend-stage-id", |
| 74 | + Short: "ID of the backend stage to apply to all rules (simplifies editing when using a single backend)", |
| 75 | + Required: false, |
| 76 | + }, |
| 77 | + editor.MarshalModeArgSpec(), |
| 78 | + }, |
| 79 | + Run: func(ctx context.Context, argsI any) (i any, e error) { |
| 80 | + args := argsI.(*edgeServicesRouteRulesEditArgs) |
| 81 | + |
| 82 | + client := core.ExtractClient(ctx) |
| 83 | + api := edgeservices.NewAPI(client) |
| 84 | + |
| 85 | + rules, err := api.ListRouteRules(&edgeservices.ListRouteRulesRequest{ |
| 86 | + RouteStageID: args.RouteStageID, |
| 87 | + }, scw.WithContext(ctx)) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to list route rules: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + setRequest := &edgeservices.SetRouteRulesRequest{ |
| 93 | + RouteStageID: args.RouteStageID, |
| 94 | + } |
| 95 | + |
| 96 | + template := edgeServicesRouteRulesEditYamlExample |
| 97 | + var ignoreFields []string |
| 98 | + if args.BackendStageID != nil { |
| 99 | + template = edgeServicesRouteRulesEditYamlExampleSimple |
| 100 | + ignoreFields = []string{"backend_stage_id"} |
| 101 | + } |
| 102 | + |
| 103 | + editedSetRequest, err := editor.UpdateResourceEditor(rules, setRequest, &editor.Config{ |
| 104 | + PutRequest: true, |
| 105 | + MarshalMode: args.Mode, |
| 106 | + Template: template, |
| 107 | + IgnoreFields: ignoreFields, |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + setRequest = editedSetRequest.(*edgeservices.SetRouteRulesRequest) |
| 114 | + |
| 115 | + if args.BackendStageID != nil { |
| 116 | + for _, rule := range setRequest.RouteRules { |
| 117 | + rule.BackendStageID = args.BackendStageID |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + resp, err := api.SetRouteRules(setRequest, scw.WithContext(ctx)) |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("failed to set route rules: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + return resp.RouteRules, nil |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
0 commit comments