use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project photon-model by vmware.
the class AWSSecurityGroupEnumerationAdapterService method createResponse.
/**
* Having the enumerated SecurityGroup Ids, query the States and provide them in the response
*/
private DeferredResult<AWSSecurityGroupEnumerationResponse> createResponse(SecurityGroupEnumContext context) {
AWSSecurityGroupEnumerationResponse response = new AWSSecurityGroupEnumerationResponse();
if (context.enumExternalResourcesIds == null || context.enumExternalResourcesIds.isEmpty()) {
DeferredResult<AWSSecurityGroupEnumerationResponse> deferredResult = new DeferredResult<>();
deferredResult.complete(response);
return deferredResult;
}
Query.Builder findSecurityGroupStates = Builder.create().addKindFieldClause(SecurityGroupState.class).addFieldClause(ResourceState.FIELD_NAME_COMPUTE_HOST_LINK, context.request.parentCompute.documentSelfLink).addInClause(SecurityGroupState.FIELD_NAME_ID, context.enumExternalResourcesIds);
QueryTop<SecurityGroupState> querySecurityGroupStates = new QueryTop<>(context.service.getHost(), findSecurityGroupStates.build(), SecurityGroupState.class, context.request.parentCompute.tenantLinks).setMaxResultsLimit(context.enumExternalResourcesIds.size());
querySecurityGroupStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return querySecurityGroupStates.queryDocuments(sgState -> response.securityGroupStates.put(sgState.id, sgState.documentSelfLink)).thenApply(aVoid -> response);
}
use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project photon-model by vmware.
the class AWSSecurityGroupClient method getDefaultSecurityGroup.
public SecurityGroup getDefaultSecurityGroup(String vpcId) {
SecurityGroup cellGroup = null;
DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withFilters(new Filter("group-name", Collections.singletonList(DEFAULT_SECURITY_GROUP_NAME)));
if (vpcId != null) {
req.withFilters(new Filter("vpc-id", Collections.singletonList(vpcId)));
}
DescribeSecurityGroupsResult cellGroups = this.client.describeSecurityGroups(req);
if (cellGroups != null && !cellGroups.getSecurityGroups().isEmpty()) {
cellGroup = cellGroups.getSecurityGroups().get(0);
}
return cellGroup;
}
use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project photon-model by vmware.
the class AWSSecurityGroupClient method getSecurityGroup.
public SecurityGroup getSecurityGroup(String name, String vpcId) {
SecurityGroup cellGroup = null;
DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withFilters(new Filter("group-name", Collections.singletonList(name)));
if (vpcId != null) {
req.withFilters(new Filter("vpc-id", Collections.singletonList(vpcId)));
}
DescribeSecurityGroupsResult cellGroups = this.client.describeSecurityGroups(req);
if (cellGroups != null && !cellGroups.getSecurityGroups().isEmpty()) {
cellGroup = cellGroups.getSecurityGroups().get(0);
}
return cellGroup;
}
use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project photon-model by vmware.
the class TestAWSSetupUtils method setUpTestVpc.
public static void setUpTestVpc(AmazonEC2AsyncClient client, Map<String, Object> awsTestContext, boolean isMock, String zoneId) {
// If the pre-set VPC does not exist, get the test VPC for the given account and use it in the tests.
if (!isMock && !vpcIdExists(client, AWS_DEFAULT_VPC_ID)) {
String vpcId = createorGetVPCForAccount(client);
awsTestContext.put(VPC_KEY, vpcId);
Subnet subnet = createOrGetSubnet(client, AWS_DEFAULT_SUBNET_CIDR, vpcId, zoneId);
awsTestContext.put(SUBNET_KEY, subnet.getSubnetId());
String internetGatewayId = createOrGetInternetGatewayForGivenVPC(client, vpcId);
awsTestContext.put(INTERNET_GATEWAY_KEY, internetGatewayId);
SecurityGroup sg = createOrGetDefaultSecurityGroupForGivenVPC(client, vpcId);
awsTestContext.put(SECURITY_GROUP_KEY, sg.getGroupId());
awsTestContext.put(SECURITY_GROUP_NAME_KEY, sg.getGroupName());
NetSpec network = new NetSpec(vpcId, vpcId, AWS_DEFAULT_VPC_CIDR);
List<NetSpec> subnets = new ArrayList<>();
subnets.add(new NetSpec(subnet.getSubnetId(), AWS_DEFAULT_SUBNET_NAME, subnet.getCidrBlock(), zoneId == null ? TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier : zoneId));
NicSpec nicSpec = NicSpec.create().withSubnetSpec(subnets.get(0)).withDynamicIpAssignment();
awsTestContext.put(NIC_SPECS_KEY, new AwsNicSpecs(network, Collections.singletonList(nicSpec)));
return;
}
awsTestContext.put(VPC_KEY, AWS_DEFAULT_VPC_ID);
awsTestContext.put(NIC_SPECS_KEY, SINGLE_NIC_SPEC);
awsTestContext.put(SUBNET_KEY, AWS_DEFAULT_SUBNET_ID);
awsTestContext.put(SECURITY_GROUP_KEY, AWS_DEFAULT_GROUP_ID);
awsTestContext.put(SECURITY_GROUP_NAME_KEY, AWS_DEFAULT_GROUP_NAME);
}
use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetDefaultSecurityGroupForGivenVPC.
/**
* Returns an existing security group for a VPC if it exists otherwise creates a new security group.
*/
public static SecurityGroup createOrGetDefaultSecurityGroupForGivenVPC(AmazonEC2AsyncClient client, String vpcID) {
List<SecurityGroup> securityGroupsInVPC = client.describeSecurityGroups().getSecurityGroups().stream().filter(sg -> sg.getVpcId().equals(vpcID)).collect(Collectors.toList());
if (securityGroupsInVPC != null && !securityGroupsInVPC.isEmpty()) {
for (SecurityGroup sg : securityGroupsInVPC) {
// Do not use newly provisioned security groups as this could interfere with the cleanup logic of other tests.
if (!sg.getGroupName().startsWith(AWS_NEW_GROUP_PREFIX)) {
return sg;
}
}
}
String securityGroupId = new AWSSecurityGroupClient(client).createDefaultSecurityGroup(vpcID);
tagResources(client, Arrays.asList(securityGroupId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_SG);
DescribeSecurityGroupsResult result = client.describeSecurityGroups(new DescribeSecurityGroupsRequest().withGroupIds(Arrays.asList(securityGroupId)));
return result.getSecurityGroups().get(0);
}
Aggregations