Repository navigation
fix: validate features before publishing plans/add-ons - #3661
Conversation
📝 WalkthroughWalkthroughAdds a namespaced feature resolver and feature-aware validators for rate cards, plans, and add‑ons. Services publishing plans/add‑ons now resolve referenced features (by id/key) and aggregate validation issues including new errors for feature not found, archived, or id/key mismatch. Changes
Sequence Diagram(s)sequenceDiagram
participant Service as PublishPlan/PublishAddon
participant Validator as ValidatePlan/AddonWithFeatures
participant RCValidator as ValidateRateCardsWithFeatures
participant Resolver as NamespacedFeatureResolver
participant FeatureSvc as FeatureConnector (ListFeatures)
Service->>Validator: Validate with NamespacedFeatureResolver(namespace)
Validator->>RCValidator: Validate rate cards (per phase/plan/add-on)
RCValidator->>Resolver: Resolve feature by id/key
Resolver->>FeatureSvc: ListFeatures(namespace, ids/keys, page=1,size=100)
FeatureSvc-->>Resolver: feature(s) or none
alt single matching feature
Resolver-->>RCValidator: feature object
RCValidator-->>Validator: ok or archived/mismatch error
else no feature
Resolver-->>RCValidator: not-found error
RCValidator-->>Validator: error
end
Validator-->>Service: aggregated errors (joined) or success
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (3)
🧰 Additional context used📓 Path-based instructions (1)**/*.go⚙️ CodeRabbit configuration file
Files:
🧠 Learnings (1)📚 Learning: 2025-04-21T08:32:31.689ZApplied to files:
🧬 Code graph analysis (4)openmeter/productcatalog/ratecard.go (5)
openmeter/productcatalog/addon/service/addon.go (4)
openmeter/productcatalog/errors.go (3)
openmeter/productcatalog/addon.go (6)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
🔇 Additional comments (7)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (2)
openmeter/productcatalog/ratecard.go (1)
847-848: Consider using the type alias for consistency.The return type
func(cards RateCards) erroris functionally equivalent tomodels.ValidatorFunc[RateCards], but using the type alias would be more consistent with other validators in this file (likeValidateRateCards()at line 605).-func ValidateRateCardsWithFeatures(ctx context.Context, resolver NamespacedFeatureResolver) func(cards RateCards) error { +func ValidateRateCardsWithFeatures(ctx context.Context, resolver NamespacedFeatureResolver) models.ValidatorFunc[RateCards] {openmeter/productcatalog/addon/service/addon.go (1)
526-527: Comment also mentions "plan" instead of "add-on".While you're at it, the comment on line 526 says "Validate plan with features" but should say "Validate add-on with features".
- // Validate plan with features + // Validate add-on with features resolver := productcatalog.NewNamespacedFeatureResolver(s.feature, params.Namespace)
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
openmeter/productcatalog/addon.go(2 hunks)openmeter/productcatalog/addon/service/addon.go(2 hunks)openmeter/productcatalog/errors.go(1 hunks)openmeter/productcatalog/featureresolver.go(1 hunks)openmeter/productcatalog/plan.go(2 hunks)openmeter/productcatalog/plan/service/plan.go(1 hunks)openmeter/productcatalog/ratecard.go(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
⚙️ CodeRabbit configuration file
**/*.go: In general when reviewing the Golang code make readability and maintainability a priority, even potentially suggest restructuring the code to improve them.Performance should be a priority in critical code paths. Anything related to event ingestion, message processing, database operations (regardless of database) should be vetted for potential performance bottlenecks.
Files:
openmeter/productcatalog/ratecard.goopenmeter/productcatalog/plan/service/plan.goopenmeter/productcatalog/plan.goopenmeter/productcatalog/addon/service/addon.goopenmeter/productcatalog/featureresolver.goopenmeter/productcatalog/addon.goopenmeter/productcatalog/errors.go
🧠 Learnings (1)
📚 Learning: 2025-04-21T08:32:31.689Z
Learnt from: chrisgacsal
Repo: openmeterio/openmeter PR: 2699
File: openmeter/productcatalog/planaddon/service/service_test.go:210-211
Timestamp: 2025-04-21T08:32:31.689Z
Learning: In `productcatalog.UsageBasedRateCard`, the `BillingCadence` field is a non-pointer `isodate.Period`, while in `productcatalog.FlatFeeRateCard`, `BillingCadence` is a pointer type (`*isodate.Period`). This means `MonthPeriod` should be used directly for `UsageBasedRateCard` (not `&MonthPeriod`).
Applied to files:
openmeter/productcatalog/ratecard.goopenmeter/productcatalog/errors.go
🧬 Code graph analysis (6)
openmeter/productcatalog/plan/service/plan.go (3)
openmeter/productcatalog/featureresolver.go (1)
NewNamespacedFeatureResolver(18-25)app/common/namespace.go (1)
Namespace(13-15)openmeter/productcatalog/plan.go (1)
ValidatePlanWithFeatures(333-352)
openmeter/productcatalog/plan.go (6)
openmeter/productcatalog/featureresolver.go (1)
NamespacedFeatureResolver(13-16)pkg/models/validator.go (1)
ValidatorFunc(10-10)openmeter/productcatalog/plan/plan.go (1)
Plan(18-30)pkg/models/fielddescriptor.go (2)
NewFieldSelectorGroup(220-236)NewFieldSelector(213-218)openmeter/productcatalog/ratecard.go (2)
ValidateRateCardsWithFeatures(847-882)RateCards(519-519)pkg/models/errors.go (1)
ErrorWithFieldPrefix(340-346)
openmeter/productcatalog/addon/service/addon.go (4)
openmeter/productcatalog/featureresolver.go (1)
NewNamespacedFeatureResolver(18-25)app/common/namespace.go (1)
Namespace(13-15)openmeter/productcatalog/addon.go (1)
ValidateAddonWithFeatures(322-326)pkg/models/errors.go (1)
NewGenericValidationError(138-140)
openmeter/productcatalog/featureresolver.go (4)
openmeter/productcatalog/feature/connector.go (1)
FeatureConnector(29-36)app/common/namespace.go (1)
Namespace(13-15)pkg/pagination/page.go (1)
Page(14-17)pkg/models/errors.go (2)
NewGenericNotFoundError(38-40)NewGenericConflictError(68-70)
openmeter/productcatalog/addon.go (4)
openmeter/productcatalog/featureresolver.go (1)
NamespacedFeatureResolver(13-16)pkg/models/validator.go (1)
ValidatorFunc(10-10)openmeter/productcatalog/addon/addon.go (1)
Addon(15-27)openmeter/productcatalog/ratecard.go (2)
ValidateRateCardsWithFeatures(847-882)RateCards(519-519)
openmeter/productcatalog/errors.go (3)
pkg/models/errors.go (1)
ErrorCode(294-294)pkg/models/validationissue.go (3)
NewValidationIssue(273-283)WithFieldString(224-232)WithCriticalSeverity(240-242)pkg/framework/commonhttp/errors.go (1)
WithHTTPStatusCodeAttribute(82-84)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
- GitHub Check: Artifacts / Container image
- GitHub Check: Artifacts / Benthos Collector Container image
- GitHub Check: Lint
- GitHub Check: Migration Checks
- GitHub Check: Test
- GitHub Check: Code Generators
- GitHub Check: Build
- GitHub Check: Repository Scan
- GitHub Check: Analyze (go)
🔇 Additional comments (9)
openmeter/productcatalog/addon.go (1)
321-326: LGTM!Clean and straightforward delegation to
ValidateRateCardsWithFeatures. The pattern is consistent with the plan validation approach, keeping things nicely DRY.openmeter/productcatalog/plan/service/plan.go (1)
512-519: LGTM!The feature validation integrates nicely into the existing error aggregation flow. Error messages include enough context (id, key, version) for debugging, which is great.
openmeter/productcatalog/plan.go (1)
332-352: LGTM!Nice implementation! The phase field selector wrapping gives good context for debugging when validation fails. The iteration pattern and error aggregation are consistent with the existing codebase style.
openmeter/productcatalog/ratecard.go (1)
861-877: The nil pointer dereference concern is not valid.The
NamespacedFeatureResolver.Resolveimplementation (featureresolver.go:56-58) explicitly returns an error when bothidandkeyare nil or empty. This error is caught by the existing error handling logic and the loop continues beforefeat.ArchivedAtis accessed, preventing any nil dereference. No code changes are necessary.Likely an incorrect or invalid review comment.
openmeter/productcatalog/featureresolver.go (3)
13-16: Clean interface design!The interface is well-defined with a clear separation of concerns. The
WithNamespacemethod allows for flexible namespace switching while maintaining immutability of the resolver.
41-44: Good defensive nil check on the service.Nice catch upfront - returning an early, clear error if the connector wasn't wired properly is solid.
64-67: Hardcoded pagination may be limiting, but likely fine for this use case.PageSize of 100 for a lookup by specific ID/key should be more than sufficient. Just a heads-up in case the use case ever expands.
openmeter/productcatalog/errors.go (2)
104-132: New error definitions look good and follow existing patterns.The three new error codes are well-structured:
- Appropriate
CriticalSeverityfor validation issues blocking publish- Consistent use of
featureKeyfield- HTTP 400 status for client-side validation errors
- Clear, descriptive messages
These align nicely with the existing error conventions in this file.
114-122: No action needed—ErrRateCardFeatureArchived is properly used.The error is correctly applied in
ratecard.goat line 876 with dedicated validation logic that explicitly checks theArchivedAtfield:if feat.ArchivedAt != nil && clock.Now().UTC().After(feat.ArchivedAt.UTC()). This validation is separate from theNamespacedFeatureResolver.Resolvebehavior and appropriately uses the error when an archived feature is detected.
e48141e to
d016ae9
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
openmeter/productcatalog/addon.go (1)
321-326: Wire addon feature validation into rate‑card validator (plus doc‑comment nit)The
ValidateAddonWithFeatureshelper looks solid: closing overctxand theNamespacedFeatureResolverand delegating toValidateRateCardsWithFeatureskeeps behavior aligned with the rate‑card implementation and the new error codes.One small polish you might consider: add a GoDoc comment so this exported helper shows up clearly in docs alongside
ValidateAddonMeta,ValidateAddonRateCards, etc.Example:
- func ValidateAddonWithFeatures(ctx context.Context, resolver NamespacedFeatureResolver) models.ValidatorFunc[Addon] { +// ValidateAddonWithFeatures returns a validator that checks addon rate cards +// against features resolved via the provided NamespacedFeatureResolver. +func ValidateAddonWithFeatures(ctx context.Context, resolver NamespacedFeatureResolver) models.ValidatorFunc[Addon] {openmeter/productcatalog/featureresolver.go (1)
41-89: Resolve logic and archived handling look correct; consider slightly richer errorsThe overall flow here looks good to me:
- Guarding against a nil
FeatureConnectorand missingid/key.- Building
IDsOrKeysfrom non‑empty pointers.- Calling
ListFeatureswithIncludeArchived: trueso downstream can distinguish “not found” vs “archived”.- Mapping zero results to
GenericNotFoundand multi‑ID results toGenericConflict, which lines up with howValidateRateCardsWithFeaturesreacts.If you ever want easier debugging in logs, you could optionally include the requested id/key in the not‑found / conflict messages, e.g.:
- if len(features.Items) == 0 { - return nil, models.NewGenericNotFoundError(errors.New("feature")) - } + if len(features.Items) == 0 { + return nil, models.NewGenericNotFoundError( + fmt.Errorf("feature not found for id=%q key=%q", lo.FromPtr(id), lo.FromPtr(key)), + ) + }Same idea could apply to the conflict error. Not required for correctness, just a small quality‑of‑life tweak if you find yourself debugging these often.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
openmeter/productcatalog/addon.go(2 hunks)openmeter/productcatalog/addon/service/addon.go(2 hunks)openmeter/productcatalog/errors.go(1 hunks)openmeter/productcatalog/featureresolver.go(1 hunks)openmeter/productcatalog/plan.go(2 hunks)openmeter/productcatalog/plan/service/plan.go(1 hunks)openmeter/productcatalog/ratecard.go(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
- openmeter/productcatalog/plan.go
- openmeter/productcatalog/ratecard.go
- openmeter/productcatalog/plan/service/plan.go
- openmeter/productcatalog/addon/service/addon.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
⚙️ CodeRabbit configuration file
**/*.go: In general when reviewing the Golang code make readability and maintainability a priority, even potentially suggest restructuring the code to improve them.Performance should be a priority in critical code paths. Anything related to event ingestion, message processing, database operations (regardless of database) should be vetted for potential performance bottlenecks.
Files:
openmeter/productcatalog/addon.goopenmeter/productcatalog/featureresolver.goopenmeter/productcatalog/errors.go
🧠 Learnings (1)
📚 Learning: 2025-04-21T08:32:31.689Z
Learnt from: chrisgacsal
Repo: openmeterio/openmeter PR: 2699
File: openmeter/productcatalog/planaddon/service/service_test.go:210-211
Timestamp: 2025-04-21T08:32:31.689Z
Learning: In `productcatalog.UsageBasedRateCard`, the `BillingCadence` field is a non-pointer `isodate.Period`, while in `productcatalog.FlatFeeRateCard`, `BillingCadence` is a pointer type (`*isodate.Period`). This means `MonthPeriod` should be used directly for `UsageBasedRateCard` (not `&MonthPeriod`).
Applied to files:
openmeter/productcatalog/errors.go
🧬 Code graph analysis (2)
openmeter/productcatalog/addon.go (6)
openmeter/productcatalog/featureresolver.go (1)
NamespacedFeatureResolver(13-16)pkg/models/validator.go (1)
ValidatorFunc(10-10)openmeter/productcatalog/addon/addon.go (1)
Addon(15-27)openmeter/ent/schema/addon.go (5)
Addon(16-18)Addon(20-24)Addon(26-51)Addon(53-68)Addon(70-85)openmeter/productcatalog/ratecard.go (2)
ValidateRateCardsWithFeatures(847-882)RateCards(519-519)openmeter/productcatalog/addon/ratecard.go (1)
RateCards(152-152)
openmeter/productcatalog/featureresolver.go (4)
openmeter/productcatalog/feature/connector.go (1)
FeatureConnector(29-36)app/common/namespace.go (1)
Namespace(13-15)pkg/pagination/page.go (1)
Page(14-17)pkg/models/errors.go (2)
NewGenericNotFoundError(38-40)NewGenericConflictError(68-70)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
- GitHub Check: Artifacts / Benthos Collector Container image
- GitHub Check: Artifacts / Container image
- GitHub Check: Test
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Build
- GitHub Check: Code Generators
- GitHub Check: Repository Scan
- GitHub Check: Analyze (go)
🔇 Additional comments (3)
openmeter/productcatalog/addon.go (1)
3-15: Import change looks goodPulling in
contexthere is exactly what you need for the new feature‑aware validator and doesn’t introduce any side effects in this file.openmeter/productcatalog/featureresolver.go (1)
13-39: Nice, clean namespaced resolver abstractionThe
NamespacedFeatureResolverinterface plus thenamespacedFeatureResolverimplementation look straightforward and easy to use.WithNamespacereusing the same connector is a nice touch for keeping call‑sites tidy when you need to hop namespaces.openmeter/productcatalog/errors.go (1)
104-132: New rate‑card feature errors fit the existing error model nicelyThese three errors line up well with the new feature‑aware validation flow:
- Separate codes for not found, archived, and id/key mismatch.
- Consistent use of
featureKeyas the field, which will work well with the field selectors in the rate card validators.- Critical severity + 400 status matches the fact that these are hard blockers for publishing.
No issues from my side here.
d016ae9 to
b1d9fa7
Compare
Overview
Validate ratecard features before publishing plans/add-ons.
Summary by CodeRabbit
New Features
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.