@Override public void allocate(Allocation allocInfo) throws Exception { try { final VmInstanceLifecycleHelper helper = VmInstanceLifecycleHelpers.get(); final PrepareNetworkResourcesType request = new PrepareNetworkResourcesType(); request.setAvailabilityZone(allocInfo.getPartition().getName()); request.setVpc( allocInfo.getSubnet() == null ? null : CloudMetadatas.toDisplayName().apply(allocInfo.getSubnet().getVpc())); request.setSubnet(CloudMetadatas.toDisplayName().apply(allocInfo.getSubnet())); request.setFeatures(Lists.<NetworkFeature>newArrayList(new DnsHostNamesFeature())); helper.prepareNetworkAllocation(allocInfo, request); final PrepareNetworkResourcesResultType result = Networking.getInstance().prepare(request); for (final ResourceToken token : allocInfo.getAllocationTokens()) { for (final NetworkResource networkResource : result.getResources()) { if (token.getInstanceId().equals(networkResource.getOwnerId())) { token .getAttribute(NetworkResourceVmInstanceLifecycleHelper.NetworkResourcesKey) .add(networkResource); } } } } catch (Exception e) { throw Objects.firstNonNull(Exceptions.findCause(e, NotEnoughResourcesException.class), e); } }
@Override public boolean apply(Allocation allocInfo) throws MetadataException { VmInstanceLifecycleHelpers.get().verifyAllocation(allocInfo); return true; }