diff options
author | smorgrav <smorgrav@verizonmedia.com> | 2020-11-09 10:55:23 +0100 |
---|---|---|
committer | smorgrav <smorgrav@verizonmedia.com> | 2020-11-09 10:56:40 +0100 |
commit | 271d1ff49b7ad9a9642fc12740af5ee026794888 (patch) | |
tree | d5346fb7db984f33f28a03838e43a283ebd8a48e /controller-server | |
parent | bb75c5975c5588b8e05e9bd62016d1dce0a6d654 (diff) |
Rest api to patch collection method - only accessible for accountants
Diffstat (limited to 'controller-server')
2 files changed, 36 insertions, 1 deletions
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 0dce968960f..f7ae9e46bce 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 @@ -19,6 +19,7 @@ import com.yahoo.slime.Slime; import com.yahoo.slime.SlimeUtils; import com.yahoo.vespa.hosted.controller.ApplicationController; import com.yahoo.vespa.hosted.controller.Controller; +import com.yahoo.vespa.hosted.controller.api.integration.billing.CollectionMethod; import com.yahoo.vespa.hosted.controller.api.integration.billing.PaymentInstrument; import com.yahoo.vespa.hosted.controller.api.integration.billing.Invoice; import com.yahoo.vespa.hosted.controller.api.integration.billing.InstrumentOwner; @@ -104,6 +105,7 @@ public class BillingApiHandler extends LoggingRequestHandler { private HttpResponse handlePATCH(HttpRequest request, Path path, String userId) { if (path.matches("/billing/v1/tenant/{tenant}/instrument")) return patchActiveInstrument(request, path.get("tenant"), userId); if (path.matches("/billing/v1/tenant/{tenant}/plan")) return patchPlan(request, path.get("tenant")); + if (path.matches("/billing/v1/tenant/{tenant}/collection")) return patchCollectionMethod(request, path.get("tenant")); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -146,6 +148,23 @@ public class BillingApiHandler extends LoggingRequestHandler { return ErrorResponse.forbidden(result.getErrorMessage().orElse("Invalid plan change")); } + private HttpResponse patchCollectionMethod(HttpRequest request, String tenant) { + var tenantName = TenantName.from(tenant); + var slime = inspectorOrThrow(request); + String newMethod = slime.field("collectionMethod").asString().toUpperCase(); + if (newMethod.isEmpty()) return ErrorResponse.badRequest("No collection method specified"); + + try { + var result = billingController.setCollectionMethod(tenantName, CollectionMethod.valueOf(newMethod)); + if (result.isSuccess()) + return new StringResponse("Collection method updated to " + newMethod); + + return ErrorResponse.forbidden(result.getErrorMessage().orElse("Invalid collection method change")); + } catch (IllegalArgumentException iea){ + return ErrorResponse.badRequest("Invalid collection method: " + newMethod); + } + } + private HttpResponse getBillingAllTenants(String until) { try { var untilDate = untilParameter(until); 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 19cfa95c682..98cdeb09b83 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 @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.controller.restapi.billing; import com.yahoo.config.provision.SystemName; import com.yahoo.config.provision.TenantName; +import com.yahoo.vespa.hosted.controller.api.integration.billing.CollectionMethod; import com.yahoo.vespa.hosted.controller.api.integration.billing.Invoice; import com.yahoo.vespa.hosted.controller.api.integration.billing.MockBillingController; import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanId; @@ -186,6 +187,22 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { assertEquals("new-plan", billingController.getPlan(tenant).value()); } + @Test + public void patch_collection_method() { + var planRequest = request("/billing/v1/tenant/tenant1/collection", PATCH) + .data("{\"collectionMethod\": \"invoice\"}") + .roles(financeAdmin); + tester.assertResponse(planRequest, "Collection method updated to INVOICE"); + assertEquals(CollectionMethod.INVOICE, billingController.getCollectionMethod(tenant)); + + // Test that not event tenant administrators can do this + planRequest = request("/billing/v1/tenant/tenant1/collection", PATCH) + .data("{\"collectionMethod\": \"epay\"}") + .roles(tenantRole); + tester.assertResponse(planRequest, accessDenied, 403); + assertEquals(CollectionMethod.INVOICE, billingController.getCollectionMethod(tenant)); + } + private Invoice createInvoice() { var start = LocalDate.of(2020, 5, 23).atStartOfDay(ZoneId.systemDefault()); var end = start.plusDays(5); @@ -199,7 +216,6 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { ); } - private Invoice.LineItem createLineItem(ZonedDateTime addedAt) { return new Invoice.LineItem( "some-id", |