mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(marketplace-reporting): emulate AWS Marketplace Reporting API (#3343)
This commit is contained in:
1 parent
ee23e26bc3
commit
3c080e1ea6
11 files changed
+259
-1
No files matched your search
@@ -53,6 +53,7 @@ import io.github.hectorvent.floci.services.s3tables.S3TablesController;
|
||||
import io.github.hectorvent.floci.services.efs.EfsController;
|
||||
import io.github.hectorvent.floci.services.marketplace.MarketplaceCatalogController;
|
||||
import io.github.hectorvent.floci.services.marketplace.MarketplaceDeploymentController;
|
||||
import io.github.hectorvent.floci.services.marketplace.MarketplaceReportingController;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@@ -658,7 +659,7 @@ public class ResolvedServiceCatalog {
|
||||
"marketplace", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON, ServiceProtocol.JSON, ServiceProtocol.CBOR),
|
||||
Set.of("AWSMPCommerceService_v20200301.", "AWSMPEntitlementService."), Set.of("aws-marketplace"), Set.of("AWS Marketplace Entitlement Service"),
|
||||
Set.of(MarketplaceCatalogController.class, MarketplaceDeploymentController.class))
|
||||
Set.of(MarketplaceCatalogController.class, MarketplaceDeploymentController.class, MarketplaceReportingController.class))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package io.github.hectorvent.floci.services.marketplace;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("/")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class MarketplaceReportingController {
|
||||
private final MarketplaceReportingService service;
|
||||
private final ObjectReader strictReader;
|
||||
private final RequestContext context;
|
||||
|
||||
@Inject
|
||||
public MarketplaceReportingController(MarketplaceReportingService service, ObjectMapper mapper, RequestContext context) {
|
||||
this.service = service;
|
||||
this.strictReader = mapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/getBuyerDashboard")
|
||||
public Response getBuyerDashboard(String body) {
|
||||
return Response.ok(service.getBuyerDashboard(parse(body), accountId())).build();
|
||||
}
|
||||
|
||||
private JsonNode parse(String body) {
|
||||
try {
|
||||
JsonNode node = strictReader.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
if (node == null || !node.isObject()) {
|
||||
throw badRequest("Request body must be a JSON object.");
|
||||
}
|
||||
return node;
|
||||
} catch (AwsException e) { throw e; }
|
||||
catch (Exception e) { throw badRequest("Request body is not valid JSON."); }
|
||||
}
|
||||
|
||||
private String accountId() { return context.getAccountId() == null ? "000000000000" : context.getAccountId(); }
|
||||
private static AwsException badRequest(String message) { return new AwsException("BadRequestException", message, 400); }
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package io.github.hectorvent.floci.services.marketplace;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.marketplace.model.MarketplaceBuyerDashboard;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class MarketplaceReportingService {
|
||||
private static final Pattern DASHBOARD = Pattern.compile(
|
||||
"arn:aws:aws-marketplace::[0-9]{12}:AWSMarketplace/ReportingData/(Agreement_V1/Dashboard/AgreementSummary_V1|BillingEvent_V1/Dashboard/CostAnalysis_V1)");
|
||||
private static final Pattern DOMAIN = Pattern.compile(
|
||||
"(https://[a-zA-Z.*0-9_-]+[.][a-zA-Z]+[a-zA-Z0-9&?/_=-]*[a-zA-Z*0-9/]+|https?://localhost(:[0-9]{1,5})?)");
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Inject
|
||||
public MarketplaceReportingService(ObjectMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public ObjectNode getBuyerDashboard(JsonNode request, String accountId) {
|
||||
String dashboard = text(request, "dashboardIdentifier");
|
||||
if (!DASHBOARD.matcher(dashboard).matches()) {
|
||||
throw badRequest("dashboardIdentifier must be a supported AWS Marketplace procurement insights dashboard ARN.");
|
||||
}
|
||||
JsonNode domains = request.get("embeddingDomains");
|
||||
if (domains == null || !domains.isArray() || domains.size() < 1 || domains.size() > 2) {
|
||||
throw badRequest("embeddingDomains must contain one or two domains.");
|
||||
}
|
||||
List<String> copiedDomains = new ArrayList<>(domains.size());
|
||||
for (JsonNode domain : domains) {
|
||||
if (!domain.isTextual() || domain.asText().length() > 2000 || !DOMAIN.matcher(domain.asText()).matches()) {
|
||||
throw badRequest("embeddingDomains contains an invalid domain.");
|
||||
}
|
||||
copiedDomains.add(domain.asText());
|
||||
}
|
||||
String token = UUID.randomUUID().toString().replace("-", "");
|
||||
String dashboardName = dashboard.substring(dashboard.lastIndexOf('/') + 1);
|
||||
String embedUrl = "https://us-east-1.quicksight.aws.amazon.com/sn/embed/share/accounts/"
|
||||
+ accountId + "/dashboards/" + URLEncoder.encode(dashboardName, StandardCharsets.UTF_8)
|
||||
+ "?code=" + token;
|
||||
MarketplaceBuyerDashboard result = new MarketplaceBuyerDashboard(
|
||||
dashboard, embedUrl, copiedDomains);
|
||||
return mapper.valueToTree(result);
|
||||
}
|
||||
|
||||
private static String text(JsonNode request, String field) {
|
||||
JsonNode value = request == null ? null : request.get(field);
|
||||
if (value == null || !value.isTextual() || value.asText().isBlank()) {
|
||||
throw badRequest(field + " is required.");
|
||||
}
|
||||
return value.asText();
|
||||
}
|
||||
|
||||
private static AwsException badRequest(String message) {
|
||||
return new AwsException("BadRequestException", message, 400);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.marketplace.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Buyer dashboard embed result returned by Marketplace Reporting. */
|
||||
@RegisterForReflection
|
||||
public record MarketplaceBuyerDashboard(
|
||||
String dashboardIdentifier,
|
||||
String embedUrl,
|
||||
List<String> embeddingDomains) {
|
||||
public MarketplaceBuyerDashboard {
|
||||
embeddingDomains = embeddingDomains == null ? List.of() : List.copyOf(embeddingDomains);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user