mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ApiGateway): implement EndpointConfiguration for REST APIs (#1116)
This commit is contained in:
1 parent
938c693f8d
commit
3989064a1c
8 files changed
+314
No files matched your search
+16
@@ -17,6 +17,8 @@ import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.ApiKey;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.CustomDomain;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.EndpointConfiguration;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.EndpointType;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.MethodConfig;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.MethodResponse;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.RequestValidator;
|
||||
@@ -1517,6 +1519,20 @@ public class ApiGatewayController {
|
||||
api.getTags().forEach(tagsNode::put);
|
||||
node.set("tags", tagsNode);
|
||||
}
|
||||
|
||||
EndpointConfiguration epConfig = api.getEndpointConfiguration();
|
||||
if (epConfig == null) {
|
||||
epConfig = new EndpointConfiguration();
|
||||
epConfig.setTypes(List.of(EndpointType.REGIONAL));
|
||||
}
|
||||
|
||||
ObjectNode epNode = objectMapper.createObjectNode();
|
||||
ArrayNode types = epNode.putArray("types");
|
||||
epConfig.getTypes().forEach(t -> types.add(t.name()));
|
||||
ArrayNode vpcIds = epNode.putArray("vpcEndpointIds");
|
||||
epConfig.getVpcEndpointIds().forEach(vpcIds::add);
|
||||
node.set("endpointConfiguration", epNode);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import io.github.hectorvent.floci.services.apigateway.model.EndpointConfiguration;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.EndpointType;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -56,6 +59,11 @@ public class ApiGatewayService {
|
||||
private final StorageBackend<String, CustomDomain> domainStore;
|
||||
private final StorageBackend<String, BasePathMapping> basePathMappingStore;
|
||||
|
||||
// Constants
|
||||
private static final String EPC_KEY = "endpointConfiguration";
|
||||
private static final String EPC_TYPES_KEY = "types";
|
||||
private static final String EPC_VPC_IDS_KEY = "vpcEndpointIds";
|
||||
|
||||
@Inject
|
||||
public ApiGatewayService(StorageFactory storageFactory, EmulatorConfig config) {
|
||||
this.apiStore = storageFactory.create("apigateway", "apigateway-apis.json",
|
||||
@@ -174,6 +182,60 @@ public class ApiGatewayService {
|
||||
api.setCreatedDate(System.currentTimeMillis() / 1000L);
|
||||
api.setTags(tags);
|
||||
|
||||
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
|
||||
if (request.get(EPC_KEY) instanceof Map<?, ?> epMap) {
|
||||
epMap.forEach((k, v) -> {
|
||||
if (k instanceof String ks && v instanceof List<?> list) {
|
||||
if (EPC_TYPES_KEY.equals(ks)) {
|
||||
List<EndpointType> types = list.stream()
|
||||
.filter(String.class::isInstance)
|
||||
.map(String.class::cast)
|
||||
.map(String::toUpperCase)
|
||||
.map(typeStr -> {
|
||||
try {
|
||||
return EndpointType.valueOf(typeStr);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Endpoint configuration type must be REGIONAL, EDGE, or PRIVATE.", 400);
|
||||
}
|
||||
})
|
||||
.toList();
|
||||
endpointConfiguration.setTypes(types);
|
||||
} else if (EPC_VPC_IDS_KEY.equals(ks)) {
|
||||
List<String> vpcIds = list.stream()
|
||||
.filter(String.class::isInstance)
|
||||
.map(String.class::cast)
|
||||
.toList();
|
||||
endpointConfiguration.setVpcEndpointIds(vpcIds);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set default type if omitted
|
||||
if (endpointConfiguration.getTypes().isEmpty()) {
|
||||
endpointConfiguration.setTypes(List.of(EndpointType.REGIONAL));
|
||||
}
|
||||
|
||||
// Enforce exactly one type
|
||||
if (endpointConfiguration.getTypes().size() != 1) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Endpoint configuration types must contain exactly one value.", 400);
|
||||
}
|
||||
|
||||
EndpointType type = endpointConfiguration.getTypes().getFirst();
|
||||
if (EndpointType.PRIVATE.equals(type)) {
|
||||
if (endpointConfiguration.getVpcEndpointIds().isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"At least one vpcEndpointId is required for PRIVATE APIs.", 400);
|
||||
}
|
||||
} else {
|
||||
// Reject/ignore vpcEndpointIds for REGIONAL and EDGE
|
||||
endpointConfiguration.setVpcEndpointIds(new ArrayList<>());
|
||||
}
|
||||
|
||||
api.setEndpointConfiguration(endpointConfiguration);
|
||||
|
||||
apiStore.put(apiKey(region, api.getId()), api);
|
||||
|
||||
// Create root resource "/"
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.services.apigateway.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class EndpointConfiguration {
|
||||
|
||||
private List<EndpointType> types = new ArrayList<>();
|
||||
private List<String> vpcEndpointIds = new ArrayList<>();
|
||||
|
||||
public List<EndpointType> getTypes() {
|
||||
return types;
|
||||
}
|
||||
|
||||
public void setTypes(List<EndpointType> types) {
|
||||
this.types = types != null ? types : new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<String> getVpcEndpointIds() {
|
||||
return vpcEndpointIds;
|
||||
}
|
||||
|
||||
public void setVpcEndpointIds(List<String> vpcEndpointIds) {
|
||||
this.vpcEndpointIds = vpcEndpointIds != null ? vpcEndpointIds : new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.apigateway.model;
|
||||
|
||||
public enum EndpointType {
|
||||
REGIONAL,
|
||||
EDGE,
|
||||
PRIVATE
|
||||
}
|
||||
@@ -13,6 +13,8 @@ public class RestApi {
|
||||
private String description;
|
||||
private long createdDate;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private EndpointConfiguration endpointConfiguration;
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -53,4 +55,12 @@ public class RestApi {
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags != null ? tags : new HashMap<>();
|
||||
}
|
||||
|
||||
public EndpointConfiguration getEndpointConfiguration() {
|
||||
return endpointConfiguration;
|
||||
}
|
||||
|
||||
public void setEndpointConfiguration(EndpointConfiguration endpointConfiguration) {
|
||||
this.endpointConfiguration = endpointConfiguration;
|
||||
}
|
||||
}
|
||||
+8
@@ -1475,6 +1475,14 @@ public class CloudFormationResourceProvisioner {
|
||||
req.put("name", name);
|
||||
req.put("description", resolveOptional(props, "Description", engine));
|
||||
|
||||
if (props.has("EndpointConfiguration")) {
|
||||
JsonNode epNode = props.get("EndpointConfiguration");
|
||||
Map<String, Object> epReq = new HashMap<>();
|
||||
epReq.put("types", resolveStringListOrEmpty(epNode, "Types", engine));
|
||||
epReq.put("vpcEndpointIds", resolveStringListOrEmpty(epNode, "VpcEndpointIds", engine));
|
||||
req.put("endpointConfiguration", epReq);
|
||||
}
|
||||
|
||||
var api = apiGatewayService.createRestApi(region, req);
|
||||
r.setPhysicalId(api.getId());
|
||||
r.getAttributes().put("RootResourceId", apiGatewayService.getResources(region, api.getId()).get(0).getId());
|
||||
|
||||
Reference in new issue
Block a user