| 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.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.NoSuchElementException; |
| 26 | |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.http.RequiresHttp; |
| 30 | import org.jclouds.rest.ConfiguresRestClient; |
| 31 | import org.jclouds.rest.ResourceNotFoundException; |
| 32 | import org.jclouds.vcloud.VCloudExpressAsyncClient; |
| 33 | import org.jclouds.vcloud.VCloudExpressClient; |
| 34 | import org.jclouds.vcloud.domain.ReferenceType; |
| 35 | import org.jclouds.vcloud.terremark.TerremarkECloudAsyncClient; |
| 36 | import org.jclouds.vcloud.terremark.TerremarkECloudClient; |
| 37 | import org.jclouds.vcloud.terremark.TerremarkVCloudAsyncClient; |
| 38 | import org.jclouds.vcloud.terremark.TerremarkVCloudClient; |
| 39 | import org.jclouds.vcloud.terremark.domain.TerremarkNetwork; |
| 40 | import org.jclouds.vcloud.terremark.domain.TerremarkOrgNetwork; |
| 41 | |
| 42 | import com.google.common.base.Predicate; |
| 43 | import com.google.common.collect.Iterables; |
| 44 | import com.google.inject.Injector; |
| 45 | import com.google.inject.Provides; |
| 46 | |
| 47 | /** |
| 48 | * Configures the VCloud authentication service connection, including logging and http transport. |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @RequiresHttp |
| 53 | @ConfiguresRestClient |
| 54 | public class TerremarkECloudRestClientModule extends |
| 55 | TerremarkRestClientModule<TerremarkECloudClient, TerremarkECloudAsyncClient> { |
| 56 | |
| 57 | public TerremarkECloudRestClientModule() { |
| 58 | super(TerremarkECloudClient.class, TerremarkECloudAsyncClient.class); |
| 59 | } |
| 60 | |
| 61 | @Provides |
| 62 | @Singleton |
| 63 | protected VCloudExpressAsyncClient provideVCloudAsyncClient(TerremarkECloudAsyncClient in) { |
| 64 | return in; |
| 65 | } |
| 66 | |
| 67 | @Provides |
| 68 | @Singleton |
| 69 | protected VCloudExpressClient provideVCloudClient(TerremarkECloudClient in) { |
| 70 | return in; |
| 71 | } |
| 72 | |
| 73 | @Provides |
| 74 | @Singleton |
| 75 | protected TerremarkVCloudAsyncClient provideTerremarkAsyncClient(TerremarkECloudAsyncClient in) { |
| 76 | return in; |
| 77 | } |
| 78 | |
| 79 | @Provides |
| 80 | @Singleton |
| 81 | protected TerremarkVCloudClient provideTerremarkClient(TerremarkECloudClient in) { |
| 82 | return in; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected URI findDefaultNetworkForVDC(org.jclouds.vcloud.domain.VDC vDC, Map<String, ReferenceType> networks, |
| 87 | final Injector injector) { |
| 88 | // TODO FIXME XXX: In Terremark Enterprise environment with multiple VDC's this does not |
| 89 | // work well. |
| 90 | // Each VDC will have differnt network subnets. So we cannot assume the default VDC's |
| 91 | // networks will |
| 92 | // work with non-default VDC's. So make PROPERTY_VCLOUD_DEFAULT_NETWORK optional. If |
| 93 | // this property |
| 94 | // is not set, they are expected to add NetworkConfig to the options when launching a |
| 95 | // server. |
| 96 | logger.warn("default network for vdc %s not set", vDC.getName()); |
| 97 | try { |
| 98 | return Iterables.find(networks.values(), new Predicate<ReferenceType>() { |
| 99 | |
| 100 | @Override |
| 101 | public boolean apply(ReferenceType input) { |
| 102 | TerremarkOrgNetwork network = injector.getInstance(TerremarkECloudClient.class).getNetwork( |
| 103 | input.getHref()); |
| 104 | TerremarkNetwork terremarkNetwork = injector.getInstance(TerremarkECloudClient.class) |
| 105 | .getTerremarkNetwork( |
| 106 | checkNotNull(checkNotNull(network, "network at: " + input).getNetworkExtension(), |
| 107 | "network extension for: " + input).getHref()); |
| 108 | return checkNotNull(terremarkNetwork, "terremark network extension at: " + network.getNetworkExtension()) |
| 109 | .getNetworkType() == TerremarkNetwork.Type.DMZ; |
| 110 | } |
| 111 | |
| 112 | }).getHref(); |
| 113 | } catch (NoSuchElementException e) { |
| 114 | throw new ResourceNotFoundException("no dmz networks in vdc " + vDC.getName() + ": " + networks); |
| 115 | } |
| 116 | } |
| 117 | } |