mirror of https://github.com/apache/cloudstack.git
cloudian: api request experiments
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
parent
77e153521a
commit
5f86ad6986
|
|
@ -17,7 +17,18 @@
|
|||
|
||||
package com.cloudian.cloudstack;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
|
|
@ -27,31 +38,70 @@ import org.apache.http.client.HttpClient;
|
|||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLContexts;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.amazonaws.util.json.JSONException;
|
||||
import com.amazonaws.util.json.JSONObject;
|
||||
import com.cloud.utils.nio.TrustAllManager;
|
||||
|
||||
public class CloudianClient {
|
||||
private static final Logger LOG = Logger.getLogger(CloudianClient.class);
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final String baseUrl;
|
||||
private final boolean validateSSLCertificate;
|
||||
|
||||
public CloudianClient(final String baseUrl, final String username, final String password, final boolean validateSSlCertificate) {
|
||||
public CloudianClient(final String baseUrl, final String username, final String password, final boolean validateSSlCertificate) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
|
||||
this.baseUrl = baseUrl;
|
||||
this.validateSSLCertificate = validateSSlCertificate;
|
||||
|
||||
|
||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
|
||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
provider.setCredentials(AuthScope.ANY, credentials);
|
||||
|
||||
SSLContextBuilder builder = new SSLContextBuilder();
|
||||
builder.loadTrustMaterial(null, new TrustStrategy() {
|
||||
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
||||
|
||||
SSLContext sslcontext = SSLContexts.custom().useSSL().build();
|
||||
sslcontext.init(null, new X509TrustManager[]{new TrustAllManager()}, new SecureRandom());
|
||||
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
this.httpClient = HttpClientBuilder.create()
|
||||
.setDefaultCredentialsProvider(provider)
|
||||
.setSSLSocketFactory(factory)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void sendGET() throws IOException {
|
||||
HttpResponse response = httpClient.execute(new HttpGet(baseUrl));
|
||||
private void sendGET(final String path) throws IOException {
|
||||
HttpResponse response = httpClient.execute(new HttpGet(baseUrl + path));
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader((response.getEntity().getContent())));
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String output;
|
||||
while ((output = br.readLine()) != null) {
|
||||
LOG.debug(output);
|
||||
result.append(output);
|
||||
}
|
||||
try {
|
||||
JSONObject o = new JSONObject(result.toString());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPOST() throws IOException {
|
||||
|
|
@ -70,6 +120,11 @@ public class CloudianClient {
|
|||
}
|
||||
|
||||
public boolean listUserAccount() {
|
||||
try {
|
||||
sendGET("/user/list?groupId=0&userType=all&userStatus=all");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,12 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
|
||||
public class CloudianClientTest {
|
||||
|
||||
private CloudianClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
client = new CloudianClient("https://admin.hs.yadav.xyz:19443", "admin", "public", false);
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
@ -19,6 +23,7 @@ public class CloudianClientTest {
|
|||
|
||||
@Test
|
||||
public void listUserAccount() throws Exception {
|
||||
client.listUserAccount();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue