-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Validate spending limits in UpdateCostCenter #13798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
47784ab
to
5beb613
Compare
5beb613
to
cc16570
Compare
cc16570
to
f253dc9
Compare
@@ -102,57 +105,112 @@ func getCostCenter(ctx context.Context, conn *gorm.DB, attributionId Attribution | |||
return costCenter, nil | |||
} | |||
|
|||
func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, costCenter CostCenter) (CostCenter, error) { | |||
func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCenter) (CostCenter, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The multi-level branching here could be simplified to shorten the code, but it's deliberately done this way for readability (at the cost of "duplicating" the handling of Other -> Stripe. I feel this is acceptable, as we win more points on understanding the flows.
f253dc9
to
27b7dfe
Compare
started the job as gitpod-build-mp-usage-cost-center-validations.5 because the annotations in the pull request description changed |
27b7dfe
to
1221e08
Compare
started the job as gitpod-build-mp-usage-cost-center-validations.7 because the annotations in the pull request description changed |
Starting to review... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! The code looks good to me, modulo a few optional nits.
I will also test this code by rebasing my PR on top of these changes (thus making the PRs dependent) to verify that the entire test flow I described now passes. 🤞
ForUsers: 500, | ||
MinForIndividualsOnStripe: 1000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Slight cognitive dissonance between "Users" above and "Individuals" below (they're actually the same thing). Maybe the new field should be called MinForUsersOnStripe
for consistency?
ForUsers: 500, | |
MinForIndividualsOnStripe: 1000, | |
ForUsers: 500, | |
MinForUsersOnStripe: 1000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code itself LGTM and tests run.
/hold Did not test, though, as Jan is doing that in combination with the UI branch here.
|
||
// Downgrading from stripe | ||
if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other { | ||
newCC.SpendingLimit = c.cfg.ForUsers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this flow still doesn't seem to work properly 🤔
- Create a team called "Gitpod [Something]"
- Go to
/billing
(you have 500 credits 👍)
(view DB state)
mysql> select * from d_b_cost_center where id = 'user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7';
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| id | spendingLimit | deleted | _lastModified | creationTime | billingStrategy | nextBillingTime |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 500 | 0 | 2022-10-13 08:59:20.491162 | 2022-10-13T08:59:20.488Z | other | 2022-11-13T08:59:20.489Z |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
1 row in set (0.01 sec)
Upgrade by adding a payment method, then refresh (limit is 100 ? 😳 Should be 1000)EDIT: Actually, I'm not sure where the 100 comes from, but in any case the upgrade should fail if the provided limit is < 1000 right?
(view DB state)
mysql> select * from d_b_cost_center where id = 'user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7';
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| id | spendingLimit | deleted | _lastModified | creationTime | billingStrategy | nextBillingTime |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 500 | 0 | 2022-10-13 08:59:20.491162 | 2022-10-13T08:59:20.488Z | other | 2022-11-13T08:59:20.489Z |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 100 | 0 | 2022-10-13 09:01:33.580476 | 2022-10-13T09:01:33.576Z | stripe | |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
2 rows in set (0.02 sec)
- Try to set the limit to 150 -- it should fail because that's < 1000, but actually it works 😳
(view DB state)
mysql> select * from d_b_cost_center where id = 'user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7';
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| id | spendingLimit | deleted | _lastModified | creationTime | billingStrategy | nextBillingTime |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 500 | 0 | 2022-10-13 08:59:20.491162 | 2022-10-13T08:59:20.488Z | other | 2022-11-13T08:59:20.489Z |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 100 | 0 | 2022-10-13 09:01:33.580476 | 2022-10-13T09:01:33.576Z | stripe | |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 100 | 0 | 2022-10-13 09:03:49.189822 | 2022-10-13T09:03:49.186Z | stripe | |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 150 | 0 | 2022-10-13 09:03:53.190384 | 2022-10-13T09:03:53.187Z | stripe | |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
4 rows in set (0.02 sec)
- Manage plan, then cancel your Stripe subscription -- the limit should reset to 500, but instead it remains at its previous value 🐛
(view DB state)
mysql> select * from d_b_cost_center where id = 'user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7';
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| id | spendingLimit | deleted | _lastModified | creationTime | billingStrategy | nextBillingTime |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 500 | 0 | 2022-10-13 08:59:20.491162 | 2022-10-13T08:59:20.488Z | other | 2022-11-13T08:59:20.489Z |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 100 | 0 | 2022-10-13 09:01:33.580476 | 2022-10-13T09:01:33.576Z | stripe | |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 100 | 0 | 2022-10-13 09:03:49.189822 | 2022-10-13T09:03:49.186Z | stripe | |
| user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 150 | 0 | 2022-10-13 09:03:53.190384 | 2022-10-13T09:03:53.187Z | stripe | |
+-------------------------------------------+---------------+---------+----------------------------+--------------------------+-----------------+--------------------------+
4 rows in set (0.01 sec)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this rolls out, it will be 0 (due to missing config) but because we set it on the server side, this is okay and the check here will simply check if new limit is > 0. We'll add the config to ensure it does not prevent setting lower in subsequent PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, so it's even 0 in preview environments 👌 thanks for clarifying. That explains the allowed < 1000 custom limit.
Still, this doesn't explain why, upon cancellation, the user cost center does not go back to other
and doesn't reset its usage limit to 500
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Manage plan, then cancel your Stripe subscription -- the limit should reset to 500, but instead it remains at its previous value 🐛
That's because the request to the usage
component didn't set the billing strategy to Other
user:f0f6f6f5-f8e4-47e4-80f9-420f8cbcbfb7 | 150 | 0 | 2022-10-13 09:03:53.190384 | 2022-10-13T09:03:53.187Z | stripe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But shouldn't that happen automatically when Stripe calls Gitpod to tell it that a subscription was cancelled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting the config (now done in preview also) addresses 3. not failing.
The default 100 comes from protected defaultSpendingLimit = 100;
defined in gitpod-server-impl.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code also looks good to me and works as advertised.
Also, the error of cost centers not resetting seems somewhat unrelated to this PR (i.e. it's probably a misconfigured webhook in preview environments), so maybe let's investigate/fix this in a follow-up issue.
Please take a look at my comments when you get a chance, and decide whether to address or ignore them. 🌴
daba166
to
4ebbe89
Compare
4ebbe89
to
304c8dc
Compare
Applied suggested changes. I've also added a default config into preview which sets the MinForStripeUsers to 1k |
/unhold |
Description
Enforces usage limit values for users & teams.
Adds a new piece of config for the default value of UsersOnStripe. When this rolls out, it will be 0 (due to missing config) but because we set it on the
server
side, this is okay and the check here will simply check if new limit is > 0. We'll add the config to ensure it does not prevent setting lower in subsequent PR.Related Issue(s)
Part of #13389
How to test
Release Notes
Documentation
Werft options:
If enabled this will build
install/preview
Valid options are
all
,workspace
,webapp
,ide