| 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.cloudloadbalancers.config; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.cloudloadbalancers.CloudLoadBalancersAsyncClient; |
| 30 | import org.jclouds.cloudloadbalancers.CloudLoadBalancersClient; |
| 31 | import org.jclouds.cloudloadbalancers.features.LoadBalancerAsyncClient; |
| 32 | import org.jclouds.cloudloadbalancers.features.LoadBalancerClient; |
| 33 | import org.jclouds.cloudloadbalancers.handlers.ParseCloudLoadBalancersErrorFromHttpResponse; |
| 34 | import org.jclouds.cloudloadbalancers.reference.RackspaceConstants; |
| 35 | import org.jclouds.http.HttpErrorHandler; |
| 36 | import org.jclouds.http.RequiresHttp; |
| 37 | import org.jclouds.http.annotation.ClientError; |
| 38 | import org.jclouds.http.annotation.Redirection; |
| 39 | import org.jclouds.http.annotation.ServerError; |
| 40 | import org.jclouds.json.config.GsonModule.DateAdapter; |
| 41 | import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; |
| 42 | import org.jclouds.location.Region; |
| 43 | import org.jclouds.location.config.ProvideRegionToURIViaProperties; |
| 44 | import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; |
| 45 | import org.jclouds.openstack.config.OpenStackAuthenticationModule; |
| 46 | import org.jclouds.openstack.reference.AuthHeaders; |
| 47 | import org.jclouds.rest.ConfiguresRestClient; |
| 48 | import org.jclouds.rest.config.RestClientModule; |
| 49 | |
| 50 | import com.google.common.base.Supplier; |
| 51 | import com.google.common.collect.ImmutableMap; |
| 52 | import com.google.common.collect.ImmutableSet; |
| 53 | import com.google.common.collect.Multimap; |
| 54 | import com.google.inject.Injector; |
| 55 | import com.google.inject.Provides; |
| 56 | import com.google.inject.Scopes; |
| 57 | import com.google.inject.TypeLiteral; |
| 58 | |
| 59 | /** |
| 60 | * Configures theRackspace Cloud Load Balancers connection. |
| 61 | * |
| 62 | * @author Adrian Cole |
| 63 | */ |
| 64 | @RequiresHttp |
| 65 | @ConfiguresRestClient |
| 66 | public class CloudLoadBalancersRestClientModule extends |
| 67 | RestClientModule<CloudLoadBalancersClient, CloudLoadBalancersAsyncClient> { |
| 68 | |
| 69 | public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()// |
| 70 | .put(LoadBalancerClient.class, LoadBalancerAsyncClient.class)// |
| 71 | .build(); |
| 72 | |
| 73 | public CloudLoadBalancersRestClientModule() { |
| 74 | super(CloudLoadBalancersClient.class, CloudLoadBalancersAsyncClient.class, DELEGATE_MAP); |
| 75 | } |
| 76 | |
| 77 | protected void bindRegionsToProvider() { |
| 78 | bindRegionsToProvider(ProvideRegionToURIViaPropertiesWithAccountID.class); |
| 79 | } |
| 80 | |
| 81 | @Singleton |
| 82 | public static class ProvideRegionToURIViaPropertiesWithAccountID extends ProvideRegionToURIViaProperties { |
| 83 | |
| 84 | @Inject |
| 85 | protected ProvideRegionToURIViaPropertiesWithAccountID(Injector injector, |
| 86 | @Named("CONSTANTS") Multimap<String, String> constants, |
| 87 | @Named(RackspaceConstants.PROPERTY_ACCOUNT_ID) String accountID) { |
| 88 | super(injector, constants); |
| 89 | constants.replaceValues(RackspaceConstants.PROPERTY_ACCOUNT_ID, ImmutableSet.of(accountID)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | protected void bindRegionsToProvider(Class<? extends javax.inject.Provider<Map<String, URI>>> providerClass) { |
| 94 | bind(new TypeLiteral<Map<String, URI>>() { |
| 95 | }).annotatedWith(Region.class).toProvider(providerClass).in(Scopes.SINGLETON); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void configure() { |
| 100 | install(new OpenStackAuthenticationModule()); |
| 101 | bind(DateAdapter.class).to(Iso8601DateAdapter.class); |
| 102 | bindRegionsToProvider(); |
| 103 | super.configure(); |
| 104 | } |
| 105 | |
| 106 | @Provides |
| 107 | @Singleton |
| 108 | @Named(RackspaceConstants.PROPERTY_ACCOUNT_ID) |
| 109 | protected String accountID(Supplier<AuthenticationResponse> in) { |
| 110 | URI serverURL = in.get().getServices().get(AuthHeaders.SERVER_MANAGEMENT_URL); |
| 111 | return serverURL.getPath().substring(serverURL.getPath().lastIndexOf('/') + 1); |
| 112 | } |
| 113 | |
| 114 | @Provides |
| 115 | @Singleton |
| 116 | @Region |
| 117 | public Set<String> regions(@Region Map<String, URI> endpoints) { |
| 118 | return endpoints.keySet(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void bindErrorHandlers() { |
| 123 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to( |
| 124 | ParseCloudLoadBalancersErrorFromHttpResponse.class); |
| 125 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( |
| 126 | ParseCloudLoadBalancersErrorFromHttpResponse.class); |
| 127 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to( |
| 128 | ParseCloudLoadBalancersErrorFromHttpResponse.class); |
| 129 | } |
| 130 | |
| 131 | } |