|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 10 | +) |
| 11 | + |
| 12 | +// The lookup doesn't rely on the name filter on GET /transformations being an |
| 13 | +// exact match. It compares names exactly and, in case the filter matches more |
| 14 | +// than one page, asks for the largest page the API allows and follows |
| 15 | +// pagination.next, up to a bound. |
| 16 | +const ( |
| 17 | + transformationLookupPageLimit = 255 |
| 18 | + transformationLookupMaxPages = 20 |
| 19 | +) |
| 20 | + |
| 21 | +// transformationLookup is the subset of the API client used to resolve |
| 22 | +// --rule-transform-name. |
| 23 | +type transformationLookup interface { |
| 24 | + GetTransformation(ctx context.Context, id string) (*hookdeck.Transformation, error) |
| 25 | + ListTransformations(ctx context.Context, params map[string]string) (*hookdeck.TransformationListResponse, error) |
| 26 | +} |
| 27 | + |
| 28 | +// findTransformationID returns the ID of the transformation whose ID or exact |
| 29 | +// name is nameOrID, or "" when there is none. |
| 30 | +func findTransformationID(ctx context.Context, client transformationLookup, nameOrID string) (string, error) { |
| 31 | + if strings.HasPrefix(nameOrID, "trs_") { |
| 32 | + trn, err := client.GetTransformation(ctx, nameOrID) |
| 33 | + if err == nil { |
| 34 | + return trn.ID, nil |
| 35 | + } |
| 36 | + if !hookdeck.IsNotFoundError(err) { |
| 37 | + return "", fmt.Errorf("failed to look up transformation '%s': %w", nameOrID, err) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + params := map[string]string{ |
| 42 | + "name": nameOrID, |
| 43 | + "limit": strconv.Itoa(transformationLookupPageLimit), |
| 44 | + } |
| 45 | + for page := 0; page < transformationLookupMaxPages; page++ { |
| 46 | + result, err := client.ListTransformations(ctx, params) |
| 47 | + if err != nil { |
| 48 | + return "", fmt.Errorf("failed to look up transformation '%s': %w", nameOrID, err) |
| 49 | + } |
| 50 | + for _, trn := range result.Models { |
| 51 | + if trn.Name == nameOrID { |
| 52 | + return trn.ID, nil |
| 53 | + } |
| 54 | + } |
| 55 | + next := result.Pagination.Next |
| 56 | + if next == nil || *next == "" { |
| 57 | + return "", nil |
| 58 | + } |
| 59 | + params["next"] = *next |
| 60 | + } |
| 61 | + return "", fmt.Errorf("failed to look up transformation '%s': more than %d transformations match the name filter; pass its ID instead", |
| 62 | + nameOrID, transformationLookupPageLimit*transformationLookupMaxPages) |
| 63 | +} |
| 64 | + |
| 65 | +// resolveRuleTransformation makes --rule-transform-name accept a transformation |
| 66 | +// name or ID, as its help text says. |
| 67 | +// |
| 68 | +// The API matches a transform rule's transformation by name and creates one when |
| 69 | +// the name is new, even without code. Sent as a name, an ID or a mistyped name |
| 70 | +// therefore created an empty transformation that failed every event. So the value |
| 71 | +// is resolved first: |
| 72 | +// - found: the rule references it by transformation_id; --rule-transform-code and |
| 73 | +// --rule-transform-env, if given, update that transformation |
| 74 | +// - not found, with --rule-transform-code: a new transformation is created with |
| 75 | +// that name, as before (not allowed for an ID) |
| 76 | +// - not found, without --rule-transform-code: an error |
| 77 | +// |
| 78 | +// Rules from --rules or --rules-file are sent as given. |
| 79 | +func resolveRuleTransformation(ctx context.Context, client transformationLookup, f *connectionRuleFlags, rules []hookdeck.Rule) error { |
| 80 | + if f.Rules != "" || f.RulesFile != "" { |
| 81 | + return nil |
| 82 | + } |
| 83 | + if f.RuleTransformName == "" { |
| 84 | + if f.RuleTransformCode != "" || f.RuleTransformEnv != "" { |
| 85 | + return fmt.Errorf("--rule-transform-name is required when using transform rule flags") |
| 86 | + } |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + idx := -1 |
| 91 | + for i, r := range rules { |
| 92 | + if r["type"] == "transform" { |
| 93 | + idx = i |
| 94 | + break |
| 95 | + } |
| 96 | + } |
| 97 | + if idx == -1 { |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + nameOrID := f.RuleTransformName |
| 102 | + id, err := findTransformationID(ctx, client, nameOrID) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if id == "" { |
| 108 | + if strings.HasPrefix(nameOrID, "trs_") { |
| 109 | + return fmt.Errorf("transformation '%s' not found. Check the ID with 'hookdeck gateway transformation list'", nameOrID) |
| 110 | + } |
| 111 | + if f.RuleTransformCode == "" { |
| 112 | + return fmt.Errorf("transformation '%s' not found. Check the name with 'hookdeck gateway transformation list', or pass --rule-transform-code to create a new transformation with this name", nameOrID) |
| 113 | + } |
| 114 | + // New transformation: keep name and code so the API creates it |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + rule := hookdeck.Rule{"type": "transform", "transformation_id": id} |
| 119 | + if existing, ok := rules[idx]["transformation"].(map[string]interface{}); ok { |
| 120 | + update := make(map[string]interface{}) |
| 121 | + for _, key := range []string{"code", "env"} { |
| 122 | + if v, ok := existing[key]; ok { |
| 123 | + update[key] = v |
| 124 | + } |
| 125 | + } |
| 126 | + if len(update) > 0 { |
| 127 | + rule["transformation"] = update |
| 128 | + } |
| 129 | + } |
| 130 | + rules[idx] = rule |
| 131 | + return nil |
| 132 | +} |
0 commit comments