diff options
author | Øyvind Grønnesby <oyving@yahooinc.com> | 2022-05-31 17:03:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-31 17:03:08 +0200 |
commit | 0b3b4099742306aeb2706e6be7251d66152f12ae (patch) | |
tree | fb40d562cd5c2af1a980c0743c3914bf67742b36 | |
parent | 00e2eae6db19c374a288e239a9df3f96e35cce77 (diff) | |
parent | da46b1496b114ef7d9bb163a56f8f4ddbac2dd16 (diff) |
Merge pull request #22808 from vespa-engine/ogronnesby/plan-list-api
Expose all plans in billing API
5 files changed, 35 insertions, 1 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistry.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistry.java index 23fb1f20fbc..6b267e538e7 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistry.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistry.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.integration.billing; +import java.util.List; import java.util.Optional; /** @@ -16,6 +17,9 @@ public interface PlanRegistry { /** Get a plan given a plan ID */ Optional<Plan> plan(PlanId planId); + /** Get a set of all plans */ + List<Plan> all(); + /** Get a plan give a plan ID */ default Optional<Plan> plan(String planId) { if (planId == null || planId.isBlank()) diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistryMock.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistryMock.java index 4757fa76224..723ffa383eb 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistryMock.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/PlanRegistryMock.java @@ -6,6 +6,7 @@ import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceUsage; import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -27,6 +28,11 @@ public class PlanRegistryMock implements PlanRegistry { .findAny(); } + @Override + public List<Plan> all() { + return List.of(freeTrial, paidPlan, nonePlan); + } + private static class MockPlan implements Plan { private final PlanId planId; private final String description; diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/PathGroup.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/PathGroup.java index c1034e6ab0d..a3c1a0315ec 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/PathGroup.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/PathGroup.java @@ -234,7 +234,8 @@ enum PathGroup { /** Paths used for invoice management */ hostedAccountant("/billing/v1/invoice/{*}", - "/billing/v1/billing"), + "/billing/v1/billing", + "/billing/v1/plans"), /** Path used for listing endpoint certificate request and re-requesting endpoint certificates */ endpointCertificates("/endpointcertificates/"), diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java index 206303adc80..d45e69f781b 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java @@ -24,6 +24,7 @@ import com.yahoo.vespa.hosted.controller.api.integration.billing.CollectionMetho import com.yahoo.vespa.hosted.controller.api.integration.billing.InstrumentOwner; import com.yahoo.vespa.hosted.controller.api.integration.billing.PaymentInstrument; 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.Tenant; @@ -56,11 +57,13 @@ public class BillingApiHandler extends ThreadedHttpRequestHandler { private final BillingController billingController; private final ApplicationController applicationController; private final TenantController tenantController; + private final PlanRegistry planRegistry; public BillingApiHandler(Executor executor, Controller controller) { super(executor); this.billingController = controller.serviceRegistry().billingController(); + this.planRegistry = controller.serviceRegistry().planRegistry(); this.applicationController = controller.applications(); this.tenantController = controller.tenants(); } @@ -103,6 +106,7 @@ public class BillingApiHandler extends ThreadedHttpRequestHandler { if (path.matches("/billing/v1/billing")) return getBillingAllTenants(request.getProperty("until")); if (path.matches("/billing/v1/invoice/export")) return getAllBills(); if (path.matches("/billing/v1/invoice/tenant/{tenant}/line-item")) return getLineItems(path.get("tenant")); + if (path.matches("/billing/v1/plans")) return getPlans(); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -295,6 +299,18 @@ public class BillingApiHandler extends ThreadedHttpRequestHandler { } } + private HttpResponse getPlans() { + 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); + } + private HttpResponse getLineItems(String tenant) { var slimeResponse = new Slime(); var root = slimeResponse.setObject(); diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerTest.java index b58e5294f66..c9e86849ed8 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerTest.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerTest.java @@ -79,6 +79,13 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { } @Test + public void list_plans() { + var listPlansRequest = request("/billing/v1/plans", GET) + .roles(Role.hostedAccountant()); + tester.assertResponse(listPlansRequest, "{\"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\"}]}"); + } + + @Test public void setting_and_deleting_instrument() { assertTrue(billingController.getDefaultInstrument(tenant).isEmpty()); |