Example usage for com.amazonaws.services.ec2.model Vpc getIsDefault

List of usage examples for com.amazonaws.services.ec2.model Vpc getIsDefault

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model Vpc getIsDefault.

Prototype


public Boolean getIsDefault() 

Source Link

Document

Indicates whether the VPC is the default VPC.

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.security.DefaultAWSAccountInfoLookup.java

License:Apache License

@Override
public String findAccountId() {
    AmazonEC2 ec2 = amazonClientProvider.getAmazonEC2(credentialsProvider, AmazonClientProvider.DEFAULT_REGION);
    try {//from  ww  w.  j  av  a 2s . c  o m
        List<Vpc> vpcs = ec2.describeVpcs().getVpcs();
        boolean supportsByName = false;
        if (vpcs.isEmpty()) {
            supportsByName = true;
        } else {
            for (Vpc vpc : vpcs) {
                if (vpc.getIsDefault()) {
                    supportsByName = true;
                    break;
                }
            }
        }

        DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
        if (supportsByName) {
            request.withGroupNames(DEFAULT_SECURITY_GROUP_NAME);
        }
        DescribeSecurityGroupsResult result = ec2.describeSecurityGroups(request);

        for (SecurityGroup sg : result.getSecurityGroups()) {
            //if there is a vpcId or it is the default security group it won't be an EC2 cross account group
            if ((sg.getVpcId() != null && sg.getVpcId().length() > 0)
                    || DEFAULT_SECURITY_GROUP_NAME.equals(sg.getGroupName())) {
                return sg.getOwnerId();
            }
        }

        throw new IllegalArgumentException("Unable to lookup accountId with provided credentials");
    } catch (AmazonServiceException ase) {
        if ("AccessDenied".equals(ase.getErrorCode())) {
            String message = ase.getMessage();
            Matcher matcher = IAM_ARN_PATTERN.matcher(message);
            if (matcher.matches()) {
                return matcher.group(1);
            }
        }
        throw ase;
    }
}