Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(timestream-influxdb): add Timestream for InfluxDB backed by InfluxDB 2.x containers (#3683)

Matheus Oliveiraeb787672b856
diff --git a/README.md b/README.md
index cdf9766bf9..8f3fee0d5d 100644
--- a/README.md
+++ b/README.md
@@ -297,7 +297,7 @@ Floci supports local emulation for application services, data services, eventing
| API and identity | API Gateway REST, API Gateway v2, AppSync, Cognito, Cognito Identity, ACM, Route53, Route 53 Resolver, Cloud Map, Global Accelerator |
| Containers and compute | ECS, EC2, Lightsail, EKS, MWAA, ECR, EFS, CodeBuild, CodeDeploy, CodePipeline, CodeGuru Reviewer, CodeArtifact, AWS Batch, Auto Scaling, Application Auto Scaling, Elastic Beanstalk, ELB v2, ELB Classic |
| Data, analytics, and AI | Athena, Glue, Lake Formation, EMR, EMR Serverless, Redshift, Redshift Data API, Firehose, Managed Service for Apache Flink, OpenSearch, S3 Tables, S3 Vectors, Textract, Transcribe, Comprehend, Rekognition, Translate, Bedrock, Bedrock Runtime, Bedrock AgentCore, Bedrock AgentCore Control, SageMaker |
-| Databases and caching | RDS, RDS Data API, Neptune, DocumentDB, MemoryDB, ElastiCache |
+| Databases and caching | RDS, RDS Data API, Neptune, DocumentDB, MemoryDB, ElastiCache, Timestream for InfluxDB |
| Messaging and transfer | SES, Kinesis, MSK, Amazon MQ, Transfer Family, DataSync, IoT Core, Amazon Connect, Amazon AppIntegrations |
| Security and governance | AWS Network Firewall, AWS RAM, Service Quotas, WAF v2, GuardDuty, Amazon Inspector, CloudTrail, CloudFront, Resource Groups Tagging API, Resource Explorer 2, CloudHSM v2, Organizations, AWS Account Management, IAM Access Analyzer, IAM Identity Center (SSO Admin, OIDC, Access Portal, SCIM), Identity Store, Amazon Macie, Amazon Detective, Security Hub, Amazon Verified Permissions, Control Catalog, Control Tower, Service Catalog, AWS Marketplace |
| Cost and billing | AWS Budgets, Pricing, Cost Explorer, Cost and Usage Reports, BCM Pricing Calculator, BCM Data Exports |
@@ -337,6 +337,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/
| ElastiCache | Real Docker | Redis / Valkey protocol, IAM auth, SigV4 validation |
| MemoryDB | Real Docker | Redis / Valkey protocol via real containers; JSON 1.1 control plane; reuses ElastiCache RESP proxy |
| RDS | Real Docker | PostgreSQL, MySQL, MariaDB, IAM auth, JDBC-compatible engines |
+| Timestream for InfluxDB | Real Docker, mock mode available | InfluxDB 2.x DB instances and read replica clusters via `influxdb:2.7`; initial auth secret in Secrets Manager; backups and restore through the influx CLI |
| RDS Data API | REST JSON over real RDS containers | Raw SQL execution and transactions for local MySQL / MariaDB RDS resources |
| Neptune | Real Docker | Graph DB via TinkerPop Gremlin Server (default) or Neo4j for openCypher/Bolt (`NEPTUNE_DB_TYPE`); RDS-shaped control plane; SigV4 proxy on port 8182 |
| DocumentDB | Real Docker, mock mode available | MongoDB-compatible cluster via real MongoDB containers; RDS-shaped control plane; MongoDB wire protocol on port 27017 |
diff --git a/compatibility-tests/sdk-test-java/pom.xml b/compatibility-tests/sdk-test-java/pom.xml
index cb72711cfe..772cb060d7 100644
--- a/compatibility-tests/sdk-test-java/pom.xml
+++ b/compatibility-tests/sdk-test-java/pom.xml
@@ -537,6 +537,11 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>bcmpricingcalculator</artifactId>
</dependency>
+ <!-- Timestream for InfluxDB client -->
+ <dependency>
+ <groupId>software.amazon.awssdk</groupId>
+ <artifactId>timestreaminfluxdb</artifactId>
+ </dependency>
<!-- SSO credential support (needed for FLOCI_TARGET=aws with SSO profiles) -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java
index 77b364a2df..a4432d4141 100644
--- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java
+++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java
@@ -41,6 +41,7 @@ import software.amazon.awssdk.services.ssooidc.SsoOidcClient;
import software.amazon.awssdk.services.identitystore.IdentitystoreClient;
import software.amazon.awssdk.services.budgets.BudgetsClient;
import software.amazon.awssdk.services.bcmpricingcalculator.BcmPricingCalculatorClient;
+import software.amazon.awssdk.services.timestreaminfluxdb.TimestreamInfluxDbClient;
import software.amazon.awssdk.services.macie2.Macie2Client;
import software.amazon.awssdk.services.controlcatalog.ControlCatalogClient;
import software.amazon.awssdk.services.marketplacecatalog.MarketplaceCatalogClient;
@@ -1339,6 +1340,14 @@ public final class TestFixtures {
.build();
}
+ public static TimestreamInfluxDbClient timestreamInfluxDbClient() {
+ return TimestreamInfluxDbClient.builder()
+ .endpointOverride(ENDPOINT)
+ .region(REGION)
+ .credentialsProvider(CREDENTIALS)
+ .build();
+ }
+
public static ResourceExplorer2Client resourceExplorer2Client() {
return resourceExplorer2Client(REGION);
}
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java
new file mode 100644
index 0000000000..aa7543c141
--- /dev/null
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java
@@ -0,0 +1,86 @@
+package com.floci.test;
+
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.services.timestreaminfluxdb.TimestreamInfluxDbClient;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.CreateDbInstanceRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.CreateDbInstanceResponse;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.CreateDbParameterGroupRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.CreateDbParameterGroupResponse;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.DeleteDbInstanceRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.GetDbInstanceRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.GetDbInstanceResponse;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.GetDbParameterGroupRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.InfluxDBv2Parameters;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.ListDbInstancesRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.ListTagsForResourceRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.LogLevel;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.Parameters;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.ResourceNotFoundException;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.TagResourceRequest;
+import software.amazon.awssdk.services.timestreaminfluxdb.model.ValidationException;
+
+import java.util.Map;
+import java.util.UUID;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TimestreamInfluxDbTest {
+
+ @Test
+ void parameterGroupAndInstanceLifecycle() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ try (TimestreamInfluxDbClient client = TestFixtures.timestreamInfluxDbClient()) {
+ CreateDbParameterGroupResponse group = client.createDbParameterGroup(CreateDbParameterGroupRequest.builder()
+ .name("sdk-group-" + suffix)
+ .parameters(Parameters.fromInfluxDBv2(InfluxDBv2Parameters.builder().logLevel(LogLevel.DEBUG).build()))
+ .build());
+ assertEquals(LogLevel.DEBUG, client.getDbParameterGroup(GetDbParameterGroupRequest.builder()
+ .identifier(group.id()).build()).parameters().influxDBv2().logLevel());
+
+ CreateDbInstanceResponse created = client.createDbInstance(CreateDbInstanceRequest.builder()
+ .name("sdk-db-" + suffix)
+ .password("password123")
+ .dbInstanceType("db.influx.medium")
+ .allocatedStorage(20)
+ .dbParameterGroupIdentifier(group.id())
+ .vpcSubnetIds("subnet-abc123")
+ .vpcSecurityGroupIds("sg-abc123")
+ .tags(Map.of("env", "compat"))
+ .build());
+ assertNotNull(created.id());
+ assertEquals("CREATING", created.statusAsString());
+
+ try {
+ GetDbInstanceResponse instance = client.getDbInstance(GetDbInstanceRequest.builder()
+ .identifier(created.id()).build());
+ assertEquals(created.arn(), instance.arn());
+ assertEquals(group.id(), instance.dbParameterGroupIdentifier());
+ assertTrue(client.listDbInstances(ListDbInstancesRequest.builder().build()).items().stream()
+ .anyMatch(summary -> summary.id().equals(created.id())));
+
+ client.tagResource(TagResourceRequest.builder()
+ .resourceArn(created.arn()).tags(Map.of("team", "metrics")).build());
+ Map<String, String> tags = client.listTagsForResource(ListTagsForResourceRequest.builder()
+ .resourceArn(created.arn()).build()).tags();
+ assertEquals("compat", tags.get("env"));
+ assertEquals("metrics", tags.get("team"));
+ } finally {
+ client.deleteDbInstance(DeleteDbInstanceRequest.builder().identifier(created.id()).build());
+ }
+
+ assertThrows(ResourceNotFoundException.class, () -> client.getDbInstance(GetDbInstanceRequest.builder()
+ .identifier(created.id()).build()));
+ assertThrows(ValidationException.class, () -> client.createDbInstance(CreateDbInstanceRequest.builder()
+ .name("sdk-bad-" + suffix)
+ .password("password123")
+ .dbInstanceType("db.influx.medium")
+ .allocatedStorage(19)
+ .vpcSubnetIds("subnet-abc123")
+ .vpcSecurityGroupIds("sg-abc123")
+ .build()));
+ }
+ }
+}
diff --git a/docs/services/index.md b/docs/services/index.md
index 2e4091a2e2..95e2ed4a91 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -63,6 +63,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [MemoryDB](memorydb.md) | `POST /` + `X-Amz-Target: AmazonMemoryDB.*` + TCP proxy | JSON 1.1 + RESP | 7 |
| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 14 |
| [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 |
+| [Timestream for InfluxDB](timestream-influxdb.md) | `POST /` + `X-Amz-Target: AmazonTimestreamInfluxDB.*` + InfluxDB container | JSON 1.0 + InfluxDB HTTP | 24 |
| [MSK](msk.md) | `/v1/clusters/...`, `/api/v2/clusters/...` + Redpanda broker | REST JSON + Kafka | 8 |
| [Amazon MQ](amazonmq.md) | `/v1/brokers/...` + RabbitMQ broker | REST JSON + AMQP | 5 |
| [MWAA](mwaa.md) | `/` REST paths for environments + CLI/web proxy | REST JSON | 10 |
diff --git a/docs/services/timestream-influxdb.md b/docs/services/timestream-influxdb.md
new file mode 100644
index 0000000000..cf443c46f2
--- /dev/null
+++ b/docs/services/timestream-influxdb.md
@@ -0,0 +1,124 @@
+# Timestream for InfluxDB
+
+**Protocol:** JSON 1.0 (`X-Amz-Target: AmazonTimestreamInfluxDB.*`)
+**Endpoint:** `POST http://localhost:4566/`
+**Signing name:** `timestream-influxdb`
+
+Floci emulates the Amazon Timestream for InfluxDB management API and backs InfluxDB 2.x DB instances with
+real InfluxDB containers. The endpoint and port returned by `CreateDbInstance`, `GetDbInstance` and
+`GetDbCluster` point at the container, so the influx CLI, the InfluxDB UI and any InfluxDB 2.x client can
+write and query data against it.
+
+## Supported Actions
+
+<!-- floci:actions:start -->
+| Action | Description |
+| --- | --- |
+| `CreateDbInstance` | Starts an InfluxDB 2.x container initialized with the requested username, password, organization and bucket |
+| `GetDbInstance` | Returns the instance, including the reachable endpoint and port once it is AVAILABLE |
+| `ListDbInstances` | Lists standalone and cluster instances with pagination |
+| `UpdateDbInstance` | Updates instance settings; a new parameter group recreates the container with its InfluxDB options |
+| `RebootDbInstance` | Restarts the backing container |
+| `DeleteDbInstance` | Stops and removes the container and its volumes |
+| `CreateDbCluster` | Creates a read replica cluster backed by one InfluxDB 2.x container, or an InfluxDB 3 cluster record |
+| `GetDbCluster` | Returns the cluster, including writer and reader endpoints |
+| `ListDbClusters` | Lists clusters with pagination |
+| `ListDbInstancesForCluster` | Lists the nodes of a cluster with their instance modes |
+| `UpdateDbCluster` | Updates cluster settings and applies them to every node |
+| `RebootDbCluster` | Restarts the cluster container; `instanceIds` must name nodes of the cluster |
+| `DeleteDbCluster` | Removes the cluster, its nodes, and its container |
+| `CreateDbParameterGroup` | Creates an InfluxDB v2, v3 Core or v3 Enterprise parameter group |
+| `GetDbParameterGroup` | Returns a parameter group and its parameters |
+| `ListDbParameterGroups` | Lists parameter groups with pagination |
+| `CreateDbBackup` | Takes an on-demand backup with `influx backup` inside the container |
+| `GetDbBackup` | Returns a backup and the resource configuration captured with it |
+| `ListDbBackups` | Lists backups, optionally filtered by `dbResourceId` |
+| `DeleteDbBackup` | Deletes a backup and its stored files |
+| `RestoreFromDbBackup` | Restores a backup into a new resource or into the existing one with `influx restore --full` |
+| `ListTagsForResource` | Lists tags on an instance, cluster, parameter group or backup |
+| `TagResource` | Adds tags, up to 200 per resource |
+| `UntagResource` | Removes tags by key |
+<!-- floci:actions:end -->
+
+## Data plane
+
+- `CreateDbInstance` returns `CREATING`. Floci starts the configured image with
+ `DOCKER_INFLUXDB_INIT_MODE=setup` and moves the instance to `AVAILABLE` once `GET /health` answers. If the
+ container fails to start or never becomes healthy, the instance becomes `FAILED`.
+- The InfluxDB HTTP listener (container port 8086) is published on a host port from
+ `host-port-base`..`host-port-max`. The returned `port` is that published port, so `UpdateDbInstance` and
+ `UpdateDbCluster` store a requested `port` but the reachable port stays the published one.
+- `organization`, `bucket` and `username` are optional in the API but required by InfluxDB setup. When they
+ are omitted Floci uses `default`, `default` and `admin`.
+- The initial authorization parameters are stored in a Secrets Manager secret named
+ `READONLY-InfluxDB-auth-parameters-<id>` with the keys `organization`, `bucket`, `username` and `password`.
+ `influxAuthParametersSecretArn` is returned once the resource leaves `CREATING`.
+- InfluxDB v2 parameter group values are passed to the container as the matching `INFLUXD_*` environment
+ variables. Assigning a different parameter group recreates the container on the same volumes.
+- Instance types, storage types and allocated storage are recorded but do not limit the container.
+- Data and the influx CLI configuration live on per-resource Docker volumes (or host directories when
+ `FLOCI_STORAGE_HOST_PERSISTENT_PATH` is an absolute path). After a Floci restart, persisted instances and
+ clusters get their containers back without running setup again.
+- When no Docker daemon is reachable, resources still reach `AVAILABLE` without a container so the
+ management API keeps working.
+
+## Clusters
+
+- An InfluxDB v2 read replica cluster (`MULTI_NODE_READ_REPLICAS`) has a `PRIMARY` and a `REPLICA` node. Both
+ nodes, the writer endpoint and the reader endpoint are served by one InfluxDB 2.x container: open source
+ InfluxDB has no read replica replication, so replication and failover are not emulated.
+- A cluster created without `password` has no container, because InfluxDB setup needs one.
+- InfluxDB 3 Core and Enterprise clusters are management records only. Nodes follow the parameter group
+ (`ingestQueryInstances`, `queryOnlyInstances`, `dedicatedCompactor`) and the default port is 8181, but no
+ InfluxDB 3 container is started.
+- Nodes can't be updated or deleted individually; use the cluster operations.
+
+## Backups
+
+- `CreateDbBackup` runs `influx backup` inside the container, copies the result to
+ `<persistent-path>/timestream-influxdb/backups`, and moves the backup from `IN_PROGRESS` to `COMPLETED`
+ (or `FAILED`).
+- `RestoreFromDbBackup` with `NEW_RESOURCE` starts a new container and runs `influx restore --full`, so the
+ restored resource has the users, tokens, organizations and buckets of the source. `REPLACE_EXISTING`
+ restores into the existing container and requires `name` to match the existing resource.
+- `restoreToTime` is rejected with `ValidationException`. Point-in-time restore is only available for
+ continuous backups, and Floci models on-demand backups only, so accepting the parameter would silently
+ restore the on-demand contents instead of the requested moment.
+- `REPLACE_EXISTING` accepts no configuration overrides: `vpcSubnetIds`, `vpcSecurityGroupIds`,
+ `publiclyAccessible`, `logDeliveryConfiguration`, `maintenanceSchedule`, `tags`, `port`, `networkType`,
+ `deploymentType`, `dbBackupConfigurations` and `kmsKeyId` are rejected with `ValidationException` naming the
+ member, because restoring into an existing resource changes only its data.
+- `dbBackupConfigurations` are validated and stored; automated backups are not scheduled.
+
+## Configuration
+
+| Variable | Default | Description |
+| --- | --- | --- |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_ENABLED` | `true` | Enable or disable the service |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_MOCK` | `false` | `true` keeps metadata only, without containers |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_DEFAULT_IMAGE` | `influxdb:2.7` | InfluxDB 2.x image for DB instances |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_HOST_PORT_BASE` | `8086` | Lowest published host port |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_HOST_PORT_MAX` | `8185` | Highest published host port |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_READINESS_TIMEOUT_SECONDS` | `120` | Time to wait for the container health check |
+| `FLOCI_SERVICES_TIMESTREAM_INFLUXDB_DOCKER_NETWORK` | unset | Docker network for InfluxDB containers |
+
+## Example
+
+```bash
+export AWS_ENDPOINT_URL=http://localhost:4566
+
+id=$(aws timestream-influxdb create-db-instance \
+ --name metrics-db \
+ --username admin --password password123 \
+ --organization acme --bucket metrics \
+ --db-instance-type db.influx.medium --allocated-storage 20 \
+ --vpc-subnet-ids subnet-abc123 --vpc-security-group-ids sg-abc123 \
+ --query id --output text)
+
+aws timestream-influxdb get-db-instance --identifier "$id" \
+ --query "[status,endpoint,port]"
+
+influx config create --config-name floci --active \
+ --host-url "http://localhost:<port>" --org acme --username-password admin:password123
+influx write --bucket metrics "cpu,host=a value=42"
+```
diff --git a/mkdocs.yml b/mkdocs.yml
index 20c9d7f9ef..a5fe8dce46 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -140,6 +140,7 @@ nav:
- MemoryDB: services/memorydb.md
- RDS: services/rds.md
- RDS Data API: services/rds-data.md
+ - Timestream for InfluxDB: services/timestream-influxdb.md
- MSK (Kafka): services/msk.md
- Amazon MQ: services/amazonmq.md
- Glue: services/glue.md
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index c2477f6c07..a215cde92b 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -741,6 +741,7 @@ public interface EmulatorConfig {
BcmDataExportsServiceConfig bcmDataExports();
OamServiceConfig oam();
BcmPricingCalculatorServiceConfig bcmPricingCalculator();
+ TimestreamInfluxDbServiceConfig timestreamInfluxdb();
ConfigServiceConfig configservice();
CloudTrailServiceConfig cloudtrail();
CloudControlServiceConfig cloudcontrol();
@@ -2075,6 +2076,34 @@ public interface EmulatorConfig {
boolean enabled();
}
+ interface TimestreamInfluxDbServiceConfig {
+ @WithDefault("true")
+ boolean enabled();
+
+ /** When true, DB instances and clusters reach AVAILABLE without a backing InfluxDB container. */
+ @WithDefault("false")
+ boolean mock();
+
+ /** InfluxDB 2.x image backing DB instances. Env: FLOCI_SERVICES_TIMESTREAM_INFLUXDB_DEFAULT_IMAGE */
+ @WithDefault("influxdb:2.7")
+ String defaultImage();
+
+ /** Lowest host port the InfluxDB HTTP listener (container port 8086) is published on. */
+ @WithDefault("8086")
+ int hostPortBase();
+
+ /** Highest host port the InfluxDB HTTP listener is published on. */
+ @WithDefault("8185")
+ int hostPortMax();
+
+ /** Seconds to wait for a started InfluxDB container to answer its health check. */
+ @WithDefault("120")
+ int readinessTimeoutSeconds();
+
+ /** Docker network to attach InfluxDB containers to. Empty uses the default network. */
+ Optional<String> dockerNetwork();
+ }
+
interface BcmDataExportsServiceConfig {
@WithDefault("true")
boolean enabled();
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java
index d125ce5a64..9b39ae53cd 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.sns.SnsJsonHandler;
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsJsonHandler;
import io.github.hectorvent.floci.services.swf.SwfJsonHandler;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbJsonHandler;
import io.github.hectorvent.floci.services.verifiedpermissions.VerifiedPermissionsJsonHandler;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
@@ -55,6 +56,7 @@ public class AwsJsonController {
private final MarketplaceJsonHandler marketplaceJsonHandler;
private final VerifiedPermissionsJsonHandler verifiedPermissionsJsonHandler;
private final BcmPricingCalculatorJsonHandler bcmPricingCalculatorJsonHandler;
+ private final TimestreamInfluxDbJsonHandler timestreamInfluxDbJsonHandler;
@Inject
public AwsJsonController(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
@@ -69,7 +71,8 @@ public class AwsJsonController {
NetworkFirewallJsonHandler networkFirewallJsonHandler,
MarketplaceJsonHandler marketplaceJsonHandler,
VerifiedPermissionsJsonHandler verifiedPermissionsJsonHandler,
- BcmPricingCalculatorJsonHandler bcmPricingCalculatorJsonHandler) {
+ BcmPricingCalculatorJsonHandler bcmPricingCalculatorJsonHandler,
+ TimestreamInfluxDbJsonHandler timestreamInfluxDbJsonHandler) {
this.objectMapper = objectMapper;
this.strictBodyReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
this.catalog = catalog;
@@ -86,6 +89,7 @@ public class AwsJsonController {
this.marketplaceJsonHandler = marketplaceJsonHandler;
this.verifiedPermissionsJsonHandler = verifiedPermissionsJsonHandler;
this.bcmPricingCalculatorJsonHandler = bcmPricingCalculatorJsonHandler;
+ this.timestreamInfluxDbJsonHandler = timestreamInfluxDbJsonHandler;
}
@POST
@@ -147,6 +151,7 @@ public class AwsJsonController {
case "marketplace" -> marketplaceJsonHandler.handle(action, request, region);
case "verifiedpermissions" -> verifiedPermissionsJsonHandler.handle(action, request, region);
case "bcm-pricing-calculator" -> bcmPricingCalculatorJsonHandler.handle(action, request, region);
+ case "timestream-influxdb" -> timestreamInfluxDbJsonHandler.handle(action, request, region);
default -> null;
};
// catalog.matchTarget is protocol-agnostic: a JSON 1.1 target
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
index 60508da9cd..3d4f49f184 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
@@ -184,7 +184,7 @@ public class ResolvedServiceCatalog {
protocols(ServiceProtocol.QUERY),
Set.of(), Set.of("neptune"), Set.of(), Set.of()),
descriptor("docdb", "docdb", config.services().docdb().enabled(), true,
- "docdb", config.storage().mode(),
+ "docdb", config.storage().mode(),
5000L, AwsNamespaces.RDS, ServiceProtocol.QUERY,
protocols(ServiceProtocol.QUERY),
Set.of(), Set.of("docdb"), Set.of(), Set.of()),
@@ -583,6 +583,11 @@ public class ResolvedServiceCatalog {
"bcmpricingcalculator", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("AWSBCMPricingCalculator."), Set.of("bcm-pricing-calculator"), Set.of(), Set.of()),
+ descriptor("timestream-influxdb", "timestreaminfluxdb",
+ config.services().timestreamInfluxdb().enabled(), true,
+ "timestreaminfluxdb", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
+ protocols(ServiceProtocol.JSON),
+ Set.of("AmazonTimestreamInfluxDB."), Set.of("timestream-influxdb"), Set.of(), Set.of()),
descriptor("bcm-data-exports", "bcmdataexports", config.services().bcmDataExports().enabled(), true,
"bcmdataexports", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
index d4671a84fa..27376cb607 100644
--- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
+++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
@@ -34,6 +34,7 @@ import io.github.hectorvent.floci.services.memorydb.container.MemoryDbContainerM
import io.github.hectorvent.floci.services.memorydb.proxy.MemoryDbProxyManager;
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbService;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.ShutdownDelayInitiatedEvent;
import io.quarkus.runtime.ShutdownEvent;
@@ -84,6 +85,7 @@ public class EmulatorLifecycle {
private final RabbitMqManager rabbitMqManager;
private final FlinkContainerManager flinkContainerManager;
private final RdsService rdsService;
+ private final TimestreamInfluxDbService timestreamInfluxDbService;
private final ElbV2Service elbV2Service;
private final ElbClassicService elbClassicService;
private final InitializationHooksRunner initializationHooksRunner;
@@ -118,6 +120,7 @@ public class EmulatorLifecycle {
RabbitMqManager rabbitMqManager,
FlinkContainerManager flinkContainerManager,
RdsService rdsService,
+ TimestreamInfluxDbService timestreamInfluxDbService,
ElbV2Service elbV2Service,
ElbClassicService elbClassicService,
InitializationHooksRunner initializationHooksRunner,
@@ -151,6 +154,7 @@ public class EmulatorLifecycle {
this.rabbitMqManager = rabbitMqManager;
this.flinkContainerManager = flinkContainerManager;
this.rdsService = rdsService;
+ this.timestreamInfluxDbService = timestreamInfluxDbService;
this.elbV2Service = elbV2Service;
this.elbClassicService = elbClassicService;
this.initializationHooksRunner = initializationHooksRunner;
@@ -207,6 +211,9 @@ public class EmulatorLifecycle {
dynamodbStreamsPoller.startPersistedPollers();
pipesService.startPersistedPollers();
rdsService.restorePersistedRuntime();
+ if (config.services().timestreamInfluxdb().enabled()) {
+ timestreamInfluxDbService.restorePersistedRuntime();
+ }
if (config.services().elasticache().enabled()) {
elastiCacheService.restorePersistedRuntime().exceptionally(ex -> {
LOG.warnv("ElastiCache cluster-mode restore failed: {0}", ex.getMessage());
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbJsonHandler.java
new file mode 100644
index 0000000000..c8808c4bf1
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbJsonHandler.java
@@ -0,0 +1,299 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
+import io.github.hectorvent.floci.core.common.PaginatedResult;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbService.Reported;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbService.RestoreResult;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbBackup;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbCluster;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbInstance;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbParameterGroup;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.core.Response;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+@ApplicationScoped
+public class TimestreamInfluxDbJsonHandler {
+
+ private final TimestreamInfluxDbService service;
+ private final ObjectMapper objectMapper;
+
+ @Inject
+ public TimestreamInfluxDbJsonHandler(TimestreamInfluxDbService service, ObjectMapper objectMapper) {
+ this.service = service;
+ this.objectMapper = objectMapper;
+ }
+
+ public Response handle(String action, JsonNode request, String region) {
+ return switch (action) {
+ case "CreateDbInstance" -> ok(reportedInstance(service.createDbInstance(request, region)));
+ case "GetDbInstance" -> ok(instanceNode(service.getDbInstance(request, region), null));
+ case "ListDbInstances" -> ok(page(service.listDbInstances(request, region), this::instanceSummary));
+ case "UpdateDbInstance" -> ok(reportedInstance(service.updateDbInstance(request, region)));
+ case "RebootDbInstance" -> ok(reportedInstance(service.rebootDbInstance(request, region)));
+ case "DeleteDbInstance" -> ok(reportedInstance(service.deleteDbInstance(request, region)));
+ case "CreateDbCluster" -> ok(clusterStatus(service.createDbCluster(request, region)));
+ case "GetDbCluster" -> ok(clusterNode(service.getDbCluster(request, region)));
+ case "ListDbClusters" -> ok(page(service.listDbClusters(request, region), this::clusterSummary));
+ case "ListDbInstancesForCluster" -> ok(page(service.listDbInstancesForCluster(request, region),
+ this::clusterMemberSummary));
+ case "UpdateDbCluster" -> ok(clusterStatusOnly(service.updateDbCluster(request, region)));
+ case "RebootDbCluster" -> ok(clusterStatusOnly(service.rebootDbCluster(request, region)));
+ case "DeleteDbCluster" -> ok(clusterStatusOnly(service.deleteDbCluster(request, region)));
+ case "CreateDbParameterGroup" -> ok(parameterGroupNode(service.createDbParameterGroup(request, region)));
+ case "GetDbParameterGroup" -> ok(parameterGroupNode(service.getDbParameterGroup(request, region)));
+ case "ListDbParameterGroups" -> ok(page(service.listDbParameterGroups(request, region),
+ this::parameterGroupSummary));
+ case "CreateDbBackup" -> ok(reportedBackup(service.createDbBackup(request, region)));
+ case "GetDbBackup" -> ok(backupNode(service.getDbBackup(request, region), null));
+ case "ListDbBackups" -> ok(page(service.listDbBackups(request, region), this::backupSummary));
+ case "DeleteDbBackup" -> ok(reportedBackup(service.deleteDbBackup(request, region)));
+ case "RestoreFromDbBackup" -> ok(restoreNode(service.restoreFromDbBackup(request, region)));
+ case "ListTagsForResource" -> ok(tagsNode(service.listTagsForResource(request)));
+ case "TagResource" -> {
+ service.tagResource(request);
+ yield ok(objectMapper.createObjectNode());
+ }
+ case "UntagResource" -> {
+ service.untagResource(request);
+ yield ok(objectMapper.createObjectNode());
+ }
+ default -> JsonErrorResponseUtils.createUnknownOperationErrorResponse("AmazonTimestreamInfluxDB." + action);
+ };
+ }
+
+ private ObjectNode reportedInstance(Reported<DbInstance> reported) {
+ return instanceNode(reported.resource(), reported.status());
+ }
+
+ private ObjectNode reportedBackup(Reported<DbBackup> reported) {
+ return backupNode(reported.resource(), reported.status());
+ }
+
+ private ObjectNode instanceNode(DbInstance instance, String reportedStatus) {
+ String status = reportedStatus != null ? reportedStatus : instance.getStatus();
+ ObjectNode node = instanceSummary(instance);
+ node.put("status", status);
+ putStrings(node, "vpcSubnetIds", instance.getVpcSubnetIds());
+ put(node, "publiclyAccessible", instance.getPubliclyAccessible());
+ putStrings(node, "vpcSecurityGroupIds", instance.getVpcSecurityGroupIds());
+ put(node, "dbParameterGroupIdentifier", instance.getDbParameterGroupIdentifier());
+ put(node, "availabilityZone", instance.getAvailabilityZone());
+ put(node, "secondaryAvailabilityZone", instance.getSecondaryAvailabilityZone());
+ putJson(node, "logDeliveryConfiguration", instance.getLogDeliveryConfiguration());
+ if (!"CREATING".equals(status)) {
+ put(node, "influxAuthParametersSecretArn", instance.getInfluxAuthParametersSecretArn());
+ }
+ put(node, "dbClusterId", instance.getDbClusterId());
+ put(node, "instanceMode", instance.getInstanceMode());
+ if (instance.getDbClusterId() != null) {
+ putStrings(node, "instanceModes", instance.getInstanceModes());
+ }
+ putJson(node, "maintenanceSchedule", instance.getMaintenanceSchedule());
+ putJson(node, "dbBackupConfigurations", instance.getDbBackupConfigurations());
+ put(node, "kmsKeyId", instance.getKmsKeyId());
+ return node;
+ }
+
+ private ObjectNode instanceSummary(DbInstance instance) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("id", instance.getId());
+ node.put("name", instance.getName());
+ node.put("arn", instance.getArn());
+ put(node, "status", instance.getStatus());
+ put(node, "endpoint", instance.getEndpoint());
+ put(node, "port", instance.getPort());
+ put(node, "networkType", instance.getNetworkType());
+ put(node, "dbInstanceType", instance.getDbInstanceType());
+ put(node, "dbStorageType", instance.getDbStorageType());
+ put(node, "allocatedStorage", instance.getAllocatedStorage());
+ put(node, "deploymentType", instance.getDeploymentType());
+ return node;
+ }
+
+ private ObjectNode clusterMemberSummary(DbInstance instance) {
+ ObjectNode node = instanceSummary(instance);
+ put(node, "instanceMode", instance.getInstanceMode());
+ putStrings(node, "instanceModes", instance.getInstanceModes());
+ return node;
+ }
+
+ private ObjectNode clusterStatus(Reported<DbCluster> reported) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("dbClusterId", reported.resource().getId());
+ node.put("dbClusterStatus", reported.status());
+ return node;
+ }
+
+ private ObjectNode clusterStatusOnly(Reported<DbCluster> reported) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("dbClusterStatus", reported.status());
+ return node;
+ }
+
+ private ObjectNode clusterNode(DbCluster cluster) {
+ ObjectNode node = clusterSummary(cluster);
+ put(node, "publiclyAccessible", cluster.getPubliclyAccessible());
+ put(node, "dbParameterGroupIdentifier", cluster.getDbParameterGroupIdentifier());
+ putJson(node, "logDeliveryConfiguration", cluster.getLogDeliveryConfiguration());
+ putJson(node, "maintenanceSchedule", cluster.getMaintenanceSchedule());
+ if (!"CREATING".equals(cluster.getStatus())) {
+ put(node, "influxAuthParametersSecretArn", cluster.getInfluxAuthParametersSecretArn());
+ }
+ putStrings(node, "vpcSubnetIds", cluster.getVpcSubnetIds());
+ putStrings(node, "vpcSecurityGroupIds", cluster.getVpcSecurityGroupIds());
+ put(node, "failoverMode", cluster.getFailoverMode());
+ putJson(node, "clusterConfiguration", cluster.getClusterConfiguration());
+ putJson(node, "dbBackupConfigurations", cluster.getDbBackupConfigurations());
+ put(node, "kmsKeyId", cluster.getKmsKeyId());
+ return node;
+ }
+
+ private ObjectNode clusterSummary(DbCluster cluster) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("id", cluster.getId());
+ node.put("name", cluster.getName());
+ node.put("arn", cluster.getArn());
+ put(node, "status", cluster.getStatus());
+ put(node, "endpoint", cluster.getEndpoint());
+ put(node, "readerEndpoint", cluster.getReaderEndpoint());
+ put(node, "port", cluster.getPort());
+ put(node, "deploymentType", cluster.getDeploymentType());
+ put(node, "dbInstanceType", cluster.getDbInstanceType());
+ put(node, "networkType", cluster.getNetworkType());
+ put(node, "dbStorageType", cluster.getDbStorageType());
+ put(node, "allocatedStorage", cluster.getAllocatedStorage());
+ put(node, "engineType", cluster.getEngineType());
+ return node;
+ }
+
+ private ObjectNode parameterGroupNode(DbParameterGroup group) {
+ ObjectNode node = parameterGroupSummary(group);
+ putJson(node, "parameters", group.getParameters());
+ return node;
+ }
+
+ private ObjectNode parameterGroupSummary(DbParameterGroup group) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("id", group.getId());
+ node.put("name", group.getName());
+ node.put("arn", group.getArn());
+ put(node, "description", group.getDescription());
+ return node;
+ }
+
+ private ObjectNode backupNode(DbBackup backup, String reportedStatus) {
+ ObjectNode node = backupSummary(backup);
+ if (reportedStatus != null) {
+ node.put("status", reportedStatus);
+ }
+ putJson(node, "clusterConfiguration", backup.getClusterConfiguration());
+ put(node, "dbParameterGroupId", backup.getDbParameterGroupId());
+ put(node, "dbInstanceType", backup.getDbInstanceType());
+ putJson(node, "logDeliveryConfiguration", backup.getLogDeliveryConfiguration());
+ put(node, "failoverMode", backup.getFailoverMode());
+ put(node, "dbStorageType", backup.getDbStorageType());
+ put(node, "allocatedStorage", backup.getAllocatedStorage());
+ putStrings(node, "vpcSubnetIds", backup.getVpcSubnetIds());
+ putStrings(node, "vpcSecurityGroupIds", backup.getVpcSecurityGroupIds());
+ put(node, "publiclyAccessible", backup.getPubliclyAccessible());
+ put(node, "port", backup.getPort());
+ put(node, "networkType", backup.getNetworkType());
+ put(node, "influxAuthParametersSecretArn", backup.getInfluxAuthParametersSecretArn());
+ putJson(node, "maintenanceSchedule", backup.getMaintenanceSchedule());
+ return node;
+ }
+
+ private ObjectNode backupSummary(DbBackup backup) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("id", backup.getId());
+ put(node, "name", backup.getName());
+ node.put("arn", backup.getArn());
+ put(node, "status", backup.getStatus());
+ if (backup.getCreatedAt() != null) {
+ node.put("createdAt", epochSeconds(backup.getCreatedAt()));
+ }
+ put(node, "expiresAfter", backup.getExpiresAfter());
+ put(node, "dbResourceId", backup.getDbResourceId());
+ put(node, "type", backup.getType());
+ put(node, "engineType", backup.getEngineType());
+ put(node, "deploymentType", backup.getDeploymentType());
+ put(node, "kmsKeyId", backup.getKmsKeyId());
+ return node;
+ }
+
+ private ObjectNode restoreNode(RestoreResult result) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("restoredDbResourceId", result.restoredDbResourceId());
+ node.put("restoreStatus", "RESTORING");
+ node.put("resourceType", result.resourceType());
+ put(node, "engineType", result.engineType());
+ put(node, "deploymentType", result.deploymentType());
+ return node;
+ }
+
+ private ObjectNode tagsNode(Map<String, String> tags) {
+ ObjectNode node = objectMapper.createObjectNode();
+ ObjectNode tagNode = node.putObject("tags");
+ tags.forEach(tagNode::put);
+ return node;
+ }
+
+ private <T> ObjectNode page(PaginatedResult<T> result, Function<T, ObjectNode> mapper) {
+ ObjectNode node = objectMapper.createObjectNode();
+ ArrayNode items = node.putArray("items");
+ for (T item : result.items()) {
+ items.add(mapper.apply(item));
+ }
+ put(node, "nextToken", result.nextToken());
+ return node;
+ }
+
+ private static Response ok(ObjectNode node) {
+ return Response.ok(node).build();
+ }
+
+ private static void put(ObjectNode node, String field, String value) {
+ if (value != null) {
+ node.put(field, value);
+ }
+ }
+
+ private static void put(ObjectNode node, String field, Integer value) {
+ if (value != null) {
+ node.put(field, value);
+ }
+ }
+
+ private static void put(ObjectNode node, String field, Boolean value) {
+ if (value != null) {
+ node.put(field, value);
+ }
+ }
+
+ private static void putJson(ObjectNode node, String field, JsonNode value) {
+ if (value != null && !value.isNull()) {
+ node.set(field, value);
+ }
+ }
+
+ private static void putStrings(ObjectNode node, String field, List<String> values) {
+ if (values != null && !values.isEmpty()) {
+ ArrayNode array = node.putArray(field);
+ values.forEach(array::add);
+ }
+ }
+
+ private static double epochSeconds(Instant instant) {
+ return instant.getEpochSecond() + instant.getNano() / 1_000_000_000.0;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java
new file mode 100644
index 0000000000..056e354b6d
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java
@@ -0,0 +1,1328 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.PaginatedResult;
+import io.github.hectorvent.floci.core.common.Pagination;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.common.Resettable;
+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbEndpoint;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbSetup;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbBackup;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbCluster;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbInstance;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbParameterGroup;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.ZoneOffset;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.regex.Matcher;
+
+import static io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbValidation.conflict;
+import static io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbValidation.notFound;
+import static io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbValidation.validation;
+
+@ApplicationScoped
+public class TimestreamInfluxDbService implements Resettable {
+
+ static final String ENGINE_V2 = "INFLUXDB_V2";
+ static final String ENGINE_V3_CORE = "INFLUXDB_V3_CORE";
+ static final String ENGINE_V3_ENTERPRISE = "INFLUXDB_V3_ENTERPRISE";
+ static final String DB_INSTANCE = "DB_INSTANCE";
+ static final String DB_CLUSTER = "DB_CLUSTER";
+ static final String DB_PARAMETER_GROUP = "DB_PARAMETER_GROUP";
+ static final String DB_BACKUP = "DB_BACKUP";
+ static final String AVAILABLE = "AVAILABLE";
+ static final String LOCAL_ENDPOINT = "localhost";
+
+ private static final Logger LOG = Logger.getLogger(TimestreamInfluxDbService.class);
+ private static final String ARN_SERVICE = "timestream-influxdb";
+ private static final String DEFAULT_USERNAME = "admin";
+ private static final String DEFAULT_ORGANIZATION = "default";
+ private static final String DEFAULT_BUCKET = "default";
+ private static final int V2_PORT = 8086;
+ private static final int V3_PORT = 8181;
+ private static final int MAX_TAGS = 200;
+ private static final int MAX_PAGE = 100;
+ private static final List<String> REPLACE_EXISTING_REJECTED_MEMBERS = List.of(
+ "vpcSubnetIds", "vpcSecurityGroupIds", "publiclyAccessible", "logDeliveryConfiguration",
+ "maintenanceSchedule", "tags", "port", "networkType", "deploymentType", "dbBackupConfigurations",
+ "kmsKeyId");
+
+ public record Reported<T>(T resource, String status) {
+ }
+
+ public record RestoreResult(String restoredDbResourceId, String resourceType, String engineType,
+ String deploymentType) {
+ }
+
+ private final AccountAwareStorageBackend<DbInstance> instances;
+ private final AccountAwareStorageBackend<DbCluster> clusters;
+ private final AccountAwareStorageBackend<DbParameterGroup> parameterGroups;
+ private final AccountAwareStorageBackend<DbBackup> backups;
+ private final RegionResolver regionResolver;
+ private final SecretsManagerService secretsManagerService;
+ private final ObjectMapper objectMapper;
+ private final TimestreamInfluxDbContainerManager containerManager;
+ private final boolean mock;
+ private final Executor executor;
+
+ @Inject
+ public TimestreamInfluxDbService(StorageFactory storageFactory, RegionResolver regionResolver,
+ SecretsManagerService secretsManagerService, ObjectMapper objectMapper,
+ EmulatorConfig config, TimestreamInfluxDbContainerManager containerManager) {
+ this(storageFactory.create("timestreaminfluxdb", "timestream-influxdb-instances.json",
+ new TypeReference<Map<String, DbInstance>>() {}),
+ storageFactory.create("timestreaminfluxdb", "timestream-influxdb-clusters.json",
+ new TypeReference<Map<String, DbCluster>>() {}),
+ storageFactory.create("timestreaminfluxdb", "timestream-influxdb-parameter-groups.json",
+ new TypeReference<Map<String, DbParameterGroup>>() {}),
+ storageFactory.create("timestreaminfluxdb", "timestream-influxdb-backups.json",
+ new TypeReference<Map<String, DbBackup>>() {}),
+ regionResolver, secretsManagerService, objectMapper, containerManager,
+ config.services().timestreamInfluxdb().mock(),
+ Executors.newFixedThreadPool(4, runnable -> {
+ Thread thread = new Thread(runnable, "timestream-influxdb-provisioner");
+ thread.setDaemon(true);
+ return thread;
+ }));
+ }
+
+ TimestreamInfluxDbService(AccountAwareStorageBackend<DbInstance> instances,
+ AccountAwareStorageBackend<DbCluster> clusters,
+ AccountAwareStorageBackend<DbParameterGroup> parameterGroups,
+ AccountAwareStorageBackend<DbBackup> backups,
+ RegionResolver regionResolver, SecretsManagerService secretsManagerService,
+ ObjectMapper objectMapper, TimestreamInfluxDbContainerManager containerManager,
+ boolean mock, Executor executor) {
+ this.instances = instances;
+ this.clusters = clusters;
+ this.parameterGroups = parameterGroups;
+ this.backups = backups;
+ this.regionResolver = regionResolver;
+ this.secretsManagerService = secretsManagerService;
+ this.objectMapper = objectMapper;
+ this.containerManager = containerManager;
+ this.mock = mock;
+ this.executor = executor;
+ }
+
+ @PreDestroy
+ void shutdown() {
+ if (executor instanceof ExecutorService executorService) {
+ executorService.shutdownNow();
+ }
+ }
+
+ public synchronized Reported<DbInstance> createDbInstance(JsonNode request, String region) {
+ String name = TimestreamInfluxDbValidation.requiredString(request, "name", 3, 40,
+ TimestreamInfluxDbValidation.RESOURCE_NAME);
+ String username = TimestreamInfluxDbValidation.optionalString(request, "username", 1, 64, null);
+ String password = TimestreamInfluxDbValidation.password(request, true);
+ String organization = TimestreamInfluxDbValidation.optionalString(request, "organization", 1, 64, null);
+ String bucket = TimestreamInfluxDbValidation.bucket(request);
+ String instanceType = TimestreamInfluxDbValidation.requiredEnum(request, "dbInstanceType",
+ TimestreamInfluxDbValidation.INSTANCE_TYPES);
+ List<String> subnets = TimestreamInfluxDbValidation.subnetIds(request, true);
+ List<String> securityGroups = TimestreamInfluxDbValidation.securityGroupIds(request, true);
+ Boolean publiclyAccessible = TimestreamInfluxDbValidation.optionalBoolean(request, "publiclyAccessible");
+ String storageType = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "dbStorageType",
+ TimestreamInfluxDbValidation.STORAGE_TYPES), "InfluxIOIncludedT1");
+ Integer allocatedStorage = TimestreamInfluxDbValidation.requiredInt(request, "allocatedStorage", 20, 15360);
+ TimestreamInfluxDbValidation.storageForType(storageType, allocatedStorage);
+ String parameterGroupId = TimestreamInfluxDbValidation.optionalString(request, "dbParameterGroupIdentifier",
+ 3, 64, TimestreamInfluxDbValidation.IDENTIFIER);
+ String deploymentType = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "deploymentType",
+ TimestreamInfluxDbValidation.INSTANCE_DEPLOYMENT_TYPES), "SINGLE_AZ");
+ JsonNode logDelivery = TimestreamInfluxDbValidation.logDeliveryConfiguration(request);
+ JsonNode maintenance = TimestreamInfluxDbValidation.maintenanceSchedule(request);
+ Map<String, String> tags = TimestreamInfluxDbValidation.tags(request, "tags", false);
+ Integer port = TimestreamInfluxDbValidation.port(request);
+ String networkType = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "networkType",
+ TimestreamInfluxDbValidation.NETWORK_TYPES), "IPV4");
+ JsonNode backupConfigurations = TimestreamInfluxDbValidation.dbBackupConfigurations(request);
+ String kmsKeyId = TimestreamInfluxDbValidation.kmsKeyId(request);
+
+ DbParameterGroup group = parameterGroupId == null ? null : requireParameterGroup(region, parameterGroupId);
+ requireV2Group(group);
+ requireUniqueInstanceName(region, name);
+
+ String accountId = regionResolver.getAccountId();
+ DbInstance instance = new DbInstance();
+ instance.setId(newId());
+ instance.setName(name);
+ instance.setArn(arn(region, "db-instance", instance.getId()));
+ instance.setAccountId(accountId);
+ instance.setRegion(region);
+ instance.setDbInstanceType(instanceType);
+ instance.setVpcSubnetIds(subnets);
+ instance.setVpcSecurityGroupIds(securityGroups);
+ instance.setPubliclyAccessible(publiclyAccessible != null && publiclyAccessible);
+ instance.setDbStorageType(storageType);
+ instance.setAllocatedStorage(allocatedStorage);
+ instance.setDbParameterGroupIdentifier(parameterGroupId);
+ instance.setDeploymentType(deploymentType);
+ instance.setLogDeliveryConfiguration(logDelivery);
+ instance.setMaintenanceSchedule(maintenance);
+ instance.setTags(tags);
+ instance.setPort(port != null ? port : V2_PORT);
+ instance.setNetworkType(networkType);
+ instance.setDbBackupConfigurations(backupConfigurations);
+ instance.setKmsKeyId(kmsKeyId);
+ instance.setEngineType(ENGINE_V2);
+ instance.setAvailabilityZone(region + "a");
+ if ("WITH_MULTIAZ_STANDBY".equals(deploymentType)) {
+ instance.setSecondaryAvailabilityZone(region + "b");
+ }
+ instance.setCreatedAt(Instant.now());
+
+ InfluxDbSetup setup = new InfluxDbSetup(orDefault(username, DEFAULT_USERNAME), password,
+ orDefault(organization, DEFAULT_ORGANIZATION), orDefault(bucket, DEFAULT_BUCKET));
+ instance.setInfluxAuthParametersSecretArn(createAuthSecret(instance.getId(), setup, region));
+
+ if (mock) {
+ instance.setEndpoint(LOCAL_ENDPOINT);
+ instance.setStatus(AVAILABLE);
+ instances.put(key(region, instance.getId()), instance);
+ } else {
+ instance.setStatus("CREATING");
+ instances.put(key(region, instance.getId()), instance);
+ provision(DB_INSTANCE, accountId, region, instance.getId(), setup, engineEnvironment(group), null,
+ "FAILED");
+ }
+ return new Reported<>(instance, "CREATING");
+ }
+
+ public DbInstance getDbInstance(JsonNode request, String region) {
+ return requireInstance(region, instanceIdentifier(request));
+ }
+
+ public PaginatedResult<DbInstance> listDbInstances(JsonNode request, String region) {
+ return page(instances.scan(regionPrefix(region)), DbInstance::getId, request);
+ }
+
+ public synchronized Reported<DbInstance> updateDbInstance(JsonNode request, String region) {
+ DbInstance instance = requireInstance(region, instanceIdentifier(request));
+ JsonNode logDelivery = TimestreamInfluxDbValidation.logDeliveryConfiguration(request);
+ String parameterGroupId = TimestreamInfluxDbValidation.optionalString(request, "dbParameterGroupIdentifier",
+ 3, 64, TimestreamInfluxDbValidation.IDENTIFIER);
+ Integer port = TimestreamInfluxDbValidation.port(request);
+ String instanceType = TimestreamInfluxDbValidation.optionalEnum(request, "dbInstanceType",
+ TimestreamInfluxDbValidation.INSTANCE_TYPES);
+ String deploymentType = TimestreamInfluxDbValidation.optionalEnum(request, "deploymentType",
+ TimestreamInfluxDbValidation.INSTANCE_DEPLOYMENT_TYPES);
+ String storageType = TimestreamInfluxDbValidation.optionalEnum(request, "dbStorageType",
+ TimestreamInfluxDbValidation.STORAGE_TYPES);
+ Integer allocatedStorage = TimestreamInfluxDbValidation.optionalInt(request, "allocatedStorage", 20, 15360);
+ JsonNode maintenance = TimestreamInfluxDbValidation.maintenanceSchedule(request);
+ JsonNode backupConfigurations = TimestreamInfluxDbValidation.dbBackupConfigurations(request);
+
+ requireStandalone(instance);
+ DbParameterGroup group = parameterGroupId == null ? null : requireParameterGroup(region, parameterGroupId);
+ requireV2Group(group);
+ TimestreamInfluxDbValidation.storageForType(orDefault(storageType, instance.getDbStorageType()),
+ allocatedStorage != null ? allocatedStorage : instance.getAllocatedStorage());
+
+ String status = "UPDATING";
+ if (instanceType != null && !instanceType.equals(instance.getDbInstanceType())) {
+ status = "UPDATING_INSTANCE_TYPE";
+ } else if (deploymentType != null && !deploymentType.equals(instance.getDeploymentType())) {
+ status = "UPDATING_DEPLOYMENT_TYPE";
+ }
+ boolean parameterGroupChanged = parameterGroupId != null
+ && !parameterGroupId.equals(instance.getDbParameterGroupIdentifier());
+
+ setIfPresent(logDelivery, instance::setLogDeliveryConfiguration);
+ setIfPresent(parameterGroupId, instance::setDbParameterGroupIdentifier);
+ setIfPresent(port, instance::setPort);
+ setIfPresent(instanceType, instance::setDbInstanceType);
+ setIfPresent(storageType, instance::setDbStorageType);
+ setIfPresent(allocatedStorage, instance::setAllocatedStorage);
+ setIfPresent(maintenance, instance::setMaintenanceSchedule);
+ setIfPresent(backupConfigurations, instance::setDbBackupConfigurations);
+ if (deploymentType != null) {
+ instance.setDeploymentType(deploymentType);
+ instance.setSecondaryAvailabilityZone("WITH_MULTIAZ_STANDBY".equals(deploymentType) ? region + "b" : null);
+ }
+
+ if (parameterGroupChanged && hasDataPlane(instance.getContainerId())) {
+ instance.setStatus(status);
+ instances.put(key(region, instance.getId()), instance);
+ provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), null, engineEnvironment(group),
+ null, "FAILED");
+ } else {
+ instance.setStatus(AVAILABLE);
+ instances.put(key(region, instance.getId()), instance);
+ }
+ return new Reported<>(instance, status);
+ }
+
+ public synchronized Reported<DbInstance> rebootDbInstance(JsonNode request, String region) {
+ DbInstance instance = requireInstance(region, instanceIdentifier(request));
+ String containerId = instance.getDbClusterId() == null
+ ? instance.getContainerId()
+ : clusterContainerId(region, instance.getDbClusterId());
+ if (hasDataPlane(containerId)) {
+ instance.setStatus("REBOOTING");
+ instances.put(key(region, instance.getId()), instance);
+ restartAsync(DB_INSTANCE, instance.getAccountId(), region, instance.getId(),
+ new InfluxDbEndpoint(containerId, instance.getEndpoint(), instance.getPort()));
+ }
+ return new Reported<>(instance, "REBOOTING");
+ }
+
+ public synchronized Reported<DbInstance> deleteDbInstance(JsonNode request, String region) {
+ DbInstance instance = requireInstance(region, instanceIdentifier(request));
+ TimestreamInfluxDbValidation.optionalBoolean(request, "retainAutomatedBackups");
+ if (instance.getDbClusterId() != null) {
+ throw conflict(DB_INSTANCE, instance.getId(), "DB instance " + instance.getId()
+ + " belongs to DB cluster " + instance.getDbClusterId()
+ + "; nodes can't be removed individually, delete the DB cluster instead.");
+ }
+ removeDataPlane(instance.getId(), instance.getContainerId());
+ instances.delete(key(region, instance.getId()));
+ return new Reported<>(instance, "DELETING");
+ }
+
+ public synchronized Reported<DbCluster> createDbCluster(JsonNode request, String region) {
+ String name = TimestreamInfluxDbValidation.requiredString(request, "name", 3, 40,
+ TimestreamInfluxDbValidation.CLUSTER_NAME);
+ String username = TimestreamInfluxDbValidation.optionalString(request, "username", 1, 64, null);
+ String password = TimestreamInfluxDbValidation.password(request, false);
+ String organization = TimestreamInfluxDbValidation.optionalString(request, "organization", 1, 64, null);
+ String bucket = TimestreamInfluxDbValidation.bucket(request);
+ Integer port = TimestreamInfluxDbValidation.port(request);
+ String parameterGroupId = TimestreamInfluxDbValidation.optionalString(request, "dbParameterGroupIdentifier",
+ 3, 64, TimestreamInfluxDbValidation.IDENTIFIER);
+ String instanceType = TimestreamInfluxDbValidation.requiredEnum(request, "dbInstanceType",
+ TimestreamInfluxDbValidation.INSTANCE_TYPES);
+ String storageType = TimestreamInfluxDbValidation.optionalEnum(request, "dbStorageType",
+ TimestreamInfluxDbValidation.STORAGE_TYPES);
+ Integer allocatedStorage = TimestreamInfluxDbValidation.optionalInt(request, "allocatedStorage", 20, 15360);
+ String networkType = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "networkType",
+ TimestreamInfluxDbValidation.NETWORK_TYPES), "IPV4");
+ Boolean publiclyAccessible = TimestreamInfluxDbValidation.optionalBoolean(request, "publiclyAccessible");
+ List<String> subnets = TimestreamInfluxDbValidation.subnetIds(request, true);
+ List<String> securityGroups = TimestreamInfluxDbValidation.securityGroupIds(request, true);
+ String deploymentType = TimestreamInfluxDbValidation.optionalEnum(request, "deploymentType",
+ TimestreamInfluxDbValidation.CLUSTER_DEPLOYMENT_TYPES);
+ String failoverMode = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "failoverMode",
+ TimestreamInfluxDbValidation.FAILOVER_MODES), "AUTOMATIC");
+ JsonNode logDelivery = TimestreamInfluxDbValidation.logDeliveryConfiguration(request);
+ JsonNode maintenance = TimestreamInfluxDbValidation.maintenanceSchedule(request);
+ JsonNode backupConfigurations = TimestreamInfluxDbValidation.dbBackupConfigurations(request);
+ String kmsKeyId = TimestreamInfluxDbValidation.kmsKeyId(request);
+ Map<String, String> tags = TimestreamInfluxDbValidation.tags(request, "tags", false);
+ if (storageType != null) {
+ TimestreamInfluxDbValidation.storageForType(storageType, allocatedStorage);
+ }
+
+ DbParameterGroup group = parameterGroupId == null ? null : requireParameterGroup(region, parameterGroupId);
+ String engineType = group == null ? ENGINE_V2 : group.getEngineType();
+ requireUniqueClusterName(region, name);
+
+ String accountId = regionResolver.getAccountId();
+ DbCluster cluster = new DbCluster();
+ cluster.setId(newId());
+ cluster.setName(name);
+ cluster.setArn(arn(region, "db-cluster", cluster.getId()));
+ cluster.setAccountId(accountId);
+ cluster.setRegion(region);
+ cluster.setEngineType(engineType);
+ cluster.setPort(port != null ? port : ENGINE_V2.equals(engineType) ? V2_PORT : V3_PORT);
+ cluster.setDbParameterGroupIdentifier(parameterGroupId);
+ cluster.setDbInstanceType(instanceType);
+ cluster.setDbStorageType(storageType != null ? storageType
+ : ENGINE_V2.equals(engineType) ? "InfluxIOIncludedT1" : null);
+ cluster.setAllocatedStorage(allocatedStorage);
+ cluster.setNetworkType(networkType);
+ cluster.setPubliclyAccessible(publiclyAccessible != null && publiclyAccessible);
+ cluster.setVpcSubnetIds(subnets);
+ cluster.setVpcSecurityGroupIds(securityGroups);
+ cluster.setDeploymentType(deploymentType != null ? deploymentType
+ : ENGINE_V2.equals(engineType) ? "MULTI_NODE_READ_REPLICAS" : null);
+ cluster.setFailoverMode(failoverMode);
+ cluster.setLogDeliveryConfiguration(logDelivery);
+ cluster.setMaintenanceSchedule(maintenance);
+ cluster.setDbBackupConfigurations(backupConfigurations);
+ cluster.setKmsKeyId(kmsKeyId);
+ cluster.setTags(tags);
+ cluster.setCreatedAt(Instant.now());
+ if (ENGINE_V3_ENTERPRISE.equals(engineType)) {
+ cluster.setClusterConfiguration(clusterConfiguration(group.getParameters().get("InfluxDBv3Enterprise")));
+ }
+
+ InfluxDbSetup setup = password == null ? null : new InfluxDbSetup(orDefault(username, DEFAULT_USERNAME),
+ password, orDefault(organization, DEFAULT_ORGANIZATION), orDefault(bucket, DEFAULT_BUCKET));
+ if (setup != null) {
+ cluster.setInfluxAuthParametersSecretArn(createAuthSecret(cluster.getId(), setup, region));
+ }
+
+ boolean containerBacked = !mock && ENGINE_V2.equals(engineType) && setup != null;
+ cluster.setStatus(containerBacked ? "CREATING" : AVAILABLE);
+ if (!containerBacked) {
+ cluster.setEndpoint(LOCAL_ENDPOINT);
+ cluster.setReaderEndpoint(LOCAL_ENDPOINT);
+ if (!mock) {
+ LOG.infov("DB cluster {0} uses engine {1}{2}; it has no InfluxDB 2.x data plane container",
+ cluster.getId(), engineType, setup == null ? " without a password" : "");
+ }
+ }
+ clusters.put(key(region, cluster.getId()), cluster);
+ for (DbInstance member : buildMembers(cluster, group)) {
+ instances.put(key(region, member.getId()), member);
+ }
+ if (containerBacked) {
+ provision(DB_CLUSTER, accountId, region, cluster.getId(), setup, engineEnvironment(group), null, "FAILED");
+ }
+ return new Reported<>(cluster, "CREATING");
+ }
+
+ public DbCluster getDbCluster(JsonNode request, String region) {
+ return requireCluster(region, clusterIdentifier(request));
+ }
+
+ public PaginatedResult<DbCluster> listDbClusters(JsonNode request, String region) {
+ return page(clusters.scan(regionPrefix(region)), DbCluster::getId, request);
+ }
+
+ public PaginatedResult<DbInstance> listDbInstancesForCluster(JsonNode request, String region) {
+ DbCluster cluster = requireCluster(region, clusterIdentifier(request));
+ return page(clusterMembers(region, cluster.getId()), DbInstance::getId, request);
+ }
+
+ public synchronized Reported<DbCluster> updateDbCluster(JsonNode request, String region) {
+ DbCluster cluster = requireCluster(region, clusterIdentifier(request));
+ JsonNode logDelivery = TimestreamInfluxDbValidation.logDeliveryConfiguration(request);
+ String parameterGroupId = TimestreamInfluxDbValidation.optionalString(request, "dbParameterGroupIdentifier",
+ 3, 64, TimestreamInfluxDbValidation.IDENTIFIER);
+ Integer port = TimestreamInfluxDbValidation.port(request);
+ String instanceType = TimestreamInfluxDbValidation.optionalEnum(request, "dbInstanceType",
+ TimestreamInfluxDbValidation.INSTANCE_TYPES);
+ String failoverMode = TimestreamInfluxDbValidation.optionalEnum(request, "failoverMode",
+ TimestreamInfluxDbValidation.FAILOVER_MODES);
+ JsonNode maintenance = TimestreamInfluxDbValidation.maintenanceSchedule(request);
+ JsonNode backupConfigurations = TimestreamInfluxDbValidation.dbBackupConfigurations(request);
+
+ DbParameterGroup group = parameterGroupId == null ? null : requireParameterGroup(region, parameterGroupId);
+ if (group != null && !group.getEngineType().equals(cluster.getEngineType())) {
+ throw validation("The DB parameter group engine " + group.getEngineType()
+ + " does not match the DB cluster engine " + cluster.getEngineType() + ".");
+ }
+ String status = instanceType != null && !instanceType.equals(cluster.getDbInstanceType())
+ ? "UPDATING_INSTANCE_TYPE" : "UPDATING";
+ boolean parameterGroupChanged = parameterGroupId != null
+ && !parameterGroupId.equals(cluster.getDbParameterGroupIdentifier());
+
+ setIfPresent(logDelivery, cluster::setLogDeliveryConfiguration);
+ setIfPresent(parameterGroupId, cluster::setDbParameterGroupIdentifier);
+ setIfPresent(port, cluster::setPort);
+ setIfPresent(instanceType, cluster::setDbInstanceType);
+ setIfPresent(failoverMode, cluster::setFailoverMode);
+ setIfPresent(maintenance, cluster::setMaintenanceSchedule);
+ setIfPresent(backupConfigurations, cluster::setDbBackupConfigurations);
+
+ boolean recreate = parameterGroupChanged && hasDataPlane(cluster.getContainerId());
+ String storedStatus = recreate ? status : AVAILABLE;
+ cluster.setStatus(storedStatus);
+ clusters.put(key(region, cluster.getId()), cluster);
+ for (DbInstance member : clusterMembers(region, cluster.getId())) {
+ setIfPresent(logDelivery, member::setLogDeliveryConfiguration);
+ setIfPresent(parameterGroupId, member::setDbParameterGroupIdentifier);
+ setIfPresent(port, member::setPort);
+ setIfPresent(instanceType, member::setDbInstanceType);
+ setIfPresent(maintenance, member::setMaintenanceSchedule);
+ member.setStatus(storedStatus);
+ instances.put(key(region, member.getId()), member);
+ }
+ if (recreate) {
+ provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), null, engineEnvironment(group),
+ null, "FAILED");
+ }
+ return new Reported<>(cluster, status);
+ }
+
+ public synchronized Reported<DbCluster> rebootDbCluster(JsonNode request, String region) {
+ DbCluster cluster = requireCluster(region, clusterIdentifier(request));
+ List<String> instanceIds = TimestreamInfluxDbValidation.stringList(request, "instanceIds", false, 0, 3,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ if (instanceIds != null) {
+ List<String> memberIds = clusterMembers(region, cluster.getId()).stream().map(DbInstance::getId).toList();
+ for (String instanceId : instanceIds) {
+ if (!memberIds.contains(instanceId)) {
+ throw notFound(DB_INSTANCE, instanceId);
+ }
+ }
+ }
+ if (hasDataPlane(cluster.getContainerId())) {
+ cluster.setStatus("REBOOTING");
+ clusters.put(key(region, cluster.getId()), cluster);
+ restartAsync(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(),
+ new InfluxDbEndpoint(cluster.getContainerId(), cluster.getEndpoint(), cluster.getPort()));
+ }
+ return new Reported<>(cluster, "REBOOTING");
+ }
+
+ public synchronized Reported<DbCluster> deleteDbCluster(JsonNode request, String region) {
+ DbCluster cluster = requireCluster(region, clusterIdentifier(request));
+ TimestreamInfluxDbValidation.optionalBoolean(request, "retainAutomatedBackups");
+ removeDataPlane(cluster.getId(), cluster.getContainerId());
+ for (DbInstance member : clusterMembers(region, cluster.getId())) {
+ instances.delete(key(region, member.getId()));
+ }
+ clusters.delete(key(region, cluster.getId()));
+ return new Reported<>(cluster, "DELETING");
+ }
+
+ public synchronized DbParameterGroup createDbParameterGroup(JsonNode request, String region) {
+ String name = TimestreamInfluxDbValidation.requiredString(request, "name", 3, 64,
+ TimestreamInfluxDbValidation.RESOURCE_NAME);
+ String description = TimestreamInfluxDbValidation.optionalString(request, "description", 0, 500, null);
+ JsonNode parameters = request.get("parameters");
+ String engineType = TimestreamInfluxDbValidation.parameters(parameters);
+ Map<String, String> tags = TimestreamInfluxDbValidation.tags(request, "tags", false);
+ parameterGroups.scan(regionPrefix(region)).stream()
+ .filter(existing -> name.equals(existing.getName()))
+ .findFirst()
+ .ifPresent(existing -> {
+ throw conflict(DB_PARAMETER_GROUP, existing.getId(),
+ "A DB parameter group named " + name + " already exists.");
+ });
+
+ DbParameterGroup group = new DbParameterGroup();
+ group.setId(newId());
+ group.setName(name);
+ group.setArn(arn(region, "db-parameter-group", group.getId()));
+ group.setDescription(description);
+ group.setParameters(parameters == null || parameters.isNull() ? null : parameters);
+ group.setEngineType(engineType);
+ group.setTags(tags);
+ group.setCreatedAt(Instant.now());
+ parameterGroups.put(key(region, group.getId()), group);
+ return group;
+ }
+
+ public DbParameterGroup getDbParameterGroup(JsonNode request, String region) {
+ String identifier = TimestreamInfluxDbValidation.requiredString(request, "identifier", 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ return requireParameterGroup(region, identifier);
+ }
+
+ public PaginatedResult<DbParameterGroup> listDbParameterGroups(JsonNode request, String region) {
+ return page(parameterGroups.scan(regionPrefix(region)), DbParameterGroup::getId, request);
+ }
+
+ public synchronized Reported<DbBackup> createDbBackup(JsonNode request, String region) {
+ String name = TimestreamInfluxDbValidation.requiredString(request, "name", 3, 40,
+ TimestreamInfluxDbValidation.RESOURCE_NAME);
+ String resourceId = TimestreamInfluxDbValidation.requiredString(request, "dbResourceId", 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ Integer retentionDays = TimestreamInfluxDbValidation.optionalInt(request, "retentionDays", 1, 3650);
+ Map<String, String> tags = TimestreamInfluxDbValidation.tags(request, "tags", false);
+ backups.scan(regionPrefix(region)).stream()
+ .filter(existing -> name.equals(existing.getName()))
+ .findFirst()
+ .ifPresent(existing -> {
+ throw conflict(DB_BACKUP, existing.getId(), "A backup named " + name + " already exists.");
+ });
+
+ DbBackup backup = new DbBackup();
+ String containerId;
+ String resourceStatus;
+ Optional<DbInstance> instance = instances.get(key(region, resourceId));
+ if (instance.isPresent()) {
+ DbInstance source = instance.get();
+ snapshotInstance(backup, source);
+ containerId = source.getDbClusterId() == null
+ ? source.getContainerId() : clusterContainerId(region, source.getDbClusterId());
+ resourceStatus = source.getStatus();
+ } else {
+ DbCluster source = clusters.get(key(region, resourceId))
+ .orElseThrow(() -> notFound(DB_INSTANCE, resourceId));
+ snapshotCluster(backup, source);
+ containerId = source.getContainerId();
+ resourceStatus = source.getStatus();
+ }
+ if (!AVAILABLE.equals(resourceStatus)) {
+ throw conflict(backup.getResourceType(), resourceId,
+ "Resource " + resourceId + " must be AVAILABLE to take a backup; it is " + resourceStatus + ".");
+ }
+
+ Instant now = Instant.now();
+ backup.setId(newId());
+ backup.setName(name);
+ backup.setArn(arn(region, "db-backup", backup.getId()));
+ backup.setAccountId(regionResolver.getAccountId());
+ backup.setRegion(region);
+ backup.setCreatedAt(now);
+ backup.setType("ON_DEMAND");
+ backup.setDbResourceId(resourceId);
+ backup.setTags(tags);
+ if (retentionDays != null) {
+ backup.setExpiresAfter(LocalDate.ofInstant(now.plus(retentionDays, ChronoUnit.DAYS), ZoneOffset.UTC)
+ .toString());
+ }
+
+ if (hasDataPlane(containerId)) {
+ backup.setStatus("IN_PROGRESS");
+ backups.put(key(region, backup.getId()), backup);
+ captureBackupAsync(backup.getAccountId(), region, backup.getId(), containerId);
+ } else {
+ backup.setStatus("COMPLETED");
+ backups.put(key(region, backup.getId()), backup);
+ }
+ return new Reported<>(backup, "IN_PROGRESS");
+ }
+
+ public DbBackup getDbBackup(JsonNode request, String region) {
+ return requireBackup(region, backupIdentifier(request, "identifier"));
+ }
+
+ public PaginatedResult<DbBackup> listDbBackups(JsonNode request, String region) {
+ String resourceId = TimestreamInfluxDbValidation.optionalString(request, "dbResourceId", 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ List<DbBackup> items = backups.scan(regionPrefix(region)).stream()
+ .filter(backup -> resourceId == null || resourceId.equals(backup.getDbResourceId()))
+ .toList();
+ return page(new ArrayList<>(items), DbBackup::getId, request);
+ }
+
+ public synchronized Reported<DbBackup> deleteDbBackup(JsonNode request, String region) {
+ DbBackup backup = requireBackup(region, backupIdentifier(request, "identifier"));
+ backups.delete(key(region, backup.getId()));
+ if (backup.isDataCaptured()) {
+ containerManager.deleteBackupArtifacts(backup.getId());
+ }
+ return new Reported<>(backup, "DELETING");
+ }
+
+ public synchronized RestoreResult restoreFromDbBackup(JsonNode request, String region) {
+ String name = TimestreamInfluxDbValidation.requiredString(request, "name", 3, 40,
+ TimestreamInfluxDbValidation.RESOURCE_NAME);
+ DbBackup backup = requireBackup(region, backupIdentifier(request, "dbBackupId"));
+ JsonNode restoreToTime = request.get("restoreToTime");
+ if (restoreToTime != null && !restoreToTime.isNull() && !restoreToTime.isNumber() && !restoreToTime.isTextual()) {
+ throw validation("restoreToTime must be a timestamp.");
+ }
+ if (restoreToTime != null && !restoreToTime.isNull()) {
+ throw validation("restoreToTime requests a point-in-time restore, which is only available for "
+ + "continuous backups and is not supported.");
+ }
+ String restoreMode = orDefault(TimestreamInfluxDbValidation.optionalEnum(request, "restoreMode",
+ TimestreamInfluxDbValidation.RESTORE_MODES), "NEW_RESOURCE");
+ List<String> subnets = TimestreamInfluxDbValidation.subnetIds(request, false);
+ List<String> securityGroups = TimestreamInfluxDbValidation.securityGroupIds(request, false);
+ Boolean publiclyAccessible = TimestreamInfluxDbValidation.optionalBoolean(request, "publiclyAccessible");
+ JsonNode logDelivery = TimestreamInfluxDbValidation.logDeliveryConfiguration(request);
+ JsonNode maintenance = TimestreamInfluxDbValidation.maintenanceSchedule(request);
+ Map<String, String> tags = TimestreamInfluxDbValidation.tags(request, "tags", false);
+ Integer port = TimestreamInfluxDbValidation.port(request);
+ String networkType = TimestreamInfluxDbValidation.optionalEnum(request, "networkType",
+ TimestreamInfluxDbValidation.NETWORK_TYPES);
+ String deploymentType = TimestreamInfluxDbValidation.optionalEnum(request, "deploymentType",
+ TimestreamInfluxDbValidation.RESOURCE_DEPLOYMENT_TYPES);
+ JsonNode backupConfigurations = TimestreamInfluxDbValidation.dbBackupConfigurations(request);
+ String kmsKeyId = TimestreamInfluxDbValidation.kmsKeyId(request);
+
+ if (!"COMPLETED".equals(backup.getStatus())) {
+ throw conflict(DB_BACKUP, backup.getId(), "Backup " + backup.getId() + " is not COMPLETED.");
+ }
+ boolean clusterBackup = DB_CLUSTER.equals(backup.getResourceType());
+ String effectiveDeployment = orDefault(deploymentType, backup.getDeploymentType());
+ if (deploymentType != null && clusterBackup != "MULTI_NODE_READ_REPLICAS".equals(deploymentType)) {
+ throw validation("deploymentType " + deploymentType + " is not valid for a "
+ + backup.getResourceType() + " backup.");
+ }
+
+ if ("REPLACE_EXISTING".equals(restoreMode)) {
+ rejectReplaceExistingOverrides(request);
+ return replaceExisting(backup, region, name, clusterBackup);
+ }
+
+ if (clusterBackup) {
+ requireUniqueClusterName(region, name);
+ DbCluster cluster = new DbCluster();
+ cluster.setId(newId());
+ cluster.setName(name);
+ cluster.setArn(arn(region, "db-cluster", cluster.getId()));
+ cluster.setAccountId(backup.getAccountId());
+ cluster.setRegion(region);
+ cluster.setEngineType(backup.getEngineType());
+ cluster.setDeploymentType(effectiveDeployment);
+ cluster.setDbInstanceType(backup.getDbInstanceType());
+ cluster.setDbStorageType(backup.getDbStorageType());
+ cluster.setAllocatedStorage(backup.getAllocatedStorage());
+ cluster.setDbParameterGroupIdentifier(backup.getDbParameterGroupId());
+ cluster.setFailoverMode(backup.getFailoverMode());
+ cluster.setClusterConfiguration(backup.getClusterConfiguration());
+ cluster.setInfluxAuthParametersSecretArn(backup.getInfluxAuthParametersSecretArn());
+ cluster.setPort(port != null ? port : backup.getPort());
+ cluster.setNetworkType(orDefault(networkType, backup.getNetworkType()));
+ cluster.setPubliclyAccessible(publiclyAccessible != null ? publiclyAccessible : backup.getPubliclyAccessible());
+ cluster.setVpcSubnetIds(subnets != null ? subnets : backup.getVpcSubnetIds());
+ cluster.setVpcSecurityGroupIds(securityGroups != null ? securityGroups : backup.getVpcSecurityGroupIds());
+ cluster.setLogDeliveryConfiguration(logDelivery != null ? logDelivery : backup.getLogDeliveryConfiguration());
+ cluster.setMaintenanceSchedule(maintenance != null ? maintenance : backup.getMaintenanceSchedule());
+ cluster.setDbBackupConfigurations(backupConfigurations != null
+ ? backupConfigurations : backup.getDbBackupConfigurations());
+ cluster.setKmsKeyId(orDefault(kmsKeyId, backup.getKmsKeyId()));
+ cluster.setTags(tags);
+ cluster.setCreatedAt(Instant.now());
+ boolean restoreData = restoresData(backup);
+ cluster.setStatus(restoreData ? "RESTORING" : AVAILABLE);
+ cluster.setEndpoint(restoreData ? null : LOCAL_ENDPOINT);
+ cluster.setReaderEndpoint(restoreData ? null : LOCAL_ENDPOINT);
+ clusters.put(key(region, cluster.getId()), cluster);
+ DbParameterGroup group = parameterGroups.get(key(region, orDefault(backup.getDbParameterGroupId(), "")))
+ .orElse(null);
+ for (DbInstance member : buildMembers(cluster, group)) {
+ instances.put(key(region, member.getId()), member);
+ }
+ if (restoreData) {
+ provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), temporarySetup(),
+ engineEnvironment(group), backup.getId(), "RESTORE_FAILED");
+ }
+ return new RestoreResult(cluster.getId(), DB_CLUSTER, cluster.getEngineType(), cluster.getDeploymentType());
+ }
+
+ requireUniqueInstanceName(region, name);
+ DbInstance instance = new DbInstance();
+ instance.setId(newId());
+ instance.setName(name);
+ instance.setArn(arn(region, "db-instance", instance.getId()));
+ instance.setAccountId(backup.getAccountId());
+ instance.setRegion(region);
+ instance.setEngineType(backup.getEngineType());
+ instance.setDeploymentType(effectiveDeployment);
+ instance.setDbInstanceType(backup.getDbInstanceType());
+ instance.setDbStorageType(backup.getDbStorageType());
+ instance.setAllocatedStorage(backup.getAllocatedStorage());
+ instance.setDbParameterGroupIdentifier(backup.getDbParameterGroupId());
+ instance.setInfluxAuthParametersSecretArn(backup.getInfluxAuthParametersSecretArn());
+ instance.setPort(port != null ? port : backup.getPort());
+ instance.setNetworkType(orDefault(networkType, backup.getNetworkType()));
+ instance.setPubliclyAccessible(publiclyAccessible != null ? publiclyAccessible : backup.getPubliclyAccessible());
+ instance.setVpcSubnetIds(subnets != null ? subnets : backup.getVpcSubnetIds());
+ instance.setVpcSecurityGroupIds(securityGroups != null ? securityGroups : backup.getVpcSecurityGroupIds());
+ instance.setLogDeliveryConfiguration(logDelivery != null ? logDelivery : backup.getLogDeliveryConfiguration());
+ instance.setMaintenanceSchedule(maintenance != null ? maintenance : backup.getMaintenanceSchedule());
+ instance.setDbBackupConfigurations(backupConfigurations != null
+ ? backupConfigurations : backup.getDbBackupConfigurations());
+ instance.setKmsKeyId(orDefault(kmsKeyId, backup.getKmsKeyId()));
+ instance.setAvailabilityZone(region + "a");
+ if ("WITH_MULTIAZ_STANDBY".equals(effectiveDeployment)) {
+ instance.setSecondaryAvailabilityZone(region + "b");
+ }
+ instance.setTags(tags);
+ instance.setCreatedAt(Instant.now());
+ boolean restoreData = restoresData(backup);
+ instance.setStatus(restoreData ? "RESTORING" : AVAILABLE);
+ instance.setEndpoint(restoreData ? null : LOCAL_ENDPOINT);
+ instances.put(key(region, instance.getId()), instance);
+ if (restoreData) {
+ DbParameterGroup group = parameterGroups.get(key(region, orDefault(backup.getDbParameterGroupId(), "")))
+ .orElse(null);
+ provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), temporarySetup(),
+ engineEnvironment(group), backup.getId(), "RESTORE_FAILED");
+ }
+ return new RestoreResult(instance.getId(), DB_INSTANCE, instance.getEngineType(), instance.getDeploymentType());
+ }
+
+ public synchronized Map<String, String> listTagsForResource(JsonNode request) {
+ return tagsOf(TimestreamInfluxDbValidation.requiredString(request, "resourceArn", 1, 1011, null));
+ }
+
+ public synchronized void tagResource(JsonNode request) {
+ String arn = TimestreamInfluxDbValidation.requiredString(request, "resourceArn", 1, 1011, null);
+ Map<String, String> added = TimestreamInfluxDbValidation.tags(request, "tags", true);
+ Map<String, String> tags = tagsOf(arn);
+ tags.putAll(added);
+ if (tags.size() > MAX_TAGS) {
+ throw new AwsException("ServiceQuotaExceededException",
+ "A resource can have at most " + MAX_TAGS + " tags.", 400);
+ }
+ saveTags(arn, tags);
+ }
+
+ public synchronized void untagResource(JsonNode request) {
+ String arn = TimestreamInfluxDbValidation.requiredString(request, "resourceArn", 1, 1011, null);
+ List<String> keys = TimestreamInfluxDbValidation.tagKeys(request);
+ Map<String, String> tags = tagsOf(arn);
+ keys.forEach(tags::remove);
+ saveTags(arn, tags);
+ }
+
+ /**
+ * Starts the InfluxDB containers of resources persisted by a previous run. Their volumes still
+ * hold the data and CLI configuration, so the image skips its setup mode and needs no credentials.
+ */
+ public void restorePersistedRuntime() {
+ if (mock) {
+ return;
+ }
+ for (DbInstance instance : instances.scanAllAccounts()) {
+ if (instance.getContainerId() != null && instance.getDbClusterId() == null) {
+ DbParameterGroup group = parameterGroup(instance.getAccountId(), instance.getRegion(),
+ instance.getDbParameterGroupIdentifier());
+ provision(DB_INSTANCE, instance.getAccountId(), instance.getRegion(), instance.getId(), null,
+ engineEnvironment(group), null, "FAILED");
+ }
+ }
+ for (DbCluster cluster : clusters.scanAllAccounts()) {
+ if (cluster.getContainerId() != null) {
+ DbParameterGroup group = parameterGroup(cluster.getAccountId(), cluster.getRegion(),
+ cluster.getDbParameterGroupIdentifier());
+ provision(DB_CLUSTER, cluster.getAccountId(), cluster.getRegion(), cluster.getId(), null,
+ engineEnvironment(group), null, "FAILED");
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ if (!mock) {
+ try {
+ containerManager.stopManagedContainers();
+ } catch (RuntimeException e) {
+ LOG.warnv("Failed to stop Timestream for InfluxDB containers during reset: {0}", e.getMessage());
+ }
+ }
+ instances.clear();
+ clusters.clear();
+ parameterGroups.clear();
+ backups.clear();
+ }
+
+ private void provision(String resourceType, String accountId, String region, String resourceId,
+ InfluxDbSetup setup, Map<String, String> engineEnvironment, String backupId,
+ String failureStatus) {
+ executor.execute(() -> {
+ InfluxDbEndpoint endpoint = null;
+ try {
+ if (!containerManager.isDockerReachable()) {
+ LOG.warnv("No Docker daemon is reachable; {0} {1} is available without an InfluxDB container",
+ resourceType, resourceId);
+ applyDataPlane(resourceType, accountId, region, resourceId, null, AVAILABLE);
+ return;
+ }
+ endpoint = containerManager.start(resourceId, accountId, region, setup, engineEnvironment);
+ containerManager.waitUntilReady(endpoint);
+ if (backupId != null) {
+ containerManager.restore(endpoint.containerId(), backupId);
+ }
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE);
+ } catch (RuntimeException e) {
+ LOG.errorv(e, "Failed to provision the InfluxDB container for {0} {1}", resourceType, resourceId);
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, failureStatus);
+ }
+ });
+ }
+
+ private void restartAsync(String resourceType, String accountId, String region, String resourceId,
+ InfluxDbEndpoint endpoint) {
+ executor.execute(() -> {
+ try {
+ containerManager.restart(endpoint.containerId());
+ containerManager.waitUntilReady(endpoint);
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE);
+ } catch (RuntimeException e) {
+ LOG.errorv(e, "Failed to reboot the InfluxDB container for {0} {1}", resourceType, resourceId);
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, "REBOOT_FAILED");
+ }
+ });
+ }
+
+ private void captureBackupAsync(String accountId, String region, String backupId, String containerId) {
+ executor.execute(() -> {
+ boolean captured = false;
+ try {
+ containerManager.backup(containerId, backupId);
+ captured = true;
+ } catch (RuntimeException e) {
+ LOG.errorv(e, "Failed to capture InfluxDB backup {0}", backupId);
+ }
+ synchronized (this) {
+ Optional<DbBackup> stored = backups.getForAccount(accountId, key(region, backupId));
+ if (stored.isEmpty()) {
+ if (captured) {
+ containerManager.deleteBackupArtifacts(backupId);
+ }
+ return;
+ }
+ DbBackup backup = stored.get();
+ backup.setDataCaptured(captured);
+ backup.setStatus(captured ? "COMPLETED" : "FAILED");
+ backups.putForAccount(accountId, key(region, backupId), backup);
+ }
+ });
+ }
+
+ private synchronized void applyDataPlane(String resourceType, String accountId, String region, String resourceId,
+ InfluxDbEndpoint endpoint, String status) {
+ String host = endpoint != null ? endpoint.host() : LOCAL_ENDPOINT;
+ if (DB_INSTANCE.equals(resourceType)) {
+ Optional<DbInstance> stored = instances.getForAccount(accountId, key(region, resourceId));
+ if (stored.isEmpty()) {
+ discardOrphan(resourceId, endpoint);
+ return;
+ }
+ DbInstance instance = stored.get();
+ instance.setEndpoint(host);
+ if (endpoint != null) {
+ instance.setPort(endpoint.port());
+ instance.setContainerId(endpoint.containerId());
+ }
+ instance.setStatus(status);
+ instances.putForAccount(accountId, key(region, resourceId), instance);
+ return;
+ }
+ Optional<DbCluster> stored = clusters.getForAccount(accountId, key(region, resourceId));
+ if (stored.isEmpty()) {
+ discardOrphan(resourceId, endpoint);
+ return;
+ }
+ DbCluster cluster = stored.get();
+ cluster.setEndpoint(host);
+ cluster.setReaderEndpoint(host);
+ if (endpoint != null) {
+ cluster.setPort(endpoint.port());
+ cluster.setContainerId(endpoint.containerId());
+ }
+ cluster.setStatus(status);
+ clusters.putForAccount(accountId, key(region, resourceId), cluster);
+ for (DbInstance member : instances.scanForAccount(accountId, regionPrefix(region))) {
+ if (resourceId.equals(member.getDbClusterId())) {
+ member.setEndpoint(host);
+ member.setPort(cluster.getPort());
+ member.setStatus(status);
+ instances.putForAccount(accountId, key(region, member.getId()), member);
+ }
+ }
+ }
+
+ private void discardOrphan(String resourceId, InfluxDbEndpoint endpoint) {
+ if (endpoint != null) {
+ removeDataPlane(resourceId, endpoint.containerId());
+ }
+ }
+
+ private void removeDataPlane(String resourceId, String containerId) {
+ if (mock) {
+ return;
+ }
+ try {
+ containerManager.stop(resourceId, containerId);
+ containerManager.removeStorage(resourceId);
+ } catch (RuntimeException e) {
+ LOG.warnv("Failed to remove the InfluxDB container for {0}: {1}", resourceId, e.getMessage());
+ }
+ }
+
+ private boolean hasDataPlane(String containerId) {
+ return !mock && containerId != null;
+ }
+
+ private boolean restoresData(DbBackup backup) {
+ return !mock && backup.isDataCaptured();
+ }
+
+ private InfluxDbSetup temporarySetup() {
+ String secret = UUID.randomUUID().toString().replace("-", "");
+ return new InfluxDbSetup("floci-restore", secret, "floci-restore", "floci-restore");
+ }
+
+ /**
+ * Restoring into an existing resource accepts no parameters beyond the resource to restore and the
+ * backup to restore from: the existing configuration is left untouched and only the data is restored.
+ */
+ private static void rejectReplaceExistingOverrides(JsonNode request) {
+ for (String member : REPLACE_EXISTING_REJECTED_MEMBERS) {
+ JsonNode value = request.get(member);
+ if (value != null && !value.isNull()) {
+ throw validation(member + " can't be supplied when restoreMode is REPLACE_EXISTING: restoring "
+ + "into an existing resource makes no configuration changes.");
+ }
+ }
+ }
+
+ private RestoreResult replaceExisting(DbBackup backup, String region, String name, boolean clusterBackup) {
+ if (clusterBackup) {
+ DbCluster cluster = requireCluster(region, backup.getDbResourceId());
+ if (!name.equals(cluster.getName())) {
+ throw validation("name must match the existing resource name when restoreMode is REPLACE_EXISTING.");
+ }
+ if (restoresData(backup) && cluster.getContainerId() != null) {
+ cluster.setStatus("RESTORING");
+ clusters.put(key(region, cluster.getId()), cluster);
+ restoreInPlaceAsync(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(),
+ new InfluxDbEndpoint(cluster.getContainerId(), cluster.getEndpoint(), cluster.getPort()),
+ backup.getId());
+ }
+ return new RestoreResult(cluster.getId(), DB_CLUSTER, cluster.getEngineType(), cluster.getDeploymentType());
+ }
+ DbInstance instance = requireInstance(region, backup.getDbResourceId());
+ if (!name.equals(instance.getName())) {
+ throw validation("name must match the existing resource name when restoreMode is REPLACE_EXISTING.");
+ }
+ if (restoresData(backup) && instance.getContainerId() != null) {
+ instance.setStatus("RESTORING");
+ instances.put(key(region, instance.getId()), instance);
+ restoreInPlaceAsync(DB_INSTANCE, instance.getAccountId(), region, instance.getId(),
+ new InfluxDbEndpoint(instance.getContainerId(), instance.getEndpoint(), instance.getPort()),
+ backup.getId());
+ }
+ return new RestoreResult(instance.getId(), DB_INSTANCE, instance.getEngineType(), instance.getDeploymentType());
+ }
+
+ private void restoreInPlaceAsync(String resourceType, String accountId, String region, String resourceId,
+ InfluxDbEndpoint endpoint, String backupId) {
+ executor.execute(() -> {
+ try {
+ containerManager.restore(endpoint.containerId(), backupId);
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE);
+ } catch (RuntimeException e) {
+ LOG.errorv(e, "Failed to restore backup {0} into {1} {2}", backupId, resourceType, resourceId);
+ applyDataPlane(resourceType, accountId, region, resourceId, endpoint, "RESTORE_FAILED");
+ }
+ });
+ }
+
+ private List<DbInstance> buildMembers(DbCluster cluster, DbParameterGroup group) {
+ List<List<String>> modes = new ArrayList<>();
+ switch (cluster.getEngineType()) {
+ case ENGINE_V3_CORE -> modes.add(List.of("INGEST", "QUERY", "COMPACT", "PROCESS"));
+ case ENGINE_V3_ENTERPRISE -> {
+ JsonNode configuration = cluster.getClusterConfiguration();
+ int ingestQuery = configuration.path("ingestQueryInstances").asInt(1);
+ int queryOnly = configuration.path("queryOnlyInstances").asInt(0);
+ boolean dedicatedCompactor = configuration.path("dedicatedCompactor").asBoolean(false);
+ for (int i = 0; i < ingestQuery; i++) {
+ modes.add(i == 0 && !dedicatedCompactor
+ ? List.of("INGEST", "QUERY", "COMPACT") : List.of("INGEST", "QUERY"));
+ }
+ for (int i = 0; i < queryOnly; i++) {
+ modes.add(List.of("QUERY"));
+ }
+ if (dedicatedCompactor) {
+ modes.add(List.of("COMPACT"));
+ }
+ }
+ default -> {
+ modes.add(List.of("PRIMARY"));
+ modes.add(List.of("REPLICA"));
+ }
+ }
+ List<DbInstance> members = new ArrayList<>();
+ String baseName = cluster.getName().length() > 36 ? cluster.getName().substring(0, 36) : cluster.getName();
+ for (int i = 0; i < modes.size(); i++) {
+ DbInstance member = new DbInstance();
+ member.setId(newId());
+ member.setName(baseName + "-" + (i + 1));
+ member.setArn(arn(cluster.getRegion(), "db-instance", member.getId()));
+ member.setAccountId(cluster.getAccountId());
+ member.setRegion(cluster.getRegion());
+ member.setStatus(cluster.getStatus());
+ member.setEndpoint(cluster.getEndpoint());
+ member.setPort(cluster.getPort());
+ member.setNetworkType(cluster.getNetworkType());
+ member.setDbInstanceType(cluster.getDbInstanceType());
+ member.setDbStorageType(cluster.getDbStorageType());
+ member.setAllocatedStorage(cluster.getAllocatedStorage());
+ member.setVpcSubnetIds(cluster.getVpcSubnetIds());
+ member.setVpcSecurityGroupIds(cluster.getVpcSecurityGroupIds());
+ member.setPubliclyAccessible(cluster.getPubliclyAccessible());
+ member.setDbParameterGroupIdentifier(group == null ? null : group.getId());
+ member.setAvailabilityZone(cluster.getRegion() + (char) ('a' + (i % 3)));
+ member.setLogDeliveryConfiguration(cluster.getLogDeliveryConfiguration());
+ member.setMaintenanceSchedule(cluster.getMaintenanceSchedule());
+ member.setInfluxAuthParametersSecretArn(cluster.getInfluxAuthParametersSecretArn());
+ member.setKmsKeyId(cluster.getKmsKeyId());
+ member.setEngineType(cluster.getEngineType());
+ member.setDbClusterId(cluster.getId());
+ member.setInstanceModes(modes.get(i));
+ member.setInstanceMode(modes.get(i).get(0));
+ member.setCreatedAt(cluster.getCreatedAt());
+ members.add(member);
+ }
+ return members;
+ }
+
+ private ObjectNode clusterConfiguration(JsonNode enterpriseParameters) {
+ ObjectNode configuration = objectMapper.createObjectNode();
+ configuration.put("ingestQueryInstances", enterpriseParameters.path("ingestQueryInstances").asInt());
+ configuration.put("queryOnlyInstances", enterpriseParameters.path("queryOnlyInstances").asInt());
+ configuration.put("dedicatedCompactor", enterpriseParameters.path("dedicatedCompactor").asBoolean());
+ return configuration;
+ }
+
+ private void snapshotInstance(DbBackup backup, DbInstance source) {
+ backup.setResourceType(DB_INSTANCE);
+ backup.setEngineType(source.getEngineType());
+ backup.setDeploymentType(source.getDeploymentType());
+ backup.setKmsKeyId(source.getKmsKeyId());
+ backup.setDbParameterGroupId(source.getDbParameterGroupIdentifier());
+ backup.setDbInstanceType(source.getDbInstanceType());
+ backup.setLogDeliveryConfiguration(source.getLogDeliveryConfiguration());
+ backup.setDbStorageType(source.getDbStorageType());
+ backup.setAllocatedStorage(source.getAllocatedStorage());
+ backup.setVpcSubnetIds(source.getVpcSubnetIds());
+ backup.setVpcSecurityGroupIds(source.getVpcSecurityGroupIds());
+ backup.setPubliclyAccessible(source.getPubliclyAccessible());
+ backup.setPort(source.getPort());
+ backup.setNetworkType(source.getNetworkType());
+ backup.setInfluxAuthParametersSecretArn(source.getInfluxAuthParametersSecretArn());
+ backup.setMaintenanceSchedule(source.getMaintenanceSchedule());
+ backup.setDbBackupConfigurations(source.getDbBackupConfigurations());
+ }
+
+ private void snapshotCluster(DbBackup backup, DbCluster source) {
+ backup.setResourceType(DB_CLUSTER);
+ backup.setEngineType(source.getEngineType());
+ backup.setDeploymentType(source.getDeploymentType());
+ backup.setKmsKeyId(source.getKmsKeyId());
+ backup.setClusterConfiguration(source.getClusterConfiguration());
+ backup.setDbParameterGroupId(source.getDbParameterGroupIdentifier());
+ backup.setDbInstanceType(source.getDbInstanceType());
+ backup.setLogDeliveryConfiguration(source.getLogDeliveryConfiguration());
+ backup.setFailoverMode(source.getFailoverMode());
+ backup.setDbStorageType(source.getDbStorageType());
+ backup.setAllocatedStorage(source.getAllocatedStorage());
+ backup.setVpcSubnetIds(source.getVpcSubnetIds());
+ backup.setVpcSecurityGroupIds(source.getVpcSecurityGroupIds());
+ backup.setPubliclyAccessible(source.getPubliclyAccessible());
+ backup.setPort(source.getPort());
+ backup.setNetworkType(source.getNetworkType());
+ backup.setInfluxAuthParametersSecretArn(source.getInfluxAuthParametersSecretArn());
+ backup.setMaintenanceSchedule(source.getMaintenanceSchedule());
+ backup.setDbBackupConfigurations(source.getDbBackupConfigurations());
+ }
+
+ private String createAuthSecret(String resourceId, InfluxDbSetup setup, String region) {
+ ObjectNode value = objectMapper.createObjectNode();
+ value.put("organization", setup.organization());
+ value.put("bucket", setup.bucket());
+ value.put("username", setup.username());
+ value.put("password", setup.password());
+ try {
+ Secret secret = secretsManagerService.createSecret("READONLY-InfluxDB-auth-parameters-" + resourceId,
+ value.toString(), null, "Initial InfluxDB authorization parameters for " + resourceId,
+ null, null, region);
+ return secret == null ? null : secret.getArn();
+ } catch (AwsException e) {
+ LOG.warnv("Could not create the InfluxDB auth parameters secret for {0}: {1}", resourceId, e.getMessage());
+ return null;
+ }
+ }
+
+ private Map<String, String> engineEnvironment(DbParameterGroup group) {
+ if (group == null || !ENGINE_V2.equals(group.getEngineType()) || group.getParameters() == null) {
+ return Map.of();
+ }
+ return TimestreamInfluxDbContainerManager.engineEnvironment(group.getParameters().get("InfluxDBv2"));
+ }
+
+ private Map<String, String> tagsOf(String arn) {
+ ArnReference reference = parseArn(arn);
+ return switch (reference.type()) {
+ case "db-instance" -> new LinkedHashMap<>(requireInstance(reference.region(), reference.id()).getTags());
+ case "db-cluster" -> new LinkedHashMap<>(requireCluster(reference.region(), reference.id()).getTags());
+ case "db-parameter-group" -> new LinkedHashMap<>(
+ requireParameterGroup(reference.region(), reference.id()).getTags());
+ default -> new LinkedHashMap<>(requireBackup(reference.region(), reference.id()).getTags());
+ };
+ }
+
+ private void saveTags(String arn, Map<String, String> tags) {
+ ArnReference reference = parseArn(arn);
+ String storageKey = key(reference.region(), reference.id());
+ switch (reference.type()) {
+ case "db-instance" -> {
+ DbInstance instance = requireInstance(reference.region(), reference.id());
+ instance.setTags(tags);
+ instances.put(storageKey, instance);
+ }
+ case "db-cluster" -> {
+ DbCluster cluster = requireCluster(reference.region(), reference.id());
+ cluster.setTags(tags);
+ clusters.put(storageKey, cluster);
+ }
+ case "db-parameter-group" -> {
+ DbParameterGroup group = requireParameterGroup(reference.region(), reference.id());
+ group.setTags(tags);
+ parameterGroups.put(storageKey, group);
+ }
+ default -> {
+ DbBackup backup = requireBackup(reference.region(), reference.id());
+ backup.setTags(tags);
+ backups.put(storageKey, backup);
+ }
+ }
+ }
+
+ private record ArnReference(String region, String accountId, String type, String id) {
+ }
+
+ private ArnReference parseArn(String arn) {
+ Matcher matcher = TimestreamInfluxDbValidation.ARN.matcher(arn);
+ if (!matcher.matches()) {
+ throw validation("resourceArn is not a valid Timestream for InfluxDB ARN.");
+ }
+ ArnReference reference = new ArnReference(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4));
+ if (!reference.accountId().equals(regionResolver.getAccountId())) {
+ throw notFound(resourceTypeFor(reference.type()), reference.id());
+ }
+ return reference;
+ }
+
+ private static String resourceTypeFor(String arnType) {
+ return switch (arnType) {
+ case "db-instance" -> DB_INSTANCE;
+ case "db-cluster" -> DB_CLUSTER;
+ case "db-parameter-group" -> DB_PARAMETER_GROUP;
+ default -> DB_BACKUP;
+ };
+ }
+
+ private void requireStandalone(DbInstance instance) {
+ if (instance.getDbClusterId() != null) {
+ throw conflict(DB_INSTANCE, instance.getId(), "DB instance " + instance.getId()
+ + " belongs to DB cluster " + instance.getDbClusterId() + "; update the DB cluster instead.");
+ }
+ }
+
+ private static void requireV2Group(DbParameterGroup group) {
+ if (group != null && !ENGINE_V2.equals(group.getEngineType())) {
+ throw validation("DB instances support only InfluxDB v2 parameter groups; use CreateDbCluster for InfluxDB 3.");
+ }
+ }
+
+ private void requireUniqueInstanceName(String region, String name) {
+ instances.scan(regionPrefix(region)).stream()
+ .filter(existing -> existing.getDbClusterId() == null && name.equals(existing.getName()))
+ .findFirst()
+ .ifPresent(existing -> {
+ throw conflict(DB_INSTANCE, existing.getId(), "A DB instance named " + name + " already exists.");
+ });
+ }
+
+ private void requireUniqueClusterName(String region, String name) {
+ clusters.scan(regionPrefix(region)).stream()
+ .filter(existing -> name.equals(existing.getName()))
+ .findFirst()
+ .ifPresent(existing -> {
+ throw conflict(DB_CLUSTER, existing.getId(), "A DB cluster named " + name + " already exists.");
+ });
+ }
+
+ private String clusterContainerId(String region, String clusterId) {
+ return clusters.get(key(region, clusterId)).map(DbCluster::getContainerId).orElse(null);
+ }
+
+ private List<DbInstance> clusterMembers(String region, String clusterId) {
+ return instances.scan(regionPrefix(region)).stream()
+ .filter(instance -> clusterId.equals(instance.getDbClusterId()))
+ .sorted(Comparator.comparing(DbInstance::getName))
+ .toList();
+ }
+
+ private DbParameterGroup parameterGroup(String accountId, String region, String groupId) {
+ if (groupId == null) {
+ return null;
+ }
+ return parameterGroups.getForAccount(accountId, key(region, groupId)).orElse(null);
+ }
+
+ private DbInstance requireInstance(String region, String id) {
+ return instances.get(key(region, id)).orElseThrow(() -> notFound(DB_INSTANCE, id));
+ }
+
+ private DbCluster requireCluster(String region, String id) {
+ return clusters.get(key(region, id)).orElseThrow(() -> notFound(DB_CLUSTER, id));
+ }
+
+ private DbParameterGroup requireParameterGroup(String region, String id) {
+ return parameterGroups.get(key(region, id)).orElseThrow(() -> notFound(DB_PARAMETER_GROUP, id));
+ }
+
+ private DbBackup requireBackup(String region, String id) {
+ return backups.get(key(region, id)).orElseThrow(() -> notFound(DB_BACKUP, id));
+ }
+
+ private static String instanceIdentifier(JsonNode request) {
+ return TimestreamInfluxDbValidation.requiredString(request, "identifier", 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ }
+
+ private static String clusterIdentifier(JsonNode request) {
+ return TimestreamInfluxDbValidation.requiredString(request, "dbClusterId", 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ }
+
+ private static String backupIdentifier(JsonNode request, String field) {
+ return TimestreamInfluxDbValidation.requiredString(request, field, 3, 64,
+ TimestreamInfluxDbValidation.IDENTIFIER);
+ }
+
+ private static <T> PaginatedResult<T> page(List<T> items, Function<T, String> cursor, JsonNode request) {
+ Integer maxResults = TimestreamInfluxDbValidation.maxResults(request);
+ String nextToken = TimestreamInfluxDbValidation.nextToken(request);
+ try {
+ return Pagination.paginate(items, cursor, maxResults, nextToken, MAX_PAGE, "ValidationException");
+ } catch (AwsException e) {
+ throw validation(e.getMessage());
+ }
+ }
+
+ private String arn(String region, String resourceType, String id) {
+ return regionResolver.buildArn(ARN_SERVICE, region, resourceType + "/" + id);
+ }
+
+ private static String newId() {
+ return UUID.randomUUID().toString().replace("-", "").substring(0, 10);
+ }
+
+ private static String key(String region, String id) {
+ return region + ":" + id;
+ }
+
+ private static Predicate<String> regionPrefix(String region) {
+ return storageKey -> storageKey.startsWith(region + ":");
+ }
+
+ private static <T> T orDefault(T value, T fallback) {
+ return value != null ? value : fallback;
+ }
+
+ private static <T> void setIfPresent(T value, Consumer<T> setter) {
+ if (value != null) {
+ setter.accept(value);
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbValidation.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbValidation.java
new file mode 100644
index 0000000000..3d349cc75e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbValidation.java
@@ -0,0 +1,379 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+final class TimestreamInfluxDbValidation {
+
+ static final Pattern RESOURCE_NAME = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*");
+ static final Pattern CLUSTER_NAME = Pattern.compile("[a-zA-z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*");
+ static final Pattern IDENTIFIER = Pattern.compile("[a-zA-Z0-9]+");
+ static final Pattern ARN = Pattern.compile(
+ "arn:aws[a-z\\-]*:timestream\\-influxdb:([a-z0-9\\-]+):([0-9]{12}):(db\\-instance|db\\-cluster|db\\-parameter\\-group|db\\-backup)/([a-zA-Z0-9]{3,64})");
+
+ static final List<String> INSTANCE_TYPES = List.of("db.influx.medium", "db.influx.large", "db.influx.xlarge",
+ "db.influx.2xlarge", "db.influx.4xlarge", "db.influx.8xlarge", "db.influx.12xlarge",
+ "db.influx.16xlarge", "db.influx.24xlarge");
+ static final List<String> STORAGE_TYPES = List.of("InfluxIOIncludedT1", "InfluxIOIncludedT2", "InfluxIOIncludedT3");
+ static final List<String> INSTANCE_DEPLOYMENT_TYPES = List.of("SINGLE_AZ", "WITH_MULTIAZ_STANDBY");
+ static final List<String> CLUSTER_DEPLOYMENT_TYPES = List.of("MULTI_NODE_READ_REPLICAS");
+ static final List<String> RESOURCE_DEPLOYMENT_TYPES = List.of("SINGLE_AZ", "WITH_MULTIAZ_STANDBY", "MULTI_NODE_READ_REPLICAS");
+ static final List<String> NETWORK_TYPES = List.of("IPV4", "DUAL");
+ static final List<String> FAILOVER_MODES = List.of("AUTOMATIC", "NO_FAILOVER");
+ static final List<String> RESTORE_MODES = List.of("NEW_RESOURCE", "REPLACE_EXISTING");
+
+ private static final List<String> AUTOMATED_BACKUP_TYPES = List.of("HOURLY", "DAILY", "WEEKLY", "MONTHLY",
+ "CUSTOM_SCHEDULE", "CONTINUOUS");
+ private static final List<String> DURATION_TYPES = List.of("hours", "minutes", "seconds", "milliseconds", "days");
+ private static final Pattern SUBNET_ID = Pattern.compile("subnet-[a-z0-9]+");
+ private static final Pattern SECURITY_GROUP_ID = Pattern.compile("sg-[a-z0-9]+");
+ private static final Pattern PASSWORD = Pattern.compile("[a-zA-Z0-9]+");
+ private static final Pattern BUCKET = Pattern.compile("[^_\"][^\"]*");
+ private static final Pattern KMS_KEY_ID = Pattern.compile("[a-zA-Z0-9:/_\\-]+");
+ private static final Pattern S3_BUCKET = Pattern.compile("[0-9a-z]+[0-9a-z\\.\\-]*[0-9a-z]+");
+ private static final Pattern TIMEZONE = Pattern.compile("(UTC|[A-Za-z_]+/[A-Za-z0-9_]+(/[A-Za-z0-9_]+)?)");
+ private static final Pattern MAINTENANCE_WINDOW = Pattern.compile(
+ "|(Mon|Tue|Wed|Thu|Fri|Sat|Sun):([01]\\d|2[0-3]):[0-5]\\d-(Mon|Tue|Wed|Thu|Fri|Sat|Sun):([01]\\d|2[0-3]):[0-5]\\d");
+ private static final Pattern CRON = Pattern.compile("cron\\(\\S+ \\S+ \\S+ \\S+ \\S+ \\S+\\)");
+ private static final Set<String> ENGINE_PARAMETER_KEYS = Set.of("InfluxDBv2", "InfluxDBv3Core", "InfluxDBv3Enterprise");
+ private static final Map<String, long[]> V2_NUMERIC_RANGES = Map.ofEntries(
+ Map.entry("queryConcurrency", new long[]{0, 256}),
+ Map.entry("queryQueueSize", new long[]{0, 256}),
+ Map.entry("sessionLength", new long[]{1, 2880}),
+ Map.entry("storageMaxConcurrentCompactions", new long[]{0, 64}),
+ Map.entry("storageSeriesFileMaxConcurrentSnapshotCompactions", new long[]{0, 64}),
+ Map.entry("storageWalMaxConcurrentWrites", new long[]{0, 256}),
+ Map.entry("influxqlMaxSelectBuckets", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("influxqlMaxSelectPoint", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("influxqlMaxSelectSeries", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("queryInitialMemoryBytes", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("queryMaxMemoryBytes", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("queryMemoryBytes", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("storageCacheMaxMemorySize", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("storageCacheSnapshotMemorySize", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("storageCompactThroughputBurst", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("storageMaxIndexLogFileSize", new long[]{0, 1_000_000_000_000L}),
+ Map.entry("storageSeriesIdSetCacheSize", new long[]{0, 1_000_000_000_000L}));
+ private static final Map<String, List<String>> PARAMETER_ENUMS = Map.of(
+ "logLevel", List.of("debug", "info", "error"),
+ "tracingType", List.of("log", "jaeger", "disabled"),
+ "logFormat", List.of("full"),
+ "dataFusionRuntimeType", List.of("multi-thread", "multi-thread-alt"));
+ private static final Set<Integer> RESERVED_PORTS = Set.of(2375, 2376, 7788, 7789, 7790, 7791, 7792, 7793, 7794,
+ 7795, 7796, 7797, 7798, 7799, 8090, 51678, 51679, 51680);
+
+ private TimestreamInfluxDbValidation() {
+ }
+
+ static AwsException validation(String message) {
+ return new AwsException("ValidationException", message, 400, Map.of("reason", "FIELD_VALIDATION_FAILED"));
+ }
+
+ static AwsException notFound(String resourceType, String resourceId) {
+ return new AwsException("ResourceNotFoundException",
+ "The requested resource " + resourceId + " was not found or does not exist.", 400,
+ Map.of("resourceId", resourceId, "resourceType", resourceType));
+ }
+
+ static AwsException conflict(String resourceType, String resourceId, String message) {
+ return new AwsException("ConflictException", message, 400,
+ Map.of("resourceId", resourceId, "resourceType", resourceType));
+ }
+
+ static String requiredString(JsonNode request, String field, int min, int max, Pattern pattern) {
+ String value = optionalString(request, field, min, max, pattern);
+ if (value == null) {
+ throw validation(field + " is required.");
+ }
+ return value;
+ }
+
+ static String optionalString(JsonNode request, String field, int min, int max, Pattern pattern) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isTextual()) {
+ throw validation(field + " must be a string.");
+ }
+ String value = node.asText();
+ if (value.length() < min || value.length() > max) {
+ throw validation(field + " must have length between " + min + " and " + max + ".");
+ }
+ if (pattern != null && !pattern.matcher(value).matches()) {
+ throw validation(field + " does not match the required pattern.");
+ }
+ return value;
+ }
+
+ static String requiredEnum(JsonNode request, String field, List<String> allowed) {
+ String value = optionalEnum(request, field, allowed);
+ if (value == null) {
+ throw validation(field + " is required.");
+ }
+ return value;
+ }
+
+ static String optionalEnum(JsonNode request, String field, List<String> allowed) {
+ String value = optionalString(request, field, 0, Integer.MAX_VALUE, null);
+ if (value != null && !allowed.contains(value)) {
+ throw validation(field + " must be one of " + allowed + ".");
+ }
+ return value;
+ }
+
+ static Integer optionalInt(JsonNode request, String field, long min, long max) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isIntegralNumber() || node.asLong() < min || node.asLong() > max) {
+ throw validation(field + " must be an integer between " + min + " and " + max + ".");
+ }
+ return node.asInt();
+ }
+
+ static Integer requiredInt(JsonNode request, String field, long min, long max) {
+ Integer value = optionalInt(request, field, min, max);
+ if (value == null) {
+ throw validation(field + " is required.");
+ }
+ return value;
+ }
+
+ static Boolean optionalBoolean(JsonNode request, String field) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isBoolean()) {
+ throw validation(field + " must be a boolean.");
+ }
+ return node.asBoolean();
+ }
+
+ static Integer port(JsonNode request) {
+ Integer port = optionalInt(request, "port", 1024, 65535);
+ if (port != null && RESERVED_PORTS.contains(port)) {
+ throw validation("port can't be 2375-2376, 7788-7799, 8090, or 51678-51680.");
+ }
+ return port;
+ }
+
+ static String password(JsonNode request, boolean required) {
+ return required
+ ? requiredString(request, "password", 8, 64, PASSWORD)
+ : optionalString(request, "password", 8, 64, PASSWORD);
+ }
+
+ static String bucket(JsonNode request) {
+ return optionalString(request, "bucket", 2, 64, BUCKET);
+ }
+
+ static String kmsKeyId(JsonNode request) {
+ return optionalString(request, "kmsKeyId", 1, 2048, KMS_KEY_ID);
+ }
+
+ static List<String> subnetIds(JsonNode request, boolean required) {
+ return stringList(request, "vpcSubnetIds", required, 1, 6, SUBNET_ID);
+ }
+
+ static List<String> securityGroupIds(JsonNode request, boolean required) {
+ return stringList(request, "vpcSecurityGroupIds", required, 1, 5, SECURITY_GROUP_ID);
+ }
+
+ static List<String> stringList(JsonNode request, String field, boolean required, int min, int max, Pattern member) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ if (required) {
+ throw validation(field + " is required.");
+ }
+ return null;
+ }
+ if (!node.isArray() || node.size() < min || node.size() > max) {
+ throw validation(field + " must contain between " + min + " and " + max + " items.");
+ }
+ List<String> values = new ArrayList<>();
+ for (JsonNode item : node) {
+ if (!item.isTextual() || item.asText().length() > 64
+ || (member != null && !member.matcher(item.asText()).matches())) {
+ throw validation(field + " contains an invalid value.");
+ }
+ values.add(item.asText());
+ }
+ return values;
+ }
+
+ static void storageForType(String storageType, Integer allocatedStorage) {
+ if (allocatedStorage != null && !"InfluxIOIncludedT1".equals(storageType) && allocatedStorage < 400) {
+ throw validation(storageType + " requires at least 400 GiB of allocatedStorage.");
+ }
+ }
+
+ static Map<String, String> tags(JsonNode request, String field, boolean required) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ if (required) {
+ throw validation(field + " is required.");
+ }
+ return new LinkedHashMap<>();
+ }
+ if (!node.isObject() || node.size() < 1 || node.size() > 200) {
+ throw validation(field + " must contain between 1 and 200 entries.");
+ }
+ Map<String, String> tags = new LinkedHashMap<>();
+ Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
+ while (fields.hasNext()) {
+ Map.Entry<String, JsonNode> entry = fields.next();
+ String value = entry.getValue().isTextual() ? entry.getValue().asText() : null;
+ if (entry.getKey().isEmpty() || entry.getKey().length() > 128 || value == null || value.length() > 256) {
+ throw validation(field + " contains an invalid tag.");
+ }
+ tags.put(entry.getKey(), value);
+ }
+ return tags;
+ }
+
+ static List<String> tagKeys(JsonNode request) {
+ List<String> keys = stringList(request, "tagKeys", true, 1, 200, null);
+ for (String key : keys) {
+ if (key.isEmpty() || key.length() > 128) {
+ throw validation("tagKeys contains an invalid key.");
+ }
+ }
+ return keys;
+ }
+
+ static JsonNode logDeliveryConfiguration(JsonNode request) {
+ JsonNode node = optionalObject(request, "logDeliveryConfiguration");
+ if (node == null) {
+ return null;
+ }
+ JsonNode s3 = node.get("s3Configuration");
+ if (s3 == null || !s3.isObject()) {
+ throw validation("logDeliveryConfiguration.s3Configuration is required.");
+ }
+ requiredString(s3, "bucketName", 3, 63, S3_BUCKET);
+ if (optionalBoolean(s3, "enabled") == null) {
+ throw validation("logDeliveryConfiguration.s3Configuration.enabled is required.");
+ }
+ return node;
+ }
+
+ static JsonNode maintenanceSchedule(JsonNode request) {
+ JsonNode node = optionalObject(request, "maintenanceSchedule");
+ if (node == null) {
+ return null;
+ }
+ requiredString(node, "timezone", 1, 64, TIMEZONE);
+ requiredString(node, "preferredMaintenanceWindow", 0, 19, MAINTENANCE_WINDOW);
+ return node;
+ }
+
+ static JsonNode dbBackupConfigurations(JsonNode request) {
+ JsonNode node = request.get("dbBackupConfigurations");
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isArray() || node.isEmpty() || node.size() > 4) {
+ throw validation("dbBackupConfigurations must contain between 1 and 4 items.");
+ }
+ for (JsonNode configuration : node) {
+ if (!configuration.isObject()) {
+ throw validation("dbBackupConfigurations contains an invalid item.");
+ }
+ String type = requiredEnum(configuration, "type", AUTOMATED_BACKUP_TYPES);
+ requiredInt(configuration, "retentionDays", 1, 365);
+ if (optionalBoolean(configuration, "enabled") == null) {
+ throw validation("dbBackupConfigurations.enabled is required.");
+ }
+ String schedule = optionalString(configuration, "customSchedule", 9, 256, CRON);
+ if ("CUSTOM_SCHEDULE".equals(type) && schedule == null) {
+ throw validation("customSchedule is required when type is CUSTOM_SCHEDULE.");
+ }
+ }
+ return node;
+ }
+
+ static String parameters(JsonNode parameters) {
+ if (parameters == null || parameters.isNull()) {
+ return TimestreamInfluxDbService.ENGINE_V2;
+ }
+ if (!parameters.isObject() || parameters.size() > 1) {
+ throw validation("parameters must specify exactly one engine parameter set.");
+ }
+ Iterator<String> names = parameters.fieldNames();
+ if (!names.hasNext()) {
+ return TimestreamInfluxDbService.ENGINE_V2;
+ }
+ String engineKey = names.next();
+ if (!ENGINE_PARAMETER_KEYS.contains(engineKey) || !parameters.get(engineKey).isObject()) {
+ throw validation("parameters contains an unsupported engine parameter set: " + engineKey + ".");
+ }
+ JsonNode values = parameters.get(engineKey);
+ validateParameterValues(values);
+ return switch (engineKey) {
+ case "InfluxDBv3Core" -> TimestreamInfluxDbService.ENGINE_V3_CORE;
+ case "InfluxDBv3Enterprise" -> {
+ requiredInt(values, "ingestQueryInstances", 1, 4);
+ requiredInt(values, "queryOnlyInstances", 0, 10);
+ if (optionalBoolean(values, "dedicatedCompactor") == null) {
+ throw validation("dedicatedCompactor is required for InfluxDBv3Enterprise parameters.");
+ }
+ yield TimestreamInfluxDbService.ENGINE_V3_ENTERPRISE;
+ }
+ default -> {
+ for (Map.Entry<String, long[]> range : V2_NUMERIC_RANGES.entrySet()) {
+ optionalInt(values, range.getKey(), range.getValue()[0], range.getValue()[1]);
+ }
+ yield TimestreamInfluxDbService.ENGINE_V2;
+ }
+ };
+ }
+
+ private static void validateParameterValues(JsonNode values) {
+ Iterator<Map.Entry<String, JsonNode>> fields = values.fields();
+ while (fields.hasNext()) {
+ Map.Entry<String, JsonNode> entry = fields.next();
+ List<String> allowed = PARAMETER_ENUMS.get(entry.getKey());
+ if (allowed != null) {
+ optionalEnum(values, entry.getKey(), allowed);
+ }
+ JsonNode value = entry.getValue();
+ if (value.isObject() && value.has("durationType")) {
+ requiredEnum(value, "durationType", DURATION_TYPES);
+ JsonNode amount = value.get("value");
+ if (amount == null || !amount.isIntegralNumber() || amount.asLong() < 0) {
+ throw validation(entry.getKey() + ".value must be a non-negative integer.");
+ }
+ }
+ }
+ }
+
+ static Integer maxResults(JsonNode request) {
+ return optionalInt(request, "maxResults", 1, 100);
+ }
+
+ static String nextToken(JsonNode request) {
+ return optionalString(request, "nextToken", 1, Integer.MAX_VALUE, null);
+ }
+
+ private static JsonNode optionalObject(JsonNode request, String field) {
+ JsonNode node = request.get(field);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isObject()) {
+ throw validation(field + " must be an object.");
+ }
+ return node;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java
new file mode 100644
index 0000000000..c58caa4de8
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java
@@ -0,0 +1,420 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.container;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.async.ResultCallback;
+import com.github.dockerjava.api.model.Frame;
+import com.github.dockerjava.api.model.StreamType;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.ContainerTeardown;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
+import io.github.hectorvent.floci.core.common.docker.PortAllocator;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Runs the InfluxDB 2.x containers that back Timestream for InfluxDB resources. The HTTP
+ * listener is published on a host port from the configured range in every topology, because no
+ * Floci proxy fronts InfluxDB traffic. Data and the influx CLI configuration written by the
+ * image's setup mode live on per-resource volumes, so restarts and parameter changes keep both.
+ */
+@ApplicationScoped
+public class TimestreamInfluxDbContainerManager implements ContainerTeardown {
+
+ static final String SERVICE = "timestream-influxdb";
+ static final int INFLUX_PORT = 8086;
+ private static final Logger LOG = Logger.getLogger(TimestreamInfluxDbContainerManager.class);
+ private static final String DATA_PATH = "/var/lib/influxdb2";
+ private static final String CONFIG_PATH = "/etc/influxdb2";
+ private static final String CLI_CONFIG_FILE = CONFIG_PATH + "/influx-configs";
+ private static final int READINESS_RETRY_MS = 500;
+ private static final int EXEC_TIMEOUT_SECONDS = 300;
+
+ public record InfluxDbSetup(String username, String password, String organization, String bucket) {
+ }
+
+ public record InfluxDbEndpoint(String containerId, String host, int port) {
+ }
+
+ private record RunningContainer(String containerId, int hostPort) {
+ }
+
+ private final ContainerBuilder containerBuilder;
+ private final ContainerLifecycleManager lifecycleManager;
+ private final ContainerDetector containerDetector;
+ private final PortAllocator portAllocator;
+ private final EmulatorConfig config;
+ private final Map<String, RunningContainer> running = new ConcurrentHashMap<>();
+
+ @Inject
+ public TimestreamInfluxDbContainerManager(ContainerBuilder containerBuilder,
+ ContainerLifecycleManager lifecycleManager,
+ ContainerDetector containerDetector,
+ PortAllocator portAllocator,
+ EmulatorConfig config) {
+ this.containerBuilder = containerBuilder;
+ this.lifecycleManager = lifecycleManager;
+ this.containerDetector = containerDetector;
+ this.portAllocator = portAllocator;
+ this.config = config;
+ }
+
+ public boolean isDockerReachable() {
+ try {
+ lifecycleManager.getDockerClient().pingCmd().exec();
+ return true;
+ } catch (RuntimeException e) {
+ LOG.debugv("Docker daemon is not reachable: {0}", e.getMessage());
+ return false;
+ }
+ }
+
+ public InfluxDbEndpoint start(String resourceId, String accountId, String region,
+ InfluxDbSetup setup, Map<String, String> engineEnvironment) {
+ EmulatorConfig.TimestreamInfluxDbServiceConfig serviceConfig = config.services().timestreamInfluxdb();
+ String containerName = containerName(resourceId);
+ LOG.infov("Starting InfluxDB container {0} for {1} using image {2}",
+ containerName, resourceId, serviceConfig.defaultImage());
+
+ lifecycleManager.removeIfExists(containerName);
+ releasePort(resourceId);
+
+ ContainerBuilder.Builder builder = containerBuilder.newContainer(serviceConfig.defaultImage())
+ .withName(containerName)
+ .withDockerNetwork(serviceConfig.dockerNetwork())
+ .withLogRotation()
+ .withLabels(ContainerStorageHelper.resourceIdentityLabels(SERVICE, resourceId, accountId, region));
+ if (setup != null) {
+ builder.withEnv("DOCKER_INFLUXDB_INIT_MODE", "setup")
+ .withEnv("DOCKER_INFLUXDB_INIT_USERNAME", setup.username())
+ .withEnv("DOCKER_INFLUXDB_INIT_PASSWORD", setup.password())
+ .withEnv("DOCKER_INFLUXDB_INIT_ORG", setup.organization())
+ .withEnv("DOCKER_INFLUXDB_INIT_BUCKET", setup.bucket());
+ }
+ for (Map.Entry<String, String> entry : engineEnvironment.entrySet()) {
+ builder.withEnv(entry.getKey(), entry.getValue());
+ }
+
+ int hostPort = portAllocator.allocate(serviceConfig.hostPortBase(), serviceConfig.hostPortMax());
+ builder.withPortBinding(INFLUX_PORT, hostPort);
+
+ ContainerInfo info;
+ try {
+ applyVolumes(builder, resourceId);
+ info = lifecycleManager.createAndStart(builder.build());
+ } catch (RuntimeException e) {
+ lifecycleManager.removeIfExists(containerName);
+ portAllocator.release(hostPort);
+ throw e;
+ }
+ running.put(resourceId, new RunningContainer(info.containerId(), hostPort));
+ EndpointInfo endpoint = info.getEndpoint(INFLUX_PORT);
+ LOG.infov("InfluxDB container {0} for {1} listening on {2} (host port {3})",
+ info.containerId(), resourceId, endpoint, String.valueOf(hostPort));
+ return new InfluxDbEndpoint(info.containerId(), endpoint.host(), endpoint.port());
+ }
+
+ public void waitUntilReady(InfluxDbEndpoint endpoint) {
+ int timeoutSeconds = config.services().timestreamInfluxdb().readinessTimeoutSeconds();
+ long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(timeoutSeconds);
+ while (System.currentTimeMillis() < deadline) {
+ if (isHealthy(endpoint)) {
+ return;
+ }
+ try {
+ Thread.sleep(READINESS_RETRY_MS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IllegalStateException("Interrupted waiting for InfluxDB at " + endpoint.host(), e);
+ }
+ }
+ throw new IllegalStateException("InfluxDB at " + endpoint.host() + ":" + endpoint.port()
+ + " did not become healthy within " + timeoutSeconds + "s");
+ }
+
+ boolean isHealthy(InfluxDbEndpoint endpoint) {
+ HttpURLConnection connection = null;
+ try {
+ connection = (HttpURLConnection) URI.create(
+ "http://" + endpoint.host() + ":" + endpoint.port() + "/health").toURL().openConnection();
+ connection.setConnectTimeout(1000);
+ connection.setReadTimeout(1000);
+ return connection.getResponseCode() == 200;
+ } catch (IOException e) {
+ LOG.debugv("InfluxDB health probe at {0}:{1} not ready: {2}",
+ endpoint.host(), String.valueOf(endpoint.port()), e.getMessage());
+ return false;
+ } finally {
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ }
+
+ public void restart(String containerId) {
+ lifecycleManager.getDockerClient().restartContainerCmd(containerId).exec();
+ }
+
+ public void stop(String resourceId, String containerId) {
+ RunningContainer container = running.remove(resourceId);
+ if (container != null) {
+ portAllocator.release(container.hostPort());
+ }
+ String effectiveId = containerId != null ? containerId : container != null ? container.containerId() : null;
+ if (effectiveId != null) {
+ lifecycleManager.stopAndRemove(effectiveId, null);
+ } else {
+ lifecycleManager.removeIfExists(containerName(resourceId));
+ }
+ }
+
+ public void removeStorage(String resourceId) {
+ if (ContainerStorageHelper.isNamedVolumeMode(config)) {
+ ContainerStorageHelper.removeStorage(config, lifecycleManager, SERVICE, null, resourceId);
+ ContainerStorageHelper.removeStorage(config, lifecycleManager, SERVICE, null, resourceId + "-config");
+ }
+ }
+
+ public void backup(String containerId, String backupId) {
+ String workDir = "/tmp/floci-backup-" + backupId;
+ execOrThrow(containerId, new String[]{"influx", "backup", workDir}, "influx backup");
+ try {
+ Path directory = backupDirectory();
+ Files.createDirectories(directory);
+ copyFromContainer(containerId, workDir, directory.resolve(backupId + ".tar"));
+ copyFromContainer(containerId, CLI_CONFIG_FILE, directory.resolve(backupId + "-cli.tar"));
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to store InfluxDB backup " + backupId, e);
+ } finally {
+ execQuietly(containerId, new String[]{"rm", "-rf", workDir});
+ }
+ }
+
+ public void restore(String containerId, String backupId) {
+ Path directory = backupDirectory();
+ String workDir = "/tmp/floci-backup-" + backupId;
+ try {
+ copyToContainer(containerId, directory.resolve(backupId + ".tar"), "/tmp");
+ execOrThrow(containerId, new String[]{"influx", "restore", "--full", workDir}, "influx restore");
+ copyToContainer(containerId, directory.resolve(backupId + "-cli.tar"), CONFIG_PATH);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to restore InfluxDB backup " + backupId, e);
+ } finally {
+ execQuietly(containerId, new String[]{"rm", "-rf", workDir});
+ }
+ }
+
+ public void deleteBackupArtifacts(String backupId) {
+ Path directory = backupDirectory();
+ for (String suffix : List.of(".tar", "-cli.tar")) {
+ try {
+ Files.deleteIfExists(directory.resolve(backupId + suffix));
+ } catch (IOException e) {
+ LOG.warnv(e, "Failed to delete InfluxDB backup artifact {0}{1}", backupId, suffix);
+ }
+ }
+ }
+
+ @Override
+ public void stopManagedContainers() {
+ List<String> resourceIds = new ArrayList<>(running.keySet());
+ if (!resourceIds.isEmpty()) {
+ LOG.infov("Stopping {0} Timestream for InfluxDB container(s) on shutdown", resourceIds.size());
+ }
+ for (String resourceId : resourceIds) {
+ stop(resourceId, null);
+ }
+ }
+
+ /**
+ * Maps the InfluxDBv2 members of a DB parameter group onto the {@code INFLUXD_*} environment
+ * variables of the same InfluxDB configuration options. Duration members become InfluxDB
+ * duration strings; InfluxDB has no day unit, so days are expressed in hours.
+ */
+ public static Map<String, String> engineEnvironment(JsonNode influxDbV2Parameters) {
+ Map<String, String> environment = new LinkedHashMap<>();
+ if (influxDbV2Parameters == null || !influxDbV2Parameters.isObject()) {
+ return environment;
+ }
+ Iterator<Map.Entry<String, JsonNode>> fields = influxDbV2Parameters.fields();
+ while (fields.hasNext()) {
+ Map.Entry<String, JsonNode> field = fields.next();
+ JsonNode value = field.getValue();
+ String rendered = value.isObject() ? duration(value) : value.asText();
+ environment.put("INFLUXD_" + upperSnake(field.getKey()), rendered);
+ }
+ return environment;
+ }
+
+ private static String duration(JsonNode value) {
+ long amount = value.path("value").asLong();
+ return switch (value.path("durationType").asText()) {
+ case "days" -> (amount * 24) + "h";
+ case "hours" -> amount + "h";
+ case "minutes" -> amount + "m";
+ case "milliseconds" -> amount + "ms";
+ default -> amount + "s";
+ };
+ }
+
+ private static String upperSnake(String camelCase) {
+ StringBuilder result = new StringBuilder();
+ for (char c : camelCase.toCharArray()) {
+ if (Character.isUpperCase(c) && !result.isEmpty()) {
+ result.append('_');
+ }
+ result.append(Character.toUpperCase(c));
+ }
+ return result.toString();
+ }
+
+ private String containerName(String resourceId) {
+ return ContainerStorageHelper.resourceName(config, SERVICE, null, resourceId);
+ }
+
+ private void releasePort(String resourceId) {
+ RunningContainer previous = running.remove(resourceId);
+ if (previous != null) {
+ portAllocator.release(previous.hostPort());
+ }
+ }
+
+ private void applyVolumes(ContainerBuilder.Builder builder, String resourceId) {
+ if (ContainerStorageHelper.isNamedVolumeMode(config)) {
+ ContainerStorageHelper.applyStorage(builder, lifecycleManager, config, SERVICE, null, resourceId, DATA_PATH);
+ ContainerStorageHelper.applyStorage(builder, lifecycleManager, config, SERVICE, null,
+ resourceId + "-config", CONFIG_PATH);
+ return;
+ }
+ Path base = ContainerStorageHelper.hostResourcePath(config, SERVICE, resourceId);
+ String dataDir = base.resolve("data").toAbsolutePath().toString();
+ String configDir = base.resolve("config").toAbsolutePath().toString();
+ if (!containerDetector.isRunningInContainer()) {
+ ContainerStorageHelper.ensureHostDir(dataDir);
+ ContainerStorageHelper.ensureHostDir(configDir);
+ }
+ builder.withBind(dataDir, DATA_PATH).withBind(configDir, CONFIG_PATH);
+ }
+
+ private Path backupDirectory() {
+ return Path.of(config.storage().persistentPath()).resolve(SERVICE).resolve("backups");
+ }
+
+ private void copyFromContainer(String containerId, String containerPath, Path target) throws IOException {
+ try (InputStream archive = lifecycleManager.getDockerClient()
+ .copyArchiveFromContainerCmd(containerId, containerPath).exec()) {
+ Files.copy(archive, target, StandardCopyOption.REPLACE_EXISTING);
+ }
+ }
+
+ private void copyToContainer(String containerId, Path archive, String remotePath) throws IOException {
+ try (InputStream stream = Files.newInputStream(archive)) {
+ lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)
+ .withRemotePath(remotePath)
+ .withTarInputStream(stream)
+ .exec();
+ }
+ }
+
+ private void execOrThrow(String containerId, String[] command, String description) {
+ ExecResult result = exec(containerId, command);
+ if (result.exitCode() != 0) {
+ throw new IllegalStateException(description + " failed with exit code " + result.exitCode()
+ + ": " + result.stderr() + result.stdout());
+ }
+ }
+
+ private void execQuietly(String containerId, String[] command) {
+ try {
+ exec(containerId, command);
+ } catch (RuntimeException e) {
+ LOG.debugv("Cleanup command in container {0} failed: {1}", containerId, e.getMessage());
+ }
+ }
+
+ private ExecResult exec(String containerId, String[] command) {
+ DockerClient dockerClient = lifecycleManager.getDockerClient();
+ String execId = dockerClient.execCreateCmd(containerId)
+ .withCmd(command)
+ .withAttachStdout(true)
+ .withAttachStderr(true)
+ .exec()
+ .getId();
+ CountDownLatch latch = new CountDownLatch(1);
+ ByteArrayOutputStream stdout = new ByteArrayOutputStream();
+ ByteArrayOutputStream stderr = new ByteArrayOutputStream();
+ Closeable callback = dockerClient.execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {
+ @Override
+ public void onNext(Frame frame) {
+ if (frame.getPayload() == null) {
+ return;
+ }
+ try {
+ if (frame.getStreamType() == StreamType.STDERR) {
+ stderr.write(frame.getPayload());
+ } else {
+ stdout.write(frame.getPayload());
+ }
+ } catch (IOException e) {
+ LOG.warnv(e, "Failed to read output of container exec {0}", execId);
+ }
+ }
+
+ @Override
+ public void onComplete() {
+ latch.countDown();
+ }
+
+ @Override
+ public void onError(Throwable throwable) {
+ LOG.warnv(throwable, "Container exec {0} failed", execId);
+ latch.countDown();
+ }
+ });
+ try {
+ if (!latch.await(EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
+ return new ExecResult(-1, stdout.toString(StandardCharsets.UTF_8), "timed out");
+ }
+ Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
+ return new ExecResult(exitCode != null ? exitCode : -1,
+ stdout.toString(StandardCharsets.UTF_8), stderr.toString(StandardCharsets.UTF_8));
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IllegalStateException("Interrupted running command in container " + containerId, e);
+ } finally {
+ try {
+ callback.close();
+ } catch (IOException e) {
+ LOG.debugv("Failed to close exec callback {0}: {1}", execId, e.getMessage());
+ }
+ }
+ }
+
+ private record ExecResult(long exitCode, String stdout, String stderr) {
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbBackup.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbBackup.java
new file mode 100644
index 0000000000..a201dd6afd
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbBackup.java
@@ -0,0 +1,116 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@RegisterForReflection
+public class DbBackup {
+ private String id;
+ private String name;
+ private String arn;
+ private String status;
+ private Instant createdAt;
+ private String expiresAfter;
+ private String dbResourceId;
+ private String resourceType;
+ private String type;
+ private String engineType;
+ private String deploymentType;
+ private String kmsKeyId;
+ private JsonNode clusterConfiguration;
+ private String dbParameterGroupId;
+ private String dbInstanceType;
+ private JsonNode logDeliveryConfiguration;
+ private String failoverMode;
+ private String dbStorageType;
+ private Integer allocatedStorage;
+ private List<String> vpcSubnetIds = new ArrayList<>();
+ private List<String> vpcSecurityGroupIds = new ArrayList<>();
+ private Boolean publiclyAccessible;
+ private Integer port;
+ private String networkType;
+ private String influxAuthParametersSecretArn;
+ private JsonNode maintenanceSchedule;
+ private JsonNode dbBackupConfigurations;
+ private Map<String, String> tags = new LinkedHashMap<>();
+ private String accountId;
+ private String region;
+ private boolean dataCaptured;
+
+ public DbBackup() {
+ }
+
+ public String getAccountId() { return accountId; }
+ public void setAccountId(String accountId) { this.accountId = accountId; }
+ public String getRegion() { return region; }
+ public void setRegion(String region) { this.region = region; }
+ public boolean isDataCaptured() { return dataCaptured; }
+ public void setDataCaptured(boolean dataCaptured) { this.dataCaptured = dataCaptured; }
+
+ public String getId() { return id; }
+ public void setId(String id) { this.id = id; }
+ public String getName() { return name; }
+ public void setName(String name) { this.name = name; }
+ public String getArn() { return arn; }
+ public void setArn(String arn) { this.arn = arn; }
+ public String getStatus() { return status; }
+ public void setStatus(String status) { this.status = status; }
+ public Instant getCreatedAt() { return createdAt; }
+ public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
+ public String getExpiresAfter() { return expiresAfter; }
+ public void setExpiresAfter(String expiresAfter) { this.expiresAfter = expiresAfter; }
+ public String getDbResourceId() { return dbResourceId; }
+ public void setDbResourceId(String dbResourceId) { this.dbResourceId = dbResourceId; }
+ public String getResourceType() { return resourceType; }
+ public void setResourceType(String resourceType) { this.resourceType = resourceType; }
+ public String getType() { return type; }
+ public void setType(String type) { this.type = type; }
+ public String getEngineType() { return engineType; }
+ public void setEngineType(String engineType) { this.engineType = engineType; }
+ public String getDeploymentType() { return deploymentType; }
+ public void setDeploymentType(String deploymentType) { this.deploymentType = deploymentType; }
+ public String getKmsKeyId() { return kmsKeyId; }
+ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
+ public JsonNode getClusterConfiguration() { return clusterConfiguration; }
+ public void setClusterConfiguration(JsonNode clusterConfiguration) { this.clusterConfiguration = clusterConfiguration; }
+ public String getDbParameterGroupId() { return dbParameterGroupId; }
+ public void setDbParameterGroupId(String dbParameterGroupId) { this.dbParameterGroupId = dbParameterGroupId; }
+ public String getDbInstanceType() { return dbInstanceType; }
+ public void setDbInstanceType(String dbInstanceType) { this.dbInstanceType = dbInstanceType; }
+ public JsonNode getLogDeliveryConfiguration() { return logDeliveryConfiguration; }
+ public void setLogDeliveryConfiguration(JsonNode logDeliveryConfiguration) { this.logDeliveryConfiguration = logDeliveryConfiguration; }
+ public String getFailoverMode() { return failoverMode; }
+ public void setFailoverMode(String failoverMode) { this.failoverMode = failoverMode; }
+ public String getDbStorageType() { return dbStorageType; }
+ public void setDbStorageType(String dbStorageType) { this.dbStorageType = dbStorageType; }
+ public Integer getAllocatedStorage() { return allocatedStorage; }
+ public void setAllocatedStorage(Integer allocatedStorage) { this.allocatedStorage = allocatedStorage; }
+ public List<String> getVpcSubnetIds() { return vpcSubnetIds; }
+ public void setVpcSubnetIds(List<String> vpcSubnetIds) { this.vpcSubnetIds = copy(vpcSubnetIds); }
+ public List<String> getVpcSecurityGroupIds() { return vpcSecurityGroupIds; }
+ public void setVpcSecurityGroupIds(List<String> vpcSecurityGroupIds) { this.vpcSecurityGroupIds = copy(vpcSecurityGroupIds); }
+ public Boolean getPubliclyAccessible() { return publiclyAccessible; }
+ public void setPubliclyAccessible(Boolean publiclyAccessible) { this.publiclyAccessible = publiclyAccessible; }
+ public Integer getPort() { return port; }
+ public void setPort(Integer port) { this.port = port; }
+ public String getNetworkType() { return networkType; }
+ public void setNetworkType(String networkType) { this.networkType = networkType; }
+ public String getInfluxAuthParametersSecretArn() { return influxAuthParametersSecretArn; }
+ public void setInfluxAuthParametersSecretArn(String influxAuthParametersSecretArn) { this.influxAuthParametersSecretArn = influxAuthParametersSecretArn; }
+ public JsonNode getMaintenanceSchedule() { return maintenanceSchedule; }
+ public void setMaintenanceSchedule(JsonNode maintenanceSchedule) { this.maintenanceSchedule = maintenanceSchedule; }
+ public JsonNode getDbBackupConfigurations() { return dbBackupConfigurations; }
+ public void setDbBackupConfigurations(JsonNode dbBackupConfigurations) { this.dbBackupConfigurations = dbBackupConfigurations; }
+ public Map<String, String> getTags() { return tags; }
+ public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
+
+ private static List<String> copy(List<String> values) {
+ return values == null ? new ArrayList<>() : new ArrayList<>(values);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java
new file mode 100644
index 0000000000..aec824ef53
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java
@@ -0,0 +1,110 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@RegisterForReflection
+public class DbCluster {
+ private String id;
+ private String name;
+ private String arn;
+ private String status;
+ private String endpoint;
+ private String readerEndpoint;
+ private Integer port;
+ private String deploymentType;
+ private String dbInstanceType;
+ private String networkType;
+ private String dbStorageType;
+ private Integer allocatedStorage;
+ private String engineType;
+ private Boolean publiclyAccessible;
+ private String dbParameterGroupIdentifier;
+ private JsonNode logDeliveryConfiguration;
+ private JsonNode maintenanceSchedule;
+ private String influxAuthParametersSecretArn;
+ private List<String> vpcSubnetIds = new ArrayList<>();
+ private List<String> vpcSecurityGroupIds = new ArrayList<>();
+ private String failoverMode;
+ private JsonNode clusterConfiguration;
+ private JsonNode dbBackupConfigurations;
+ private String kmsKeyId;
+ private Instant createdAt;
+ private Map<String, String> tags = new LinkedHashMap<>();
+ private String accountId;
+ private String region;
+ private String containerId;
+
+ public DbCluster() {
+ }
+
+ public String getAccountId() { return accountId; }
+ public void setAccountId(String accountId) { this.accountId = accountId; }
+ public String getRegion() { return region; }
+ public void setRegion(String region) { this.region = region; }
+ public String getContainerId() { return containerId; }
+ public void setContainerId(String containerId) { this.containerId = containerId; }
+
+ public String getId() { return id; }
+ public void setId(String id) { this.id = id; }
+ public String getName() { return name; }
+ public void setName(String name) { this.name = name; }
+ public String getArn() { return arn; }
+ public void setArn(String arn) { this.arn = arn; }
+ public String getStatus() { return status; }
+ public void setStatus(String status) { this.status = status; }
+ public String getEndpoint() { return endpoint; }
+ public void setEndpoint(String endpoint) { this.endpoint = endpoint; }
+ public String getReaderEndpoint() { return readerEndpoint; }
+ public void setReaderEndpoint(String readerEndpoint) { this.readerEndpoint = readerEndpoint; }
+ public Integer getPort() { return port; }
+ public void setPort(Integer port) { this.port = port; }
+ public String getDeploymentType() { return deploymentType; }
+ public void setDeploymentType(String deploymentType) { this.deploymentType = deploymentType; }
+ public String getDbInstanceType() { return dbInstanceType; }
+ public void setDbInstanceType(String dbInstanceType) { this.dbInstanceType = dbInstanceType; }
+ public String getNetworkType() { return networkType; }
+ public void setNetworkType(String networkType) { this.networkType = networkType; }
+ public String getDbStorageType() { return dbStorageType; }
+ public void setDbStorageType(String dbStorageType) { this.dbStorageType = dbStorageType; }
+ public Integer getAllocatedStorage() { return allocatedStorage; }
+ public void setAllocatedStorage(Integer allocatedStorage) { this.allocatedStorage = allocatedStorage; }
+ public String getEngineType() { return engineType; }
+ public void setEngineType(String engineType) { this.engineType = engineType; }
+ public Boolean getPubliclyAccessible() { return publiclyAccessible; }
+ public void setPubliclyAccessible(Boolean publiclyAccessible) { this.publiclyAccessible = publiclyAccessible; }
+ public String getDbParameterGroupIdentifier() { return dbParameterGroupIdentifier; }
+ public void setDbParameterGroupIdentifier(String dbParameterGroupIdentifier) { this.dbParameterGroupIdentifier = dbParameterGroupIdentifier; }
+ public JsonNode getLogDeliveryConfiguration() { return logDeliveryConfiguration; }
+ public void setLogDeliveryConfiguration(JsonNode logDeliveryConfiguration) { this.logDeliveryConfiguration = logDeliveryConfiguration; }
+ public JsonNode getMaintenanceSchedule() { return maintenanceSchedule; }
+ public void setMaintenanceSchedule(JsonNode maintenanceSchedule) { this.maintenanceSchedule = maintenanceSchedule; }
+ public String getInfluxAuthParametersSecretArn() { return influxAuthParametersSecretArn; }
+ public void setInfluxAuthParametersSecretArn(String influxAuthParametersSecretArn) { this.influxAuthParametersSecretArn = influxAuthParametersSecretArn; }
+ public List<String> getVpcSubnetIds() { return vpcSubnetIds; }
+ public void setVpcSubnetIds(List<String> vpcSubnetIds) { this.vpcSubnetIds = copy(vpcSubnetIds); }
+ public List<String> getVpcSecurityGroupIds() { return vpcSecurityGroupIds; }
+ public void setVpcSecurityGroupIds(List<String> vpcSecurityGroupIds) { this.vpcSecurityGroupIds = copy(vpcSecurityGroupIds); }
+ public String getFailoverMode() { return failoverMode; }
+ public void setFailoverMode(String failoverMode) { this.failoverMode = failoverMode; }
+ public JsonNode getClusterConfiguration() { return clusterConfiguration; }
+ public void setClusterConfiguration(JsonNode clusterConfiguration) { this.clusterConfiguration = clusterConfiguration; }
+ public JsonNode getDbBackupConfigurations() { return dbBackupConfigurations; }
+ public void setDbBackupConfigurations(JsonNode dbBackupConfigurations) { this.dbBackupConfigurations = dbBackupConfigurations; }
+ public String getKmsKeyId() { return kmsKeyId; }
+ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
+ public Instant getCreatedAt() { return createdAt; }
+ public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
+ public Map<String, String> getTags() { return tags; }
+ public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
+
+ private static List<String> copy(List<String> values) {
+ return values == null ? new ArrayList<>() : new ArrayList<>(values);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java
new file mode 100644
index 0000000000..5c82286a9e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java
@@ -0,0 +1,116 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@RegisterForReflection
+public class DbInstance {
+ private String id;
+ private String name;
+ private String arn;
+ private String status;
+ private String endpoint;
+ private Integer port;
+ private String networkType;
+ private String dbInstanceType;
+ private String dbStorageType;
+ private Integer allocatedStorage;
+ private String deploymentType;
+ private List<String> vpcSubnetIds = new ArrayList<>();
+ private List<String> vpcSecurityGroupIds = new ArrayList<>();
+ private Boolean publiclyAccessible;
+ private String dbParameterGroupIdentifier;
+ private String availabilityZone;
+ private String secondaryAvailabilityZone;
+ private JsonNode logDeliveryConfiguration;
+ private String influxAuthParametersSecretArn;
+ private String dbClusterId;
+ private String instanceMode;
+ private List<String> instanceModes = new ArrayList<>();
+ private JsonNode maintenanceSchedule;
+ private JsonNode dbBackupConfigurations;
+ private String kmsKeyId;
+ private String engineType;
+ private Instant createdAt;
+ private Map<String, String> tags = new LinkedHashMap<>();
+ private String accountId;
+ private String region;
+ private String containerId;
+
+ public DbInstance() {
+ }
+
+ public String getAccountId() { return accountId; }
+ public void setAccountId(String accountId) { this.accountId = accountId; }
+ public String getRegion() { return region; }
+ public void setRegion(String region) { this.region = region; }
+ public String getContainerId() { return containerId; }
+ public void setContainerId(String containerId) { this.containerId = containerId; }
+
+ public String getId() { return id; }
+ public void setId(String id) { this.id = id; }
+ public String getName() { return name; }
+ public void setName(String name) { this.name = name; }
+ public String getArn() { return arn; }
+ public void setArn(String arn) { this.arn = arn; }
+ public String getStatus() { return status; }
+ public void setStatus(String status) { this.status = status; }
+ public String getEndpoint() { return endpoint; }
+ public void setEndpoint(String endpoint) { this.endpoint = endpoint; }
+ public Integer getPort() { return port; }
+ public void setPort(Integer port) { this.port = port; }
+ public String getNetworkType() { return networkType; }
+ public void setNetworkType(String networkType) { this.networkType = networkType; }
+ public String getDbInstanceType() { return dbInstanceType; }
+ public void setDbInstanceType(String dbInstanceType) { this.dbInstanceType = dbInstanceType; }
+ public String getDbStorageType() { return dbStorageType; }
+ public void setDbStorageType(String dbStorageType) { this.dbStorageType = dbStorageType; }
+ public Integer getAllocatedStorage() { return allocatedStorage; }
+ public void setAllocatedStorage(Integer allocatedStorage) { this.allocatedStorage = allocatedStorage; }
+ public String getDeploymentType() { return deploymentType; }
+ public void setDeploymentType(String deploymentType) { this.deploymentType = deploymentType; }
+ public List<String> getVpcSubnetIds() { return vpcSubnetIds; }
+ public void setVpcSubnetIds(List<String> vpcSubnetIds) { this.vpcSubnetIds = copy(vpcSubnetIds); }
+ public List<String> getVpcSecurityGroupIds() { return vpcSecurityGroupIds; }
+ public void setVpcSecurityGroupIds(List<String> vpcSecurityGroupIds) { this.vpcSecurityGroupIds = copy(vpcSecurityGroupIds); }
+ public Boolean getPubliclyAccessible() { return publiclyAccessible; }
+ public void setPubliclyAccessible(Boolean publiclyAccessible) { this.publiclyAccessible = publiclyAccessible; }
+ public String getDbParameterGroupIdentifier() { return dbParameterGroupIdentifier; }
+ public void setDbParameterGroupIdentifier(String dbParameterGroupIdentifier) { this.dbParameterGroupIdentifier = dbParameterGroupIdentifier; }
+ public String getAvailabilityZone() { return availabilityZone; }
+ public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
+ public String getSecondaryAvailabilityZone() { return secondaryAvailabilityZone; }
+ public void setSecondaryAvailabilityZone(String secondaryAvailabilityZone) { this.secondaryAvailabilityZone = secondaryAvailabilityZone; }
+ public JsonNode getLogDeliveryConfiguration() { return logDeliveryConfiguration; }
+ public void setLogDeliveryConfiguration(JsonNode logDeliveryConfiguration) { this.logDeliveryConfiguration = logDeliveryConfiguration; }
+ public String getInfluxAuthParametersSecretArn() { return influxAuthParametersSecretArn; }
+ public void setInfluxAuthParametersSecretArn(String influxAuthParametersSecretArn) { this.influxAuthParametersSecretArn = influxAuthParametersSecretArn; }
+ public String getDbClusterId() { return dbClusterId; }
+ public void setDbClusterId(String dbClusterId) { this.dbClusterId = dbClusterId; }
+ public String getInstanceMode() { return instanceMode; }
+ public void setInstanceMode(String instanceMode) { this.instanceMode = instanceMode; }
+ public List<String> getInstanceModes() { return instanceModes; }
+ public void setInstanceModes(List<String> instanceModes) { this.instanceModes = copy(instanceModes); }
+ public JsonNode getMaintenanceSchedule() { return maintenanceSchedule; }
+ public void setMaintenanceSchedule(JsonNode maintenanceSchedule) { this.maintenanceSchedule = maintenanceSchedule; }
+ public JsonNode getDbBackupConfigurations() { return dbBackupConfigurations; }
+ public void setDbBackupConfigurations(JsonNode dbBackupConfigurations) { this.dbBackupConfigurations = dbBackupConfigurations; }
+ public String getKmsKeyId() { return kmsKeyId; }
+ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
+ public String getEngineType() { return engineType; }
+ public void setEngineType(String engineType) { this.engineType = engineType; }
+ public Instant getCreatedAt() { return createdAt; }
+ public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
+ public Map<String, String> getTags() { return tags; }
+ public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
+
+ private static List<String> copy(List<String> values) {
+ return values == null ? new ArrayList<>() : new ArrayList<>(values);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbParameterGroup.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbParameterGroup.java
new file mode 100644
index 0000000000..9f6caa9d88
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbParameterGroup.java
@@ -0,0 +1,40 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+@RegisterForReflection
+public class DbParameterGroup {
+ private String id;
+ private String name;
+ private String arn;
+ private String description;
+ private JsonNode parameters;
+ private String engineType;
+ private Instant createdAt;
+ private Map<String, String> tags = new LinkedHashMap<>();
+
+ public DbParameterGroup() {
+ }
+
+ public String getId() { return id; }
+ public void setId(String id) { this.id = id; }
+ public String getName() { return name; }
+ public void setName(String name) { this.name = name; }
+ public String getArn() { return arn; }
+ public void setArn(String arn) { this.arn = arn; }
+ public String getDescription() { return description; }
+ public void setDescription(String description) { this.description = description; }
+ public JsonNode getParameters() { return parameters; }
+ public void setParameters(JsonNode parameters) { this.parameters = parameters; }
+ public String getEngineType() { return engineType; }
+ public void setEngineType(String engineType) { this.engineType = engineType; }
+ public Instant getCreatedAt() { return createdAt; }
+ public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
+ public Map<String, String> getTags() { return tags; }
+ public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 6cfb985867..210dcd0907 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -664,6 +664,14 @@ floci:
staging-bucket: floci-cur-staging
bcm-pricing-calculator:
enabled: true
+ timestream-influxdb:
+ enabled: true
+ mock: false # FLOCI_SERVICES_TIMESTREAM_INFLUXDB_MOCK: true = metadata only, no Docker
+ default-image: "influxdb:2.7"
+ host-port-base: 8086
+ host-port-max: 8185
+ readiness-timeout-seconds: 120
+ # docker-network: my-network # Docker network to attach InfluxDB containers to
bcm-data-exports:
enabled: true
emit-mode: synchronous
diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
index 75a1b5874b..801cc6c7da 100644
--- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
+++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
@@ -24,6 +24,7 @@ import io.github.hectorvent.floci.services.pipes.PipesService;
import io.github.hectorvent.floci.services.rds.RdsService;
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbService;
import io.quarkus.runtime.ShutdownDelayInitiatedEvent;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
@@ -79,6 +80,8 @@ class EmulatorLifecycleTest {
@Mock private io.github.hectorvent.floci.services.amazonmq.container.RabbitMqManager rabbitMqManager;
@Mock private io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager flinkContainerManager;
@Mock private RdsService rdsService;
+ @Mock private TimestreamInfluxDbService timestreamInfluxDbService;
+ @Mock private EmulatorConfig.TimestreamInfluxDbServiceConfig timestreamInfluxDbServiceConfig;
@Mock private io.github.hectorvent.floci.services.elbv2.ElbV2Service elbV2Service;
@Mock private io.github.hectorvent.floci.services.elb.ElbClassicService elbClassicService;
@Mock private InitializationHooksRunner initializationHooksRunner;
@@ -111,6 +114,8 @@ class EmulatorLifecycleTest {
Mockito.lenient().when(elbServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(servicesConfig.eks()).thenReturn(eksServiceConfig);
Mockito.lenient().when(eksServiceConfig.enabled()).thenReturn(false);
+ Mockito.lenient().when(servicesConfig.timestreamInfluxdb()).thenReturn(timestreamInfluxDbServiceConfig);
+ Mockito.lenient().when(timestreamInfluxDbServiceConfig.enabled()).thenReturn(true);
Mockito.lenient().when(config.tls()).thenReturn(tlsConfig);
Mockito.lenient().when(tlsConfig.enabled()).thenReturn(false);
Mockito.lenient().when(config.port()).thenReturn(4566);
@@ -122,7 +127,8 @@ class EmulatorLifecycleTest {
elastiCacheProxyManager, rdsContainerManager, rdsProxyManager,
memoryDbContainerManager, memoryDbProxyManager,
docDbContainerManager, neptuneContainerManager, neptuneProxyManager,
- rabbitMqManager, flinkContainerManager, rdsService, elbV2Service, elbClassicService,
+ rabbitMqManager, flinkContainerManager, rdsService, timestreamInfluxDbService,
+ elbV2Service, elbClassicService,
initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller,
pipesService, ec2MetadataServer, ecrRegistryManager, flociUiManager, initLifecycleState,
schemaCreationWorker, stepFunctionsService, containerTeardowns, persistentPathValidator);
diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbDockerIntegrationTest.java
new file mode 100644
index 0000000000..0450a18627
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbDockerIntegrationTest.java
@@ -0,0 +1,155 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import com.github.dockerjava.api.DockerClient;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import io.restassured.response.ValidatableResponse;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.Base64;
+import java.util.Map;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+@Tag("docker")
+@QuarkusTest
+@TestProfile(TimestreamInfluxDbDockerIntegrationTest.RealContainerProfile.class)
+class TimestreamInfluxDbDockerIntegrationTest {
+
+ private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
+ private static final String TARGET = "AmazonTimestreamInfluxDB.";
+ private static final String USERNAME = "operator";
+ private static final String PASSWORD = "password123";
+
+ public static class RealContainerProfile implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of(
+ "floci.services.timestream-influxdb.mock", "false",
+ "floci.services.timestream-influxdb.host-port-base", "18186",
+ "floci.services.timestream-influxdb.host-port-max", "18285");
+ }
+ }
+
+ @Inject
+ DockerClient dockerClient;
+
+ private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
+
+ @BeforeEach
+ void requireDocker() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ boolean available;
+ try {
+ dockerClient.pingCmd().exec();
+ available = true;
+ } catch (RuntimeException e) {
+ available = false;
+ }
+ Assumptions.assumeTrue(available, "Docker daemon must be available for InfluxDB container tests");
+ }
+
+ @Test
+ void instanceEndpointServesInfluxDbAndBackupsRestoreItsData() throws Exception {
+ String sourceId = call("CreateDbInstance", """
+ {"name":"docker-source","username":"%s","password":"%s","organization":"acme","bucket":"metrics",
+ "dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":["subnet-abc123"],"vpcSecurityGroupIds":["sg-abc123"]}
+ """.formatted(USERNAME, PASSWORD)).statusCode(200).extract().path("id");
+ String restoredId = null;
+ try {
+ Map<String, Object> source = awaitInstance(sourceId, "AVAILABLE");
+ String sourceUrl = "http://" + source.get("endpoint") + ":" + source.get("port");
+
+ String session = signIn(sourceUrl);
+ HttpResponse<String> write = http.send(HttpRequest.newBuilder(
+ URI.create(sourceUrl + "/api/v2/write?org=acme&bucket=metrics&precision=s"))
+ .header("Cookie", session)
+ .POST(HttpRequest.BodyPublishers.ofString("cpu,host=a value=42 1700000000"))
+ .build(), HttpResponse.BodyHandlers.ofString());
+ assertEquals(204, write.statusCode(), write.body());
+ assertTrue(query(sourceUrl, session).contains(",42,value,cpu,a"));
+
+ String backupId = call("CreateDbBackup", "{\"name\":\"docker-snap\",\"dbResourceId\":\"" + sourceId + "\"}")
+ .statusCode(200).extract().path("id");
+ awaitStatus("GetDbBackup", backupId, "COMPLETED");
+
+ restoredId = call("RestoreFromDbBackup", "{\"name\":\"docker-restored\",\"dbBackupId\":\"" + backupId + "\"}")
+ .statusCode(200).extract().path("restoredDbResourceId");
+ Map<String, Object> restored = awaitInstance(restoredId, "AVAILABLE");
+ String restoredUrl = "http://" + restored.get("endpoint") + ":" + restored.get("port");
+
+ assertTrue(query(restoredUrl, signIn(restoredUrl)).contains(",42,value,cpu,a"));
+ } finally {
+ call("DeleteDbInstance", "{\"identifier\":\"" + sourceId + "\"}");
+ if (restoredId != null) {
+ call("DeleteDbInstance", "{\"identifier\":\"" + restoredId + "\"}");
+ }
+ }
+ }
+
+ private String signIn(String baseUrl) throws Exception {
+ String basic = Base64.getEncoder().encodeToString((USERNAME + ":" + PASSWORD).getBytes(StandardCharsets.UTF_8));
+ HttpResponse<String> response = http.send(HttpRequest.newBuilder(URI.create(baseUrl + "/api/v2/signin"))
+ .header("Authorization", "Basic " + basic)
+ .POST(HttpRequest.BodyPublishers.noBody())
+ .build(), HttpResponse.BodyHandlers.ofString());
+ assertEquals(204, response.statusCode(), response.body());
+ return response.headers().firstValue("Set-Cookie").orElseThrow().split(";")[0];
+ }
+
+ private String query(String baseUrl, String session) throws Exception {
+ HttpResponse<String> response = http.send(HttpRequest.newBuilder(URI.create(baseUrl + "/api/v2/query?org=acme"))
+ .header("Cookie", session)
+ .header("Content-Type", "application/vnd.flux")
+ .POST(HttpRequest.BodyPublishers.ofString(
+ "from(bucket:\"metrics\") |> range(start: 0) |> filter(fn: (r) => r._measurement == \"cpu\")"))
+ .build(), HttpResponse.BodyHandlers.ofString());
+ assertEquals(200, response.statusCode(), response.body());
+ return response.body();
+ }
+
+ private Map<String, Object> awaitInstance(String id, String expectedStatus) throws InterruptedException {
+ awaitStatus("GetDbInstance", id, expectedStatus);
+ return call("GetDbInstance", "{\"identifier\":\"" + id + "\"}").extract().jsonPath().getMap("$");
+ }
+
+ private void awaitStatus(String action, String id, String expectedStatus) throws InterruptedException {
+ long deadline = System.currentTimeMillis() + 180_000;
+ String status = null;
+ while (System.currentTimeMillis() < deadline) {
+ status = call(action, "{\"identifier\":\"" + id + "\"}").statusCode(200).extract().path("status");
+ if (expectedStatus.equals(status)) {
+ return;
+ }
+ if (status.endsWith("FAILED")) {
+ break;
+ }
+ Thread.sleep(1000);
+ }
+ fail(action + " " + id + " ended in status " + status + " instead of " + expectedStatus);
+ }
+
+ private static ValidatableResponse call(String action, String body) {
+ return given().contentType(CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET + action)
+ .body(body)
+ .when().post("/")
+ .then();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbIntegrationTest.java
new file mode 100644
index 0000000000..4025554196
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbIntegrationTest.java
@@ -0,0 +1,496 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ValidatableResponse;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.matchesPattern;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+@QuarkusTest
+class TimestreamInfluxDbIntegrationTest {
+
+ private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
+ private static final String TARGET = "AmazonTimestreamInfluxDB.";
+ private static final String ACCOUNT = "000000000000";
+ private static final String ID_PATTERN = "[a-z0-9]{10}";
+
+ @BeforeAll
+ static void setup() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void instanceLifecycleReportsTransitionalStatusesAndSimulatedEndpoint() {
+ String id = createInstance("lifecycle-db", "")
+ .statusCode(200)
+ .body("id", matchesPattern(ID_PATTERN))
+ .body("name", equalTo("lifecycle-db"))
+ .body("status", equalTo("CREATING"))
+ .body("port", equalTo(8086))
+ .body("deploymentType", equalTo("SINGLE_AZ"))
+ .body("networkType", equalTo("IPV4"))
+ .body("dbStorageType", equalTo("InfluxIOIncludedT1"))
+ .body("publiclyAccessible", equalTo(false))
+ .body("availabilityZone", equalTo("us-east-1a"))
+ .body("vpcSubnetIds", contains("subnet-abc123"))
+ .body("vpcSecurityGroupIds", contains("sg-abc123"))
+ .body("influxAuthParametersSecretArn", nullValue())
+ .extract().path("id");
+
+ call("GetDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("AVAILABLE"))
+ .body("arn", equalTo("arn:aws:timestream-influxdb:us-east-1:" + ACCOUNT + ":db-instance/" + id))
+ .body("endpoint", equalTo("localhost"))
+ .body("influxAuthParametersSecretArn", startsWith("arn:aws:secretsmanager:us-east-1:" + ACCOUNT + ":secret:"));
+
+ call("ListDbInstances", "{}")
+ .statusCode(200)
+ .body("items.id", hasItem(id));
+
+ call("UpdateDbInstance", """
+ {"identifier":"%s","dbInstanceType":"db.influx.xlarge","port":9000}
+ """.formatted(id))
+ .statusCode(200)
+ .body("status", equalTo("UPDATING_INSTANCE_TYPE"))
+ .body("dbInstanceType", equalTo("db.influx.xlarge"))
+ .body("port", equalTo(9000));
+
+ call("RebootDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("REBOOTING"));
+
+ call("GetDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("AVAILABLE"))
+ .body("port", equalTo(9000));
+
+ call("DeleteDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("DELETING"));
+
+ call("GetDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"))
+ .body("resourceId", equalTo(id))
+ .body("resourceType", equalTo("DB_INSTANCE"));
+ }
+
+ @Test
+ void createInstanceStoresInitialAuthParametersInSecretsManager() {
+ String id = createInstance("secret-db", ",\"username\":\"operator\",\"organization\":\"acme\",\"bucket\":\"metrics\"")
+ .statusCode(200)
+ .extract().path("id");
+ String secretArn = call("GetDbInstance", "{\"identifier\":\"" + id + "\"}")
+ .statusCode(200)
+ .extract().path("influxAuthParametersSecretArn");
+
+ String secret = given().contentType("application/x-amz-json-1.1")
+ .header("X-Amz-Target", "secretsmanager.GetSecretValue")
+ .body("{\"SecretId\":\"" + secretArn + "\"}")
+ .when().post("/")
+ .then().statusCode(200)
+ .extract().path("SecretString");
+
+ assertEquals("{\"organization\":\"acme\",\"bucket\":\"metrics\",\"username\":\"operator\",\"password\":\"password123\"}",
+ secret);
+ }
+
+ @Test
+ void createInstanceRejectsInputOutsideTheModelConstraints() {
+ call("CreateDbInstance", """
+ {"name":"no-password","dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":["subnet-abc123"],"vpcSecurityGroupIds":["sg-abc123"]}
+ """)
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("reason", equalTo("FIELD_VALIDATION_FAILED"));
+
+ assertValidation(instanceBody("1bad", "db.influx.medium", 20, "8086"));
+ assertValidation(instanceBody("bad-storage", "db.influx.medium", 19, "8086"));
+ assertValidation(instanceBody("bad-type", "db.influx.tiny", 20, "8086"));
+ assertValidation(instanceBody("reserved-port", "db.influx.medium", 20, "8090"));
+ assertValidation("""
+ {"name":"no-subnets","password":"password123","dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":[],"vpcSecurityGroupIds":["sg-abc123"]}
+ """);
+ }
+
+ @Test
+ void instanceNamesAreUniquePerAccountAndRegion() {
+ String id = createInstance("dup-db", "").statusCode(200).extract().path("id");
+
+ createInstance("dup-db", "")
+ .statusCode(400)
+ .body("__type", equalTo("ConflictException"))
+ .body("resourceId", equalTo(id))
+ .body("resourceType", equalTo("DB_INSTANCE"));
+ }
+
+ @Test
+ void listDbParameterGroupsPaginatesWithMaxResultsAndNextToken() {
+ for (int i = 0; i < 3; i++) {
+ call("CreateDbParameterGroup", "{\"name\":\"page-group-" + i + "\"}").statusCode(200);
+ }
+ int total = call("ListDbParameterGroups", "{}").statusCode(200).extract().path("items.size()");
+
+ String token = call("ListDbParameterGroups", "{\"maxResults\":2}")
+ .statusCode(200)
+ .body("items", hasSize(2))
+ .body("nextToken", notNullValue())
+ .extract().path("nextToken");
+
+ call("ListDbParameterGroups", "{\"maxResults\":100,\"nextToken\":\"" + token + "\"}")
+ .statusCode(200)
+ .body("items", hasSize(total - 2))
+ .body("nextToken", nullValue());
+
+ assertValidationFor("ListDbParameterGroups", "{\"maxResults\":0}");
+ assertValidationFor("ListDbParameterGroups", "{\"maxResults\":101}");
+ }
+
+ @Test
+ void parameterGroupsAreRetrievableAndReferencedByInstances() {
+ String groupId = call("CreateDbParameterGroup", """
+ {"name":"tuned-v2","description":"tuned","parameters":{"InfluxDBv2":{"logLevel":"debug","queryConcurrency":10}},
+ "tags":{"team":"metrics"}}
+ """)
+ .statusCode(200)
+ .body("id", matchesPattern(ID_PATTERN))
+ .body("arn", endsWithResource("db-parameter-group"))
+ .body("parameters.InfluxDBv2.logLevel", equalTo("debug"))
+ .extract().path("id");
+
+ call("GetDbParameterGroup", "{\"identifier\":\"" + groupId + "\"}")
+ .statusCode(200)
+ .body("name", equalTo("tuned-v2"))
+ .body("description", equalTo("tuned"))
+ .body("parameters.InfluxDBv2.queryConcurrency", equalTo(10));
+
+ call("CreateDbParameterGroup", "{\"name\":\"tuned-v2\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ConflictException"))
+ .body("resourceType", equalTo("DB_PARAMETER_GROUP"));
+
+ assertValidationFor("CreateDbParameterGroup",
+ "{\"name\":\"bad-level\",\"parameters\":{\"InfluxDBv2\":{\"logLevel\":\"verbose\"}}}");
+
+ createInstance("grouped-db", ",\"dbParameterGroupIdentifier\":\"" + groupId + "\"")
+ .statusCode(200)
+ .body("dbParameterGroupIdentifier", equalTo(groupId));
+
+ createInstance("missing-group-db", ",\"dbParameterGroupIdentifier\":\"nosuchgroup\"")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"))
+ .body("resourceType", equalTo("DB_PARAMETER_GROUP"));
+ }
+
+ @Test
+ void clusterLifecycleManagesItsMemberInstances() {
+ String clusterId = call("CreateDbCluster", """
+ {"name":"lifecycle-cluster","password":"password123","dbInstanceType":"db.influx.large",
+ "allocatedStorage":40,"vpcSubnetIds":["subnet-abc123","subnet-def456"],"vpcSecurityGroupIds":["sg-abc123"]}
+ """)
+ .statusCode(200)
+ .body("dbClusterId", matchesPattern(ID_PATTERN))
+ .body("dbClusterStatus", equalTo("CREATING"))
+ .extract().path("dbClusterId");
+
+ call("GetDbCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("AVAILABLE"))
+ .body("arn", equalTo("arn:aws:timestream-influxdb:us-east-1:" + ACCOUNT + ":db-cluster/" + clusterId))
+ .body("endpoint", equalTo("localhost"))
+ .body("readerEndpoint", equalTo("localhost"))
+ .body("engineType", equalTo("INFLUXDB_V2"))
+ .body("deploymentType", equalTo("MULTI_NODE_READ_REPLICAS"))
+ .body("failoverMode", equalTo("AUTOMATIC"))
+ .body("port", equalTo(8086));
+
+ call("ListDbClusters", "{}")
+ .statusCode(200)
+ .body("items.id", hasItem(clusterId));
+
+ ValidatableResponse members = call("ListDbInstancesForCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("items", hasSize(2))
+ .body("items.instanceMode", containsInAnyOrder("PRIMARY", "REPLICA"))
+ .body("items.dbInstanceType", contains("db.influx.large", "db.influx.large"));
+ List<String> memberIds = members.extract().path("items.id");
+
+ call("GetDbInstance", "{\"identifier\":\"" + memberIds.get(0) + "\"}")
+ .statusCode(200)
+ .body("dbClusterId", equalTo(clusterId));
+
+ call("DeleteDbInstance", "{\"identifier\":\"" + memberIds.get(0) + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ConflictException"));
+
+ call("UpdateDbCluster", "{\"dbClusterId\":\"" + clusterId + "\",\"dbInstanceType\":\"db.influx.2xlarge\"}")
+ .statusCode(200)
+ .body("dbClusterStatus", equalTo("UPDATING_INSTANCE_TYPE"));
+
+ call("ListDbInstancesForCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("items.dbInstanceType", contains("db.influx.2xlarge", "db.influx.2xlarge"));
+
+ call("RebootDbCluster", "{\"dbClusterId\":\"" + clusterId + "\",\"instanceIds\":[\"" + memberIds.get(1) + "\"]}")
+ .statusCode(200)
+ .body("dbClusterStatus", equalTo("REBOOTING"));
+
+ call("RebootDbCluster", "{\"dbClusterId\":\"" + clusterId + "\",\"instanceIds\":[\"notamember\"]}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"))
+ .body("resourceType", equalTo("DB_INSTANCE"));
+
+ call("DeleteDbCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("dbClusterStatus", equalTo("DELETING"));
+
+ call("ListDbInstancesForCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"))
+ .body("resourceType", equalTo("DB_CLUSTER"));
+
+ call("ListDbInstances", "{}")
+ .statusCode(200)
+ .body("items.id", not(hasItem(memberIds.get(0))));
+ }
+
+ @Test
+ void enterpriseClusterIsSizedFromItsParameterGroup() {
+ String groupId = call("CreateDbParameterGroup", """
+ {"name":"enterprise-group","parameters":{"InfluxDBv3Enterprise":
+ {"ingestQueryInstances":2,"queryOnlyInstances":1,"dedicatedCompactor":true}}}
+ """)
+ .statusCode(200)
+ .extract().path("id");
+
+ String clusterId = call("CreateDbCluster", """
+ {"name":"enterprise-cluster","dbInstanceType":"db.influx.large","dbParameterGroupIdentifier":"%s",
+ "vpcSubnetIds":["subnet-abc123"],"vpcSecurityGroupIds":["sg-abc123"]}
+ """.formatted(groupId))
+ .statusCode(200)
+ .extract().path("dbClusterId");
+
+ call("GetDbCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("engineType", equalTo("INFLUXDB_V3_ENTERPRISE"))
+ .body("port", equalTo(8181))
+ .body("dbParameterGroupIdentifier", equalTo(groupId))
+ .body("clusterConfiguration.ingestQueryInstances", equalTo(2))
+ .body("clusterConfiguration.queryOnlyInstances", equalTo(1))
+ .body("clusterConfiguration.dedicatedCompactor", equalTo(true));
+
+ call("ListDbInstancesForCluster", "{\"dbClusterId\":\"" + clusterId + "\"}")
+ .statusCode(200)
+ .body("items", hasSize(4));
+
+ assertValidationFor("CreateDbParameterGroup",
+ "{\"name\":\"enterprise-missing\",\"parameters\":{\"InfluxDBv3Enterprise\":{\"queryOnlyInstances\":1}}}");
+
+ createInstance("v3-standalone", ",\"dbParameterGroupIdentifier\":\"" + groupId + "\"")
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+ }
+
+ @Test
+ void tagsCanBeAddedListedAndRemovedByArn() {
+ String arn = createInstance("tagged-db", ",\"tags\":{\"env\":\"dev\"}")
+ .statusCode(200)
+ .extract().path("arn");
+
+ call("TagResource", "{\"resourceArn\":\"" + arn + "\",\"tags\":{\"team\":\"metrics\"}}")
+ .statusCode(200);
+
+ call("ListTagsForResource", "{\"resourceArn\":\"" + arn + "\"}")
+ .statusCode(200)
+ .body("tags.env", equalTo("dev"))
+ .body("tags.team", equalTo("metrics"));
+
+ call("UntagResource", "{\"resourceArn\":\"" + arn + "\",\"tagKeys\":[\"env\"]}")
+ .statusCode(200);
+
+ call("ListTagsForResource", "{\"resourceArn\":\"" + arn + "\"}")
+ .statusCode(200)
+ .body("tags.env", nullValue())
+ .body("tags.team", equalTo("metrics"));
+
+ call("ListTagsForResource",
+ "{\"resourceArn\":\"arn:aws:timestream-influxdb:us-east-1:" + ACCOUNT + ":db-instance/missing123\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"));
+
+ assertValidationFor("ListTagsForResource", "{\"resourceArn\":\"arn:aws:s3:::bucket\"}");
+ }
+
+ @Test
+ void backupsCanBeTakenRestoredAndDeleted() {
+ String instanceId = createInstance("backup-source", "").statusCode(200).extract().path("id");
+
+ String backupId = call("CreateDbBackup", """
+ {"name":"nightly","dbResourceId":"%s","retentionDays":7}
+ """.formatted(instanceId))
+ .statusCode(200)
+ .body("id", matchesPattern(ID_PATTERN))
+ .body("arn", endsWithResource("db-backup"))
+ .body("status", equalTo("IN_PROGRESS"))
+ .body("type", equalTo("ON_DEMAND"))
+ .body("dbResourceId", equalTo(instanceId))
+ .body("engineType", equalTo("INFLUXDB_V2"))
+ .body("createdAt", instanceOf(Number.class))
+ .body("expiresAfter", matchesPattern("\\d{4}-\\d{2}-\\d{2}"))
+ .extract().path("id");
+
+ call("GetDbBackup", "{\"identifier\":\"" + backupId + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("COMPLETED"))
+ .body("dbInstanceType", equalTo("db.influx.medium"))
+ .body("allocatedStorage", equalTo(20));
+
+ call("ListDbBackups", "{\"dbResourceId\":\"" + instanceId + "\"}")
+ .statusCode(200)
+ .body("items.id", contains(backupId));
+
+ call("CreateDbBackup", "{\"name\":\"nightly\",\"dbResourceId\":\"" + instanceId + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ConflictException"));
+
+ call("CreateDbBackup", "{\"name\":\"orphan\",\"dbResourceId\":\"missing123\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"));
+
+ String restoredId = call("RestoreFromDbBackup", "{\"name\":\"restored-db\",\"dbBackupId\":\"" + backupId + "\"}")
+ .statusCode(200)
+ .body("restoreStatus", equalTo("RESTORING"))
+ .body("resourceType", equalTo("DB_INSTANCE"))
+ .body("engineType", equalTo("INFLUXDB_V2"))
+ .body("deploymentType", equalTo("SINGLE_AZ"))
+ .extract().path("restoredDbResourceId");
+ assertNotEquals(instanceId, restoredId);
+
+ call("GetDbInstance", "{\"identifier\":\"" + restoredId + "\"}")
+ .statusCode(200)
+ .body("name", equalTo("restored-db"))
+ .body("status", equalTo("AVAILABLE"))
+ .body("dbInstanceType", equalTo("db.influx.medium"));
+
+ call("RestoreFromDbBackup", "{\"name\":\"backup-source\",\"dbBackupId\":\"" + backupId + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ConflictException"));
+
+ assertValidationFor("RestoreFromDbBackup",
+ "{\"name\":\"other-name\",\"dbBackupId\":\"" + backupId + "\",\"restoreMode\":\"REPLACE_EXISTING\"}");
+
+ call("RestoreFromDbBackup",
+ "{\"name\":\"backup-source\",\"dbBackupId\":\"" + backupId + "\",\"restoreMode\":\"REPLACE_EXISTING\"}")
+ .statusCode(200)
+ .body("restoredDbResourceId", equalTo(instanceId));
+
+ call("DeleteDbBackup", "{\"identifier\":\"" + backupId + "\"}")
+ .statusCode(200)
+ .body("status", equalTo("DELETING"));
+
+ call("GetDbBackup", "{\"identifier\":\"" + backupId + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"))
+ .body("resourceType", equalTo("DB_BACKUP"));
+ }
+
+ @Test
+ void pointInTimeRestoreIsRejectedAndLeavesNothingBehind() {
+ String instanceId = createInstance("pitr-source", "").statusCode(200).extract().path("id");
+ String backupId = call("CreateDbBackup", "{\"name\":\"pitr-snap\",\"dbResourceId\":\"" + instanceId + "\"}")
+ .statusCode(200).extract().path("id");
+
+ assertValidationFor("RestoreFromDbBackup",
+ "{\"name\":\"pitr-restored\",\"dbBackupId\":\"" + backupId + "\",\"restoreToTime\":1700000000}");
+
+ call("ListDbInstances", "{\"maxResults\":100}")
+ .statusCode(200)
+ .body("items.name", not(hasItem("pitr-restored")));
+ }
+
+ @Test
+ void replaceExistingRejectsConfigurationOverrides() {
+ String instanceId = createInstance("replace-source", "").statusCode(200).extract().path("id");
+ String backupId = call("CreateDbBackup", "{\"name\":\"replace-snap\",\"dbResourceId\":\"" + instanceId + "\"}")
+ .statusCode(200).extract().path("id");
+ String replaceBase = "{\"name\":\"replace-source\",\"dbBackupId\":\"" + backupId
+ + "\",\"restoreMode\":\"REPLACE_EXISTING\"";
+
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"port\":9100}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"tags\":{\"env\":\"dev\"}}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"networkType\":\"DUAL\"}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"vpcSubnetIds\":[\"subnet-zzz999\"]}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"publiclyAccessible\":true}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"deploymentType\":\"WITH_MULTIAZ_STANDBY\"}");
+ assertValidationFor("RestoreFromDbBackup", replaceBase + ",\"kmsKeyId\":\"alias/floci\"}");
+
+ call("GetDbInstance", "{\"identifier\":\"" + instanceId + "\"}")
+ .statusCode(200)
+ .body("port", equalTo(8086))
+ .body("networkType", equalTo("IPV4"))
+ .body("deploymentType", equalTo("SINGLE_AZ"))
+ .body("publiclyAccessible", equalTo(false))
+ .body("vpcSubnetIds", contains("subnet-abc123"));
+
+ call("RestoreFromDbBackup", replaceBase + "}")
+ .statusCode(200)
+ .body("restoredDbResourceId", equalTo(instanceId));
+ }
+
+ private static org.hamcrest.Matcher<String> endsWithResource(String resourceType) {
+ return matchesPattern("arn:aws:timestream-influxdb:us-east-1:" + ACCOUNT + ":" + resourceType + "/" + ID_PATTERN);
+ }
+
+ private static ValidatableResponse createInstance(String name, String extraFields) {
+ return call("CreateDbInstance", """
+ {"name":"%s","password":"password123","dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":["subnet-abc123"],"vpcSecurityGroupIds":["sg-abc123"]%s}
+ """.formatted(name, extraFields));
+ }
+
+ private static String instanceBody(String name, String instanceType, int storage, String port) {
+ return """
+ {"name":"%s","password":"password123","dbInstanceType":"%s","allocatedStorage":%d,"port":%s,
+ "vpcSubnetIds":["subnet-abc123"],"vpcSecurityGroupIds":["sg-abc123"]}
+ """.formatted(name, instanceType, storage, port);
+ }
+
+ private static void assertValidation(String body) {
+ assertValidationFor("CreateDbInstance", body);
+ }
+
+ private static void assertValidationFor(String action, String body) {
+ call(action, body)
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("reason", equalTo("FIELD_VALIDATION_FAILED"));
+ }
+
+ private static ValidatableResponse call(String action, String body) {
+ return given().contentType(CONTENT_TYPE)
+ .header("X-Amz-Target", TARGET + action)
+ .body(body)
+ .when().post("/")
+ .then();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java
new file mode 100644
index 0000000000..200947cb44
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java
@@ -0,0 +1,308 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.TimestreamInfluxDbService.RestoreResult;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbEndpoint;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbSetup;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbBackup;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbCluster;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbInstance;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.model.DbParameterGroup;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class TimestreamInfluxDbServiceTest {
+
+ private static final String ACCOUNT = "000000000000";
+ private static final String REGION = "us-east-1";
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+ private TimestreamInfluxDbContainerManager containerManager;
+ private TimestreamInfluxDbService service;
+
+ @BeforeEach
+ void setUp() {
+ containerManager = mock(TimestreamInfluxDbContainerManager.class);
+ when(containerManager.isDockerReachable()).thenReturn(true);
+ when(containerManager.start(anyString(), anyString(), anyString(), any(), anyMap()))
+ .thenReturn(new InfluxDbEndpoint("container-1", "localhost", 18086));
+ SecretsManagerService secrets = mock(SecretsManagerService.class);
+ Secret secret = mock(Secret.class);
+ when(secret.getArn()).thenReturn("arn:aws:secretsmanager:us-east-1:000000000000:secret:influx");
+ when(secrets.createSecret(anyString(), anyString(), isNull(), anyString(), isNull(), isNull(), eq(REGION)))
+ .thenReturn(secret);
+ service = new TimestreamInfluxDbService(
+ AccountAwareStorageBackend.<DbInstance>inMemory(ACCOUNT),
+ AccountAwareStorageBackend.<DbCluster>inMemory(ACCOUNT),
+ AccountAwareStorageBackend.<DbParameterGroup>inMemory(ACCOUNT),
+ AccountAwareStorageBackend.<DbBackup>inMemory(ACCOUNT),
+ new RegionResolver(REGION, ACCOUNT), secrets, objectMapper, containerManager, false, Runnable::run);
+ }
+
+ @Test
+ void createInstanceStartsAContainerWithTheInitialAuthParametersAndBecomesAvailable() throws Exception {
+ DbInstance created = service.createDbInstance(json("""
+ {"name":"real-db","username":"operator","password":"password123","organization":"acme",
+ "bucket":"metrics","dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]}
+ """), REGION).resource();
+
+ ArgumentCaptor<InfluxDbSetup> setup = ArgumentCaptor.forClass(InfluxDbSetup.class);
+ verify(containerManager).start(eq(created.getId()), eq(ACCOUNT), eq(REGION), setup.capture(), eq(Map.of()));
+ assertEquals(new InfluxDbSetup("operator", "password123", "acme", "metrics"), setup.getValue());
+
+ DbInstance stored = service.getDbInstance(json("{\"identifier\":\"" + created.getId() + "\"}"), REGION);
+ assertEquals("AVAILABLE", stored.getStatus());
+ assertEquals("localhost", stored.getEndpoint());
+ assertEquals(18086, stored.getPort());
+ assertEquals("container-1", stored.getContainerId());
+ }
+
+ @Test
+ void instanceIsFailedWhenTheContainerDoesNotBecomeHealthy() throws Exception {
+ doThrow(new IllegalStateException("not healthy")).when(containerManager).waitUntilReady(any());
+
+ String id = createInstance("unhealthy-db", null).getId();
+
+ assertEquals("FAILED", service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION).getStatus());
+ }
+
+ @Test
+ void instanceIsAvailableWithoutAContainerWhenNoDockerDaemonIsReachable() throws Exception {
+ when(containerManager.isDockerReachable()).thenReturn(false);
+
+ String id = createInstance("no-docker-db", null).getId();
+
+ DbInstance stored = service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION);
+ assertEquals("AVAILABLE", stored.getStatus());
+ assertNull(stored.getContainerId());
+ verify(containerManager, never()).start(anyString(), anyString(), anyString(), any(), anyMap());
+ }
+
+ @Test
+ void parameterGroupValuesReachTheContainerAsInfluxdEnvironment() throws Exception {
+ String groupId = service.createDbParameterGroup(json("""
+ {"name":"tuned","parameters":{"InfluxDBv2":{"logLevel":"debug",
+ "httpIdleTimeout":{"durationType":"minutes","value":5}}}}
+ """), REGION).getId();
+
+ createInstance("tuned-db", groupId);
+
+ verify(containerManager).start(anyString(), eq(ACCOUNT), eq(REGION), any(),
+ eq(Map.of("INFLUXD_LOG_LEVEL", "debug", "INFLUXD_HTTP_IDLE_TIMEOUT", "5m")));
+ }
+
+ @Test
+ void switchingParameterGroupRecreatesTheContainerWithoutSetup() throws Exception {
+ String id = createInstance("switch-db", null).getId();
+ String groupId = service.createDbParameterGroup(json("""
+ {"name":"no-ui","parameters":{"InfluxDBv2":{"uiDisabled":true}}}
+ """), REGION).getId();
+
+ service.updateDbInstance(json("{\"identifier\":\"" + id + "\",\"dbParameterGroupIdentifier\":\"" + groupId + "\"}"),
+ REGION);
+
+ verify(containerManager).start(eq(id), eq(ACCOUNT), eq(REGION), isNull(), eq(Map.of("INFLUXD_UI_DISABLED", "true")));
+ assertEquals("AVAILABLE", service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION).getStatus());
+ }
+
+ @Test
+ void rebootRestartsTheContainerAndDeleteRemovesContainerAndVolumes() throws Exception {
+ String id = createInstance("reboot-db", null).getId();
+
+ service.rebootDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION);
+ verify(containerManager).restart("container-1");
+ assertEquals("AVAILABLE", service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION).getStatus());
+
+ service.deleteDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION);
+ verify(containerManager).stop(id, "container-1");
+ verify(containerManager).removeStorage(id);
+ }
+
+ @Test
+ void rebootFailureIsReportedAsRebootFailed() throws Exception {
+ String id = createInstance("reboot-fail-db", null).getId();
+ doThrow(new IllegalStateException("restart failed")).when(containerManager).restart("container-1");
+
+ service.rebootDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION);
+
+ assertEquals("REBOOT_FAILED", service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION).getStatus());
+ }
+
+ @Test
+ void backupCapturesContainerDataAndRestoreLoadsItIntoANewContainer() throws Exception {
+ String sourceId = createInstance("backup-db", null).getId();
+
+ DbBackup backup = service.createDbBackup(json("{\"name\":\"snap\",\"dbResourceId\":\"" + sourceId + "\"}"), REGION)
+ .resource();
+ verify(containerManager).backup("container-1", backup.getId());
+ DbBackup stored = service.getDbBackup(json("{\"identifier\":\"" + backup.getId() + "\"}"), REGION);
+ assertEquals("COMPLETED", stored.getStatus());
+ assertTrue(stored.isDataCaptured());
+
+ when(containerManager.start(anyString(), anyString(), anyString(), any(), anyMap()))
+ .thenReturn(new InfluxDbEndpoint("container-2", "localhost", 18087));
+ RestoreResult result = service.restoreFromDbBackup(
+ json("{\"name\":\"restored\",\"dbBackupId\":\"" + backup.getId() + "\"}"), REGION);
+
+ verify(containerManager).restore("container-2", backup.getId());
+ DbInstance restored = service.getDbInstance(json("{\"identifier\":\"" + result.restoredDbResourceId() + "\"}"),
+ REGION);
+ assertEquals("AVAILABLE", restored.getStatus());
+ assertEquals(18087, restored.getPort());
+
+ service.deleteDbBackup(json("{\"identifier\":\"" + backup.getId() + "\"}"), REGION);
+ verify(containerManager).deleteBackupArtifacts(backup.getId());
+ }
+
+ @Test
+ void failedCaptureMarksTheBackupFailedAndBlocksRestore() throws Exception {
+ String sourceId = createInstance("failed-backup-db", null).getId();
+ doThrow(new IllegalStateException("influx backup failed")).when(containerManager).backup(anyString(), anyString());
+
+ String backupId = service.createDbBackup(json("{\"name\":\"broken\",\"dbResourceId\":\"" + sourceId + "\"}"),
+ REGION).resource().getId();
+
+ assertEquals("FAILED", service.getDbBackup(json("{\"identifier\":\"" + backupId + "\"}"), REGION).getStatus());
+ AwsException error = assertThrows(AwsException.class, () -> service.restoreFromDbBackup(
+ json("{\"name\":\"never\",\"dbBackupId\":\"" + backupId + "\"}"), REGION));
+ assertEquals("ConflictException", error.getErrorCode());
+ }
+
+ @Test
+ void readReplicaClusterSharesOneContainerAcrossItsNodes() throws Exception {
+ String clusterId = service.createDbCluster(json("""
+ {"name":"replicas","password":"password123","dbInstanceType":"db.influx.large",
+ "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]}
+ """), REGION).resource().getId();
+
+ verify(containerManager).start(eq(clusterId), eq(ACCOUNT), eq(REGION), any(), anyMap());
+ DbCluster cluster = service.getDbCluster(json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION);
+ assertEquals("AVAILABLE", cluster.getStatus());
+ List<DbInstance> members = service.listDbInstancesForCluster(
+ json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION).items();
+ assertEquals(2, members.size());
+ for (DbInstance member : members) {
+ assertEquals("AVAILABLE", member.getStatus());
+ assertEquals(18086, member.getPort());
+ }
+ }
+
+ @Test
+ void influxDb3ClustersHaveNoInfluxDb2Container() throws Exception {
+ String groupId = service.createDbParameterGroup(json("{\"name\":\"core\",\"parameters\":{\"InfluxDBv3Core\":{}}}"),
+ REGION).getId();
+
+ String clusterId = service.createDbCluster(json("""
+ {"name":"core-cluster","dbInstanceType":"db.influx.large","dbParameterGroupIdentifier":"%s",
+ "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]}
+ """.formatted(groupId)), REGION).resource().getId();
+
+ verify(containerManager, never()).start(anyString(), anyString(), anyString(), any(), anyMap());
+ DbCluster cluster = service.getDbCluster(json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION);
+ assertEquals("AVAILABLE", cluster.getStatus());
+ assertEquals(8181, cluster.getPort());
+ List<DbInstance> members = service.listDbInstancesForCluster(
+ json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION).items();
+ assertEquals(1, members.size());
+ assertEquals(List.of("INGEST", "QUERY", "COMPACT", "PROCESS"), members.get(0).getInstanceModes());
+ }
+
+ @Test
+ void restoreToTimeIsRejectedBecausePointInTimeRestoreNeedsContinuousBackups() throws Exception {
+ String sourceId = createInstance("pitr-db", null).getId();
+ String backupId = service.createDbBackup(json("{\"name\":\"pitr\",\"dbResourceId\":\"" + sourceId + "\"}"),
+ REGION).resource().getId();
+
+ AwsException error = assertThrows(AwsException.class, () -> service.restoreFromDbBackup(json(
+ "{\"name\":\"pitr-copy\",\"dbBackupId\":\"" + backupId + "\",\"restoreToTime\":1700000000}"), REGION));
+
+ assertEquals("ValidationException", error.getErrorCode());
+ assertTrue(error.getMessage().contains("continuous"), error.getMessage());
+ verify(containerManager, never()).restore(anyString(), anyString());
+ }
+
+ @Test
+ void replaceExistingRejectsOverridesAndRestoresDataWithoutThem() throws Exception {
+ String sourceId = createInstance("replace-db", null).getId();
+ String backupId = service.createDbBackup(json("{\"name\":\"replace\",\"dbResourceId\":\"" + sourceId + "\"}"),
+ REGION).resource().getId();
+ String replaceBase = "{\"name\":\"replace-db\",\"dbBackupId\":\"" + backupId
+ + "\",\"restoreMode\":\"REPLACE_EXISTING\"";
+
+ AwsException error = assertThrows(AwsException.class, () -> service.restoreFromDbBackup(
+ json(replaceBase + ",\"port\":9100}"), REGION));
+ assertEquals("ValidationException", error.getErrorCode());
+ assertTrue(error.getMessage().contains("port"), error.getMessage());
+ verify(containerManager, never()).restore(anyString(), anyString());
+ assertEquals(18086, service.getDbInstance(json("{\"identifier\":\"" + sourceId + "\"}"), REGION).getPort());
+
+ service.restoreFromDbBackup(json(replaceBase + "}"), REGION);
+ verify(containerManager).restore("container-1", backupId);
+ }
+
+ @Test
+ void tagsBeyondTheQuotaAreRejected() throws Exception {
+ String arn = createInstance("quota-db", null).getArn();
+ StringBuilder tags = new StringBuilder();
+ for (int i = 0; i < 200; i++) {
+ tags.append(i == 0 ? "" : ",").append("\"k").append(i).append("\":\"v\"");
+ }
+ service.tagResource(json("{\"resourceArn\":\"" + arn + "\",\"tags\":{" + tags + "}}"));
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.tagResource(json("{\"resourceArn\":\"" + arn + "\",\"tags\":{\"extra\":\"v\"}}")));
+ assertEquals("ServiceQuotaExceededException", error.getErrorCode());
+ }
+
+ @Test
+ void higherStorageTiersRequireAtLeast400GiB() {
+ AwsException error = assertThrows(AwsException.class, () -> service.createDbInstance(json("""
+ {"name":"t2-db","password":"password123","dbInstanceType":"db.influx.medium","allocatedStorage":100,
+ "dbStorageType":"InfluxIOIncludedT2","vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]}
+ """), REGION));
+ assertEquals("ValidationException", error.getErrorCode());
+ }
+
+ private DbInstance createInstance(String name, String parameterGroupId) throws Exception {
+ String groupField = parameterGroupId == null ? "" : ",\"dbParameterGroupIdentifier\":\"" + parameterGroupId + "\"";
+ return service.createDbInstance(json("""
+ {"name":"%s","password":"password123","dbInstanceType":"db.influx.medium","allocatedStorage":20,
+ "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]%s}
+ """.formatted(name, groupField)), REGION).resource();
+ }
+
+ private JsonNode json(String value) {
+ try {
+ return objectMapper.readTree(value);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java
new file mode 100644
index 0000000000..d7fa1fdc5e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java
@@ -0,0 +1,137 @@
+package io.github.hectorvent.floci.services.timestreaminfluxdb.container;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
+import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
+import io.github.hectorvent.floci.core.common.docker.PortAllocator;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbEndpoint;
+import io.github.hectorvent.floci.services.timestreaminfluxdb.container.TimestreamInfluxDbContainerManager.InfluxDbSetup;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class TimestreamInfluxDbContainerManagerTest {
+
+ private ContainerLifecycleManager lifecycleManager;
+ private ContainerBuilder.Builder builder;
+ private PortAllocator portAllocator;
+ private TimestreamInfluxDbContainerManager manager;
+
+ @BeforeEach
+ void setUp() {
+ lifecycleManager = mock(ContainerLifecycleManager.class);
+ ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
+ builder = mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
+ when(containerBuilder.newContainer(anyString())).thenReturn(builder);
+ when(builder.build()).thenReturn(mock(ContainerSpec.class));
+ portAllocator = mock(PortAllocator.class);
+ when(portAllocator.allocate(8086, 8185)).thenReturn(8090);
+
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
+ EmulatorConfig.TimestreamInfluxDbServiceConfig influx = mock(EmulatorConfig.TimestreamInfluxDbServiceConfig.class);
+ EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
+ when(config.services()).thenReturn(services);
+ when(config.storage()).thenReturn(storage);
+ when(services.timestreamInfluxdb()).thenReturn(influx);
+ when(influx.defaultImage()).thenReturn("influxdb:2.7");
+ when(influx.hostPortBase()).thenReturn(8086);
+ when(influx.hostPortMax()).thenReturn(8185);
+ when(influx.dockerNetwork()).thenReturn(Optional.empty());
+
+ manager = new TimestreamInfluxDbContainerManager(containerBuilder, lifecycleManager,
+ mock(ContainerDetector.class), portAllocator, config);
+ }
+
+ @Test
+ void startRunsImageSetupPublishesTheHttpPortAndLabelsTheContainer() {
+ when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id",
+ Map.of(8086, new EndpointInfo("localhost", 8090))));
+
+ InfluxDbEndpoint endpoint = manager.start("abc1234567", "000000000000", "us-east-1",
+ new InfluxDbSetup("admin", "password123", "acme", "metrics"), Map.of("INFLUXD_LOG_LEVEL", "debug"));
+
+ assertEquals(new InfluxDbEndpoint("container-id", "localhost", 8090), endpoint);
+ verify(builder).withEnv("DOCKER_INFLUXDB_INIT_MODE", "setup");
+ verify(builder).withEnv("DOCKER_INFLUXDB_INIT_USERNAME", "admin");
+ verify(builder).withEnv("DOCKER_INFLUXDB_INIT_PASSWORD", "password123");
+ verify(builder).withEnv("DOCKER_INFLUXDB_INIT_ORG", "acme");
+ verify(builder).withEnv("DOCKER_INFLUXDB_INIT_BUCKET", "metrics");
+ verify(builder).withEnv("INFLUXD_LOG_LEVEL", "debug");
+ verify(builder).withPortBinding(8086, 8090);
+ verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567", "/var/lib/influxdb2");
+ verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567-config", "/etc/influxdb2");
+ verify(builder).withLabels(Map.of(
+ "io.floci", "aws",
+ "io.floci.service", "timestream-influxdb",
+ "io.floci.resource-id", "abc1234567",
+ "io.floci.account", "000000000000",
+ "io.floci.region", "us-east-1"));
+ }
+
+ @Test
+ void restartingOnExistingVolumesSkipsSetup() {
+ when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id",
+ Map.of(8086, new EndpointInfo("localhost", 8090))));
+
+ manager.start("abc1234567", "000000000000", "us-east-1", null, Map.of());
+
+ verify(builder, never()).withEnv("DOCKER_INFLUXDB_INIT_MODE", "setup");
+ }
+
+ @Test
+ void failedStartRemovesTheContainerAndReleasesThePort() {
+ when(lifecycleManager.createAndStart(any())).thenThrow(new RuntimeException("port already allocated"));
+
+ assertThrows(RuntimeException.class, () -> manager.start("abc1234567", "000000000000", "us-east-1",
+ new InfluxDbSetup("admin", "password123", "acme", "metrics"), Map.of()));
+
+ verify(portAllocator).release(8090);
+ verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-timestream-influxdb-abc1234567");
+ }
+
+ @Test
+ void stopReleasesThePublishedPort() {
+ when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id",
+ Map.of(8086, new EndpointInfo("localhost", 8090))));
+ manager.start("abc1234567", "000000000000", "us-east-1", null, Map.of());
+
+ manager.stop("abc1234567", "container-id");
+
+ verify(lifecycleManager).stopAndRemove("container-id", null);
+ verify(portAllocator).release(8090);
+ }
+
+ @Test
+ void parameterGroupMembersMapToInfluxdEnvironmentVariables() throws Exception {
+ Map<String, String> environment = TimestreamInfluxDbContainerManager.engineEnvironment(new ObjectMapper().readTree("""
+ {"fluxLogEnabled":true,"queryConcurrency":10,"influxqlMaxSelectBuckets":5,
+ "storageCacheSnapshotWriteColdDuration":{"durationType":"days","value":2},
+ "httpWriteTimeout":{"durationType":"milliseconds","value":250}}
+ """));
+
+ assertEquals(Map.of(
+ "INFLUXD_FLUX_LOG_ENABLED", "true",
+ "INFLUXD_QUERY_CONCURRENCY", "10",
+ "INFLUXD_INFLUXQL_MAX_SELECT_BUCKETS", "5",
+ "INFLUXD_STORAGE_CACHE_SNAPSHOT_WRITE_COLD_DURATION", "48h",
+ "INFLUXD_HTTP_WRITE_TIMEOUT", "250ms"), environment);
+ }
+}
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 3adc12dc76..45c3bf0be5 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -369,6 +369,9 @@ floci:
emit-mode: off
bcm-pricing-calculator:
enabled: true
+ timestream-influxdb:
+ enabled: true
+ mock: true
bcm-data-exports:
enabled: true
emit-mode: off
diff --git a/tools/docs/services.yaml b/tools/docs/services.yaml
index 9d926d6a87..9b98d0dc89 100644
--- a/tools/docs/services.yaml
+++ b/tools/docs/services.yaml
@@ -366,6 +366,10 @@ services:
doc: docs/services/verifiedpermissions.md
sources:
- { path: src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsJsonHandler.java, mode: switch }
+ - service: timestream-influxdb
+ doc: docs/services/timestream-influxdb.md
+ sources:
+ - { path: src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbJsonHandler.java, mode: switch }
- service: sqs
doc: docs/services/sqs.md
sources: