/** * Async create the subnet and return it. */ public DeferredResult<Subnet> createSubnetAsync(String subnetCidr, String vpcId, String zoneId) { CreateSubnetRequest req = new CreateSubnetRequest() .withCidrBlock(subnetCidr) .withVpcId(vpcId) .withAvailabilityZone(zoneId); String message = "Create AWS Subnet with CIDR [" + subnetCidr + "] for vpc id [" + vpcId + "]."; AWSDeferredResultAsyncHandler<CreateSubnetRequest, CreateSubnetResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.createSubnetAsync(req, handler); return handler.toDeferredResult() .thenApply(CreateSubnetResult::getSubnet); }
/** * Creates the subnet and return it. */ public Subnet createSubnet(String subnetCidr, String vpcId) { CreateSubnetRequest req = new CreateSubnetRequest() .withCidrBlock(subnetCidr) .withVpcId(vpcId); CreateSubnetResult res = this.client.createSubnet(req); return res.getSubnet(); }
/** * Creates a Subnet if not exist and return the Subnet id. */ public static String createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) { List<Filter> filters = new ArrayList<>(); Filter cidrBlockFilter = new Filter(); cidrBlockFilter.withName("cidrBlock"); cidrBlockFilter.withValues(subnetCidr); filters.add(cidrBlockFilter); if (zoneId != null) { Filter azFilter = new Filter(); azFilter.withName("availabilityZone"); azFilter.withValues(zoneId); filters.add(azFilter); } DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest() .withFilters(filters)); if (result.getSubnets() != null && !result.getSubnets().isEmpty()) { return result.getSubnets().get(0).getSubnetId(); } else { CreateSubnetRequest req = new CreateSubnetRequest() .withCidrBlock(subnetCidr) .withVpcId(vpcId); if (zoneId != null) { req.withAvailabilityZone(zoneId); } CreateSubnetResult res = client.createSubnet(req); return res.getSubnet().getSubnetId(); } }
private Subnet createAwsSubnet() { if (this.isMock) { Subnet subnet = new Subnet(); subnet.setSubnetId(UUID.randomUUID().toString()); return subnet; } CreateSubnetRequest createRequest = new CreateSubnetRequest(AWS_DEFAULT_VPC_ID, AWS_NON_EXISTING_SUBNET_CIDR); return this.client.createSubnet(createRequest).getSubnet(); }
@Override public Subnet createSubnet(CreateSubnetRequest request, ResultCapture<CreateSubnetResult> extractor) { ActionResult result = resource.performAction("CreateSubnet", request, extractor); if (result == null) return null; return new SubnetImpl(result.getResource()); }
@Override public Subnet createSubnet(String cidrBlock, ResultCapture<CreateSubnetResult> extractor) { CreateSubnetRequest request = new CreateSubnetRequest() .withCidrBlock(cidrBlock); return createSubnet(request, extractor); }
@Override public Subnet createSubnet(CreateSubnetRequest request, ResultCapture<CreateSubnetResult> extractor) { ActionResult result = service.performAction("CreateSubnet", request, extractor); if (result == null) return null; return new SubnetImpl(result.getResource()); }
@Override public Subnet createSubnet(String cidrBlock, String vpcId, ResultCapture<CreateSubnetResult> extractor) { CreateSubnetRequest request = new CreateSubnetRequest() .withCidrBlock(cidrBlock) .withVpcId(vpcId); return createSubnet(request, extractor); }
/** * Create Subnet. * * @param cidrBlock the cidr block * @param vpcId the vpc id * @return Subnet */ protected final Subnet createSubnet(final String cidrBlock, final String vpcId) { Subnet subnet = null; CreateSubnetRequest req = new CreateSubnetRequest(); req.setCidrBlock(cidrBlock); req.setVpcId(vpcId); CreateSubnetResult result = amazonEC2Client.createSubnet(req); if (result != null) { subnet = result.getSubnet(); } return subnet; }
@Override public CreateSubnetResult createSubnet(CreateSubnetRequest createSubnetRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
public com.amazonaws.services.ec2.model.Subnet createSubnet(CreateSubnetRequest request) { logger.info("create subnet, request={}", request); return ec2.createSubnet(request).getSubnet(); }
@Override public void execute(Context context) throws Exception { List<String> zones = AWS.ec2.availabilityZones(); List<String> subnetIds = new ArrayList<>(); for (int i = 0; i < resource.cidrs.size(); i++) { String cidr = resource.cidrs.get(i); com.amazonaws.services.ec2.model.Subnet subnet = AWS.vpc.createSubnet(new CreateSubnetRequest(resource.vpc.remoteVPC.getVpcId(), cidr) .withAvailabilityZone(zones.get(i))); subnetIds.add(subnet.getSubnetId()); } while (true) { logger.info("wait until all subnets are available"); Threads.sleepRoughly(Duration.ofSeconds(10)); List<com.amazonaws.services.ec2.model.Subnet> subnets = AWS.vpc.describeSubnets(subnetIds); boolean allOK = subnets.stream().allMatch(subnet -> { logger.info("subnet {} => {}", subnet.getSubnetId(), subnet.getState()); return "available".equals(subnet.getState()); }); if (allOK) { resource.remoteSubnets.addAll(subnets); break; } } if (resource.type == SubnetType.PUBLIC) { for (String subnetId : subnetIds) { AWS.vpc.ec2.modifySubnetAttribute(new ModifySubnetAttributeRequest() .withSubnetId(subnetId) .withMapPublicIpOnLaunch(true)); } } EC2TagHelper tagHelper = new EC2TagHelper(context.env); AWS.ec2.createTags(new CreateTagsRequest() .withResources(subnetIds) .withTags(tagHelper.env(), tagHelper.resourceId(resource.id), tagHelper.name(resource.id))); logger.info("associate route table, subnet={}, routeTable={}", resource.id, resource.routeTable.id); for (com.amazonaws.services.ec2.model.Subnet remoteSubnet : resource.remoteSubnets) { AWS.vpc.ec2.associateRouteTable(new AssociateRouteTableRequest() .withRouteTableId(resource.routeTable.remoteRouteTable.getRouteTableId()) .withSubnetId(remoteSubnet.getSubnetId())); } }
@Override public Subnet createSubnet(CreateSubnetRequest request) { return createSubnet(request, null); }
/** * Performs the <code>CreateSubnet</code> action. * * <p> * 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>VpcId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>Subnet</code> resource object associated with the * result of this action. * @see CreateSubnetRequest */ Subnet createSubnet(CreateSubnetRequest request);
/** * Performs the <code>CreateSubnet</code> action and use a ResultCapture to * retrieve the low-level client response. * * <p> * 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>VpcId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>Subnet</code> resource object associated with the * result of this action. * @see CreateSubnetRequest */ Subnet createSubnet(CreateSubnetRequest request, ResultCapture<CreateSubnetResult> extractor);
/** * Performs the <code>CreateSubnet</code> action. * * <p> * * @return The <code>Subnet</code> resource object associated with the * result of this action. * @see CreateSubnetRequest */ com.amazonaws.resources.ec2.Subnet createSubnet(CreateSubnetRequest request) ;
/** * Performs the <code>CreateSubnet</code> action and use a ResultCapture to * retrieve the low-level client response. * * <p> * * @return The <code>Subnet</code> resource object associated with the * result of this action. * @see CreateSubnetRequest */ com.amazonaws.resources.ec2.Subnet createSubnet(CreateSubnetRequest request, ResultCapture<CreateSubnetResult> extractor);