@Override protected void consumeSuccess(DescribeVpcsRequest request, DescribeVpcsResult result) { URI adapterUri = AdapterUriUtil.buildAdapterUri(getHost(), AWSUriPaths.AWS_NETWORK_ADAPTER); for (Vpc resultVPC : result.getVpcs()) { NetworkState networkState = mapVPCToNetworkState(resultVPC, this.context.request.regionId, this.context.request.request.resourcePoolLink, this.context.request.request.endpointLink, this.context.request.endpointAuth.documentSelfLink, this.context.request.parentComputeLink, this.context.request.tenantLinks, adapterUri); if (networkState.subnetCIDR == null) { logWarning(() -> String.format("AWS did not return CIDR information for VPC %s", resultVPC.toString())); } this.context.awsVpcs.put(resultVPC.getVpcId(), resultVPC); this.context.vpcs.put(resultVPC.getVpcId(), networkState); } }
@Override @Cacheable(value = CachingConfiguration.VPC_CACHE, key = "#vpcId", condition = "#bypassCache == false") public Vpc getVpcInRegion(final String vpcId, final String region, boolean bypassCache) { Preconditions.checkArgument(StringUtils.isNotBlank(vpcId), "vpcId may not be null or blank"); Preconditions.checkArgument(StringUtils.isNotBlank(region), "region may not be null or blank"); LOG.info("Retrieving VPC {} in region {} ({})", vpcId, region, bypassCache); DescribeVpcsRequest request = new DescribeVpcsRequest() .withVpcIds(vpcId); DescribeVpcsResult result = getClientForRegion(region).describeVpcs(request); List<Vpc> results = result.getVpcs(); if (results.size() != 1) { throw new IllegalArgumentException("Did not get expected result"); } return results.get(0); }
protected String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) { AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork()); String region = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVPC()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.info("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVPC()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.info("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), subnetCidrs.stream().collect(Collectors.joining(","))); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }
public Vpc getVPC(String vpcId) { DescribeVpcsRequest req = new DescribeVpcsRequest().withVpcIds(vpcId); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); if (vpcs != null && vpcs.size() == 1) { return vpcs.get(0); } return null; }
/** * Get the default VPC - return null if no default specified */ public Vpc getDefaultVPC() { DescribeVpcsRequest req = new DescribeVpcsRequest(); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); for (Vpc vpc : vpcs) { if (vpc.isDefault()) { return vpc; } } return null; }
public Vpc createVPC() throws InterruptedException { logger.info("create VPC"); String vpcId = ec2.createVpc(new CreateVpcRequest().withCidrBlock("10.0.0.0/16")).getVpc().getVpcId(); while (true) { Threads.sleepRoughly(Duration.ofSeconds(20)); DescribeVpcsResult result = ec2.describeVpcs(new DescribeVpcsRequest().withVpcIds(vpcId)); Vpc remoteVPC = result.getVpcs().get(0); if ("available".equals(remoteVPC.getState())) { enableVPCDNS(vpcId); return remoteVPC; } } }
@Override public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) { Map<String, Set<CloudNetwork>> result = new HashMap<>(); Set<CloudNetwork> cloudNetworks = new HashSet<>(); AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value()); //create vpc filter view PlatformResourceVpcFilterView filter = new PlatformResourceVpcFilterView(filters); DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest(); // If the filtervalue is provided then we should filter only for those vpc if (!Strings.isNullOrEmpty(filter.getVpcId())) { describeVpcsRequest.withVpcIds(filter.getVpcId()); } for (Vpc vpc : ec2Client.describeVpcs(describeVpcsRequest).getVpcs()) { Map<String, String> subnetMap = new HashMap<>(); List<Subnet> subnets = ec2Client.describeSubnets(createVpcDescribeRequest(vpc)).getSubnets(); Map<String, Object> properties = new HashMap<>(); properties.put("cidrBlock", vpc.getCidrBlock()); properties.put("default", vpc.getIsDefault()); properties.put("dhcpOptionsId", vpc.getDhcpOptionsId()); properties.put("instanceTenancy", vpc.getInstanceTenancy()); properties.put("state", vpc.getState()); for (Subnet subnet : subnets) { subnetMap.put(subnet.getSubnetId(), subnet.getSubnetId()); } cloudNetworks.add(new CloudNetwork(vpc.getVpcId(), vpc.getVpcId(), subnetMap, properties)); } result.put(region.value(), cloudNetworks); return new CloudNetworks(result); }
@Override public List<AbstractResource<?>> describeVpcs(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeVpcsRequest req = new DescribeVpcsRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing vpcs for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeVpcsResult res = ec2.describeVpcs(req); return converter.toVpcVpcs(res.getVpcs(), account.getId(), region, dt); }
/** * Describe VPCs. * * @return List of vpcs */ protected final List<Vpc> describeVpcs() { DescribeVpcsRequest req = new DescribeVpcsRequest(); DescribeVpcsResult result = amazonEC2Client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); return vpcs; }
@Override public DescribeVpcsResult describeVpcs(DescribeVpcsRequest describeVpcsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
private void refreshVPCInformation(AWSNetworkStateCreationContext aws, AWSNetworkStateCreationStage next) { DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest(); AWSVPCAsyncHandler asyncHandler = new AWSVPCAsyncHandler(next, aws); aws.amazonEC2Client.describeVpcsAsync(vpcRequest, asyncHandler); }
public Vpc describeVPC(String vpcId) { logger.info("describe vpc, vpcId={}", vpcId); DescribeVpcsResult result = ec2.describeVpcs(new DescribeVpcsRequest().withVpcIds(vpcId)); return result.getVpcs().get(0); }
@Override public boolean load(DescribeVpcsRequest request) { return load(request, null); }
@Override public boolean load(DescribeVpcsRequest request, ResultCapture<DescribeVpcsResult> extractor) { return resource.load(request, extractor); }
@Override public VpcCollection getVpcs() { return getVpcs((DescribeVpcsRequest)null); }
@Override public VpcCollection getVpcs(DescribeVpcsRequest request) { ResourceCollectionImpl result = service.getCollection("Vpcs", request); if (result == null) return null; return new VpcCollectionImpl(result); }
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet. * The following request parameters will be populated from the data of this * <code>Vpc</code> resource, and any conflicting parameter value set in the * request will be overridden: * <ul> * <li> * <b><code>VpcIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeVpcsRequest */ boolean load(DescribeVpcsRequest request);
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet, and use a ResultCapture to retrieve the low-level * client response * The following request parameters will be populated from the data of this * <code>Vpc</code> resource, and any conflicting parameter value set in the * request will be overridden: * <ul> * <li> * <b><code>VpcIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeVpcsRequest */ boolean load(DescribeVpcsRequest request, ResultCapture<DescribeVpcsResult> extractor);
/** * Retrieves the Vpcs collection referenced by this resource. */ VpcCollection getVpcs(DescribeVpcsRequest request);