| 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.savvis.vpdc.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 org.jclouds.Constants.PROPERTY_IDENTITY; |
| 25 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 26 | import static org.jclouds.savvis.vpdc.reference.VPDCConstants.PROPERTY_VPDC_TIMEOUT_TASK_COMPLETED; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | import java.util.concurrent.atomic.AtomicReference; |
| 33 | |
| 34 | import javax.inject.Named; |
| 35 | import javax.inject.Singleton; |
| 36 | |
| 37 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 38 | import org.jclouds.http.HttpErrorHandler; |
| 39 | import org.jclouds.http.annotation.ClientError; |
| 40 | import org.jclouds.http.annotation.Redirection; |
| 41 | import org.jclouds.http.annotation.ServerError; |
| 42 | import org.jclouds.json.Json; |
| 43 | import org.jclouds.location.Provider; |
| 44 | import org.jclouds.predicates.RetryablePredicate; |
| 45 | import org.jclouds.rest.AsyncClientFactory; |
| 46 | import org.jclouds.rest.AuthorizationException; |
| 47 | import org.jclouds.rest.config.RestClientModule; |
| 48 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 49 | import org.jclouds.savvis.vpdc.VPDCAsyncClient; |
| 50 | import org.jclouds.savvis.vpdc.VPDCClient; |
| 51 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 52 | import org.jclouds.savvis.vpdc.domain.internal.VCloudSession; |
| 53 | import org.jclouds.savvis.vpdc.features.BrowsingAsyncClient; |
| 54 | import org.jclouds.savvis.vpdc.features.BrowsingClient; |
| 55 | import org.jclouds.savvis.vpdc.features.FirewallAsyncClient; |
| 56 | import org.jclouds.savvis.vpdc.features.FirewallClient; |
| 57 | import org.jclouds.savvis.vpdc.features.ServiceManagementAsyncClient; |
| 58 | import org.jclouds.savvis.vpdc.features.ServiceManagementClient; |
| 59 | import org.jclouds.savvis.vpdc.features.VMAsyncClient; |
| 60 | import org.jclouds.savvis.vpdc.features.VMClient; |
| 61 | import org.jclouds.savvis.vpdc.handlers.VPDCErrorHandler; |
| 62 | import org.jclouds.savvis.vpdc.internal.LoginAsyncClient; |
| 63 | import org.jclouds.savvis.vpdc.internal.VCloudToken; |
| 64 | import org.jclouds.savvis.vpdc.predicates.TaskSuccess; |
| 65 | import org.jclouds.util.Strings2; |
| 66 | |
| 67 | import com.google.common.base.Predicate; |
| 68 | import com.google.common.base.Supplier; |
| 69 | import com.google.common.collect.ImmutableMap; |
| 70 | import com.google.common.collect.Iterables; |
| 71 | import com.google.inject.Injector; |
| 72 | import com.google.inject.Provides; |
| 73 | import com.google.inject.TypeLiteral; |
| 74 | |
| 75 | /** |
| 76 | * |
| 77 | * @author Adrian Cole |
| 78 | * |
| 79 | */ |
| 80 | public class VPDCRestClientModule extends RestClientModule<VPDCClient, VPDCAsyncClient> { |
| 81 | @Provides |
| 82 | @Singleton |
| 83 | protected LoginAsyncClient provideVCloudVersions(AsyncClientFactory factory) { |
| 84 | return factory.create(LoginAsyncClient.class); |
| 85 | } |
| 86 | |
| 87 | @VCloudToken |
| 88 | @Provides |
| 89 | String provideVCloudToken(Supplier<VCloudSession> cache) { |
| 90 | return checkNotNull(cache.get().getVCloudToken(), "No token present in session"); |
| 91 | } |
| 92 | |
| 93 | @Provides |
| 94 | @org.jclouds.savvis.vpdc.internal.Org |
| 95 | @Singleton |
| 96 | protected Set<org.jclouds.savvis.vpdc.domain.Resource> provideOrgs(Supplier<VCloudSession> cache, |
| 97 | @Named(PROPERTY_IDENTITY) String user) { |
| 98 | VCloudSession discovery = cache.get(); |
| 99 | checkState(discovery.getOrgs().size() > 0, "No orgs present for user: " + user); |
| 100 | return discovery.getOrgs(); |
| 101 | } |
| 102 | |
| 103 | @Provides |
| 104 | @org.jclouds.savvis.vpdc.internal.Org |
| 105 | @Singleton |
| 106 | protected String provideDefaultOrgId(@org.jclouds.savvis.vpdc.internal.Org Set<Resource> orgs) { |
| 107 | return Iterables.get(orgs, 0).getId(); |
| 108 | } |
| 109 | |
| 110 | protected AtomicReference<AuthorizationException> authException = new AtomicReference<AuthorizationException>(); |
| 111 | |
| 112 | @Provides |
| 113 | @Singleton |
| 114 | protected Predicate<String> successTester(Injector injector, |
| 115 | @Named(PROPERTY_VPDC_TIMEOUT_TASK_COMPLETED) long completed) { |
| 116 | return new RetryablePredicate<String>(injector.getInstance(TaskSuccess.class), completed); |
| 117 | } |
| 118 | |
| 119 | public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()// |
| 120 | .put(BrowsingClient.class, BrowsingAsyncClient.class)// |
| 121 | .put(VMClient.class, VMAsyncClient.class)// |
| 122 | .put(FirewallClient.class, FirewallAsyncClient.class)// |
| 123 | .put(ServiceManagementClient.class, ServiceManagementAsyncClient.class)// |
| 124 | .build(); |
| 125 | |
| 126 | public VPDCRestClientModule() { |
| 127 | super(VPDCClient.class, VPDCAsyncClient.class, DELEGATE_MAP); |
| 128 | } |
| 129 | |
| 130 | @Singleton |
| 131 | @Provides |
| 132 | protected Set<CIMOperatingSystem> provideOperatingSystems(Json json, @Provider String providerName) |
| 133 | throws IOException { |
| 134 | return json.fromJson(Strings2.toStringAndClose(getClass().getResourceAsStream( |
| 135 | "/" + providerName + "/predefined_operatingsystems.json")), new TypeLiteral<Set<CIMOperatingSystem>>() { |
| 136 | }.getType()); |
| 137 | } |
| 138 | |
| 139 | @Provides |
| 140 | @Singleton |
| 141 | protected Supplier<VCloudSession> provideVCloudTokenCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 142 | final LoginAsyncClient login) { |
| 143 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<VCloudSession>(authException, seconds, |
| 144 | new Supplier<VCloudSession>() { |
| 145 | |
| 146 | @Override |
| 147 | public VCloudSession get() { |
| 148 | try { |
| 149 | return login.login().get(10, TimeUnit.SECONDS); |
| 150 | } catch (Exception e) { |
| 151 | propagate(e); |
| 152 | assert false : e; |
| 153 | return null; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | protected void bindErrorHandlers() { |
| 162 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(VPDCErrorHandler.class); |
| 163 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(VPDCErrorHandler.class); |
| 164 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(VPDCErrorHandler.class); |
| 165 | } |
| 166 | |
| 167 | } |