diff --git a/src/main/java/io/github/hectorvent/floci/core/common/EpochSeconds.java b/src/main/java/io/github/hectorvent/floci/core/common/EpochSeconds.java new file mode 100644 index 0000000000..7092dee083 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/EpochSeconds.java @@ -0,0 +1,14 @@ +package io.github.hectorvent.floci.core.common; + +import java.time.Instant; + +/** Converts an Instant to millisecond-precision epoch seconds for AWS JSON timestamps. */ +public final class EpochSeconds { + + private EpochSeconds() { + } + + public static double fromInstant(Instant instant) { + return instant.toEpochMilli() / 1000.0; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java index 2824e62f41..3fa69a5ed1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/acm/AcmJsonHandler.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.services.acm.model.Certificate; import io.github.hectorvent.floci.services.acm.model.CertificateOptions; @@ -385,19 +386,19 @@ public class AcmJsonHandler { } if (cert.getCreatedAt() != null) { - node.put("CreatedAt", cert.getCreatedAt().toEpochMilli() / 1000.0); + node.put("CreatedAt", EpochSeconds.fromInstant(cert.getCreatedAt())); } if (cert.getIssuedAt() != null) { - node.put("IssuedAt", cert.getIssuedAt().toEpochMilli() / 1000.0); + node.put("IssuedAt", EpochSeconds.fromInstant(cert.getIssuedAt())); } if (cert.getImportedAt() != null) { - node.put("ImportedAt", cert.getImportedAt().toEpochMilli() / 1000.0); + node.put("ImportedAt", EpochSeconds.fromInstant(cert.getImportedAt())); } if (cert.getNotBefore() != null) { - node.put("NotBefore", cert.getNotBefore().toEpochMilli() / 1000.0); + node.put("NotBefore", EpochSeconds.fromInstant(cert.getNotBefore())); } if (cert.getNotAfter() != null) { - node.put("NotAfter", cert.getNotAfter().toEpochMilli() / 1000.0); + node.put("NotAfter", EpochSeconds.fromInstant(cert.getNotAfter())); } ArrayNode inUseBy = objectMapper.createArrayNode(); @@ -492,19 +493,19 @@ public class AcmJsonHandler { node.set("ExtendedKeyUsages", extKeyUsages); if (cert.getCreatedAt() != null) { - node.put("CreatedAt", cert.getCreatedAt().toEpochMilli() / 1000.0); + node.put("CreatedAt", EpochSeconds.fromInstant(cert.getCreatedAt())); } if (cert.getIssuedAt() != null) { - node.put("IssuedAt", cert.getIssuedAt().toEpochMilli() / 1000.0); + node.put("IssuedAt", EpochSeconds.fromInstant(cert.getIssuedAt())); } if (cert.getImportedAt() != null) { - node.put("ImportedAt", cert.getImportedAt().toEpochMilli() / 1000.0); + node.put("ImportedAt", EpochSeconds.fromInstant(cert.getImportedAt())); } if (cert.getNotBefore() != null) { - node.put("NotBefore", cert.getNotBefore().toEpochMilli() / 1000.0); + node.put("NotBefore", EpochSeconds.fromInstant(cert.getNotBefore())); } if (cert.getNotAfter() != null) { - node.put("NotAfter", cert.getNotAfter().toEpochMilli() / 1000.0); + node.put("NotAfter", EpochSeconds.fromInstant(cert.getNotAfter())); } node.put("RenewalEligibility", "INELIGIBLE"); diff --git a/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java b/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java index 82896a4313..2b7840a37a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.aps; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -208,8 +209,8 @@ public class ApsController { node.put("name", namespace.getName()); node.set("status", namespaceStatusNode(namespace)); node.set("tags", objectMapper.valueToTree(namespace.getTags())); - node.put("createdAt", namespace.getCreatedAt().toEpochMilli() / 1000.0); - node.put("modifiedAt", namespace.getModifiedAt().toEpochMilli() / 1000.0); + node.put("createdAt", EpochSeconds.fromInstant(namespace.getCreatedAt())); + node.put("modifiedAt", EpochSeconds.fromInstant(namespace.getModifiedAt())); return node; } @@ -237,7 +238,7 @@ public class ApsController { node.set("status", statusNode(workspace)); // Smithy restJson1 timestamps with no timestampFormat trait are epoch-seconds; an ISO // string here fails deserialization inside the AWS SDKs. - node.put("createdAt", workspace.getCreatedAt().toEpochMilli() / 1000.0); + node.put("createdAt", EpochSeconds.fromInstant(workspace.getCreatedAt())); node.set("tags", objectMapper.valueToTree(workspace.getTags())); if (workspace.getKmsKeyArn() != null) { node.put("kmsKeyArn", workspace.getKmsKeyArn()); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2JsonHandler.java index f2f5516363..4ba931db1e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2JsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2JsonHandler.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils; import io.github.hectorvent.floci.services.cloudhsmv2.model.Backup; import io.github.hectorvent.floci.services.cloudhsmv2.model.BackupRetentionPolicy; @@ -242,7 +243,7 @@ public class CloudHsmV2JsonHandler { node.put("SecurityGroup", cluster.getSecurityGroup()); } if (cluster.getCreateTimestamp() != null) { - node.put("CreateTimestamp", cluster.getCreateTimestamp().toEpochMilli() / 1000.0); + node.put("CreateTimestamp", EpochSeconds.fromInstant(cluster.getCreateTimestamp())); } if (cluster.getBackupPolicy() != null) { node.put("BackupPolicy", cluster.getBackupPolicy()); @@ -431,7 +432,7 @@ public class CloudHsmV2JsonHandler { Backup backup = service.copyBackupToRegion(destRegion, backupId, region); ObjectNode response = objectMapper.createObjectNode(); ObjectNode destBackup = objectMapper.createObjectNode(); - destBackup.put("CreateTimestamp", backup.getCreateTimestamp().toEpochMilli() / 1000.0); + destBackup.put("CreateTimestamp", EpochSeconds.fromInstant(backup.getCreateTimestamp())); destBackup.put("SourceBackup", backup.getSourceBackup()); destBackup.put("SourceCluster", backup.getSourceCluster()); destBackup.put("SourceRegion", backup.getSourceRegion()); @@ -479,13 +480,13 @@ public class CloudHsmV2JsonHandler { node.put("HsmType", backup.getHsmType()); } if (backup.getCreateTimestamp() != null) { - node.put("CreateTimestamp", backup.getCreateTimestamp().toEpochMilli() / 1000.0); + node.put("CreateTimestamp", EpochSeconds.fromInstant(backup.getCreateTimestamp())); } if (backup.getDeleteTimestamp() != null) { - node.put("DeleteTimestamp", backup.getDeleteTimestamp().toEpochMilli() / 1000.0); + node.put("DeleteTimestamp", EpochSeconds.fromInstant(backup.getDeleteTimestamp())); } if (backup.getCopyTimestamp() != null) { - node.put("CopyTimestamp", backup.getCopyTimestamp().toEpochMilli() / 1000.0); + node.put("CopyTimestamp", EpochSeconds.fromInstant(backup.getCopyTimestamp())); } if (backup.getNeverExpires() != null) { node.put("NeverExpires", "True".equalsIgnoreCase(backup.getNeverExpires())); diff --git a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java index 95d910989c..a36d9bfb25 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.storage.StorageBackedMap; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.codebuild.model.Build; @@ -159,7 +160,7 @@ public class CodeBuildService { throw new AwsException("InvalidInputException", "artifacts.type is required", 400); } - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); Project project = new Project(); project.setName(name); project.setArn(AwsArnUtils.Arn.of("codebuild", region, account, "project/" + name).toString()); @@ -221,7 +222,7 @@ public class CodeBuildService { if (logsConfig != null) { project.setLogsConfig(logsConfig); } if (vpcConfig != null) { project.setVpcConfig(vpcConfig); } if (concurrentBuildLimit != null) { project.setConcurrentBuildLimit(concurrentBuildLimit); } - project.setLastModified(Instant.now().toEpochMilli() / 1000.0); + project.setLastModified(EpochSeconds.fromInstant(Instant.now())); persistRegion(projects, region); return project; @@ -308,7 +309,7 @@ public class CodeBuildService { throw new AwsException("InvalidInputException", "type is required", 400); } - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); ReportGroup rg = new ReportGroup(); rg.setArn(arn); rg.setName(name); @@ -334,7 +335,7 @@ public class CodeBuildService { } if (exportConfig != null) { rg.setExportConfig(exportConfig); } if (tags != null) { rg.setTags(tags); } - rg.setLastModified(Instant.now().toEpochMilli() / 1000.0); + rg.setLastModified(EpochSeconds.fromInstant(Instant.now())); persistRegion(reportGroups, region); return rg; } @@ -579,7 +580,7 @@ public class CodeBuildService { build.setCurrentPhase("SUBMITTED"); build.setProjectName(projectName); build.setInitiator("user"); - build.setStartTime(Instant.now().toEpochMilli() / 1000.0); + build.setStartTime(EpochSeconds.fromInstant(Instant.now())); build.setSource(project.getSource()); build.setArtifacts(artifactsOverride != null ? artifactsOverride : project.getArtifacts()); build.setTimeoutInMinutes(timeoutOverride != null ? timeoutOverride : project.getTimeoutInMinutes()); diff --git a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java index 9eacef4d08..be03949028 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -173,7 +174,7 @@ public class CodeDeployService { if (store == null) { continue; } - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); boolean changed = false; for (String name : BUILT_IN_CONFIG_NAMES) { if (!store.containsKey(name)) { @@ -263,7 +264,7 @@ public class CodeDeployService { private Map deploymentConfigsFor(String region) { return deploymentConfigs.computeIfAbsent(region, r -> { ConcurrentHashMap store = new ConcurrentHashMap<>(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); for (String name : BUILT_IN_CONFIG_NAMES) { store.put(name, buildBuiltInConfig(name, now)); } @@ -338,7 +339,7 @@ public class CodeDeployService { Application app = new Application(); app.setApplicationId(UUID.randomUUID().toString()); app.setApplicationName(name); - app.setCreateTime(Instant.now().toEpochMilli() / 1000.0); + app.setCreateTime(EpochSeconds.fromInstant(Instant.now())); app.setLinkedToGitHub(false); app.setComputePlatform(computePlatform != null ? computePlatform : "Server"); store.put(name, app); @@ -520,7 +521,7 @@ public class CodeDeployService { cfg.setDeploymentConfigId(UUID.randomUUID().toString()); cfg.setDeploymentConfigName(name); cfg.setMinimumHealthyHosts(minimumHealthyHosts); - cfg.setCreateTime(Instant.now().toEpochMilli() / 1000.0); + cfg.setCreateTime(EpochSeconds.fromInstant(Instant.now())); cfg.setComputePlatform(computePlatform != null ? computePlatform : "Server"); cfg.setTrafficRoutingConfig(trafficRoutingConfig); cfg.setZonalConfig(zonalConfig); @@ -590,7 +591,7 @@ public class CodeDeployService { String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName(); String deploymentId = generateDeploymentId(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); Deployment deployment = new Deployment(); deployment.setDeploymentId(deploymentId); @@ -723,7 +724,7 @@ public class CodeDeployService { "instance/" + instanceName).toString()); inst.setIamSessionArn(iamSessionArn); inst.setIamUserArn(iamUserArn); - inst.setRegisterTime(Instant.now().toEpochMilli() / 1000.0); + inst.setRegisterTime(EpochSeconds.fromInstant(Instant.now())); inst.setRegistrationStatus("Registered"); store.put(instanceName, inst); persistRegion(onPremisesInstances, region); @@ -732,7 +733,7 @@ public class CodeDeployService { public void deregisterOnPremisesInstance(String region, String instanceName) { OnPremisesInstance inst = requireOnPremisesInstance(region, instanceName); - inst.setDeregisterTime(Instant.now().toEpochMilli() / 1000.0); + inst.setDeregisterTime(EpochSeconds.fromInstant(Instant.now())); inst.setRegistrationStatus("Deregistered"); persistRegion(onPremisesInstances, region); } @@ -793,7 +794,7 @@ public class CodeDeployService { String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName(); String deploymentId = generateDeploymentId(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); Deployment deployment = new Deployment(); deployment.setDeploymentId(deploymentId); @@ -853,7 +854,7 @@ public class CodeDeployService { String deploymentId = deployment.getDeploymentId(); try { deployment.setStatus("InProgress"); - deployment.setStartTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setStartTime(EpochSeconds.fromInstant(Instant.now())); instanceTargetMaps.forEach(m -> updateTargetStatus(m, "InProgress")); boolean anyFailed = false; @@ -861,7 +862,7 @@ public class CodeDeployService { if (stopFlag.get()) { instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped")); deployment.setStatus("Stopped"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); return; } String instanceId = instanceIds.get(i); @@ -881,18 +882,18 @@ public class CodeDeployService { } else { deployment.setStatus("Succeeded"); } - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } catch (InterruptedException e) { Thread.currentThread().interrupt(); instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped")); deployment.setStatus("Stopped"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } catch (Exception e) { LOG.warnv("Server deployment {0} failed: {1}", deploymentId, e.getMessage()); instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Failed")); deployment.setStatus("Failed"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); deployment.setErrorInformation(Map.of("code", "DeploymentFailed", "message", e.getMessage() != null ? e.getMessage() : "Unknown error")); } finally { @@ -1238,7 +1239,7 @@ public class CodeDeployService { String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName(); String deploymentId = generateDeploymentId(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); Deployment deployment = new Deployment(); deployment.setDeploymentId(deploymentId); @@ -1338,7 +1339,7 @@ public class CodeDeployService { TaskSet blueTaskSet = null; try { deployment.setStatus("InProgress"); - deployment.setStartTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setStartTime(EpochSeconds.fromInstant(Instant.now())); updateEcsTargetStatus(ecsTargetMap, "InProgress"); if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; } @@ -1455,7 +1456,7 @@ public class CodeDeployService { updateEcsTargetStatus(ecsTargetMap, "Succeeded"); deployment.setStatus("Succeeded"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -1546,20 +1547,20 @@ public class CodeDeployService { private void updateEcsTargetStatus(Map ecsTargetMap, String status) { ecsTargetMap.put("status", status); - ecsTargetMap.put("lastUpdatedAt", Instant.now().toEpochMilli() / 1000.0); + ecsTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now())); } private void finishEcsStopped(Deployment deployment, Map ecsTargetMap) { updateEcsTargetStatus(ecsTargetMap, "Skipped"); deployment.setStatus("Stopped"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } private void finishEcsFailed(Deployment deployment, Map ecsTargetMap, String errorCode, String message) { updateEcsTargetStatus(ecsTargetMap, "Failed"); deployment.setStatus("Failed"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : "")); } @@ -1598,7 +1599,7 @@ public class CodeDeployService { String deploymentId = deployment.getDeploymentId(); try { deployment.setStatus("InProgress"); - deployment.setStartTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setStartTime(EpochSeconds.fromInstant(Instant.now())); updateTargetStatus(lambdaTargetMap, "InProgress"); if (stopFlag.get()) { finishStopped(deployment, lambdaTargetMap); return; } @@ -1627,7 +1628,7 @@ public class CodeDeployService { updateTargetStatus(lambdaTargetMap, "Succeeded"); deployment.setStatus("Succeeded"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -1773,18 +1774,18 @@ public class CodeDeployService { private Map addLifecycleEvent(Map lambdaTargetMap, String name) { Map event = new ConcurrentHashMap<>(); event.put("lifecycleEventName", name); - event.put("startTime", Instant.now().toEpochMilli() / 1000.0); + event.put("startTime", EpochSeconds.fromInstant(Instant.now())); event.put("status", "InProgress"); List> events = (List>) lambdaTargetMap.get("lifecycleEvents"); if (events != null) { events.add(event); } - lambdaTargetMap.put("lastUpdatedAt", Instant.now().toEpochMilli() / 1000.0); + lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now())); return event; } private void finishLifecycleEvent(Map event, String status) { - event.put("endTime", Instant.now().toEpochMilli() / 1000.0); + event.put("endTime", EpochSeconds.fromInstant(Instant.now())); event.put("status", status); } @@ -1796,20 +1797,20 @@ public class CodeDeployService { private void updateTargetStatus(Map lambdaTargetMap, String status) { lambdaTargetMap.put("status", status); - lambdaTargetMap.put("lastUpdatedAt", Instant.now().toEpochMilli() / 1000.0); + lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now())); } private void finishStopped(Deployment deployment, Map lambdaTargetMap) { updateTargetStatus(lambdaTargetMap, "Skipped"); deployment.setStatus("Stopped"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); } private void finishFailed(Deployment deployment, Map lambdaTargetMap, String errorCode, String message) { updateTargetStatus(lambdaTargetMap, "Failed"); deployment.setStatus("Failed"); - deployment.setCompleteTime(Instant.now().toEpochMilli() / 1000.0); + deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now())); deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : "")); } diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java index 8354f4f7e5..c935796c18 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.DeadlineCharSequence; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -2037,7 +2038,7 @@ public class CodePipelineService { revision.put("revisionChangeIdentifier", unquoteETag(object.getETag())); revision.put("revisionSummary", bucket + "/" + key); revision.put("revisionUrl", "s3://" + bucket + "/" + key); - revision.put("created", object.getLastModified().toEpochMilli() / 1000.0); + revision.put("created", EpochSeconds.fromInstant(object.getLastModified())); execution.getArtifactRevisions().add(revision); upsertSourceRevision(execution, state.getActionName(), revisionId, bucket, key, object); pinResolvedS3Version(execution, state.getActionName(), object.getVersionId()); @@ -3164,7 +3165,7 @@ public class CodePipelineService { } private static double now() { - return Instant.now().toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(Instant.now()); } private static void putIfNotNull(ObjectNode node, String field, Object value) { diff --git a/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncJsonHandler.java index 58da917615..d04d6bc163 100644 --- a/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncJsonHandler.java @@ -5,6 +5,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.services.datasync.model.DataSyncAgent; import io.github.hectorvent.floci.services.datasync.model.DataSyncLocation; import io.github.hectorvent.floci.services.datasync.model.DataSyncLocationType; @@ -252,7 +253,7 @@ public class DataSyncJsonHandler { } private static double epochSeconds(Instant instant) { - return (instant != null ? instant : Instant.EPOCH).toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant((instant != null ? instant : Instant.EPOCH)); } private static String text(JsonNode request, String member) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java index 7d65aa597c..ca24cd8475 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -778,7 +779,7 @@ public class DmsService implements Resettable { } private static double now() { - return Instant.now().toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(Instant.now()); } /** DMS-generated ARN suffixes are 26 characters of the base32 alphabet. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlController.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlController.java index a365bfa35b..301b512dfe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlController.java +++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlController.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.dsql; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.dsql.model.DsqlCluster; import jakarta.inject.Inject; @@ -220,7 +221,7 @@ public class DsqlController { } private static double epochSeconds(DsqlCluster cluster) { - return cluster.getCreationTime().toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(cluster.getCreationTime()); } private static String string(Object value) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java index 40d3a9fbde..f5469ada59 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.services.ecr.model.AuthorizationData; import io.github.hectorvent.floci.services.ecr.model.Image; @@ -728,7 +729,7 @@ public class EcrJsonHandler { request.path("principalArn").asText(null), region); ObjectNode response = objectMapper.createObjectNode(); response.put("principalArn", exclusion.principalArn()); - response.put("createdAt", exclusion.createdAt().toEpochMilli() / 1000.0); + response.put("createdAt", EpochSeconds.fromInstant(exclusion.createdAt())); return Response.ok(response).build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java index 75fc9d18b3..b31bb5f156 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy; import io.github.hectorvent.floci.services.ecs.model.Attribute; @@ -1140,9 +1141,9 @@ public class EcsJsonHandler { brief.put("serviceArn", d.getServiceArn()); brief.put("clusterArn", d.getClusterArn()); brief.put("status", d.getStatus()); - if (d.getCreatedAt() != null) { brief.put("createdAt", d.getCreatedAt().toEpochMilli() / 1000.0); } - if (d.getStartedAt() != null) { brief.put("startedAt", d.getStartedAt().toEpochMilli() / 1000.0); } - if (d.getFinishedAt() != null) { brief.put("finishedAt", d.getFinishedAt().toEpochMilli() / 1000.0); } + if (d.getCreatedAt() != null) { brief.put("createdAt", EpochSeconds.fromInstant(d.getCreatedAt())); } + if (d.getStartedAt() != null) { brief.put("startedAt", EpochSeconds.fromInstant(d.getStartedAt())); } + if (d.getFinishedAt() != null) { brief.put("finishedAt", EpochSeconds.fromInstant(d.getFinishedAt())); } if (d.getTargetServiceRevisionArn() != null) { brief.put("targetServiceRevisionArn", d.getTargetServiceRevisionArn()); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java index 25f3629688..4746bf1963 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsResponseWriter.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecs; 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.EpochSeconds; import io.github.hectorvent.floci.services.ecs.model.Attribute; import io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration; import io.github.hectorvent.floci.services.ecs.model.CapacityProvider; @@ -1167,7 +1168,7 @@ public class EcsResponseWriter { /** ECS timestamps go on the wire as epoch seconds with a fractional part. */ private static void putInstant(ObjectNode target, String field, Instant value) { if (value != null) { - target.put(field, value.toEpochMilli() / 1000.0); + target.put(field, EpochSeconds.fromInstant(value)); } } } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java index 203431edaa..8817a778f9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.storage.StorageBackend; @@ -134,7 +135,7 @@ public class EksAddonService { List associationArns = createOrLinkPodIdentityAssociations(cluster, addonName, request.podIdentityAssociations()); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); String addonArn = generateAddonArn(cluster, addonName); Optional addonInfo = catalog.findAddon(addonName); String owner = addonInfo.map(AddonInfo::owner).orElse("aws"); @@ -257,7 +258,7 @@ public class EksAddonService { params.add(new UpdateParam("PodIdentityAssociations", serializeAssociations(request.podIdentityAssociations()))); } - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); Addon updatedAddon = new Addon( current.addonArn(), current.addonName(), @@ -324,7 +325,7 @@ public class EksAddonService { "DELETING", current.health(), current.createdAt(), - Instant.now().toEpochMilli() / 1000.0, + EpochSeconds.fromInstant(Instant.now()), current.serviceAccountRoleArn(), current.configurationValues(), current.tags(), diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java index 3409e25b21..aa25c933d2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -129,7 +130,7 @@ public class EksPodIdentityAssociationService { cluster.getAccountId() != null ? cluster.getAccountId() : regionResolver.getDefaultAccountId()); String associationArn = AwsArnUtils.Arn.of("eks", region, accountId, "podidentityassociation/" + cluster.getName() + "/" + associationId).toString(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); String externalId = UUID.randomUUID().toString(); PodIdentityAssociation association = new PodIdentityAssociation( @@ -206,7 +207,7 @@ public class EksPodIdentityAssociationService { ? request.disableSessionTags() : current.disableSessionTags(); String newPolicy = request.policy() != null ? request.policy() : current.policy(); - double now = Instant.now().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(Instant.now()); PodIdentityAssociation updated = new PodIdentityAssociation( current.clusterName(), current.namespace(), diff --git a/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java b/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java index df4eb8ea10..e670498f16 100644 --- a/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java +++ b/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.TagHandler; import io.github.hectorvent.floci.core.storage.StorageBackend; @@ -1655,7 +1656,7 @@ public class FisService implements TagHandler { } private double now() { - return Instant.now().toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(Instant.now()); } private String key(String region, String type, String id) { diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java index 2a58998429..849dbdbf6c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java @@ -5,6 +5,7 @@ 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.AwsJsonTimestamp; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.services.glue.model.*; import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService; import io.github.hectorvent.floci.services.glue.schemaregistry.model.Registry; @@ -1179,7 +1180,7 @@ public class GlueJsonHandler { } private static double epochSeconds(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } private static String iso(Instant instant) { diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java index e7c5c6d287..ce834aa1ac 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -1930,7 +1931,7 @@ public class GlueService { Map details = new LinkedHashMap<>(classifier.selectedDetails()); validateClassifierDetails(classifier.selectedKind(), details); - double now = Instant.now().toEpochMilli() / 1000.0d; + double now = EpochSeconds.fromInstant(Instant.now()); details.put("CreationTime", now); details.put("LastUpdated", now); details.put("Version", 1L); @@ -1969,7 +1970,7 @@ public class GlueService { validateClassifierDetails(existing.selectedKind(), details); Number version = (Number) existing.selectedDetails().get("Version"); details.put("CreationTime", existing.selectedDetails().get("CreationTime")); - details.put("LastUpdated", Instant.now().toEpochMilli() / 1000.0d); + details.put("LastUpdated", EpochSeconds.fromInstant(Instant.now())); details.put("Version", (version == null ? 1L : version.longValue()) + 1L); classifierStore.put(name, existing.copyWithDetails(details)); LOG.infov("Updated Glue Classifier: {0}", name); diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java index 227bfdd32c..589ee08636 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.glue; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.core.storage.WriteProfile; @@ -673,7 +674,7 @@ public class GlueTriggerService { } private static double epochSeconds(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java index e67ccd421d..df7a59a1dd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java +++ b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java @@ -5,6 +5,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.guardduty.model.Detector; import io.github.hectorvent.floci.services.guardduty.model.DetectorFeature; @@ -336,7 +337,7 @@ public class GuardDutyController { } response.put("arn", plan.getArn()); // The model types createdAt as a timestamp, which restJson1 carries as epoch seconds. - response.put("createdAt", Instant.parse(plan.getCreatedAt()).toEpochMilli() / 1000.0); + response.put("createdAt", EpochSeconds.fromInstant(Instant.parse(plan.getCreatedAt()))); ObjectNode bucket = response.putObject("protectedResource").putObject("s3Bucket"); bucket.put("bucketName", plan.getBucketName()); if (plan.getObjectPrefixes() != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java index 290f0895b5..bb6a250afe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iot; 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.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.iot.model.IotAuthorizer; import jakarta.inject.Inject; @@ -137,9 +138,9 @@ public class IotAuthorizerController { description.put("authorizerArn", authorizer.getAuthorizerArn()); description.put("authorizerFunctionArn", authorizer.getAuthorizerFunctionArn()); description.put("authorizerName", authorizer.getAuthorizerName()); - description.put("creationDate", authorizer.getCreationDate().toEpochMilli() / 1000.0); + description.put("creationDate", EpochSeconds.fromInstant(authorizer.getCreationDate())); description.put("enableCachingForHttp", authorizer.isEnableCachingForHttp()); - description.put("lastModifiedDate", authorizer.getLastModifiedDate().toEpochMilli() / 1000.0); + description.put("lastModifiedDate", EpochSeconds.fromInstant(authorizer.getLastModifiedDate())); description.put("signingDisabled", authorizer.isSigningDisabled()); description.put("status", authorizer.getStatus()); description.put("tokenKeyName", authorizer.getTokenKeyName()); diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotController.java index 84ac0d0eda..0bd926b704 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotController.java @@ -6,6 +6,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.iot.model.IotCertificate; import io.github.hectorvent.floci.services.iot.model.IotJob; @@ -1224,7 +1225,7 @@ public class IotController { private void putEpoch(ObjectNode node, String field, Instant instant) { if (instant != null) { - node.put(field, instant.toEpochMilli() / 1000.0); + node.put(field, EpochSeconds.fromInstant(instant)); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java index 1043a46e87..f4d30b0a1b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iot; 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.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.iot.model.IotDomainConfiguration; import io.github.hectorvent.floci.services.iot.model.IotDomainConfiguration.AuthorizerConfig; @@ -137,7 +138,7 @@ public class IotDomainConfigurationController { response.put("serviceType", configuration.getServiceType()); response.put("domainType", configuration.getDomainType()); if (configuration.getLastStatusChangeDate() != null) { - response.put("lastStatusChangeDate", configuration.getLastStatusChangeDate().toEpochMilli() / 1000.0); + response.put("lastStatusChangeDate", EpochSeconds.fromInstant(configuration.getLastStatusChangeDate())); } if (configuration.getTlsConfig() != null) { putIfPresent(response.putObject("tlsConfig"), "securityPolicy", configuration.getTlsConfig().securityPolicy()); diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java index a968392453..0aa68e51cf 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsEventStreamEncoder; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.services.kinesis.model.KinesisConsumer; import io.github.hectorvent.floci.services.kinesis.model.KinesisRecord; import io.github.hectorvent.floci.services.kinesis.model.KinesisShard; @@ -471,7 +472,7 @@ public class KinesisJsonHandler { node.put("ConsumerName", c.getConsumerName()); node.put("ConsumerARN", c.getConsumerArn()); node.put("ConsumerStatus", c.getConsumerStatus()); - node.put("ConsumerCreationTimestamp", c.getConsumerCreationTimestamp().toEpochMilli() / 1000.0); + node.put("ConsumerCreationTimestamp", EpochSeconds.fromInstant(c.getConsumerCreationTimestamp())); if (c.getStreamArn() != null) { node.put("StreamARN", c.getStreamArn()); } diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java index c11fd80917..3cbfd74c48 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsErrorResponse; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; @@ -238,7 +239,7 @@ public class KinesisAnalyticsV2JsonHandler { node.put("SnapshotStatus", snapshot.getSnapshotStatus().name()); node.put("ApplicationVersionId", snapshot.getApplicationVersionId()); if (snapshot.getSnapshotCreationTimestamp() != null) { - node.put("SnapshotCreationTimestamp", snapshot.getSnapshotCreationTimestamp().toEpochMilli() / 1000.0); + node.put("SnapshotCreationTimestamp", EpochSeconds.fromInstant(snapshot.getSnapshotCreationTimestamp())); } if (snapshot.getRuntimeEnvironment() != null) { node.put("RuntimeEnvironment", snapshot.getRuntimeEnvironment()); @@ -319,10 +320,10 @@ public class KinesisAnalyticsV2JsonHandler { detail.put("ApplicationMode", app.getApplicationMode()); } if (app.getCreateTimestamp() != null) { - detail.put("CreateTimestamp", app.getCreateTimestamp().toEpochMilli() / 1000.0); + detail.put("CreateTimestamp", EpochSeconds.fromInstant(app.getCreateTimestamp())); } if (app.getLastUpdateTimestamp() != null) { - detail.put("LastUpdateTimestamp", app.getLastUpdateTimestamp().toEpochMilli() / 1000.0); + detail.put("LastUpdateTimestamp", EpochSeconds.fromInstant(app.getLastUpdateTimestamp())); } if (app.hasCode()) { detail.set("ApplicationConfigurationDescription", applicationConfigurationNode(app)); diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java index ffe290d78f..46b3fca2e8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.Resettable; import io.github.hectorvent.floci.services.kinesis.KinesisService; import io.github.hectorvent.floci.services.kinesis.model.KinesisRecord; @@ -422,7 +423,7 @@ public class KinesisEventSourcePoller implements Resettable { kinesisNode.put("sequenceNumber", rec.getSequenceNumber()); kinesisNode.put("data", Base64.getEncoder().encodeToString(rec.getData())); kinesisNode.put("approximateArrivalTimestamp", - rec.getApproximateArrivalTimestamp().toEpochMilli() / 1000.0); + EpochSeconds.fromInstant(rec.getApproximateArrivalTimestamp())); ObjectNode record = objectMapper.createObjectNode(); record.set("kinesis", kinesisNode); record.put("eventSource", "aws:kinesis"); diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java index 9b6a44f129..080a6956df 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService.Microvm; import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService.MicrovmBuild; @@ -534,7 +535,7 @@ public class LambdaMicrovmsController { } private double epoch(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } private JsonNode parse(String body) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lightsail/LightsailService.java b/src/main/java/io/github/hectorvent/floci/services/lightsail/LightsailService.java index 5c3ae49b0b..34843f3791 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lightsail/LightsailService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lightsail/LightsailService.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsPartitions; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.resource.ExplorerResource; import io.github.hectorvent.floci.core.resource.ResourceProvider; @@ -879,7 +880,7 @@ public class LightsailService implements ResourceProvider { } private static double epochSeconds(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } private static String privateIpFor(String name) { diff --git a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java index 32c7455871..c6db325e94 100644 --- a/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java +++ b/src/main/java/io/github/hectorvent/floci/services/marketplace/MarketplaceAgreementService.java @@ -6,6 +6,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.common.Resettable; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; @@ -771,7 +772,7 @@ public class MarketplaceAgreementService implements Resettable { return Double.parseDouble(value); } catch (NumberFormatException e) { try { - return Instant.parse(value).toEpochMilli() / 1000.0d; + return EpochSeconds.fromInstant(Instant.parse(value)); } catch (Exception ignored) { throw validation("Time filters must be valid timestamps."); } @@ -792,7 +793,7 @@ public class MarketplaceAgreementService implements Resettable { } private static double epoch() { - return Instant.now().toEpochMilli() / 1000.0d; + return EpochSeconds.fromInstant(Instant.now()); } private static String compactId() { diff --git a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java index 25be75a66f..e143718be2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java @@ -5,6 +5,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; @@ -782,7 +783,7 @@ public class OrganizationsJsonHandler { private void putTimestamp(ObjectNode node, String field, Instant timestamp) { if (timestamp != null) { - node.put(field, timestamp.toEpochMilli() / 1000.0); + node.put(field, EpochSeconds.fromInstant(timestamp)); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java b/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java index 0dcce0d5d1..b5468c22e9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -235,8 +236,8 @@ public class RamController { node.put("associatedEntity", association.associatedEntity()); node.put("associationType", association.associationType()); node.put("status", "ASSOCIATED"); - node.put("creationTime", association.creationTime().toEpochMilli() / 1000.0); - node.put("lastUpdatedTime", association.lastUpdatedTime().toEpochMilli() / 1000.0); + node.put("creationTime", EpochSeconds.fromInstant(association.creationTime())); + node.put("lastUpdatedTime", EpochSeconds.fromInstant(association.lastUpdatedTime())); node.put("external", false); associations.add(node); } @@ -265,8 +266,8 @@ public class RamController { ObjectNode node = objectMapper.createObjectNode(); node.put("id", principal.id()); node.put("resourceShareArn", principal.resourceShareArn()); - node.put("creationTime", principal.creationTime().toEpochMilli() / 1000.0); - node.put("lastUpdatedTime", principal.lastUpdatedTime().toEpochMilli() / 1000.0); + node.put("creationTime", EpochSeconds.fromInstant(principal.creationTime())); + node.put("lastUpdatedTime", EpochSeconds.fromInstant(principal.lastUpdatedTime())); node.put("external", principal.external()); array.add(node); } @@ -293,8 +294,8 @@ public class RamController { node.put("name", permission.name()); node.put("resourceType", permission.resourceType()); node.put("status", "ATTACHABLE"); - node.put("creationTime", permission.creationTime().toEpochMilli() / 1000.0); - node.put("lastUpdatedTime", permission.lastUpdatedTime().toEpochMilli() / 1000.0); + node.put("creationTime", EpochSeconds.fromInstant(permission.creationTime())); + node.put("lastUpdatedTime", EpochSeconds.fromInstant(permission.lastUpdatedTime())); node.put("isResourceTypeDefault", true); node.put("permissionType", "AWS_MANAGED"); node.put("featureSet", "STANDARD"); @@ -387,7 +388,7 @@ public class RamController { node.put("resourceShareName", invitation.resourceShareName()); node.put("senderAccountId", invitation.senderAccountId()); node.put("receiverAccountId", invitation.receiverAccountId()); - node.put("invitationTimestamp", invitation.invitationTimestamp().toEpochMilli() / 1000.0); + node.put("invitationTimestamp", EpochSeconds.fromInstant(invitation.invitationTimestamp())); node.put("status", invitation.status()); return node; } @@ -427,8 +428,8 @@ public class RamController { // Shares created through the API are STANDARD; CREATED_FROM_POLICY covers shares RAM // derives from a resource policy, which floci has no path to produce. node.put("featureSet", "STANDARD"); - node.put("creationTime", share.getCreationTime().toEpochMilli() / 1000.0); - node.put("lastUpdatedTime", share.getLastUpdatedTime().toEpochMilli() / 1000.0); + node.put("creationTime", EpochSeconds.fromInstant(share.getCreationTime())); + node.put("lastUpdatedTime", EpochSeconds.fromInstant(share.getLastUpdatedTime())); ArrayNode tags = objectMapper.createArrayNode(); share.getTags().forEach((key, value) -> { ObjectNode tag = objectMapper.createObjectNode(); @@ -444,7 +445,7 @@ public class RamController { private ArrayNode associationArray(ResourceShare share, List resourceArns, List principals, String status) { ArrayNode array = objectMapper.createArrayNode(); - double now = share.getLastUpdatedTime().toEpochMilli() / 1000.0; + double now = EpochSeconds.fromInstant(share.getLastUpdatedTime()); for (String resourceArn : resourceArns) { array.add(associationNode(share, resourceArn, "RESOURCE", status, now)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java index b079bd76c1..6864b615db 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.common.Resettable; import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql; @@ -1344,7 +1345,7 @@ public class RedshiftDataService implements Resettable { } static double epochSeconds(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } private String clusterIdentifierOrNull(JsonNode request) { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java index 69765a0e00..7f644dfbb5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java @@ -5,6 +5,7 @@ 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.AwsJsonTimestamp; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.services.redshiftserverless.model.CustomDomainAssociation; @@ -387,7 +388,7 @@ public class RedshiftServerlessAdminJsonHandler { } private static double epoch(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } // ── Tracks, managed workgroups and reservations ──────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/s3vectors/S3VectorsController.java b/src/main/java/io/github/hectorvent/floci/services/s3vectors/S3VectorsController.java index e9847a80bc..efa02aff22 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3vectors/S3VectorsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3vectors/S3VectorsController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.s3vectors; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket; import io.github.hectorvent.floci.services.s3vectors.model.VectorData; @@ -291,7 +292,7 @@ public class S3VectorsController { String region = regionResolver.resolveRegion(headers); VectorBucket bucket = service.getVectorBucket(request.vectorBucketName(), region); VectorBucketRepresentation rep = new VectorBucketRepresentation(bucket.getVectorBucketName(), - bucket.getVectorBucketArn(), bucket.getCreationTime().toEpochMilli() / 1000.0, bucket.getEncryptionConfiguration()); + bucket.getVectorBucketArn(), EpochSeconds.fromInstant(bucket.getCreationTime()), bucket.getEncryptionConfiguration()); return Response.ok(new GetVectorBucketResponse(rep)).build(); } @@ -302,7 +303,7 @@ public class S3VectorsController { List buckets = service.listVectorBuckets(region); List reps = buckets.stream() .map(b -> new VectorBucketSummary(b.getVectorBucketName(), b.getVectorBucketArn(), - b.getCreationTime().toEpochMilli() / 1000.0)) + EpochSeconds.fromInstant(b.getCreationTime()))) .toList(); return Response.ok(new ListVectorBucketsResponse(reps, null)).build(); } @@ -340,7 +341,7 @@ public class S3VectorsController { String region = regionResolver.resolveRegion(headers); VectorIndex index = service.getIndex(request.vectorBucketName(), request.indexName(), region); IndexRepresentation rep = new IndexRepresentation( - index.getCreationTime().toEpochMilli() / 1000.0, + EpochSeconds.fromInstant(index.getCreationTime()), index.getVectorBucketName(), index.getIndexName(), index.getIndexArn(), @@ -361,7 +362,7 @@ public class S3VectorsController { List indexes = service.listIndexes(request.vectorBucketName(), region); List reps = indexes.stream() .map(i -> new IndexSummary( - i.getCreationTime().toEpochMilli() / 1000.0, + EpochSeconds.fromInstant(i.getCreationTime()), i.getVectorBucketName(), i.getIndexName(), i.getIndexArn() diff --git a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerJsonHandler.java index 5d3bab5253..095edba39e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerJsonHandler.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.IamEnforcementFilter; import io.github.hectorvent.floci.services.secretsmanager.model.Secret; import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion; @@ -185,7 +186,7 @@ public class SecretsManagerJsonHandler { node.put("SecretBinary", value.secretBinary()); } if (value.createdDate() != null) { - node.put("CreatedDate", value.createdDate().toEpochMilli() / 1000.0); + node.put("CreatedDate", EpochSeconds.fromInstant(value.createdDate())); } putVersionStages(node, value.versionStages()); secretValues.add(node); @@ -259,7 +260,7 @@ public class SecretsManagerJsonHandler { response.put("SecretBinary", version.getSecretBinary()); } if (version.getCreatedDate() != null) { - response.put("CreatedDate", version.getCreatedDate().toEpochMilli() / 1000.0); + response.put("CreatedDate", EpochSeconds.fromInstant(version.getCreatedDate())); } putVersionStages(response, version.getVersionStages()); return Response.ok(response).build(); @@ -361,21 +362,21 @@ public class SecretsManagerJsonHandler { response.set("RotationRules", rotationRulesNode(secret.getRotationRules())); } if (secret.getLastRotatedDate() != null) { - response.put("LastRotatedDate", secret.getLastRotatedDate().toEpochMilli() / 1000.0); + response.put("LastRotatedDate", EpochSeconds.fromInstant(secret.getLastRotatedDate())); } Instant nextRotationDate = nextRotationDate(secret); if (nextRotationDate != null) { - response.put("NextRotationDate", nextRotationDate.toEpochMilli() / 1000.0); + response.put("NextRotationDate", EpochSeconds.fromInstant(nextRotationDate)); } if (secret.getCreatedDate() != null) { - response.put("CreatedDate", secret.getCreatedDate().toEpochMilli() / 1000.0); + response.put("CreatedDate", EpochSeconds.fromInstant(secret.getCreatedDate())); } if (secret.getLastChangedDate() != null) { - response.put("LastChangedDate", secret.getLastChangedDate().toEpochMilli() / 1000.0); + response.put("LastChangedDate", EpochSeconds.fromInstant(secret.getLastChangedDate())); } if (secret.getDeletedDate() != null) { - response.put("DeletedDate", secret.getDeletedDate().toEpochMilli() / 1000.0); + response.put("DeletedDate", EpochSeconds.fromInstant(secret.getDeletedDate())); } if (secret.getOwningService() != null) { response.put("OwningService", secret.getOwningService()); @@ -573,23 +574,23 @@ public class SecretsManagerJsonHandler { node.set("RotationRules", rotationRulesNode(secret.getRotationRules())); } if (secret.getLastRotatedDate() != null) { - node.put("LastRotatedDate", secret.getLastRotatedDate().toEpochMilli() / 1000.0); + node.put("LastRotatedDate", EpochSeconds.fromInstant(secret.getLastRotatedDate())); } Instant nextRotation = nextRotationDate(secret); if (nextRotation != null) { - node.put("NextRotationDate", nextRotation.toEpochMilli() / 1000.0); + node.put("NextRotationDate", EpochSeconds.fromInstant(nextRotation)); } if (secret.getCreatedDate() != null) { - node.put("CreatedDate", secret.getCreatedDate().toEpochMilli() / 1000.0); + node.put("CreatedDate", EpochSeconds.fromInstant(secret.getCreatedDate())); } if (secret.getLastChangedDate() != null) { - node.put("LastChangedDate", secret.getLastChangedDate().toEpochMilli() / 1000.0); + node.put("LastChangedDate", EpochSeconds.fromInstant(secret.getLastChangedDate())); } if (secret.getLastAccessedDate() != null) { - node.put("LastAccessedDate", secret.getLastAccessedDate().toEpochMilli() / 1000.0); + node.put("LastAccessedDate", EpochSeconds.fromInstant(secret.getLastAccessedDate())); } if (secret.getDeletedDate() != null) { - node.put("DeletedDate", secret.getDeletedDate().toEpochMilli() / 1000.0); + node.put("DeletedDate", EpochSeconds.fromInstant(secret.getDeletedDate())); } if (secret.getOwningService() != null) { node.put("OwningService", secret.getOwningService()); @@ -623,7 +624,7 @@ public class SecretsManagerJsonHandler { response.put("ARN", secret.getArn()); response.put("Name", secret.getName()); if (secret.getDeletedDate() != null) { - response.put("DeletionDate", secret.getDeletedDate().toEpochMilli() / 1000.0); + response.put("DeletionDate", EpochSeconds.fromInstant(secret.getDeletedDate())); } return Response.ok(response).build(); } @@ -804,10 +805,10 @@ public class SecretsManagerJsonHandler { } SecretVersion sv = secret.getVersions() != null ? secret.getVersions().get(versionId) : null; if (sv != null && sv.getCreatedDate() != null) { - versionNode.put("CreatedDate", sv.getCreatedDate().toEpochMilli() / 1000.0); + versionNode.put("CreatedDate", EpochSeconds.fromInstant(sv.getCreatedDate())); } if (sv != null && sv.getLastAccessedDate() != null) { - versionNode.put("LastAccessedDate", sv.getLastAccessedDate().toEpochMilli() / 1000.0); + versionNode.put("LastAccessedDate", EpochSeconds.fromInstant(sv.getLastAccessedDate())); } versions.add(versionNode); } @@ -935,7 +936,7 @@ public class SecretsManagerJsonHandler { node.put("StatusMessage", status.statusMessage()); } if (status.lastAccessedDate() != null) { - node.put("LastAccessedDate", status.lastAccessedDate().toEpochMilli() / 1000.0); + node.put("LastAccessedDate", EpochSeconds.fromInstant(status.lastAccessedDate())); } statuses.add(node); } diff --git a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java index 370f9095e1..a97dee1fe0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java +++ b/src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.iam.PassedRoleLookup; @@ -71,7 +72,7 @@ public class ServiceCatalogService implements PassedRoleLookup { } private double nowEpochSeconds() { - return Instant.now(clock).toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(Instant.now(clock)); } public ObjectNode createPortfolio(JsonNode request, String region, String accountId) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java index dab295d2bd..80026fd8b4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.services.ses.model.Tag; @@ -358,7 +359,7 @@ final class SesV2Json { * ISO string, as the v1 Query path writes, breaks the SDK's unixTimestamp unmarshaller. */ static double epochSeconds(Instant instant) { - return instant.toEpochMilli() / 1000.0; + return EpochSeconds.fromInstant(instant); } static void putTimestamp(ObjectNode node, String field, Instant value) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java index cd059b9f6c..a877de9b8c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.IamEnforcementFilter; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; @@ -314,7 +315,7 @@ public class SsmJsonHandler { if (h.getKeyId() != null) { node.put("KeyId", h.getKeyId()); } - node.put("LastModifiedDate", h.getLastModifiedDate().toEpochMilli() / 1000.0); + node.put("LastModifiedDate", EpochSeconds.fromInstant(h.getLastModifiedDate())); if (h.getDescription() != null) { node.put("Description", h.getDescription()); } @@ -402,7 +403,7 @@ public class SsmJsonHandler { node.put("ARN", p.getArn()); node.put("Type", p.getType()); node.put("Version", p.getVersion()); - node.put("LastModifiedDate", p.getLastModifiedDate().toEpochMilli() / 1000.0); + node.put("LastModifiedDate", EpochSeconds.fromInstant(p.getLastModifiedDate())); if (p.getDescription() != null) { node.put("Description", p.getDescription()); } @@ -773,7 +774,7 @@ public class SsmJsonHandler { ObjectNode docNode = objectMapper.createObjectNode(); docNode.put("Name", d.getName()); if (d.getCreatedDate() != null) { - docNode.put("CreatedDate", d.getCreatedDate().toEpochMilli() / 1000.0); + docNode.put("CreatedDate", EpochSeconds.fromInstant(d.getCreatedDate())); } docNode.put("DocumentType", d.getDocumentType()); docNode.put("DocumentVersion", String.valueOf(d.getDocumentVersion())); @@ -998,7 +999,7 @@ public class SsmJsonHandler { node.put("ScheduleExpression", assoc.getScheduleExpression()); } if (assoc.getLastExecutionDate() != null) { - node.put("LastExecutionDate", assoc.getLastExecutionDate().toEpochMilli() / 1000.0); + node.put("LastExecutionDate", EpochSeconds.fromInstant(assoc.getLastExecutionDate())); } return node; } @@ -1049,7 +1050,7 @@ public class SsmJsonHandler { statusNode.put("Message", assoc.getStatus().getMessage()); } if (assoc.getStatus().getDate() != null) { - statusNode.put("Date", assoc.getStatus().getDate().toEpochMilli() / 1000.0); + statusNode.put("Date", EpochSeconds.fromInstant(assoc.getStatus().getDate())); } if (assoc.getStatus().getAdditionalInfo() != null) { statusNode.put("AdditionalInfo", assoc.getStatus().getAdditionalInfo()); @@ -1063,10 +1064,10 @@ public class SsmJsonHandler { desc.set("Overview", overviewNode); } if (assoc.getCreatedDate() != null) { - desc.put("Date", assoc.getCreatedDate().toEpochMilli() / 1000.0); + desc.put("Date", EpochSeconds.fromInstant(assoc.getCreatedDate())); } if (assoc.getLastExecutionDate() != null) { - desc.put("LastExecutionDate", assoc.getLastExecutionDate().toEpochMilli() / 1000.0); + desc.put("LastExecutionDate", EpochSeconds.fromInstant(assoc.getLastExecutionDate())); } if (assoc.getComplianceSeverity() != null) { desc.put("ComplianceSeverity", assoc.getComplianceSeverity()); @@ -1319,7 +1320,7 @@ public class SsmJsonHandler { } node.put("Type", p.getType()); node.put("Version", p.getVersion()); - node.put("LastModifiedDate", p.getLastModifiedDate().toEpochMilli() / 1000.0); + node.put("LastModifiedDate", EpochSeconds.fromInstant(p.getLastModifiedDate())); node.put("ARN", p.getArn()); if (p.getDataType() != null) { node.put("DataType", p.getDataType()); @@ -1404,7 +1405,7 @@ public class SsmJsonHandler { ObjectNode node = objectMapper.createObjectNode(); node.put("SettingId", s.getSettingId()); node.put("SettingValue", s.getSettingValue()); - node.put("LastModifiedDate", s.getLastModifiedDate().toEpochMilli() / 1000.0); + node.put("LastModifiedDate", EpochSeconds.fromInstant(s.getLastModifiedDate())); node.put("LastModifiedUser", s.getLastModifiedUser()); node.put("ARN", s.getArn()); node.put("Status", s.getStatus()); @@ -1489,8 +1490,8 @@ public class SsmJsonHandler { node.put("DocumentName", c.getDocumentName()); if (c.getDocumentVersion() != null) node.put("DocumentVersion", c.getDocumentVersion()); if (c.getComment() != null) node.put("Comment", c.getComment()); - if (c.getRequestedDateTime() != null) node.put("RequestedDateTime", c.getRequestedDateTime().toEpochMilli() / 1000.0); - if (c.getExpiresAfter() != null) node.put("ExpiresAfter", c.getExpiresAfter().toEpochMilli() / 1000.0); + if (c.getRequestedDateTime() != null) node.put("RequestedDateTime", EpochSeconds.fromInstant(c.getRequestedDateTime())); + if (c.getExpiresAfter() != null) node.put("ExpiresAfter", EpochSeconds.fromInstant(c.getExpiresAfter())); // c is the same live, shared Command object SsmCommandService's writers (updateCommandStatus, // cancelCommand) synchronize on before touching Status/StatusDetails together. Reading the // pair here without the same lock would let this read straddle a concurrent writer's update - @@ -1528,7 +1529,7 @@ public class SsmJsonHandler { if (inv.getComment() != null) node.put("Comment", inv.getComment()); node.put("DocumentName", inv.getDocumentName()); if (inv.getDocumentVersion() != null) node.put("DocumentVersion", inv.getDocumentVersion()); - if (inv.getRequestedDateTime() != null) node.put("RequestedDateTime", inv.getRequestedDateTime().toEpochMilli() / 1000.0); + if (inv.getRequestedDateTime() != null) node.put("RequestedDateTime", EpochSeconds.fromInstant(inv.getRequestedDateTime())); // Same reasoning as commandToNode: inv is the same live, shared CommandInvocation object its // writers (cancelCommand, direct/agent completion, timeout sweeps) now synchronize on. synchronized (inv) { @@ -1563,8 +1564,8 @@ public class SsmJsonHandler { if (info.getIpAddress() != null) node.put("IPAddress", info.getIpAddress()); if (info.getComputerName() != null) node.put("ComputerName", info.getComputerName()); node.put("ResourceType", info.getResourceType()); - if (info.getLastPingDateTime() != null) node.put("LastPingDateTime", info.getLastPingDateTime().toEpochMilli() / 1000.0); - if (info.getRegistrationDate() != null) node.put("RegistrationDate", info.getRegistrationDate().toEpochMilli() / 1000.0); + if (info.getLastPingDateTime() != null) node.put("LastPingDateTime", EpochSeconds.fromInstant(info.getLastPingDateTime())); + if (info.getRegistrationDate() != null) node.put("RegistrationDate", EpochSeconds.fromInstant(info.getRegistrationDate())); return node; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java b/src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java index 16c545af35..8702530564 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java @@ -6,6 +6,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.Resettable; @@ -307,7 +308,7 @@ public class SsoAdminService implements Resettable { } public double regionAddedDateEpochSeconds(RegionMetadata region) { - return Instant.parse(region.addedDate()).toEpochMilli() / 1000.0d; + return EpochSeconds.fromInstant(Instant.parse(region.addedDate())); } public synchronized TrustedTokenIssuer createTrustedTokenIssuer(JsonNode request, String callerAccountId) { diff --git a/src/main/java/io/github/hectorvent/floci/services/transfer/TransferHandler.java b/src/main/java/io/github/hectorvent/floci/services/transfer/TransferHandler.java index 4d1ee849da..86e10d0daa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/transfer/TransferHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/transfer/TransferHandler.java @@ -6,6 +6,7 @@ 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.AwsException; +import io.github.hectorvent.floci.core.common.EpochSeconds; import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.services.transfer.model.HomeDirectoryMapping; @@ -359,7 +360,7 @@ public class TransferHandler { kNode.put("SshPublicKeyId", k.getSshPublicKeyId()); kNode.put("SshPublicKeyBody", k.getSshPublicKeyBody()); if (k.getDateImported() != null) { - kNode.put("DateImported", k.getDateImported().toEpochMilli() / 1000.0); + kNode.put("DateImported", EpochSeconds.fromInstant(k.getDateImported())); } keys.add(kNode); } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/EpochSecondsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/EpochSecondsTest.java new file mode 100644 index 0000000000..51cb8ed837 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/EpochSecondsTest.java @@ -0,0 +1,28 @@ +package io.github.hectorvent.floci.core.common; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class EpochSecondsTest { + + @Test + void keepsMillisecondFractionAndDropsSubmillisecondPrecision() { + assertEquals(1_790_312_384.592, EpochSeconds.fromInstant( + Instant.ofEpochSecond(1_790_312_384L, 592_999_999L))); + } + + @Test + void floorsNegativeSubmillisecondInstantToPreviousMillisecond() { + assertEquals(-0.001, EpochSeconds.fromInstant(Instant.ofEpochSecond(-1, 999_999_999))); + } + + @Test + void retainsNullAndOverflowFailures() { + assertThrows(NullPointerException.class, () -> EpochSeconds.fromInstant(null)); + assertThrows(ArithmeticException.class, () -> EpochSeconds.fromInstant(Instant.MAX)); + } +}