| 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.vcloud.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | import static com.google.common.base.Throwables.propagate; |
| 24 | import static com.google.common.collect.Iterables.concat; |
| 25 | import static com.google.common.collect.Iterables.getLast; |
| 26 | import static com.google.common.collect.Iterables.transform; |
| 27 | import static com.google.common.collect.Maps.transformValues; |
| 28 | import static com.google.common.collect.Maps.uniqueIndex; |
| 29 | import static org.jclouds.Constants.PROPERTY_API_VERSION; |
| 30 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 31 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_TIMEOUT_TASK_COMPLETED; |
| 32 | |
| 33 | import java.net.URI; |
| 34 | import java.util.Map; |
| 35 | import java.util.Map.Entry; |
| 36 | import java.util.SortedMap; |
| 37 | import java.util.concurrent.ExecutionException; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | import java.util.concurrent.TimeoutException; |
| 40 | import java.util.concurrent.atomic.AtomicReference; |
| 41 | |
| 42 | import javax.inject.Inject; |
| 43 | import javax.inject.Named; |
| 44 | import javax.inject.Singleton; |
| 45 | |
| 46 | import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler; |
| 47 | import org.jclouds.domain.Location; |
| 48 | import org.jclouds.http.HttpErrorHandler; |
| 49 | import org.jclouds.http.RequiresHttp; |
| 50 | import org.jclouds.http.annotation.ClientError; |
| 51 | import org.jclouds.http.annotation.Redirection; |
| 52 | import org.jclouds.http.annotation.ServerError; |
| 53 | import org.jclouds.predicates.RetryablePredicate; |
| 54 | import org.jclouds.rest.AsyncClientFactory; |
| 55 | import org.jclouds.rest.AuthorizationException; |
| 56 | import org.jclouds.rest.ConfiguresRestClient; |
| 57 | import org.jclouds.rest.config.RestClientModule; |
| 58 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 59 | import org.jclouds.vcloud.VCloudAsyncClient; |
| 60 | import org.jclouds.vcloud.VCloudClient; |
| 61 | import org.jclouds.vcloud.VCloudToken; |
| 62 | import org.jclouds.vcloud.VCloudVersionsAsyncClient; |
| 63 | import org.jclouds.vcloud.compute.functions.FindLocationForResource; |
| 64 | import org.jclouds.vcloud.domain.Catalog; |
| 65 | import org.jclouds.vcloud.domain.CatalogItem; |
| 66 | import org.jclouds.vcloud.domain.Org; |
| 67 | import org.jclouds.vcloud.domain.ReferenceType; |
| 68 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 69 | import org.jclouds.vcloud.domain.VCloudSession; |
| 70 | import org.jclouds.vcloud.domain.VDC; |
| 71 | import org.jclouds.vcloud.endpoints.OrgList; |
| 72 | import org.jclouds.vcloud.features.CatalogAsyncClient; |
| 73 | import org.jclouds.vcloud.features.CatalogClient; |
| 74 | import org.jclouds.vcloud.features.NetworkAsyncClient; |
| 75 | import org.jclouds.vcloud.features.NetworkClient; |
| 76 | import org.jclouds.vcloud.features.OrgAsyncClient; |
| 77 | import org.jclouds.vcloud.features.OrgClient; |
| 78 | import org.jclouds.vcloud.features.TaskAsyncClient; |
| 79 | import org.jclouds.vcloud.features.TaskClient; |
| 80 | import org.jclouds.vcloud.features.VAppAsyncClient; |
| 81 | import org.jclouds.vcloud.features.VAppClient; |
| 82 | import org.jclouds.vcloud.features.VAppTemplateAsyncClient; |
| 83 | import org.jclouds.vcloud.features.VAppTemplateClient; |
| 84 | import org.jclouds.vcloud.features.VDCAsyncClient; |
| 85 | import org.jclouds.vcloud.features.VDCClient; |
| 86 | import org.jclouds.vcloud.features.VmAsyncClient; |
| 87 | import org.jclouds.vcloud.features.VmClient; |
| 88 | import org.jclouds.vcloud.functions.AllCatalogItemsInCatalog; |
| 89 | import org.jclouds.vcloud.functions.AllCatalogItemsInOrg; |
| 90 | import org.jclouds.vcloud.functions.AllCatalogsInOrg; |
| 91 | import org.jclouds.vcloud.functions.AllVDCsInOrg; |
| 92 | import org.jclouds.vcloud.functions.OrgsForLocations; |
| 93 | import org.jclouds.vcloud.functions.OrgsForNames; |
| 94 | import org.jclouds.vcloud.functions.VAppTemplatesForCatalogItems; |
| 95 | import org.jclouds.vcloud.handlers.ParseVCloudErrorFromHttpResponse; |
| 96 | import org.jclouds.vcloud.internal.VCloudLoginAsyncClient; |
| 97 | import org.jclouds.vcloud.predicates.TaskSuccess; |
| 98 | import org.jclouds.vcloud.xml.ovf.VCloudResourceAllocationSettingDataHandler; |
| 99 | |
| 100 | import com.google.common.base.Function; |
| 101 | import com.google.common.base.Predicate; |
| 102 | import static com.google.common.base.Predicates.*; |
| 103 | import com.google.common.base.Supplier; |
| 104 | import static com.google.common.base.Suppliers.*; |
| 105 | import com.google.common.collect.ImmutableMap; |
| 106 | import com.google.common.collect.ImmutableMap.Builder; |
| 107 | import static com.google.common.collect.Iterables.*; |
| 108 | import com.google.inject.Injector; |
| 109 | import com.google.inject.Provides; |
| 110 | import com.google.inject.TypeLiteral; |
| 111 | |
| 112 | /** |
| 113 | * Configures the VCloud authentication service connection, including logging |
| 114 | * and http transport. |
| 115 | * |
| 116 | * @author Adrian Cole |
| 117 | */ |
| 118 | @RequiresHttp |
| 119 | @ConfiguresRestClient |
| 120 | public class VCloudRestClientModule extends RestClientModule<VCloudClient, VCloudAsyncClient> { |
| 121 | |
| 122 | public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()// |
| 123 | .put(VAppTemplateClient.class, VAppTemplateAsyncClient.class)// |
| 124 | .put(VAppClient.class, VAppAsyncClient.class)// |
| 125 | .put(VmClient.class, VmAsyncClient.class)// |
| 126 | .put(CatalogClient.class, CatalogAsyncClient.class)// |
| 127 | .put(TaskClient.class, TaskAsyncClient.class)// |
| 128 | .put(VDCClient.class, VDCAsyncClient.class)// |
| 129 | .put(NetworkClient.class, NetworkAsyncClient.class)// |
| 130 | .put(OrgClient.class, OrgAsyncClient.class)// |
| 131 | .build(); |
| 132 | |
| 133 | public VCloudRestClientModule() { |
| 134 | super(VCloudClient.class, VCloudAsyncClient.class, DELEGATE_MAP); |
| 135 | } |
| 136 | |
| 137 | @Provides |
| 138 | @Singleton |
| 139 | protected VCloudLoginAsyncClient provideVCloudLogin(AsyncClientFactory factory) { |
| 140 | return factory.create(VCloudLoginAsyncClient.class); |
| 141 | } |
| 142 | |
| 143 | @Provides |
| 144 | @Singleton |
| 145 | protected Supplier<VCloudSession> provideVCloudTokenCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 146 | AtomicReference<AuthorizationException> authException, final VCloudLoginAsyncClient login) { |
| 147 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<VCloudSession>(authException, seconds, |
| 148 | new Supplier<VCloudSession>() { |
| 149 | |
| 150 | @Override |
| 151 | public VCloudSession get() { |
| 152 | try { |
| 153 | return login.login().get(10, TimeUnit.SECONDS); |
| 154 | } catch (Exception e) { |
| 155 | propagate(e); |
| 156 | assert false : e; |
| 157 | return null; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected void configure() { |
| 166 | super.configure(); |
| 167 | bind(new TypeLiteral<Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>>>() { |
| 168 | }).to(new TypeLiteral<VAppTemplatesForCatalogItems>() { |
| 169 | }); |
| 170 | bind(ResourceAllocationSettingDataHandler.class).to(VCloudResourceAllocationSettingDataHandler.class); |
| 171 | // Ensures we don't retry on authorization failures |
| 172 | bind(new TypeLiteral<AtomicReference<AuthorizationException>>() { |
| 173 | }).toInstance(new AtomicReference<AuthorizationException>()); |
| 174 | installDefaultVCloudEndpointsModule(); |
| 175 | bind(new TypeLiteral<Function<ReferenceType, Location>>() { |
| 176 | }).to(new TypeLiteral<FindLocationForResource>() { |
| 177 | }); |
| 178 | bind(new TypeLiteral<Function<Org, Iterable<? extends Catalog>>>() { |
| 179 | }).to(new TypeLiteral<AllCatalogsInOrg>() { |
| 180 | }); |
| 181 | bind(new TypeLiteral<Function<Org, Iterable<? extends VDC>>>() { |
| 182 | }).to(new TypeLiteral<AllVDCsInOrg>() { |
| 183 | }); |
| 184 | bind(new TypeLiteral<Function<Iterable<String>, Iterable<? extends Org>>>() { |
| 185 | }).to(new TypeLiteral<OrgsForNames>() { |
| 186 | }); |
| 187 | bind(new TypeLiteral<Function<Iterable<? extends Location>, Iterable<? extends Org>>>() { |
| 188 | }).to(new TypeLiteral<OrgsForLocations>() { |
| 189 | }); |
| 190 | bind(new TypeLiteral<Function<Catalog, Iterable<? extends CatalogItem>>>() { |
| 191 | }).to(new TypeLiteral<AllCatalogItemsInCatalog>() { |
| 192 | }); |
| 193 | bind(new TypeLiteral<Function<Org, Iterable<? extends CatalogItem>>>() { |
| 194 | }).to(new TypeLiteral<AllCatalogItemsInOrg>() { |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | @Provides |
| 199 | @Singleton |
| 200 | @org.jclouds.vcloud.endpoints.VDC |
| 201 | protected Supplier<Map<String, String>> provideVDCtoORG(Supplier<Map<String, ? extends Org>> orgNameToOrgSuppier) { |
| 202 | return compose(new Function<Map<String, ? extends Org>, Map<String, String>>() { |
| 203 | |
| 204 | @Override |
| 205 | public Map<String, String> apply(Map<String, ? extends Org> arg0) { |
| 206 | Builder<String, String> returnVal = ImmutableMap.<String, String> builder(); |
| 207 | for (Entry<String, ? extends Org> orgr : arg0.entrySet()) { |
| 208 | for (String vdc : orgr.getValue().getVDCs().keySet()) { |
| 209 | returnVal.put(vdc, orgr.getKey()); |
| 210 | } |
| 211 | } |
| 212 | return returnVal.build(); |
| 213 | } |
| 214 | }, orgNameToOrgSuppier); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | @Provides |
| 219 | @Singleton |
| 220 | protected Supplier<Map<String, ? extends Org>> provideOrgMapCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 221 | AtomicReference<AuthorizationException> authException, OrgMapSupplier supplier) { |
| 222 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, ? extends Org>>( |
| 223 | authException, seconds, supplier); |
| 224 | } |
| 225 | |
| 226 | @Provides |
| 227 | @Singleton |
| 228 | @OrgList |
| 229 | URI provideOrgListURI(Supplier<VCloudSession> sessionSupplier) { |
| 230 | VCloudSession session = sessionSupplier.get(); |
| 231 | return URI.create(getLast(session.getOrgs().values()).getHref().toASCIIString().replaceAll("org/.*", "org")); |
| 232 | } |
| 233 | |
| 234 | @Singleton |
| 235 | public static class OrgMapSupplier implements Supplier<Map<String, ? extends Org>> { |
| 236 | protected final Supplier<VCloudSession> sessionSupplier; |
| 237 | protected final Function<Iterable<String>, Iterable<? extends Org>> organizationsForNames; |
| 238 | |
| 239 | @Inject |
| 240 | protected OrgMapSupplier(Supplier<VCloudSession> sessionSupplier, |
| 241 | Function<Iterable<String>, Iterable<? extends Org>> organizationsForNames) { |
| 242 | this.sessionSupplier = sessionSupplier; |
| 243 | this.organizationsForNames = organizationsForNames; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public Map<String, ? extends Org> get() { |
| 248 | return uniqueIndex(organizationsForNames.apply(sessionSupplier.get().getOrgs().keySet()), name); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | protected void installDefaultVCloudEndpointsModule() { |
| 253 | install(new DefaultVCloudReferencesModule()); |
| 254 | } |
| 255 | |
| 256 | @Singleton |
| 257 | public static class OrgCatalogSupplier implements |
| 258 | Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>> { |
| 259 | protected final Supplier<Map<String, ? extends Org>> orgSupplier; |
| 260 | protected final Function<Org, Iterable<? extends org.jclouds.vcloud.domain.Catalog>> allCatalogsInOrg; |
| 261 | |
| 262 | @Inject |
| 263 | protected OrgCatalogSupplier(Supplier<Map<String, ? extends Org>> orgSupplier, |
| 264 | Function<Org, Iterable<? extends org.jclouds.vcloud.domain.Catalog>> allCatalogsInOrg) { |
| 265 | this.orgSupplier = orgSupplier; |
| 266 | this.allCatalogsInOrg = allCatalogsInOrg; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>> get() { |
| 271 | return transformValues( |
| 272 | transformValues(orgSupplier.get(), allCatalogsInOrg), |
| 273 | new Function<Iterable<? extends org.jclouds.vcloud.domain.Catalog>, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>() { |
| 274 | |
| 275 | @Override |
| 276 | public Map<String, ? extends org.jclouds.vcloud.domain.Catalog> apply( |
| 277 | Iterable<? extends org.jclouds.vcloud.domain.Catalog> from) { |
| 278 | return uniqueIndex(from, name); |
| 279 | } |
| 280 | |
| 281 | }); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | @VCloudToken |
| 286 | @Provides |
| 287 | String provideVCloudToken(Supplier<VCloudSession> cache) { |
| 288 | return checkNotNull(cache.get().getVCloudToken(), "No token present in session"); |
| 289 | } |
| 290 | |
| 291 | @Provides |
| 292 | @org.jclouds.vcloud.endpoints.Org |
| 293 | @Singleton |
| 294 | protected Supplier<Map<String, ReferenceType>> provideVDCtoORG(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 295 | AtomicReference<AuthorizationException> authException, OrgNameToOrgSupplier supplier) { |
| 296 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, ReferenceType>>( |
| 297 | authException, seconds, supplier); |
| 298 | } |
| 299 | |
| 300 | @Provides |
| 301 | @Singleton |
| 302 | protected Supplier<Map<URI, ? extends org.jclouds.vcloud.domain.VDC>> provideURIToVDC( |
| 303 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, AtomicReference<AuthorizationException> authException, |
| 304 | URItoVDC supplier) { |
| 305 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<URI, ? extends org.jclouds.vcloud.domain.VDC>>( |
| 306 | authException, seconds, supplier); |
| 307 | } |
| 308 | |
| 309 | @Singleton |
| 310 | public static class URItoVDC implements Supplier<Map<URI, ? extends org.jclouds.vcloud.domain.VDC>> { |
| 311 | private final Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>> orgVDCMap; |
| 312 | |
| 313 | @Inject |
| 314 | URItoVDC(Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>> orgVDCMap) { |
| 315 | this.orgVDCMap = orgVDCMap; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public Map<URI, ? extends org.jclouds.vcloud.domain.VDC> get() { |
| 320 | return uniqueIndex( |
| 321 | concat(transform( |
| 322 | orgVDCMap.get().values(), |
| 323 | new Function<Map<String, ? extends org.jclouds.vcloud.domain.VDC>, Iterable<? extends org.jclouds.vcloud.domain.VDC>>() { |
| 324 | |
| 325 | @Override |
| 326 | public Iterable<? extends org.jclouds.vcloud.domain.VDC> apply( |
| 327 | Map<String, ? extends org.jclouds.vcloud.domain.VDC> from) { |
| 328 | return from.values(); |
| 329 | } |
| 330 | |
| 331 | })), new Function<org.jclouds.vcloud.domain.VDC, URI>() { |
| 332 | |
| 333 | @Override |
| 334 | public URI apply(org.jclouds.vcloud.domain.VDC from) { |
| 335 | return from.getHref(); |
| 336 | } |
| 337 | |
| 338 | }); |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
| 343 | final static Function<ReferenceType, String> name = new Function<ReferenceType, String>() { |
| 344 | |
| 345 | @Override |
| 346 | public String apply(ReferenceType from) { |
| 347 | return from.getName(); |
| 348 | } |
| 349 | |
| 350 | }; |
| 351 | |
| 352 | @Provides |
| 353 | @Singleton |
| 354 | @org.jclouds.vcloud.endpoints.VCloudLogin |
| 355 | protected URI provideAuthenticationURI(VCloudVersionsAsyncClient versionService, |
| 356 | @Named(PROPERTY_API_VERSION) String version) throws InterruptedException, ExecutionException, TimeoutException { |
| 357 | SortedMap<String, URI> versions = versionService.getSupportedVersions().get(180, TimeUnit.SECONDS); |
| 358 | checkState(versions.size() > 0, "No versions present"); |
| 359 | checkState(versions.containsKey(version), "version " + version + " not present in: " + versions); |
| 360 | return versions.get(version); |
| 361 | } |
| 362 | |
| 363 | @Singleton |
| 364 | private static class OrgNameToOrgSupplier implements Supplier<Map<String, ReferenceType>> { |
| 365 | private final Supplier<VCloudSession> sessionSupplier; |
| 366 | |
| 367 | @SuppressWarnings("unused") |
| 368 | @Inject |
| 369 | OrgNameToOrgSupplier(Supplier<VCloudSession> sessionSupplier) { |
| 370 | this.sessionSupplier = sessionSupplier; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public Map<String, ReferenceType> get() { |
| 375 | return sessionSupplier.get().getOrgs(); |
| 376 | } |
| 377 | |
| 378 | } |
| 379 | |
| 380 | @Provides |
| 381 | @Singleton |
| 382 | protected VCloudVersionsAsyncClient provideVCloudVersions(AsyncClientFactory factory) { |
| 383 | return factory.create(VCloudVersionsAsyncClient.class); |
| 384 | } |
| 385 | |
| 386 | @Provides |
| 387 | @Singleton |
| 388 | protected Org provideOrg(Supplier<Map<String, ? extends Org>> orgSupplier, |
| 389 | @org.jclouds.vcloud.endpoints.Org ReferenceType defaultOrg) { |
| 390 | return orgSupplier.get().get(defaultOrg.getName()); |
| 391 | } |
| 392 | |
| 393 | @Provides |
| 394 | @Singleton |
| 395 | protected Predicate<URI> successTester(Injector injector, |
| 396 | @Named(PROPERTY_VCLOUD_TIMEOUT_TASK_COMPLETED) long completed) { |
| 397 | return new RetryablePredicate<URI>(injector.getInstance(TaskSuccess.class), completed); |
| 398 | } |
| 399 | |
| 400 | @Provides |
| 401 | @Singleton |
| 402 | protected Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>> provideOrgCatalogItemMapSupplierCache( |
| 403 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, AtomicReference<AuthorizationException> authException, |
| 404 | OrgCatalogSupplier supplier) { |
| 405 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>>( |
| 406 | authException, seconds, supplier); |
| 407 | } |
| 408 | |
| 409 | @Provides |
| 410 | @Singleton |
| 411 | protected Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>> provideOrgVDCSupplierCache( |
| 412 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, AtomicReference<AuthorizationException> authException, |
| 413 | OrgVDCSupplier supplier) { |
| 414 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>>( |
| 415 | authException, seconds, supplier); |
| 416 | } |
| 417 | |
| 418 | @Singleton |
| 419 | public static class OrgVDCSupplier implements |
| 420 | Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>> { |
| 421 | protected final Supplier<Map<String, ? extends Org>> orgSupplier; |
| 422 | private final Function<Org, Iterable<? extends org.jclouds.vcloud.domain.VDC>> allVDCsInOrg; |
| 423 | |
| 424 | @Inject |
| 425 | protected OrgVDCSupplier(Supplier<Map<String, ? extends Org>> orgSupplier, |
| 426 | Function<Org, Iterable<? extends org.jclouds.vcloud.domain.VDC>> allVDCsInOrg) { |
| 427 | this.orgSupplier = orgSupplier; |
| 428 | this.allVDCsInOrg = allVDCsInOrg; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public Map<String, Map<String, ? extends org.jclouds.vcloud.domain.VDC>> get() { |
| 433 | return transformValues( |
| 434 | transformValues(orgSupplier.get(), allVDCsInOrg), |
| 435 | new Function<Iterable<? extends org.jclouds.vcloud.domain.VDC>, Map<String, ? extends org.jclouds.vcloud.domain.VDC>>() { |
| 436 | |
| 437 | @Override |
| 438 | public Map<String, ? extends org.jclouds.vcloud.domain.VDC> apply( |
| 439 | Iterable<? extends org.jclouds.vcloud.domain.VDC> from) { |
| 440 | return uniqueIndex(from, name); |
| 441 | } |
| 442 | |
| 443 | }); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | @Singleton |
| 448 | public static class OrgCatalogItemSupplier implements |
| 449 | Supplier<Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>> { |
| 450 | protected final Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>> catalogSupplier; |
| 451 | protected final Function<org.jclouds.vcloud.domain.Catalog, Iterable<? extends CatalogItem>> allCatalogItemsInCatalog; |
| 452 | |
| 453 | @Inject |
| 454 | protected OrgCatalogItemSupplier( |
| 455 | Supplier<Map<String, Map<String, ? extends org.jclouds.vcloud.domain.Catalog>>> catalogSupplier, |
| 456 | Function<org.jclouds.vcloud.domain.Catalog, Iterable<? extends CatalogItem>> allCatalogItemsInCatalog) { |
| 457 | this.catalogSupplier = catalogSupplier; |
| 458 | this.allCatalogItemsInCatalog = allCatalogItemsInCatalog; |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>> get() { |
| 463 | return transformValues( |
| 464 | catalogSupplier.get(), |
| 465 | new Function<Map<String, ? extends org.jclouds.vcloud.domain.Catalog>, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>() { |
| 466 | |
| 467 | @Override |
| 468 | public Map<String, Map<String, ? extends CatalogItem>> apply( |
| 469 | Map<String, ? extends org.jclouds.vcloud.domain.Catalog> from) { |
| 470 | return transformValues( |
| 471 | from, |
| 472 | new Function<org.jclouds.vcloud.domain.Catalog, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>() { |
| 473 | |
| 474 | @Override |
| 475 | public Map<String, ? extends CatalogItem> apply(org.jclouds.vcloud.domain.Catalog from) { |
| 476 | return uniqueIndex(filter(allCatalogItemsInCatalog.apply(from), notNull()), name); |
| 477 | } |
| 478 | }); |
| 479 | |
| 480 | } |
| 481 | }); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | @Provides |
| 486 | @Singleton |
| 487 | protected Supplier<Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>> provideOrgCatalogItemSupplierCache( |
| 488 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, AtomicReference<AuthorizationException> authException, |
| 489 | OrgCatalogItemSupplier supplier) { |
| 490 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, Map<String, Map<String, ? extends org.jclouds.vcloud.domain.CatalogItem>>>>( |
| 491 | authException, seconds, supplier); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | protected void bindErrorHandlers() { |
| 496 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseVCloudErrorFromHttpResponse.class); |
| 497 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseVCloudErrorFromHttpResponse.class); |
| 498 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseVCloudErrorFromHttpResponse.class); |
| 499 | } |
| 500 | } |