Java Code Examples for com.amazonaws.services.ec2.model.DescribeSubnetsResult#getSubnets()

The following examples show how to use com.amazonaws.services.ec2.model.DescribeSubnetsResult#getSubnets() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubnetTableProvider.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Calls DescribeSubnets on the AWS EC2 Client returning all subnets that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific subnet) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();

    ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("id");
    if (idConstraint != null && idConstraint.isSingleValue()) {
        request.setSubnetIds(Collections.singletonList(idConstraint.getSingleValue().toString()));
    }

    DescribeSubnetsResult response = ec2.describeSubnets(request);
    for (Subnet subnet : response.getSubnets()) {
        instanceToRow(subnet, spiller);
    }
}
 
Example 2
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch subnets.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<Subnet>> fetchSubnets(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
	Map<String,List<Subnet>> subnets = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Subnet\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				DescribeSubnetsResult rslt = ec2Client.describeSubnets();
				List<Subnet> subnetsTemp =rslt.getSubnets();
				if(! subnetsTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Subnet "+region.getName() + " >> "+subnetsTemp.size());
					subnets.put(accountId+delimiter+accountName+delimiter+region.getName(),subnetsTemp);
				}

			}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"subnet",e.getMessage());
		}
	}

	return subnets;
}
 
Example 3
Source File: EC2Communication.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether exiting Subnet is present.
 * 
 * @param subnetString
 * @return <code>Subnet </code> if the matches one of the subnetString
 * 
 */
public Subnet resolveSubnet(String subnetString) throws APPlatformException {
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    DescribeSubnetsResult result = getEC2().describeSubnets(
            request.withSubnetIds(subnetString));
    List<Subnet> subnets = result.getSubnets();
    if (!subnets.isEmpty()) {
        LOGGER.debug(" number of subnets found: " + subnets.size());
        for (Subnet subnet : subnets) {
            LOGGER.debug("return subnet with id " + subnet.getSubnetId());
            return subnet;
        }

    }
    throw new APPlatformException(
            Messages.getAll("error_invalid_subnet_id") + subnetString);

}
 
Example 4
Source File: AwsIaasGatewayScriptService.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasSubnets(String vpcId) throws AutoException {
    if (StringUtils.isEmpty(vpcId)) {
        log.info(platform.getPlatformName() + " にvpcIdが有りません");
        System.out.println("VPCID_EMPTY");
        return false;
    }

    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(vpcId));
    DescribeSubnetsResult result = ec2Client.describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    if (subnets.isEmpty()) {
        log.info(platform.getPlatformName() + " にサブネットが有りません");
        System.out.println("SUBNET_EMPTY");
        return false;
    }

    return true;
}
 
Example 5
Source File: AwsSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateExistingSubnet(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
    if (awsNetworkView.isExistingSubnet()) {
        DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
        describeSubnetsRequest.withSubnetIds(awsNetworkView.getSubnetList());
        DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
        if (describeSubnetsResult.getSubnets().size() < awsNetworkView.getSubnetList().size()) {
            throw new CloudConnectorException(String.format(SUBNET_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet()));
        } else {
            for (Subnet subnet : describeSubnetsResult.getSubnets()) {
                String vpcId = subnet.getVpcId();
                if (vpcId != null && !vpcId.equals(awsNetworkView.getExistingVpc())) {
                    throw new CloudConnectorException(String.format(SUBNETVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet(),
                            awsNetworkView.getExistingVpc()));
                }
            }
        }
    }
}
 
Example 6
Source File: Ec2DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation uses the DescribeSubnets API.
 */
@Override
public List<Subnet> getSubnets(Collection<String> subnetIds, AwsParamsDto awsParamsDto)
{
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
    describeSubnetsRequest.setSubnetIds(subnetIds);
    try
    {
        DescribeSubnetsResult describeSubnetsResult = ec2Operations.describeSubnets(ec2Client, describeSubnetsRequest);
        return describeSubnetsResult.getSubnets();
    }
    catch (AmazonServiceException amazonServiceException)
    {
        /*
         * AWS throws a 400 error when any one of the specified subnet ID is not found.
         * We want to catch it and throw as an handled herd error as a 404 not found.
         */
        if (ERROR_CODE_SUBNET_ID_NOT_FOUND.equals(amazonServiceException.getErrorCode()))
        {
            throw new ObjectNotFoundException(amazonServiceException.getErrorMessage(), amazonServiceException);
        }
        // Any other type of error we throw as is because they are unexpected.
        else
        {
            throw amazonServiceException;
        }
    }
}
 
Example 7
Source File: AwsDescribeServiceImpl.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<Subnet> getSubnets(Long userNo, Long platformNo) {
    // VPCかどうかのチェック
    PlatformAws platformAws = platformAwsDao.read(platformNo);
    if (BooleanUtils.isNotTrue(platformAws.getVpc())) {
        // 非VPCの場合、サブネットはない
        return new ArrayList<Subnet>();
    }

    // サブネットを取得
    AwsProcessClient awsProcessClient = awsProcessClientFactory.createAwsProcessClient(userNo, platformNo);
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(platformAws.getVpcId()));
    DescribeSubnetsResult result = awsProcessClient.getEc2Client().describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    // プラットフォームにサブネットが指定されている場合、そのサブネットのみに制限する
    if (StringUtils.isNotEmpty(awsProcessClient.getPlatformAws().getSubnetId())) {
        List<String> subnetIds = new ArrayList<String>();
        for (String subnetId : StringUtils.split(awsProcessClient.getPlatformAws().getSubnetId(), ",")) {
            subnetIds.add(subnetId.trim());
        }

        List<Subnet> subnets2 = new ArrayList<Subnet>();
        for (Subnet subnet : subnets) {
            if (subnetIds.contains(subnet.getSubnetId())) {
                subnets2.add(subnet);
            }
        }
        subnets = subnets2;
    }

    // ソート
    Collections.sort(subnets, Comparators.COMPARATOR_SUBNET);

    return subnets;
}
 
Example 8
Source File: AwsCommonProcess.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
public List<Subnet> describeSubnetsByVpcId(AwsProcessClient awsProcessClient, String vpcId) {
    DescribeSubnetsRequest request = new DescribeSubnetsRequest();
    request.withFilters(new Filter().withName("vpc-id").withValues(vpcId));
    DescribeSubnetsResult result = awsProcessClient.getEc2Client().describeSubnets(request);
    List<Subnet> subnets = result.getSubnets();

    return subnets;
}
 
Example 9
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe Subnets.
 * @return List of Subnet
 */
protected final List<Subnet> getSubnets() {
    List<Subnet> subnets = null;

    DescribeSubnetsRequest req = new DescribeSubnetsRequest();
    DescribeSubnetsResult result = amazonEC2Client.describeSubnets(req);
    if (result != null && !result.getSubnets().isEmpty()) {
        subnets = result.getSubnets();
    }

     return subnets;
  }
 
Example 10
Source File: Ec2Utils.java    From pacbot with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the subnets for region.
 *
 * @param ec2ServiceClient the ec 2 service client
 * @param region the region
 * @param describeSubnetsRequest the describe subnets request
 * @return the subnets for region
 */
public static List<Subnet> getSubnetsForRegion(AmazonEC2 ec2ServiceClient,
		Region region, DescribeSubnetsRequest describeSubnetsRequest) {
	ec2ServiceClient.setRegion(region);
	DescribeSubnetsResult describeSubnetsResult = ec2ServiceClient.describeSubnets(describeSubnetsRequest);
	return describeSubnetsResult.getSubnets();
}