| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.trmk.ecloud.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.http.RequiresHttp; |
| 29 | import org.jclouds.rest.ConfiguresRestClient; |
| 30 | import org.jclouds.trmk.ecloud.TerremarkECloudAsyncClient; |
| 31 | import org.jclouds.trmk.ecloud.TerremarkECloudClient; |
| 32 | import org.jclouds.trmk.ecloud.features.DataCenterOperationsAsyncClient; |
| 33 | import org.jclouds.trmk.ecloud.features.DataCenterOperationsClient; |
| 34 | import org.jclouds.trmk.ecloud.features.TagOperationsAsyncClient; |
| 35 | import org.jclouds.trmk.ecloud.features.TagOperationsClient; |
| 36 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudAsyncClient; |
| 37 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient; |
| 38 | import org.jclouds.trmk.vcloud_0_8.config.DefaultVCloudReferencesModule; |
| 39 | import org.jclouds.trmk.vcloud_0_8.config.TerremarkVCloudRestClientModule; |
| 40 | import org.jclouds.trmk.vcloud_0_8.domain.Network; |
| 41 | import org.jclouds.trmk.vcloud_0_8.domain.NetworkExtendedInfo; |
| 42 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 43 | |
| 44 | import com.google.common.base.Predicate; |
| 45 | import com.google.common.collect.ImmutableMap; |
| 46 | import com.google.inject.Injector; |
| 47 | import com.google.inject.Provides; |
| 48 | |
| 49 | /** |
| 50 | * Configures the VCloud authentication service connection, including logging |
| 51 | * and http transport. |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @RequiresHttp |
| 56 | @ConfiguresRestClient |
| 57 | public class TerremarkECloudRestClientModule extends |
| 58 | TerremarkVCloudRestClientModule<TerremarkECloudClient, TerremarkECloudAsyncClient> { |
| 59 | |
| 60 | public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()// |
| 61 | .put(DataCenterOperationsClient.class, DataCenterOperationsAsyncClient.class)// |
| 62 | .put(TagOperationsClient.class, TagOperationsAsyncClient.class)// |
| 63 | .build(); |
| 64 | |
| 65 | public TerremarkECloudRestClientModule() { |
| 66 | super(TerremarkECloudClient.class, TerremarkECloudAsyncClient.class, DELEGATE_MAP); |
| 67 | } |
| 68 | |
| 69 | @Provides |
| 70 | @Singleton |
| 71 | protected TerremarkVCloudAsyncClient provideTerremarkAsyncClient(TerremarkECloudAsyncClient in) { |
| 72 | return in; |
| 73 | } |
| 74 | |
| 75 | @Provides |
| 76 | @Singleton |
| 77 | protected TerremarkVCloudClient provideTerremarkClient(TerremarkECloudClient in) { |
| 78 | return in; |
| 79 | } |
| 80 | |
| 81 | @Singleton |
| 82 | public static class IsDMZNetwork implements Predicate<ReferenceType> { |
| 83 | private final TerremarkECloudClient client; |
| 84 | |
| 85 | @Inject |
| 86 | public IsDMZNetwork(TerremarkECloudClient client) { |
| 87 | this.client = client; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean apply(ReferenceType arg0) { |
| 92 | // TODO FIXME XXX: In Terremark Enterprise environment with multiple |
| 93 | // VDC's |
| 94 | // this does not |
| 95 | // work well. |
| 96 | // Each VDC will have differnt network subnets. So we cannot assume the |
| 97 | // default VDC's |
| 98 | // networks will |
| 99 | // work with non-default VDC's. So make PROPERTY_VCLOUD_DEFAULT_NETWORK |
| 100 | // optional. If |
| 101 | // this property |
| 102 | // is not set, they are expected to add NetworkConfig to the options |
| 103 | // when |
| 104 | // launching a |
| 105 | // server. |
| 106 | Network orgNetwork = client.getNetwork(arg0.getHref()); |
| 107 | NetworkExtendedInfo terremarkNetwork = client.getNetworkExtendedInfo(checkNotNull( |
| 108 | checkNotNull(orgNetwork, "network at: " + arg0).getNetworkExtension(), "network extension for: " + arg0) |
| 109 | .getHref()); |
| 110 | return checkNotNull(terremarkNetwork, "terremark network extension at: " + orgNetwork.getNetworkExtension()) |
| 111 | .getNetworkType() == NetworkExtendedInfo.Type.DMZ; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void installDefaultVCloudEndpointsModule() { |
| 117 | install(new DefaultVCloudReferencesModule() { |
| 118 | |
| 119 | @Override |
| 120 | protected Predicate<ReferenceType> provideDefaultNetworkSelector(Injector i) { |
| 121 | return i.getInstance(IsDMZNetwork.class); |
| 122 | } |
| 123 | |
| 124 | }); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | } |