diff options
author | Øyvind Grønnesby <oyving@yahooinc.com> | 2022-06-16 13:40:00 +0200 |
---|---|---|
committer | Øyvind Grønnesby <oyving@yahooinc.com> | 2022-06-16 13:40:00 +0200 |
commit | 63e868c098fb5a9b56efb7231265a7bc9301f5c5 (patch) | |
tree | be4d0228fdc8749d22469613453c7b0285941d77 /controller-server | |
parent | 5e00300349d15d4aad0df4ab0631a811df526b79 (diff) |
Improve plans in billing API v2
Diffstat (limited to 'controller-server')
2 files changed, 30 insertions, 3 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2.java index 419d1f4cb33..b80a487ec50 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2.java @@ -21,6 +21,7 @@ import com.yahoo.vespa.hosted.controller.api.integration.billing.Bill; import com.yahoo.vespa.hosted.controller.api.integration.billing.BillingController; import com.yahoo.vespa.hosted.controller.api.integration.billing.CollectionMethod; import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanId; +import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanRegistry; import com.yahoo.vespa.hosted.controller.api.role.Role; import com.yahoo.vespa.hosted.controller.api.role.SecurityContext; import com.yahoo.vespa.hosted.controller.tenant.CloudTenant; @@ -44,6 +45,7 @@ public class BillingApiHandlerV2 extends RestApiRequestHandler<BillingApiHandler private final ApplicationController applications; private final TenantController tenants; private final BillingController billing; + private final PlanRegistry planRegistry; private final Clock clock; public BillingApiHandlerV2(ThreadedHttpRequestHandler.Context context, Controller controller) { @@ -51,6 +53,7 @@ public class BillingApiHandlerV2 extends RestApiRequestHandler<BillingApiHandler this.applications = controller.applications(); this.tenants = controller.tenants(); this.billing = controller.serviceRegistry().billingController(); + this.planRegistry = controller.serviceRegistry().planRegistry(); this.clock = controller.serviceRegistry().clock(); } @@ -76,6 +79,8 @@ public class BillingApiHandlerV2 extends RestApiRequestHandler<BillingApiHandler .addRoute(RestApi.route("/billing/v2/accountant/preview/tenant/{tenant}") .get(self::previewBill) .post(Slime.class, self::createBill)) + .addRoute(RestApi.route("/billing/v2/accountant/plans") + .get(self::plans)) .addExceptionMapper(RuntimeException.class, (__, e) -> ErrorResponse.internalServerError(e.getMessage())) .build(); } @@ -86,13 +91,16 @@ public class BillingApiHandlerV2 extends RestApiRequestHandler<BillingApiHandler var tenantName = TenantName.from(requestContext.pathParameters().getStringOrThrow("tenant")); var tenant = tenants.require(tenantName, CloudTenant.class); - var plan = billing.getPlan(tenant.name()); + var plan = planRegistry.plan(billing.getPlan(tenant.name())).orElseThrow(); var collectionMethod = billing.getCollectionMethod(tenant.name()); var response = new Slime(); var cursor = response.setObject(); cursor.setString("tenant", tenant.name().value()); - cursor.setString("plan", plan.value()); + + var planCursor = cursor.setObject("plan"); + planCursor.setString("id", plan.id().value()); + planCursor.setString("name", plan.displayName()); cursor.setString("collection", collectionMethod.name()); return response; } @@ -236,6 +244,18 @@ public class BillingApiHandlerV2 extends RestApiRequestHandler<BillingApiHandler return new MessageResponse("Created bill " + invoiceId.value()); } + private HttpResponse plans(RestApi.RequestContext ctx) { + var slime = new Slime(); + var root = slime.setObject(); + var plans = root.setArray("plans"); + for (var plan : planRegistry.all()) { + var p = plans.addObject(); + p.setString("id", plan.id().value()); + p.setString("name", plan.displayName()); + } + return new SlimeJsonResponse(slime); + } + // --------- INVOICE RENDERING ---------- diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2Test.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2Test.java index 69245f0cded..1235dfb33b7 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2Test.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2Test.java @@ -70,7 +70,7 @@ public class BillingApiHandlerV2Test extends ControllerContainerCloudTest { @Test public void require_tenant_info() { var request = request("/billing/v2/tenant/" + tenant.value()).roles(tenantReader); - tester.assertResponse(request, "{\"tenant\":\"tenant1\",\"plan\":\"trial\",\"collection\":\"AUTO\"}"); + tester.assertResponse(request, "{\"tenant\":\"tenant1\",\"plan\":{\"id\":\"trial\",\"name\":\"Free Trial - for testing purposes\"},\"collection\":\"AUTO\"}"); } @Test @@ -136,4 +136,11 @@ public class BillingApiHandlerV2Test extends ControllerContainerCloudTest { .data("{\"from\": \"2020-05-01\",\"to\": \"2020-06-01\"}"); tester.assertResponse(accountantRequest, "{\"message\":\"Created bill id-123\"}"); } + + @Test + public void require_list_of_all_plans() { + var accountantRequest = request("/billing/v2/accountant/plans") + .roles(Role.hostedAccountant()); + tester.assertResponse(accountantRequest, "{\"plans\":[{\"id\":\"trial\",\"name\":\"Free Trial - for testing purposes\"},{\"id\":\"paid\",\"name\":\"Paid Plan - for testing purposes\"},{\"id\":\"none\",\"name\":\"None Plan - for testing purposes\"}]}"); + } } |