11package org .openstack .client ;
22
33import java .io .IOException ;
4- import java .util .List ;
5- import java .util .NoSuchElementException ;
64import java .util .Properties ;
75import java .util .logging .Logger ;
86
97import javax .ws .rs .client .Target ;
10- import javax .ws .rs .ext .FilterContext ;
11- import javax .ws .rs .ext .RequestFilter ;
128
139import org .glassfish .jersey .filter .LoggingFilter ;
1410import org .openstack .api .common .Resource ;
2218import org .openstack .model .exceptions .OpenstackException ;
2319import org .openstack .model .identity .Access ;
2420import org .openstack .model .identity .Authentication ;
25- import org .openstack .model .identity .ServiceEndpoint ;
2621import org .openstack .model .identity .keystone .KeystoneAuthentication ;
27- import org .openstack .model .identity .keystone .ServiceCatalogEntry ;
2822
2923import com .google .common .base .Preconditions ;
30- import com .google .common .base .Predicate ;
31- import com .google .common .collect .Iterables ;
3224
3325public class OpenStackClient {
3426
@@ -38,25 +30,9 @@ public class OpenStackClient {
3830
3931 private Access access ;
4032
41- private RequestFilter authFilter = new RequestFilter () {
33+ private XAuthTokenFilter authFilter ;
4234
43- @ Override
44- public void preFilter (FilterContext context ) throws IOException {
45- context .getRequestBuilder ().header ("X-Auth-Token" , access .getToken ().getId ());
46-
47- }
48-
49- };
50-
51- private RequestFilter authAsAdministratorFilter = new RequestFilter () {
52-
53- @ Override
54- public void preFilter (FilterContext context ) throws IOException {
55- context .getRequestBuilder ().header ("X-Auth-Token" , properties .get ("identity.admin.token" ));
56-
57- }
58-
59- };
35+ private XAuthTokenFilter authAsAdministratorFilter ;
6036
6137 private OpenStackClient () {
6238
@@ -66,13 +42,14 @@ public static OpenStackClient authenticate(Properties properties, Access access)
6642 OpenStackClient client = new OpenStackClient ();
6743 client .properties = properties ;
6844 client .access = access ;
45+ client .authFilter = new XAuthTokenFilter (access .getToken ().getId ());
46+ client .authAsAdministratorFilter = new XAuthTokenFilter (properties .getProperty ("identity.admin.token" ));
6947 return client ;
7048 }
7149
7250 public static OpenStackClient authenticate (Properties properties ) {
7351 OpenStackClient client = new OpenStackClient ();
7452 client .properties = properties ;
75- //String endpoint = properties.getProperty("auth.endpoint");
7653 String username = properties .getProperty ("auth.username" );
7754 String password = properties .getProperty ("auth.password" );
7855 String tenantId = properties .getProperty ("auth.tenant.id" );
@@ -108,6 +85,10 @@ public void exchangeTokenForTenant(String tenantId) {
10885 this .access = target (endpoint , IdentityPublicEndpoint .class ).tokens ().post (authentication );
10986 }
11087
88+ public Access getAccess () {
89+ return this .access ;
90+ }
91+
11192 public IdentityPublicEndpoint getIdentityEndpoint () {
11293 String url = properties .getProperty ("identity.endpoint.publicURL" );
11394 Preconditions .checkNotNull (url , "'identity.endpoint.publicURL' property not found" );
@@ -126,40 +107,52 @@ public IdentityAdministrationEndpoint getIdentityAdministationEndpoint() {
126107 return target (url , IdentityAdministrationEndpoint .class , true );
127108 }
128109
110+ public ComputeClient getComputeClient () {
111+ return new ComputeClient (getComputeEndpoint ());
112+ }
113+
129114 public TenantResource getComputeEndpoint () {
130- return target (getEndpoint ("compute" , null ).getPublicURL (), TenantResource .class );
115+ return target (access .getEndpoint ("compute" , null ).getPublicURL (), TenantResource .class );
116+ }
117+
118+ public ComputeClient getComputeInternalClient () {
119+ return new ComputeClient (getComputeInternalEndpoint ());
131120 }
132121
133122 public TenantResource getComputeInternalEndpoint () {
134- return target (getEndpoint ("compute" , null ).getInternalURL (), TenantResource .class );
123+ return target (access .getEndpoint ("compute" , null ).getInternalURL (), TenantResource .class );
124+ }
125+
126+ public ComputeClient getComputeAdministrationClient () {
127+ return new ComputeClient (getComputeAdministationEndpoint ());
135128 }
136129
137130 public TenantResource getComputeAdministationEndpoint () {
138- return target (getEndpoint ("compute" , null ).getAdminURL (), TenantResource .class );
131+ return target (access . getEndpoint ("compute" , null ).getAdminURL (), TenantResource .class );
139132 }
140133
141134 public ImagesResource getImagesEndpoint () {
142- return target (getEndpoint ("image" , null ).getPublicURL ().concat ("/images" ), ImagesResource .class );
135+ return target (access . getEndpoint ("image" , null ).getPublicURL ().concat ("/images" ), ImagesResource .class );
143136 }
144137
145138 public ImagesResource getImagesInternalEndpoint () {
146- return target (getEndpoint ("image" , null ).getAdminURL ().concat ("/images" ), ImagesResource .class );
139+ return target (access . getEndpoint ("image" , null ).getAdminURL ().concat ("/images" ), ImagesResource .class );
147140 }
148141
149142 public ImagesResource getImagesAdministationEndpoint () {
150- return target (getEndpoint ("image" , null ).getAdminURL ().concat ("/images" ), ImagesResource .class );
143+ return target (access . getEndpoint ("image" , null ).getAdminURL ().concat ("/images" ), ImagesResource .class );
151144 }
152145
153146 public AccountResource getStorageEndpoint () {
154- return target (getEndpoint ("object-store" , null ).getPublicURL (), AccountResource .class );
147+ return target (access . getEndpoint ("object-store" , null ).getPublicURL (), AccountResource .class );
155148 }
156149
157150 public AccountResource getStorageInternalEndpoint () {
158- return target (getEndpoint ("object-store" , null ).getInternalURL ().replace ("AUTH_" , "" ), AccountResource .class );
151+ return target (access . getEndpoint ("object-store" , null ).getInternalURL ().replace ("AUTH_" , "" ), AccountResource .class );
159152 }
160153
161154 public AccountResource getStorageAdministationEndpoint () {
162- return target (getEndpoint ("object-store" , null ).getAdminURL ().replace ("AUTH_" , "" ), AccountResource .class );
155+ return target (access . getEndpoint ("object-store" , null ).getAdminURL ().replace ("AUTH_" , "" ), AccountResource .class );
163156 }
164157
165158 public <T extends Resource > T target (String absoluteURL , Class <T > clazz ) {
@@ -182,38 +175,4 @@ private <T extends Resource> T target(String absoluteURL, Class<T> clazz, boolea
182175
183176 }
184177
185- private ServiceEndpoint getEndpoint (final String type , final String region ) {
186- Preconditions .checkNotNull (access , "You must be authenticated before get a identity client" );
187- try {
188- ServiceCatalogEntry service = Iterables .find (access .getServices (), new Predicate <ServiceCatalogEntry >() {
189-
190- @ Override
191- public boolean apply (ServiceCatalogEntry service ) {
192- return type .equals (service .getType ());
193- }
194-
195- });
196- List <ServiceEndpoint > endpoints = service .getEndpoints ();
197- if (region != null ) {
198- return Iterables .find (endpoints , new Predicate <ServiceEndpoint >() {
199-
200- @ Override
201- public boolean apply (ServiceEndpoint endpoint ) {
202- return region .equals (endpoint .getRegion ());
203- }
204- });
205- } else {
206- return endpoints .get (0 );
207- }
208- } catch (NoSuchElementException e ) {
209- throw new OpenstackException ("Service " + type + " not found, you can try openstack.target(<endpoint>, <resource class>) method instead" , e );
210- } catch (Exception e ) {
211- throw new RuntimeException (e .getMessage (), e );
212- }
213- }
214-
215-
216-
217-
218-
219178}
0 commit comments