Search in sources :

Example 1 with DeleteSecurityGroupRequest

use of software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest in project photon-model by vmware.

the class AWSRemoteCleanup method deleteSecurityGroups.

private void deleteSecurityGroups(String vpcId, AmazonEC2 usEastEc2Client) {
    DescribeSecurityGroupsRequest securityGroupsRequest = new DescribeSecurityGroupsRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
    DescribeSecurityGroupsResult securityGroupsResult = usEastEc2Client.describeSecurityGroups(securityGroupsRequest);
    securityGroupsResult.getSecurityGroups().forEach(securityGroup -> {
        if (!(securityGroup.getGroupName().equalsIgnoreCase(DEFAULT_TAG))) {
            DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest().withGroupId(securityGroup.getGroupId());
            this.host.log("Terminating stale security group: %s", securityGroup.getGroupId());
            usEastEc2Client.deleteSecurityGroup(deleteSecurityGroupRequest);
        }
    });
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) Filter(com.amazonaws.services.ec2.model.Filter) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Example 2 with DeleteSecurityGroupRequest

use of software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest in project photon-model by vmware.

the class AWSSecurityGroupClient method deleteSecurityGroup.

public void deleteSecurityGroup(String securityGroupId) {
    DeleteSecurityGroupRequest req = new DeleteSecurityGroupRequest().withGroupId(securityGroupId);
    this.client.deleteSecurityGroup(req);
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest)

Example 3 with DeleteSecurityGroupRequest

use of software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest in project aws-doc-sdk-examples by awsdocs.

the class DeleteSecurityGroup method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a security group id\n" + "Ex: DeleteSecurityGroup <security-group-id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String group_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest().withGroupId(group_id);
    DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);
    System.out.printf("Successfully deleted security group with id %s", group_id);
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) DeleteSecurityGroupResult(com.amazonaws.services.ec2.model.DeleteSecurityGroupResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 4 with DeleteSecurityGroupRequest

use of software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest in project Synapse-Stack-Builder by Sage-Bionetworks.

the class EC2SecuritySetupTest method testTeardownResources.

@Test
public void testTeardownResources() {
    resources.setElasticBeanstalkEC2SecurityGroup(new SecurityGroup().withGroupName(config.getElasticSecurityGroupName()));
    DeleteSecurityGroupRequest req = new DeleteSecurityGroupRequest().withGroupName(resources.getElasticBeanstalkEC2SecurityGroup().getGroupName());
    ec2SecuritySetup.teardownResources();
    assertNotNull(resources.getElasticBeanstalkEC2SecurityGroup());
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Test(org.junit.Test)

Example 5 with DeleteSecurityGroupRequest

use of software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest in project photon-model by vmware.

the class TestAWSSetupUtils method deleteSecurityGroupUsingEC2Client.

public static void deleteSecurityGroupUsingEC2Client(AmazonEC2AsyncClient client, VerificationHost host, String awsGroupId) {
    host.log(Level.INFO, "Starting to delete aws Security group with id %s", awsGroupId);
    if (awsGroupId == null) {
        return;
    }
    try {
        DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest().withGroupId(awsGroupId);
        client.deleteSecurityGroup(deleteSecurityGroupRequest);
        host.waitFor("Timeout waiting for AWS to delete a SecurityGroup with name " + awsGroupId, () -> {
            // Check if the SG is actually not present on AWS after the delete operation
            SecurityGroup discoveredSGOnAWS = getSecurityGroupsIdUsingEC2Client(client, awsGroupId);
            if (discoveredSGOnAWS != null) {
                // Requested SG was not deleted from AWS
                return false;
            }
            host.log("Deleted SG with id: %s", awsGroupId);
            return true;
        });
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("The security group '" + awsGroupId + "' already exists")) {
            throw e;
        }
    }
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DeleteSecurityGroupRequest (com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest)5 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 DeleteSecurityGroupResult (com.amazonaws.services.ec2.model.DeleteSecurityGroupResult)1 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)1 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 UnknownHostException (java.net.UnknownHostException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 DeleteSecurityGroupRequest (software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1