diff options
author | Øyvind Grønnesby <oyving@verizonmedia.com> | 2020-11-25 11:10:26 +0100 |
---|---|---|
committer | Øyvind Grønnesby <oyving@verizonmedia.com> | 2020-11-25 11:10:26 +0100 |
commit | dd29c756814446282767f70e47f7a2f779e3898c (patch) | |
tree | 318b6900d2f722298c7cd6665ce2ea45abe4b37b /controller-server | |
parent | 24ecd0adfd9f389f7d8a57008971b8514c38ff16 (diff) |
Export all bills/invoices as CSV
Diffstat (limited to 'controller-server')
5 files changed, 68 insertions, 4 deletions
diff --git a/controller-server/pom.xml b/controller-server/pom.xml index 0ddc1ecd8be..ea3bbcf1e49 100644 --- a/controller-server/pom.xml +++ b/controller-server/pom.xml @@ -124,6 +124,11 @@ <!-- compile --> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-csv</artifactId> + </dependency> + + <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> 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 f5b86ea846a..367f8e728bf 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 @@ -27,11 +27,14 @@ import com.yahoo.vespa.hosted.controller.api.integration.billing.BillingControll import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanId; import com.yahoo.vespa.hosted.controller.tenant.Tenant; import com.yahoo.yolean.Exceptions; +import org.apache.commons.csv.CSVFormat; import javax.ws.rs.BadRequestException; import javax.ws.rs.ForbiddenException; import javax.ws.rs.NotFoundException; import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.security.Principal; import java.time.LocalDate; @@ -44,6 +47,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.Executor; import java.util.logging.Level; +import java.util.stream.Collectors; /** * @author andreer @@ -101,10 +105,28 @@ public class BillingApiHandler extends LoggingRequestHandler { if (path.matches("/billing/v1/tenant/{tenant}/billing")) return getBilling(path.get("tenant"), request.getProperty("until")); if (path.matches("/billing/v1/tenant/{tenant}/plan")) return getPlan(path.get("tenant")); if (path.matches("/billing/v1/billing")) return getBillingAllTenants(request.getProperty("until")); + if (path.matches("/billing/v1/invoice/export")) return getAllInvoices(); if (path.matches("/billing/v1/invoice/tenant/{tenant}/line-item")) return getLineItems(path.get("tenant")); return ErrorResponse.notFoundError("Nothing at " + path); } + private HttpResponse getAllInvoices() { + var invoices = billingController.getInvoices(); + var headers = new String[]{ "ID", "Tenant", "From", "To", "CpuHours", "MemoryHours", "DiskHours", "Cpu", "Memory", "Disk" }; + var rows = invoices.stream() + .map(invoice -> { + return new Object[] { + invoice.id().value(), invoice.tenant().value(), + invoice.getStartTime().format(DateTimeFormatter.ISO_LOCAL_DATE), + invoice.getEndTime().format(DateTimeFormatter.ISO_LOCAL_DATE), + invoice.sumCpuHours(), invoice.sumMemoryHours(), invoice.sumDiskHours(), + invoice.sumCpuCost(), invoice.sumMemoryCost(), invoice.sumDiskCost() + }; + }) + .collect(Collectors.toList()); + return new CsvResponse(headers, rows); + } + 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")); @@ -462,4 +484,27 @@ public class BillingApiHandler extends LoggingRequestHandler { .count() > 0; } + private static class CsvResponse extends HttpResponse { + private final String[] header; + private final List<Object[]> rows; + + CsvResponse(String[] header, List<Object[]> rows) { + super(200); + this.header = header; + this.rows = rows; + } + + @Override + public void render(OutputStream outputStream) throws IOException { + var writer = new OutputStreamWriter(outputStream); + var printer = CSVFormat.DEFAULT.withRecordSeparator('\n').withHeader(this.header).print(writer); + for (var row : this.rows) printer.printRecord(row); + printer.flush(); + } + + @Override + public String getContentType() { + return "text/csv; encoding=utf-8"; + } + } } diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ContainerTester.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ContainerTester.java index 62b52d0d087..bda5a708a94 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ContainerTester.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ContainerTester.java @@ -78,9 +78,13 @@ public class ContainerTester { } public void assertResponse(Request request, File responseFile, int expectedStatusCode) { + assertResponse(request, responseFile, expectedStatusCode, true); + } + + public void assertResponse(Request request, File responseFile, int expectedStatusCode, boolean removeWhitespace) { String expectedResponse = readTestFile(responseFile.toString()); expectedResponse = include(expectedResponse); - expectedResponse = expectedResponse.replaceAll("(\"[^\"]*\")|\\s*", "$1"); // Remove whitespace + if (removeWhitespace) expectedResponse = expectedResponse.replaceAll("(\"[^\"]*\")|\\s*", "$1"); // Remove whitespace FilterResult filterResult = invokeSecurityFilters(request); request = filterResult.request; Response response = filterResult.response != null ? filterResult.response : container.handleRequest(request); @@ -95,11 +99,11 @@ public class ContainerTester { // until the first stop character String stopCharacters = "[^,:\\\\[\\\\]{}]"; String expectedResponsePattern = Pattern.quote(expectedResponse) - .replaceAll("\"?\\(ignore\\)\"?", "\\\\E" + - stopCharacters + "*\\\\Q"); + .replaceAll("\"?\\(ignore\\)\"?", "\\\\E" + + stopCharacters + "*\\\\Q"); if (!Pattern.matches(expectedResponsePattern, responseString)) { throw new ComparisonFailure(responseFile.toString() + " (with ignored fields)", - expectedResponsePattern, responseString); + expectedResponsePattern, responseString); } } else { assertEquals(responseFile.toString(), expectedResponse, responseString); 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 eb4f7b7560d..c8f1643e140 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 @@ -192,6 +192,14 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { } @Test + public void csv_export() { + var invoice = createInvoice(); + billingController.addInvoice(tenant, invoice, true); + var csvRequest = request("/billing/v1/invoice", GET).roles(financeAdmin); + tester.assertResponse(csvRequest.get(), new File("billing-all-invoices"), 200, false); + } + + @Test public void patch_collection_method() { test_patch_collection_with_field_name("collectionMethod"); test_patch_collection_with_field_name("collection"); diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/responses/billing-all-invoices b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/responses/billing-all-invoices new file mode 100644 index 00000000000..157470106f4 --- /dev/null +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/responses/billing-all-invoices @@ -0,0 +1,2 @@ +ID,Tenant,From,To,CpuHours,MemoryHours,DiskHours,Cpu,Memory,Disk +id-1,default,2020-05-23,2020-05-28,0.00,0.00,0.00,0.00,0.00,0.00 |