Example usage for com.amazonaws.services.ec2 AmazonEC2 describeVpcs

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 describeVpcs

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 describeVpcs.

Prototype

DescribeVpcsResult describeVpcs();

Source Link

Document

Simplified method form for invoking the DescribeVpcs operation.

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.deploy.handlers.MigrateSecurityGroupStrategy.java

License:Apache License

private Map<String, String> getVpcMappings(List<NetflixAmazonCredentials> accounts) {
    Map<String, String> mappings = new HashMap<>();
    if (target.getVpcId() == null) {
        return mappings;
    }/*from ww  w.  j a va2s  . co  m*/
    AmazonEC2 baseTarget = getAmazonClientProvider().getAmazonEC2(target.getCredentials(), target.getRegion());
    Vpc targetVpc = baseTarget.describeVpcs().getVpcs().stream()
            .filter(vpc -> vpc.getVpcId().equals(target.getVpcId())).findFirst().orElse(null);

    String targetName = AmazonVpcProvider.getVpcName(targetVpc);

    accounts.forEach(account -> {
        List<Vpc> vpcs = getAmazonClientProvider().getAmazonEC2(account, target.getRegion()).describeVpcs()
                .getVpcs();
        Vpc match = vpcs.stream().filter(vpc -> AmazonVpcProvider.getVpcName(vpc).equals(targetName))
                .findFirst().orElse(null);
        mappings.put(account.getAccountId(), match.getVpcId());
    });
    return mappings;
}

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 {/*ww w.  j  a v a2s. 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;
    }
}

From source file:org.jenkinsci.plugins.amazonwebservices.cloud.EC2.java

License:Open Source License

public static List<String> getVPCs(AmazonEC2 ec2) {
    DescribeVpcsResult ec2res = ec2.describeVpcs();
    List<String> res = new ArrayList<String>();
    for (Vpc v : ec2res.getVpcs()) {
        res.add(v.getVpcId());/*  ww w .  j a v  a 2s  .c  o m*/
    }
    return res;
}