| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.terremark.suppliers; |
| 20 | |
| 21 | import static org.jclouds.vcloud.terremark.options.AddInternetServiceOptions.Builder.withDescription; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | import org.jclouds.rest.InsufficientResourcesException; |
| 35 | import org.jclouds.vcloud.domain.VCloudExpressVApp; |
| 36 | import org.jclouds.vcloud.terremark.TerremarkECloudClient; |
| 37 | import org.jclouds.vcloud.terremark.domain.InternetService; |
| 38 | import org.jclouds.vcloud.terremark.domain.Protocol; |
| 39 | import org.jclouds.vcloud.terremark.domain.PublicIpAddress; |
| 40 | |
| 41 | import com.google.common.collect.ImmutableMap; |
| 42 | import com.google.common.collect.Iterables; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | @Singleton |
| 48 | public class TerremarkECloudInternetServiceAndPublicIpAddressSupplier implements |
| 49 | InternetServiceAndPublicIpAddressSupplier { |
| 50 | |
| 51 | @Resource |
| 52 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 53 | public Logger logger = Logger.NULL; |
| 54 | protected final TerremarkECloudClient client; |
| 55 | |
| 56 | @Inject |
| 57 | public TerremarkECloudInternetServiceAndPublicIpAddressSupplier(TerremarkECloudClient client) { |
| 58 | this.client = client; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Entry<InternetService, PublicIpAddress> getNewInternetServiceAndIp(VCloudExpressVApp vApp, int port, |
| 63 | Protocol protocol) { |
| 64 | logger.debug(">> creating InternetService in vDC %s:%s:%d", vApp.getVDC().getName(), protocol, port); |
| 65 | InternetService is = null; |
| 66 | PublicIpAddress ip = null; |
| 67 | try { |
| 68 | ip = client.activatePublicIpInVDC(vApp.getVDC().getHref()); |
| 69 | } catch (InsufficientResourcesException e) { |
| 70 | logger.warn(">> no more ip addresses available, looking for one to re-use"); |
| 71 | for (PublicIpAddress existingIp : client.getPublicIpsAssociatedWithVDC(vApp.getVDC().getHref())) { |
| 72 | Set<InternetService> services = client.getInternetServicesOnPublicIp(existingIp.getId()); |
| 73 | if (services.size() == 0) { |
| 74 | ip = existingIp; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (ip == null) |
| 79 | throw e; |
| 80 | |
| 81 | } |
| 82 | is = client.addInternetServiceToExistingIp(ip.getId(), vApp.getName() + "-" + port, protocol, port, |
| 83 | withDescription(String.format("port %d access to serverId: %s name: %s", port, vApp.getName(), vApp |
| 84 | .getName()))); |
| 85 | Map<InternetService, PublicIpAddress> result = ImmutableMap.<InternetService, PublicIpAddress> of(is, ip); |
| 86 | Entry<InternetService, PublicIpAddress> entry = Iterables.getOnlyElement(result.entrySet()); |
| 87 | return entry; |
| 88 | } |
| 89 | } |