forked from apache/deltacloud
/
DeltaCloudClientImpl.java
255 lines (229 loc) · 9.7 KB
/
DeltaCloudClientImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*************************************************************************/
package org.jboss.tools.deltacloud.client;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import org.jboss.tools.deltacloud.client.API.Driver;
import org.jboss.tools.deltacloud.client.request.CreateInstanceRequest;
import org.jboss.tools.deltacloud.client.request.CreateKeyRequest;
import org.jboss.tools.deltacloud.client.request.DeltaCloudRequest;
import org.jboss.tools.deltacloud.client.request.ListHardwareProfileRequest;
import org.jboss.tools.deltacloud.client.request.ListHardwareProfilesRequest;
import org.jboss.tools.deltacloud.client.request.ListImageRequest;
import org.jboss.tools.deltacloud.client.request.ListImagesRequest;
import org.jboss.tools.deltacloud.client.request.ListInstanceRequest;
import org.jboss.tools.deltacloud.client.request.ListInstancesRequest;
import org.jboss.tools.deltacloud.client.request.ListKeyRequest;
import org.jboss.tools.deltacloud.client.request.ListKeysRequest;
import org.jboss.tools.deltacloud.client.request.ListRealmRequest;
import org.jboss.tools.deltacloud.client.request.ListRealmsRequest;
import org.jboss.tools.deltacloud.client.request.PerformActionRequest;
import org.jboss.tools.deltacloud.client.request.TypeRequest;
import org.jboss.tools.deltacloud.client.transport.IHttpTransport;
import org.jboss.tools.deltacloud.client.transport.URLConnectionTransport;
import org.jboss.tools.deltacloud.client.unmarshal.APIUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.HardwareProfileUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.HardwareProfilesUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.ImageUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.ImagesUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.InstanceUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.InstancesUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.KeyUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.KeysUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.RealmUnmarshaller;
import org.jboss.tools.deltacloud.client.unmarshal.RealmsUnmarshaller;
/**
* @author Andre Dietisheim (based on prior implementation by Martyn Taylor)
*/
public class DeltaCloudClientImpl implements DeltaCloudClient {
private String baseUrl;
private IHttpTransport transport;
public DeltaCloudClientImpl(String url) throws MalformedURLException, DeltaCloudClientException {
this(url, null, null);
}
public DeltaCloudClientImpl(String url, String username, String password) throws MalformedURLException,
DeltaCloudClientException {
this(url, new URLConnectionTransport(username, password));
}
public DeltaCloudClientImpl(String url, IHttpTransport transport) throws DeltaCloudClientException {
this.baseUrl = url;
this.transport = transport;
}
protected InputStream request(DeltaCloudRequest deltaCloudRequest) throws DeltaCloudClientException {
return transport.request(deltaCloudRequest);
}
public Driver getServerType() {
try {
InputStream response = request(new TypeRequest(baseUrl));
API api = new APIUnmarshaller().unmarshall(response, new API());
return api.getDriver();
} catch (DeltaCloudClientException e) {
return Driver.UNKNOWN;
}
}
@Override
public Instance createInstance(String imageId) throws DeltaCloudClientException {
try {
InputStream response = request(new CreateInstanceRequest(baseUrl, imageId));
return new InstanceUnmarshaller().unmarshall(response, new Instance());
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
public Instance createInstance(String name, String imageId, String profileId, String realmId, String memory,
String storage) throws DeltaCloudClientException {
return createInstance(name, imageId, profileId, realmId, null, memory, storage);
}
public Instance createInstance(String name, String imageId, String profileId, String realmId, String keyId,
String memory, String storage) throws DeltaCloudClientException {
try {
InputStream response = request(
new CreateInstanceRequest(baseUrl, name, imageId, profileId, realmId, keyId, memory, storage));
Instance instance = new InstanceUnmarshaller().unmarshall(response, new Instance());
// TODO: WORKAROUND for
// https://issues.jboss.org/browse/JBIDE-8005
if (keyId != null) {
instance.setKeyId(keyId);
}
// TODO: WORKAROUND for
// https://issues.jboss.org/browse/JBIDE-8005
return instance;
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
@Override
public HardwareProfile listProfile(String profileId) throws DeltaCloudClientException {
try {
InputStream response = request(new ListHardwareProfileRequest(baseUrl, profileId));
return new HardwareProfileUnmarshaller().unmarshall(response, new HardwareProfile());
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
@Override
public List<HardwareProfile> listProfiles() throws DeltaCloudClientException {
try {
InputStream response = request(new ListHardwareProfilesRequest(baseUrl));
List<HardwareProfile> profiles = new ArrayList<HardwareProfile>();
new HardwareProfilesUnmarshaller().unmarshall(response, profiles);
return profiles;
} catch (Exception e) {
throw new DeltaCloudClientException(MessageFormat.format("could not get realms on cloud at \"{0}\"",
baseUrl), e);
}
}
@Override
public List<Image> listImages() throws DeltaCloudClientException {
InputStream response = request(new ListImagesRequest(baseUrl));
List<Image> images = new ArrayList<Image>();
new ImagesUnmarshaller().unmarshall(response, images);
return images;
}
@Override
public Image listImages(String imageId) throws DeltaCloudClientException {
InputStream response = request(new ListImageRequest(baseUrl, imageId));
return new ImageUnmarshaller().unmarshall(response, new Image());
}
@Override
public List<Instance> listInstances() throws DeltaCloudClientException {
InputStream inputStream = request(new ListInstancesRequest(baseUrl));
List<Instance> instances = new ArrayList<Instance>();
new InstancesUnmarshaller().unmarshall(inputStream, instances);
return instances;
}
@Override
public Instance listInstances(String instanceId) throws DeltaCloudClientException {
try {
InputStream response = request(new ListInstanceRequest(baseUrl, instanceId));
return new InstanceUnmarshaller().unmarshall(response, new Instance());
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
@Override
public List<Realm> listRealms() throws DeltaCloudClientException {
try {
InputStream inputStream = request(new ListRealmsRequest(baseUrl));
List<Realm> realms = new ArrayList<Realm>();
new RealmsUnmarshaller().unmarshall(inputStream, realms);
return realms;
} catch (Exception e) {
throw new DeltaCloudClientException(
MessageFormat.format("could not get realms on cloud at \"{0}\"", baseUrl), e);
}
}
@Override
public Realm listRealms(String realmId) throws DeltaCloudClientException {
try {
InputStream response = request(new ListRealmRequest(baseUrl, realmId));
return new RealmUnmarshaller().unmarshall(response, new Realm());
} catch (Exception e) {
throw new DeltaCloudClientException(
MessageFormat.format("could not get realms on cloud at \"{0}\"", baseUrl), e);
}
}
public Key createKey(String keyname) throws DeltaCloudClientException {
try {
CreateKeyRequest keyRequest = new CreateKeyRequest(baseUrl, keyname);
InputStream inputStream = request(keyRequest);
Key key = new KeyUnmarshaller().unmarshall(inputStream, new Key());
return key;
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
public List<Key> listKeys() throws DeltaCloudClientException {
InputStream inputStream = request(new ListKeysRequest(baseUrl));
List<Key> keys = new ArrayList<Key>();
new KeysUnmarshaller().unmarshall(inputStream, keys);
return keys;
}
public Key listKey(String id) throws DeltaCloudClientException {
InputStream inputStream = request(new ListKeyRequest(baseUrl, id));
Key key = new KeyUnmarshaller().unmarshall(inputStream, new Key());
return key;
}
public InputStream performAction(Action<?> action) throws DeltaCloudClientException {
InputStream in = null;
if (action != null) {
try {
in = request(new PerformActionRequest(action.getUrl(), action.getMethod()));
} catch (DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException(e);
}
}
return in;
}
}