/** * Associate a subnet to an existing route table */ public DeferredResult<Void> associateSubnetToRouteTable(String routeTableId, String subnetId) { AssociateRouteTableRequest req = new AssociateRouteTableRequest() .withSubnetId(subnetId) .withRouteTableId(routeTableId); String message = "Associate AWS Subnet [" + subnetId + "] to route table [" + routeTableId + "]."; AWSDeferredResultAsyncHandler<AssociateRouteTableRequest, AssociateRouteTableResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.associateRouteTableAsync(req, handler); return handler.toDeferredResult() .thenAccept(ignore -> { }); }
@Override public RouteTableAssociation associateWithSubnet(AssociateRouteTableRequest request, ResultCapture<AssociateRouteTableResult> extractor) { ActionResult result = resource.performAction("AssociateWithSubnet", request, extractor); if (result == null) return null; return new RouteTableAssociationImpl(result.getResource()); }
@Override public AssociateRouteTableResult associateRouteTable(AssociateRouteTableRequest associateRouteTableRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@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 RouteTableAssociation associateWithSubnet(AssociateRouteTableRequest request) { return associateWithSubnet(request, null); }
/** * Performs the <code>AssociateWithSubnet</code> action. * * <p> * The following request parameters will be populated from the data of this * <code>RouteTable</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>RouteTableId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>RouteTableAssociation</code> resource object associated * with the result of this action. * @see AssociateRouteTableRequest */ RouteTableAssociation associateWithSubnet(AssociateRouteTableRequest request );
/** * Performs the <code>AssociateWithSubnet</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>RouteTable</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>RouteTableId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>RouteTableAssociation</code> resource object associated * with the result of this action. * @see AssociateRouteTableRequest */ RouteTableAssociation associateWithSubnet(AssociateRouteTableRequest request , ResultCapture<AssociateRouteTableResult> extractor);