diff options
author | Torbjørn Smørgrav <smorgrav@users.noreply.github.com> | 2020-11-10 09:49:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 09:49:14 +0100 |
commit | 65d2ee07336a92ce84f5156a8cab66970e806f29 (patch) | |
tree | 4a8e487ecf5de04b4aad99e0788c4916fb8ac484 /controller-server | |
parent | 1a96731f45eaeb9d975be083ebdb2d547588cd86 (diff) | |
parent | 271d1ff49b7ad9a9642fc12740af5ee026794888 (diff) |
Merge pull request #15227 from vespa-engine/smorgrav/billing_api_collection_patch
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 dfef5b85e40..a4dd13cb920 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; @@ -103,6 +104,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); } @@ -145,6 +147,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", |