Skip to content

[UBP] Fix usage gRPC sugar.ts file #12843

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

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 46 additions & 37 deletions components/usage-api/typescript/src/usage/v1/sugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import { BillingServiceClient } from "./billing_grpc_pb";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import * as opentracing from "opentracing";
import { Metadata } from "@grpc/grpc-js";
import { CostCenter as ProtocolCostCenter, GetCostCenterRequest, GetCostCenterResponse, ListUsageRequest, ListUsageResponse, SetCostCenterRequest, SetCostCenterResponse } from "./usage_pb";
import {
CostCenter as ProtocolCostCenter,
GetCostCenterRequest,
GetCostCenterResponse,
ListUsageRequest,
ListUsageResponse,
SetCostCenterRequest,
SetCostCenterResponse,
} from "./usage_pb";
import {
GetUpcomingInvoiceRequest,
GetUpcomingInvoiceResponse,
Expand All @@ -22,7 +30,7 @@ import { createClientCallMetricsInterceptor, IClientCallMetrics } from "@gitpod/
import * as grpc from "@grpc/grpc-js";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { CostCenter } from "@gitpod/gitpod-protocol";
import { CostCenter, BillingStrategy } from "@gitpod/gitpod-protocol";

export const UsageServiceClientProvider = Symbol("UsageServiceClientProvider");
export const BillingServiceClientProvider = Symbol("BillingServiceClientProvider");
Expand Down Expand Up @@ -148,10 +156,7 @@ export class PromisifiedUsageServiceClient {
);
}

public async listUsage(
_ctx: TraceContext,
request: ListUsageRequest,
): Promise<ListUsageResponse> {
public async listUsage(_ctx: TraceContext, request: ListUsageRequest): Promise<ListUsageResponse> {
const ctx = TraceContext.childContext(`/usage-service/listUsage`, _ctx);
try {
const response = await new Promise<ListUsageResponse>((resolve, reject) => {
Expand All @@ -176,59 +181,63 @@ export class PromisifiedUsageServiceClient {
}
}

public async getCostCenter(
attributionID: AttributionId,
): Promise<CostCenter | undefined> {
public async getCostCenter(attributionID: AttributionId): Promise<CostCenter | undefined> {
const request = new GetCostCenterRequest();
request.setAttributionId(AttributionId.render(attributionID))
request.setAttributionId(AttributionId.render(attributionID));

const response = await new Promise<GetCostCenterResponse>((resolve, reject) => {
this.client.getCostCenter(
request,
(err: grpc.ServiceError | null, response: GetCostCenterResponse) => {
if (err) {
reject(err);
return;
}
resolve(response);
},
);
this.client.getCostCenter(request, (err: grpc.ServiceError | null, response: GetCostCenterResponse) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
if (!response.hasCostCenter()) {
return undefined;
}
return <CostCenter> {
id: AttributionId.parse(response.getCostCenter()!.getAttributionId()),

const attrId = AttributionId.parse(response.getCostCenter()!.getAttributionId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It would be a contract violation of the GRPC service to return a costCenter without an attributionID. We should enforce the contract in the API impl itself rather than on the TypeScript bindings.

if (!attrId) {
return undefined;
}

let billingStrategy: BillingStrategy = "other";
if (
response.getCostCenter()!.getBillingStrategy() ===
ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_STRIPE
) {
billingStrategy = "stripe";
}

return {
id: attrId,
spendingLimit: response.getCostCenter()!.getSpendingLimit(),
billingStrategy: response.getCostCenter()!.getBillingStrategy() || 'other'
billingStrategy: billingStrategy,
};
}

public async setCostCenter(
costCenter: CostCenter,
): Promise<void> {
public async setCostCenter(costCenter: CostCenter): Promise<void> {
const request = new SetCostCenterRequest();
const cc = new ProtocolCostCenter();
cc.setAttributionId(AttributionId.render(costCenter.id));
let billingStrategy = ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_OTHER;
if (costCenter.billingStrategy == 'stripe') {
if (costCenter.billingStrategy == "stripe") {
billingStrategy = ProtocolCostCenter.BillingStrategy.BILLING_STRATEGY_STRIPE;
}
cc.setBillingStrategy(billingStrategy);
cc.setSpendingLimit(costCenter.spendingLimit);
request.setCostCenter(cc);

await new Promise<SetCostCenterResponse>((resolve, reject) => {
this.client.setCostCenter(
request,
(err: grpc.ServiceError | null, response: SetCostCenterResponse) => {
if (err) {
reject(err);
return;
}
resolve(response);
},
);
this.client.setCostCenter(request, (err: grpc.ServiceError | null, response: SetCostCenterResponse) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
}

Expand Down