Skip to content

Commit 8d471d5

Browse files
committed
high level api
1 parent 87f21b3 commit 8d471d5

File tree

12 files changed

+277
-77
lines changed

12 files changed

+277
-77
lines changed

src/main/java/org/openstack/api/compute/FlavorResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public Flavor get() {
1717
return target.request(MediaType.APPLICATION_JSON).get(NovaFlavor.class);
1818
}
1919

20+
public void delete() {
21+
// TODO Auto-generated method stub
22+
23+
}
24+
2025
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package org.openstack.client;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.openstack.api.compute.TenantResource;
7+
import org.openstack.model.compute.Flavor;
8+
import org.openstack.model.compute.FlavorList;
9+
import org.openstack.model.compute.FloatingIp;
10+
import org.openstack.model.compute.ImageList;
11+
import org.openstack.model.compute.KeyPair;
12+
import org.openstack.model.compute.KeyPairListItem;
13+
import org.openstack.model.compute.SecurityGroup;
14+
import org.openstack.model.compute.Server;
15+
import org.openstack.model.compute.ServerList;
16+
import org.openstack.model.compute.Snapshot;
17+
import org.openstack.model.compute.Volume;
18+
19+
import com.google.common.base.Function;
20+
import com.google.common.collect.Lists;
21+
22+
public class ComputeClient {
23+
24+
private TenantResource resource;
25+
26+
public ComputeClient(TenantResource resource) {
27+
this.resource = resource;
28+
}
29+
30+
public ServerList listServers() {
31+
return resource.servers().get();
32+
}
33+
34+
public ServerList listServers(int offset, int max) {
35+
return resource.servers().get();
36+
}
37+
38+
public ServerList listServers(Map<String, Object> params) {
39+
return resource.servers().get();
40+
}
41+
42+
public Server showServer(String id) {
43+
return resource.servers().server(id).get();
44+
}
45+
46+
public void deleteServer(String id) {
47+
resource.servers().server(id).delete();
48+
}
49+
50+
public FlavorList listFlavors() {
51+
return resource.flavors().get();
52+
}
53+
54+
public FlavorList listFlavors(int offset, int max) {
55+
return resource.flavors().get();
56+
}
57+
58+
public FlavorList listFlavors(Map<String, Object> params) {
59+
return resource.flavors().get();
60+
}
61+
62+
public Flavor showFlavor(String id) {
63+
return resource.flavors().flavor(id).get();
64+
}
65+
66+
public void deleteFlavor(String id) {
67+
resource.flavors().flavor(id).delete();
68+
}
69+
70+
public ImageList listImages() {
71+
return resource.images().get();
72+
}
73+
74+
public ImageList listImages(int offset, int max) {
75+
return resource.images().get();
76+
}
77+
78+
public ImageList listImages(Map<String, Object> params) {
79+
return resource.images().get();
80+
}
81+
82+
public List<FloatingIp> listFloatingIps() {
83+
return resource.floatingIps().get().getList();
84+
}
85+
86+
public FloatingIp createFloatingIp(String pool) {
87+
return resource.floatingIps().post(pool);
88+
}
89+
90+
public void deleteFloatingIp(Integer id) {
91+
resource.floatingIps().floatingIp(id).delete();
92+
}
93+
94+
public List<KeyPair> listKeyPairs() {
95+
return Lists.transform(resource.keyPairs().get().getList(), new Function<KeyPairListItem, KeyPair>() {
96+
97+
@Override
98+
public KeyPair apply(KeyPairListItem item) {
99+
return item.getKeypair();
100+
}
101+
});
102+
}
103+
104+
public KeyPair createKeyPair(String name) {
105+
return resource.keyPairs().post(null);
106+
}
107+
108+
public void deleteKeyPair(String name) {
109+
resource.keyPairs().keypair(name).delete();
110+
}
111+
112+
public List<SecurityGroup> listSecurityGroups() {
113+
return resource.securityGroups().get().getList();
114+
}
115+
116+
public SecurityGroup createSecurityGroup(String name, String description) {
117+
return resource.securityGroups().post(null);
118+
}
119+
120+
public SecurityGroup showSecurityGroup(Integer id) {
121+
return resource.securityGroups().securityGroup(id).get();
122+
}
123+
124+
public void deleteSecurityGroup(Integer id) {
125+
resource.securityGroups().securityGroup(id).delete();
126+
}
127+
128+
public List<Volume> listVolumes() {
129+
return resource.volumes().get().getList();
130+
}
131+
132+
public Volume createVolume(String name, String description) {
133+
return resource.volumes().post(null);
134+
}
135+
136+
public void showVolume(Integer id) {
137+
resource.volumes().volume(id).get();
138+
}
139+
140+
public void deleteVolume(Integer id) {
141+
resource.volumes().volume(id).delete();
142+
}
143+
144+
public List<Snapshot> listSnapshots() {
145+
return resource.snapshots().get().getList();
146+
}
147+
148+
public Snapshot createSnapshot(String name, String description) {
149+
return resource.snapshots().post(null);
150+
}
151+
152+
public void showSnapshot(Integer id) {
153+
resource.snapshots().snapshot(id).get();
154+
}
155+
156+
public void deleteSnapshot(Integer id) {
157+
resource.snapshots().snapshot(id).delete();
158+
}
159+
160+
}
Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package org.openstack.client;
22

33
import java.io.IOException;
4-
import java.util.List;
5-
import java.util.NoSuchElementException;
64
import java.util.Properties;
75
import java.util.logging.Logger;
86

97
import javax.ws.rs.client.Target;
10-
import javax.ws.rs.ext.FilterContext;
11-
import javax.ws.rs.ext.RequestFilter;
128

139
import org.glassfish.jersey.filter.LoggingFilter;
1410
import org.openstack.api.common.Resource;
@@ -22,13 +18,9 @@
2218
import org.openstack.model.exceptions.OpenstackException;
2319
import org.openstack.model.identity.Access;
2420
import org.openstack.model.identity.Authentication;
25-
import org.openstack.model.identity.ServiceEndpoint;
2621
import org.openstack.model.identity.keystone.KeystoneAuthentication;
27-
import org.openstack.model.identity.keystone.ServiceCatalogEntry;
2822

2923
import com.google.common.base.Preconditions;
30-
import com.google.common.base.Predicate;
31-
import com.google.common.collect.Iterables;
3224

3325
public 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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.openstack.client;
2+
3+
import java.io.IOException;
4+
5+
import javax.ws.rs.ext.FilterContext;
6+
import javax.ws.rs.ext.RequestFilter;
7+
8+
public class XAuthTokenFilter implements RequestFilter {
9+
10+
private String token;
11+
12+
public XAuthTokenFilter(String token) {
13+
this.token = token;
14+
}
15+
16+
@Override
17+
public void preFilter(FilterContext context) throws IOException {
18+
context.getRequestBuilder().header("X-Auth-Token", token);
19+
}
20+
21+
}

src/main/java/org/openstack/model/compute/FlavorList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import java.util.List;
44

5-
import org.openstack.model.compute.nova.NovaFlavor;
6-
7-
public interface FlavorList {
5+
public interface FlavorList extends Iterable<Flavor> {
86

97
List<Flavor> getList();
108

src/main/java/org/openstack/model/compute/ImageList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import java.util.List;
44

5-
import org.openstack.model.compute.nova.NovaImage;
6-
7-
public interface ImageList {
5+
public interface ImageList extends Iterable<Image> {
86

97
List<Image> getList();
108

0 commit comments

Comments
 (0)