mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(stepfunctions): add state machine version APIs (Publish/List/DeleteStateMachineVersion) (#1562)
* feat(stepfunctions): add state machine version APIs Add PublishStateMachineVersion, ListStateMachineVersions, and DeleteStateMachineVersion, plus publish-on-create support (publish=true). The Terraform AWS provider calls ListStateMachineVersions after creating an aws_sfn_state_machine; Floci previously returned UnsupportedOperation, failing both apply and destroy. Versions are tracked per state machine (<arn>:<n>). Integration-tested; verified end-to-end with terraform apply + destroy of an aws_sfn_state_machine against Floci. * fix(stepfunctions): return a defensive copy from listStateMachineVersions Greptile review on #1562: listStateMachineVersions returned the StateMachine's internal list directly, exposing it to external mutation and concurrent-modification hazards. Return List.copyOf(...) instead. * fix(stepfunctions): ListStateMachineVersions newest-first and InvalidArn for missing SM AWS lists state machine versions newest first (descending by creationDate, tie-broken on the version number since creationDate is only second-resolution); returning insertion order made the Terraform provider latch onto the wrong version ARN once more than one version existed. Listing versions for a non-existent state machine now returns InvalidArn rather than StateMachineDoesNotExist, which is not a declared error for this operation. Adds a two-version ordering test and a missing-state-machine InvalidArn test.
This commit is contained in:
1 parent
a895802b8b
commit
3fe7a60a58
5 files changed
+220
No files matched your search
+33
@@ -37,6 +37,9 @@ public class StepFunctionsJsonHandler {
|
||||
case "DescribeStateMachine" -> handleDescribeStateMachine(request);
|
||||
case "ListStateMachines" -> handleListStateMachines(request, region);
|
||||
case "DeleteStateMachine" -> handleDeleteStateMachine(request);
|
||||
case "PublishStateMachineVersion" -> handlePublishStateMachineVersion(request);
|
||||
case "ListStateMachineVersions" -> handleListStateMachineVersions(request);
|
||||
case "DeleteStateMachineVersion" -> handleDeleteStateMachineVersion(request);
|
||||
case "ValidateStateMachineDefinition" -> handleValidateStateMachineDefinition(request);
|
||||
case "StartExecution" -> handleStartExecution(request, region);
|
||||
case "StartSyncExecution" -> handleStartSyncExecution(request, region);
|
||||
@@ -73,9 +76,39 @@ public class StepFunctionsJsonHandler {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("stateMachineArn", sm.getStateMachineArn());
|
||||
response.put("creationDate", sm.getCreationDate());
|
||||
// Publishing a version on create is opt-in (publish=true).
|
||||
if (request.path("publish").asBoolean(false)) {
|
||||
var version = service.publishStateMachineVersion(sm.getStateMachineArn());
|
||||
response.put("stateMachineVersionArn", version.getStateMachineVersionArn());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handlePublishStateMachineVersion(JsonNode request) {
|
||||
var version = service.publishStateMachineVersion(request.path("stateMachineArn").asText());
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("stateMachineVersionArn", version.getStateMachineVersionArn());
|
||||
response.put("creationDate", version.getCreationDate());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleListStateMachineVersions(JsonNode request) {
|
||||
var versions = service.listStateMachineVersions(request.path("stateMachineArn").asText());
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("stateMachineVersions");
|
||||
for (var v : versions) {
|
||||
ObjectNode item = array.addObject();
|
||||
item.put("stateMachineVersionArn", v.getStateMachineVersionArn());
|
||||
item.put("creationDate", v.getCreationDate());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteStateMachineVersion(JsonNode request) {
|
||||
service.deleteStateMachineVersion(request.path("stateMachineVersionArn").asText());
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleDescribeStateMachine(JsonNode request) {
|
||||
StateMachine sm = service.describeStateMachine(request.path("stateMachineArn").asText());
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
|
||||
+46
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.stepfunctions.model.ActivityTask;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachineVersion;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -107,6 +108,51 @@ public class StepFunctionsService implements Resettable {
|
||||
return stateMachineStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
// ── State machine versions ──────────────────────────────────────────────
|
||||
|
||||
public StateMachineVersion publishStateMachineVersion(String stateMachineArn) {
|
||||
StateMachine sm = describeStateMachine(stateMachineArn);
|
||||
int next = sm.getVersionCounter() + 1;
|
||||
sm.setVersionCounter(next);
|
||||
StateMachineVersion version = new StateMachineVersion(
|
||||
stateMachineArn + ":" + next, next, System.currentTimeMillis() / 1000.0);
|
||||
sm.getVersions().add(version);
|
||||
stateMachineStore.put(stateMachineArn, sm);
|
||||
return version;
|
||||
}
|
||||
|
||||
public List<StateMachineVersion> listStateMachineVersions(String stateMachineArn) {
|
||||
// AWS returns InvalidArn for a non-existent state machine here — StateMachineDoesNotExist is
|
||||
// not one of ListStateMachineVersions' declared errors (Publish, which does declare it, keeps
|
||||
// using describeStateMachine).
|
||||
StateMachine sm = stateMachineStore.get(stateMachineArn)
|
||||
.orElseThrow(() -> new AwsException("InvalidArn",
|
||||
"Invalid Arn: '" + stateMachineArn + "'", 400));
|
||||
// AWS lists versions newest first (descending by creationDate). creationDate is only
|
||||
// second-resolution, so tie-break on the version number (also descending) to keep the order
|
||||
// correct when several versions are published within the same second — otherwise the
|
||||
// Terraform provider can latch onto the wrong version ARN.
|
||||
List<StateMachineVersion> versions = new ArrayList<>(sm.getVersions());
|
||||
versions.sort(Comparator
|
||||
.comparingDouble(StateMachineVersion::getCreationDate)
|
||||
.thenComparingInt(StateMachineVersion::getVersion)
|
||||
.reversed());
|
||||
// Defensive copy so callers can't mutate (or trip over concurrent mutation of) the stored list.
|
||||
return List.copyOf(versions);
|
||||
}
|
||||
|
||||
public void deleteStateMachineVersion(String stateMachineVersionArn) {
|
||||
int lastColon = stateMachineVersionArn.lastIndexOf(':');
|
||||
if (lastColon < 0) {
|
||||
return;
|
||||
}
|
||||
String baseArn = stateMachineVersionArn.substring(0, lastColon);
|
||||
stateMachineStore.get(baseArn).ifPresent(sm -> {
|
||||
sm.getVersions().removeIf(v -> stateMachineVersionArn.equals(v.getStateMachineVersionArn()));
|
||||
stateMachineStore.put(baseArn, sm);
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteStateMachine(String arn) {
|
||||
stateMachineStore.delete(arn);
|
||||
}
|
||||
|
||||
+10
@@ -3,7 +3,9 @@ package io.github.hectorvent.floci.services.stepfunctions.model;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@@ -17,6 +19,8 @@ public class StateMachine {
|
||||
private String status = "ACTIVE";
|
||||
private double creationDate;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private int versionCounter = 0;
|
||||
private List<StateMachineVersion> versions = new ArrayList<>();
|
||||
|
||||
public StateMachine() {
|
||||
this.creationDate = System.currentTimeMillis() / 1000.0;
|
||||
@@ -45,4 +49,10 @@ public class StateMachine {
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
|
||||
public int getVersionCounter() { return versionCounter; }
|
||||
public void setVersionCounter(int versionCounter) { this.versionCounter = versionCounter; }
|
||||
|
||||
public List<StateMachineVersion> getVersions() { return versions; }
|
||||
public void setVersions(List<StateMachineVersion> versions) { this.versions = versions; }
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/** A published version of a state machine ({@code <stateMachineArn>:<version>}). */
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class StateMachineVersion {
|
||||
private String stateMachineVersionArn;
|
||||
private int version;
|
||||
private double creationDate;
|
||||
|
||||
public StateMachineVersion() {
|
||||
}
|
||||
|
||||
public StateMachineVersion(String stateMachineVersionArn, int version, double creationDate) {
|
||||
this.stateMachineVersionArn = stateMachineVersionArn;
|
||||
this.version = version;
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
|
||||
public String getStateMachineVersionArn() { return stateMachineVersionArn; }
|
||||
public void setStateMachineVersionArn(String stateMachineVersionArn) { this.stateMachineVersionArn = stateMachineVersionArn; }
|
||||
|
||||
public int getVersion() { return version; }
|
||||
public void setVersion(int version) { this.version = version; }
|
||||
|
||||
public double getCreationDate() { return creationDate; }
|
||||
public void setCreationDate(double creationDate) { this.creationDate = creationDate; }
|
||||
}
|
||||
Reference in new issue
Block a user