mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cloudcontrol): list resources from local services (#1602)
* feat(cloudcontrol): list resources from local services * fix(cloudcontrol): align list resources response
This commit is contained in:
1 parent
a25aa0e59f
commit
4a198fd29b
10 files changed
+320
-4
No files matched your search
@@ -485,6 +485,7 @@ public interface EmulatorConfig {
|
||||
BcmDataExportsServiceConfig bcmDataExports();
|
||||
ConfigServiceConfig configservice();
|
||||
CloudTrailServiceConfig cloudtrail();
|
||||
CloudControlServiceConfig cloudcontrol();
|
||||
CloudFrontServiceConfig cloudfront();
|
||||
AppSyncServiceConfig appsync();
|
||||
BatchServiceConfig batch();
|
||||
@@ -525,6 +526,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface CloudControlServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface S3VectorsServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsT
|
||||
import io.github.hectorvent.floci.services.bcmdataexports.BcmDataExportsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.ce.CostExplorerJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cloudcontrol.CloudControlJsonHandler;
|
||||
import io.github.hectorvent.floci.services.configservice.ConfigServiceJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cur.CurJsonHandler;
|
||||
import io.github.hectorvent.floci.services.pricing.PricingJsonHandler;
|
||||
@@ -91,6 +92,7 @@ public class AwsJson11Controller {
|
||||
private final BcmDataExportsJsonHandler bcmDataExportsJsonHandler;
|
||||
private final ConfigServiceJsonHandler configServiceJsonHandler;
|
||||
private final CloudTrailJsonHandler cloudTrailJsonHandler;
|
||||
private final CloudControlJsonHandler cloudControlJsonHandler;
|
||||
|
||||
@Inject
|
||||
public AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
|
||||
@@ -122,7 +124,8 @@ public class AwsJson11Controller {
|
||||
CurJsonHandler curJsonHandler,
|
||||
BcmDataExportsJsonHandler bcmDataExportsJsonHandler,
|
||||
ConfigServiceJsonHandler configServiceJsonHandler,
|
||||
CloudTrailJsonHandler cloudTrailJsonHandler) {
|
||||
CloudTrailJsonHandler cloudTrailJsonHandler,
|
||||
CloudControlJsonHandler cloudControlJsonHandler) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.catalog = catalog;
|
||||
this.regionResolver = regionResolver;
|
||||
@@ -158,6 +161,7 @@ public class AwsJson11Controller {
|
||||
this.bcmDataExportsJsonHandler = bcmDataExportsJsonHandler;
|
||||
this.configServiceJsonHandler = configServiceJsonHandler;
|
||||
this.cloudTrailJsonHandler = cloudTrailJsonHandler;
|
||||
this.cloudControlJsonHandler = cloudControlJsonHandler;
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -219,6 +223,7 @@ public class AwsJson11Controller {
|
||||
case "bcm-data-exports" -> bcmDataExportsJsonHandler.handle(action, request, region);
|
||||
case "config" -> configServiceJsonHandler.handle(action, request, region);
|
||||
case "cloudtrail" -> cloudTrailJsonHandler.handle(action, request, region);
|
||||
case "cloudcontrol" -> cloudControlJsonHandler.handle(action, request, region);
|
||||
default -> null;
|
||||
};
|
||||
// catalog.matchTarget is protocol-agnostic: a JSON 1.0 target
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.cloudcontrol.CloudControlJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbResponses;
|
||||
@@ -39,6 +40,7 @@ public class AwsJsonController {
|
||||
private final SnsJsonHandler snsJsonHandler;
|
||||
private final StepFunctionsJsonHandler sfnJsonHandler;
|
||||
private final CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler;
|
||||
private final CloudControlJsonHandler cloudControlJsonHandler;
|
||||
|
||||
@Inject
|
||||
public AwsJsonController(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
|
||||
@@ -47,7 +49,8 @@ public class AwsJsonController {
|
||||
DynamoDbStreamsJsonHandler dynamoDbStreamsJsonHandler,
|
||||
SqsJsonHandler sqsJsonHandler, SnsJsonHandler snsJsonHandler,
|
||||
StepFunctionsJsonHandler sfnJsonHandler,
|
||||
CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler) {
|
||||
CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler,
|
||||
CloudControlJsonHandler cloudControlJsonHandler) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.catalog = catalog;
|
||||
this.regionResolver = regionResolver;
|
||||
@@ -57,6 +60,7 @@ public class AwsJsonController {
|
||||
this.snsJsonHandler = snsJsonHandler;
|
||||
this.sfnJsonHandler = sfnJsonHandler;
|
||||
this.cloudWatchMetricsJsonHandler = cloudWatchMetricsJsonHandler;
|
||||
this.cloudControlJsonHandler = cloudControlJsonHandler;
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -96,6 +100,7 @@ public class AwsJsonController {
|
||||
case "sns" -> snsJsonHandler.handle(action, request, region);
|
||||
case "states" -> sfnJsonHandler.handle(action, request, region);
|
||||
case "monitoring" -> cloudWatchMetricsJsonHandler.handle(action, request, region);
|
||||
case "cloudcontrol" -> cloudControlJsonHandler.handle(action, request, region);
|
||||
default -> null;
|
||||
};
|
||||
// catalog.matchTarget is protocol-agnostic: a JSON 1.1 target
|
||||
|
||||
@@ -289,6 +289,10 @@ public class ResolvedServiceCatalog {
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101."),
|
||||
Set.of("cloudtrail"), Set.of(), Set.of()),
|
||||
descriptor("cloudcontrol", "cloudcontrol", config.services().cloudcontrol().enabled(), true,
|
||||
null, null, 5000L, null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("CloudApiService."), Set.of("cloudcontrolapi"), Set.of(), Set.of()),
|
||||
descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,
|
||||
"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY),
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package io.github.hectorvent.floci.services.cloudcontrol;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudControlJsonHandler {
|
||||
|
||||
private final CloudControlService service;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Inject
|
||||
public CloudControlJsonHandler(CloudControlService service, ObjectMapper mapper) {
|
||||
this.service = service;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
return switch (action) {
|
||||
case "ListResources" -> listResources(request, region);
|
||||
default -> throw new AwsException("UnsupportedOperation",
|
||||
"Operation " + action + " is not supported.", 400);
|
||||
};
|
||||
}
|
||||
|
||||
private Response listResources(JsonNode request, String region) {
|
||||
String typeName = request.path("TypeName").asText(null);
|
||||
if (typeName == null || typeName.isBlank()) {
|
||||
throw new AwsException("ValidationException", "TypeName is required.", 400);
|
||||
}
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.put("TypeName", typeName);
|
||||
ArrayNode resources = response.putArray("ResourceDescriptions");
|
||||
for (CloudControlService.ResourceDescription resource : service.listResources(region, typeName)) {
|
||||
ObjectNode node = mapper.createObjectNode();
|
||||
node.put("Identifier", resource.identifier());
|
||||
node.put("Properties", resource.properties());
|
||||
resources.add(node);
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package io.github.hectorvent.floci.services.cloudcontrol;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.s3.model.Bucket;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudControlService {
|
||||
|
||||
private final S3Service s3Service;
|
||||
private final Ec2Service ec2Service;
|
||||
private final IamService iamService;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Inject
|
||||
public CloudControlService(S3Service s3Service, Ec2Service ec2Service,
|
||||
IamService iamService, ObjectMapper mapper) {
|
||||
this.s3Service = s3Service;
|
||||
this.ec2Service = ec2Service;
|
||||
this.iamService = iamService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public List<ResourceDescription> listResources(String region, String typeName) {
|
||||
return switch (typeName) {
|
||||
case "AWS::S3::Bucket" -> s3Buckets();
|
||||
case "AWS::EC2::VPC" -> vpcs(region);
|
||||
case "AWS::EC2::Subnet" -> subnets(region);
|
||||
case "AWS::EC2::SecurityGroup" -> securityGroups(region);
|
||||
case "AWS::IAM::Role" -> roles();
|
||||
case "AWS::IAM::User" -> users();
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
private List<ResourceDescription> s3Buckets() {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (Bucket bucket : s3Service.listBuckets()) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("BucketName", bucket.getName());
|
||||
resources.add(new ResourceDescription(bucket.getName(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<ResourceDescription> vpcs(String region) {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (Vpc vpc : ec2Service.describeVpcs(region, List.of(), Map.of())) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("VpcId", vpc.getVpcId());
|
||||
properties.put("CidrBlock", vpc.getCidrBlock());
|
||||
properties.put("InstanceTenancy", vpc.getInstanceTenancy());
|
||||
resources.add(new ResourceDescription(vpc.getVpcId(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<ResourceDescription> subnets(String region) {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (Subnet subnet : ec2Service.describeSubnets(region, List.of(), Map.of())) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("SubnetId", subnet.getSubnetId());
|
||||
properties.put("VpcId", subnet.getVpcId());
|
||||
properties.put("CidrBlock", subnet.getCidrBlock());
|
||||
properties.put("AvailabilityZone", subnet.getAvailabilityZone());
|
||||
resources.add(new ResourceDescription(subnet.getSubnetId(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<ResourceDescription> securityGroups(String region) {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (SecurityGroup group : ec2Service.describeSecurityGroups(region, List.of(), List.of(), Map.of())) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("GroupId", group.getGroupId());
|
||||
properties.put("GroupName", group.getGroupName());
|
||||
properties.put("GroupDescription", group.getDescription());
|
||||
properties.put("VpcId", group.getVpcId());
|
||||
resources.add(new ResourceDescription(group.getGroupId(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<ResourceDescription> roles() {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (IamRole role : iamService.listRoles("/")) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("Arn", role.getArn());
|
||||
properties.put("RoleName", role.getRoleName());
|
||||
properties.put("Path", role.getPath());
|
||||
resources.add(new ResourceDescription(role.getRoleName(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<ResourceDescription> users() {
|
||||
List<ResourceDescription> resources = new ArrayList<>();
|
||||
for (IamUser user : iamService.listUsers("/")) {
|
||||
ObjectNode properties = mapper.createObjectNode();
|
||||
properties.put("Arn", user.getArn());
|
||||
properties.put("UserName", user.getUserName());
|
||||
properties.put("Path", user.getPath());
|
||||
resources.add(new ResourceDescription(user.getUserName(), propertiesString(properties)));
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
private String propertiesString(ObjectNode properties) {
|
||||
try {
|
||||
return mapper.writeValueAsString(properties);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AwsException("InternalFailure",
|
||||
"Failed to serialize CloudControl resource properties.", 500);
|
||||
}
|
||||
}
|
||||
|
||||
public record ResourceDescription(String identifier, String properties) {}
|
||||
}
|
||||
Reference in new issue
Block a user