Skip to content

Commit 849170f

Browse files
easyCZroboquat
authored andcommitted
[usage] Return CostCenter from SetCostCenter udpates - implementation
1 parent 922024d commit 849170f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

components/usage/pkg/apiv1/usage.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,19 @@ func (s *UsageService) GetCostCenter(ctx context.Context, in *v1.GetCostCenterRe
185185
return nil, err
186186
}
187187
return &v1.GetCostCenterResponse{
188-
CostCenter: &v1.CostCenter{
189-
AttributionId: string(result.ID),
190-
SpendingLimit: result.SpendingLimit,
191-
BillingStrategy: convertBillingStrategyToAPI(result.BillingStrategy),
192-
NextBillingTime: timestamppb.New(result.NextBillingTime.Time()),
193-
},
188+
CostCenter: dbCostCenterToAPI(result),
194189
}, nil
195190
}
196191

192+
func dbCostCenterToAPI(c db.CostCenter) *v1.CostCenter {
193+
return &v1.CostCenter{
194+
AttributionId: string(c.ID),
195+
SpendingLimit: c.SpendingLimit,
196+
BillingStrategy: convertBillingStrategyToAPI(c.BillingStrategy),
197+
NextBillingTime: timestamppb.New(c.NextBillingTime.Time()),
198+
}
199+
}
200+
197201
func convertBillingStrategyToDB(in v1.CostCenter_BillingStrategy) db.BillingStrategy {
198202
if in == v1.CostCenter_BILLING_STRATEGY_STRIPE {
199203
return db.CostCenter_Stripe
@@ -223,11 +227,13 @@ func (s *UsageService) SetCostCenter(ctx context.Context, in *v1.SetCostCenterRe
223227
SpendingLimit: in.CostCenter.SpendingLimit,
224228
BillingStrategy: convertBillingStrategyToDB(in.CostCenter.BillingStrategy),
225229
}
226-
_, err = s.costCenterManager.UpdateCostCenter(ctx, costCenter)
230+
result, err := s.costCenterManager.UpdateCostCenter(ctx, costCenter)
227231
if err != nil {
228232
return nil, err
229233
}
230-
return &v1.SetCostCenterResponse{}, nil
234+
return &v1.SetCostCenterResponse{
235+
CostCenter: dbCostCenterToAPI(result),
236+
}, nil
231237
}
232238

233239
func (s *UsageService) ReconcileUsage(ctx context.Context, req *v1.ReconcileUsageRequest) (*v1.ReconcileUsageResponse, error) {

components/usage/pkg/apiv1/usage_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,10 @@ func TestGetAndSetCostCenter(t *testing.T) {
251251
service := newUsageService(t, conn)
252252

253253
for _, costCenter := range costCenterUpdates {
254-
_, err := service.SetCostCenter(context.Background(), &v1.SetCostCenterRequest{
254+
retrieved, err := service.SetCostCenter(context.Background(), &v1.SetCostCenterRequest{
255255
CostCenter: costCenter,
256256
})
257257
require.NoError(t, err)
258-
retrieved, err := service.GetCostCenter(context.Background(), &v1.GetCostCenterRequest{
259-
AttributionId: costCenter.AttributionId,
260-
})
261-
require.NoError(t, err)
262258

263259
require.Equal(t, costCenter.SpendingLimit, retrieved.CostCenter.SpendingLimit)
264260
require.Equal(t, costCenter.BillingStrategy, retrieved.CostCenter.BillingStrategy)

0 commit comments

Comments
 (0)