diff options
9 files changed, 135 insertions, 16 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/BillingController.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/BillingController.java index 90c4622d803..0fc20095b41 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/BillingController.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/BillingController.java @@ -49,7 +49,9 @@ public interface BillingController { InstrumentList listInstruments(TenantName tenant, String userId); - List<Invoice> getInvoices(TenantName tenant); + List<Invoice> getInvoicesForTenant(TenantName tenant); + + List<Invoice> getInvoices(); void deleteBillingInfo(TenantName tenant, Set<User> users, boolean isPrivileged); diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/Invoice.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/Invoice.java index 44f8f6e786d..39d974378b4 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/Invoice.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/Invoice.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.controller.api.integration.billing; import com.yahoo.config.provision.ApplicationId; +import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.zone.ZoneId; import java.math.BigDecimal; @@ -13,6 +14,7 @@ import java.util.Optional; import java.util.SortedMap; import java.util.TreeMap; import java.util.UUID; +import java.util.function.Function; /** @@ -31,13 +33,15 @@ public class Invoice { private static final BigDecimal SCALED_ZERO = new BigDecimal("0.00"); private final Id id; + private final TenantName tenant; private final List<LineItem> lineItems; private final StatusHistory statusHistory; private final ZonedDateTime startTime; private final ZonedDateTime endTime; - public Invoice(Id id, StatusHistory statusHistory, List<LineItem> lineItems, ZonedDateTime startTime, ZonedDateTime endTime) { + public Invoice(Id id, TenantName tenant, StatusHistory statusHistory, List<LineItem> lineItems, ZonedDateTime startTime, ZonedDateTime endTime) { this.id = id; + this.tenant = tenant; this.lineItems = List.copyOf(lineItems); this.statusHistory = statusHistory; this.startTime = startTime; @@ -48,6 +52,10 @@ public class Invoice { return id; } + public TenantName tenant() { + return tenant; + } + public String status() { return statusHistory.current(); } @@ -72,6 +80,40 @@ public class Invoice { return lineItems.stream().map(LineItem::amount).reduce(SCALED_ZERO, BigDecimal::add); } + public BigDecimal sumCpuHours() { + return sumResourceValues(LineItem::getCpuHours); + } + + public BigDecimal sumMemoryHours() { + return sumResourceValues(LineItem::getMemoryHours); + } + + public BigDecimal sumDiskHours() { + return sumResourceValues(LineItem::getDiskHours); + } + + public BigDecimal sumCpuCost() { + return sumResourceValues(LineItem::getCpuCost); + } + + public BigDecimal sumMemoryCost() { + return sumResourceValues(LineItem::getMemoryCost); + } + + public BigDecimal sumDiskCost() { + return sumResourceValues(LineItem::getDiskCost); + } + + public BigDecimal sumAdditionalCost() { + // anything that is not covered by the cost for resources is "additional" costs + var resourceCosts = sumCpuCost().add(sumMemoryCost()).add(sumDiskCost()); + return sum().subtract(resourceCosts); + } + + private BigDecimal sumResourceValues(Function<LineItem, Optional<BigDecimal>> f) { + return lineItems.stream().flatMap(li -> f.apply(li).stream()).reduce(SCALED_ZERO, BigDecimal::add); + } + public static final class Id { private final String value; diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/MockBillingController.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/MockBillingController.java index 420b964d7aa..21eada37ab1 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/MockBillingController.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/MockBillingController.java @@ -8,11 +8,13 @@ import java.math.BigDecimal; import java.time.LocalDate; import java.time.ZonedDateTime; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; /** * @author olaa @@ -53,6 +55,7 @@ public class MockBillingController implements BillingController { committedInvoices.computeIfAbsent(tenant, l -> new ArrayList<>()) .add(new Invoice( invoiceId, + tenant, Invoice.StatusHistory.open(), List.of(), startTime, @@ -134,11 +137,16 @@ public class MockBillingController implements BillingController { } @Override - public List<Invoice> getInvoices(TenantName tenant) { + public List<Invoice> getInvoicesForTenant(TenantName tenant) { return committedInvoices.getOrDefault(tenant, List.of()); } @Override + public List<Invoice> getInvoices() { + return committedInvoices.values().stream().flatMap(Collection::stream).collect(Collectors.toList()); + } + + @Override public void deleteBillingInfo(TenantName tenant, Set<User> users, boolean isPrivileged) {} @Override @@ -177,6 +185,6 @@ public class MockBillingController implements BillingController { } private Invoice emptyInvoice() { - return new Invoice(Invoice.Id.of("empty"), Invoice.StatusHistory.open(), List.of(), ZonedDateTime.now(), ZonedDateTime.now()); + return new Invoice(Invoice.Id.of("empty"), TenantName.defaultName(), Invoice.StatusHistory.open(), List.of(), ZonedDateTime.now(), ZonedDateTime.now()); } } 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 d6c6f5ff167..cedae5b5a46 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,29 @@ 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", "Additional" }; + 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(), + invoice.sumAdditionalCost() + }; + }) + .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")); @@ -330,7 +353,7 @@ public class BillingApiHandler extends LoggingRequestHandler { } private List<Invoice> getInvoicesForTenant(TenantName tenant) { - return billingController.getInvoices(tenant); + return billingController.getInvoicesForTenant(tenant); } private void renderInvoices(Cursor cursor, List<Invoice> invoices) { @@ -462,4 +485,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 1d0f0935c05..5b46df1ad1f 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,7 +2,6 @@ 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.ControllerTester; 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; @@ -12,15 +11,12 @@ import com.yahoo.vespa.hosted.controller.restapi.ContainerTester; import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerCloudTest; import com.yahoo.vespa.hosted.controller.security.Auth0Credentials; import com.yahoo.vespa.hosted.controller.security.CloudTenantSpec; -import com.yahoo.vespa.hosted.controller.security.Credentials; -import com.yahoo.vespa.hosted.controller.security.TenantSpec; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.io.File; import java.math.BigDecimal; -import java.security.Principal; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -110,7 +106,7 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { @Test public void test_invoice_creation() { - var invoices = billingController.getInvoices(tenant); + var invoices = billingController.getInvoicesForTenant(tenant); assertEquals(0, invoices.size()); String requestBody = "{\"tenant\":\"tenant1\", \"startTime\":\"2020-04-20\", \"endTime\":\"2020-05-20\"}"; @@ -122,7 +118,7 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { request.roles(financeAdmin); tester.assertResponse(request, new File("invoice-creation-response")); - invoices = billingController.getInvoices(tenant); + invoices = billingController.getInvoicesForTenant(tenant); assertEquals(1, invoices.size()); Invoice invoice = invoices.get(0); assertEquals(invoice.getStartTime().toString(), "2020-04-20T00:00Z[UTC]"); @@ -165,7 +161,7 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { .roles(financeAdmin); tester.assertResponse(request, "{\"message\":\"Updated status of invoice id-1\"}"); - var invoice = billingController.getInvoices(tenant).get(0); + var invoice = billingController.getInvoicesForTenant(tenant).get(0); assertEquals("DONE", invoice.status()); } @@ -196,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/export", 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"); @@ -222,6 +226,7 @@ public class BillingApiHandlerTest extends ControllerContainerCloudTest { var statusHistory = new Invoice.StatusHistory(new TreeMap<>(Map.of(start, "OPEN"))); return new Invoice( Invoice.Id.of("id-1"), + TenantName.defaultName(), statusHistory, List.of(createLineItem(start)), start, 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..957ed858951 --- /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,Additional +id-1,default,2020-05-23,2020-05-28,0.00,0.00,0.00,0.00,0.00,0.00,123.00 diff --git a/parent/pom.xml b/parent/pom.xml index 72aa330fe13..9f975e06d45 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -570,6 +570,11 @@ <version>${apache.httpclient.version}</version> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-csv</artifactId> + <version>1.8</version> + </dependency> + <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>${apache.httpcore.version}</version> |