use of software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsRequest 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.DescribeSecurityGroupsRequest 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);
}
use of software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsRequest in project Gatekeeper by FINRAOS.
the class SGLookupService method loadSgsForAccountRegion.
private List<String> loadSgsForAccountRegion(AWSEnvironment environment) {
logger.info("Grabbing SGs for environment " + environment);
DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest();
Filter groupNameFilter = new Filter();
groupNameFilter.setName("group-name");
groupNameFilter.setValues(Collections.singletonList(securityGroupName));
AmazonEC2Client amazonEC2Client = awsSessionService.getEC2Session(environment);
DescribeSecurityGroupsResult result = amazonEC2Client.describeSecurityGroups(describeSecurityGroupsRequest.withFilters(groupNameFilter));
logger.info("found " + result.getSecurityGroups().size() + " Security Groups with name '" + securityGroupName + "'");
return result.getSecurityGroups().stream().map(SecurityGroup::getGroupId).collect(Collectors.toList());
}
use of software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsRequest in project cloudbreak by hortonworks.
the class AwsPlatformResources method securityGroups.
@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
Set<CloudSecurityGroup> cloudSecurityGroups = new HashSet<>();
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
// create securitygroup filter view
PlatformResourceSecurityGroupFilterView filter = new PlatformResourceSecurityGroupFilterView(filters);
DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest();
// If the filtervalue is provided then we should filter only for those securitygroups
if (!Strings.isNullOrEmpty(filter.getVpcId())) {
describeSecurityGroupsRequest.withFilters(new Filter("vpc-id", singletonList(filter.getVpcId())));
}
if (!Strings.isNullOrEmpty(filter.getGroupId())) {
describeSecurityGroupsRequest.withGroupIds(filter.getGroupId());
}
if (!Strings.isNullOrEmpty(filter.getGroupName())) {
describeSecurityGroupsRequest.withGroupNames(filter.getGroupName());
}
for (SecurityGroup securityGroup : ec2Client.describeSecurityGroups(describeSecurityGroupsRequest).getSecurityGroups()) {
Map<String, Object> properties = new HashMap<>();
properties.put("vpcId", securityGroup.getVpcId());
properties.put("description", securityGroup.getDescription());
properties.put("ipPermissions", securityGroup.getIpPermissions());
properties.put("ipPermissionsEgress", securityGroup.getIpPermissionsEgress());
cloudSecurityGroups.add(new CloudSecurityGroup(securityGroup.getGroupName(), securityGroup.getGroupId(), properties));
}
result.put(region.value(), cloudSecurityGroups);
return new CloudSecurityGroups(result);
}
use of software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsRequest in project aws-doc-sdk-examples by awsdocs.
the class DescribeSecurityGroups method main.
public static void main(String[] args) {
final String USAGE = "To run this example, supply a group id\n" + "Ex: DescribeSecurityGroups <group-id>\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String group_id = args[0];
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest().withGroupIds(group_id);
DescribeSecurityGroupsResult response = ec2.describeSecurityGroups(request);
for (SecurityGroup group : response.getSecurityGroups()) {
System.out.printf("Found security group with id %s, " + "vpc id %s " + "and description %s", group.getGroupId(), group.getVpcId(), group.getDescription());
}
}
Aggregations