* 'master' of https://github.com/andreaturli/jclouds:
  testing different version of vCenter - unstable
This commit is contained in:
Adrian Cole 2011-01-23 12:21:54 -08:00
commit ee4845905d
6 changed files with 113 additions and 57 deletions

View File

@ -49,12 +49,19 @@ import com.vmware.vim25.mo.util.MorUtil;
import com.vmware.vim25.ws.WSClient;
/**
*
* jclouds
*
* The managed object class corresponding to the one defined in VI SDK API reference.
* @author Steve JIN (sjin@vmware.com)
*/
public class ServiceInstance extends ManagedObject
{
public class ServiceInstance extends ManagedObject {
// @Resource
// @Named(ComputeServiceConstants.COMPUTE_LOGGER)
// protected Logger logger = Logger.NULL;
private ServiceContent serviceContent = null;
final static ManagedObjectReference SERVICE_INSTANCE_MOR;
public final static String VIM25_NAMESPACE = " xmlns=\"urn:vim25\">";
@ -67,24 +74,28 @@ public class ServiceInstance extends ManagedObject
SERVICE_INSTANCE_MOR.setType("ServiceInstance");
}
public ServiceInstance(WSClient wsc, String username, String password)
throws RemoteException, MalformedURLException
{
if(username==null)
{
public ServiceInstance(WSClient wsc, String username, String password, String ignoreCert)
throws RemoteException, MalformedURLException {
//logger.debug(">> ServiceInstance(%s, %s, %s, %s)", username, password, ignoreCert);
if(ignoreCert.equals("true")) {
this.ignoreCertificate();
}
if(username == null) {
throw new NullPointerException("username cannot be null.");
}
setMOR(SERVICE_INSTANCE_MOR);
VimPortType vimService_ = new VimPortType(wsc.getBaseUrl().toString(), ignoreCert.equals("true"));
VimPortType vimService = new VimPortType(wsc);
vimService.getWsc().setVimNameSpace(wsc.getVimNameSpace());
serviceContent = vimService.retrieveServiceContent(SERVICE_INSTANCE_MOR);
vimService.getWsc().setSoapActionOnApiVersion(serviceContent.getAbout().getApiVersion());
setServerConnection(new ServerConnection(wsc.getBaseUrl(), vimService, this));
// escape 5 special chars
// http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
password = password.replace("&", "&")
@ -326,6 +337,12 @@ public class ServiceInstance extends ManagedObject
return MorUtil.createExactManagedObject(getServerConnection(), mor);
}
private void ignoreCertificate()
{
System.setProperty("org.apache.axis.components.net.SecureSocketFactory",
"org.apache.axis.components.net.SunFakeTrustSocketFactory");
}
// TODO vim.VirtualizationManager is defined in servicecontent but no documentation there. Filed a bug already
}

View File

@ -39,6 +39,7 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.rmi.RemoteException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
@ -56,8 +57,9 @@ import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.vmware.vim25.ManagedObjectReference;
import java.rmi.RemoteException;
import com.vmware.vim25.ObjectSpec;
import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertySpec;
/**
*
@ -82,36 +84,38 @@ public class WSClient
private int connectTimeout = 0;
private int readTimeout = 0;
public WSClient(String serverUrl) throws MalformedURLException
{
public WSClient(String serverUrl) throws MalformedURLException {
this(serverUrl, true);
}
public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException
{
if(serverUrl.endsWith("/"))
{
public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException {
if(serverUrl.endsWith("/")) {
serverUrl = serverUrl.substring(0, serverUrl.length()-1);
}
this.baseUrl = new URL(serverUrl);
if(ignoreCert)
{
try
{
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier
(
new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
return true;
}
}
);
} catch (Exception e) {}
if(ignoreCert) {
ignoreCertificates();
}
}
/**
*
*/
public void ignoreCertificates() {
try {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
});
} catch (Exception e) {
// TODO
}
}
public Object invoke(ManagedObjectReference mor, String methodName, Argument[] paras, String returnType) throws IOException
{
@ -127,6 +131,20 @@ public class WSClient
Element body = (Element) root.elements().get(0);
Element resp = (Element) body.elements().get(0);
// System.out.println("\n\n++++ methodName " + methodName);
//
// for (Argument argument : paras) {
// System.out.println("par: " + argument.getName() + ", " + argument.getValue());
// if(argument.getValue() instanceof ManagedObjectReference)
// System.out.println("\t** " + ((ManagedObjectReference) argument.getValue()).getVal());
// else if(argument.getValue() instanceof PropertyFilterSpec) {
// for (ObjectSpec p : ((PropertyFilterSpec) argument.getValue()).getObjectSet()) {
// System.out.println("\t-- " + p.getObj().getVal());
// }
//
// }
// }
if(resp.getName().indexOf("Fault")!=-1)
{
SoapFaultException sfe = null;
@ -167,8 +185,8 @@ public class WSClient
}
}
public Element invoke(String methodName, Argument[] paras) throws RemoteException
{
public Element invoke(String methodName, Argument[] paras) throws RemoteException {
String soapMsg = createSoapMessage(methodName, paras);
Element root = null;
@ -206,8 +224,7 @@ public class WSClient
}
}
private String createSoapMessage(String methodName, Argument[] paras)
{
private String createSoapMessage(String methodName, Argument[] paras) {
StringBuffer sb = new StringBuffer();
sb.append(SOAP_HEADER);
@ -223,13 +240,14 @@ public class WSClient
sb.append("</" + methodName + ">");
sb.append(SOAP_END);
// System.out.println("sb.tostring: " + sb.toString());
return sb.toString();
}
public InputStream post(String soapMsg) throws IOException
{
HttpURLConnection postCon = (HttpURLConnection) baseUrl.openConnection();
if(connectTimeout > 0)
postCon.setConnectTimeout(connectTimeout);
if(readTimeout > 0)
@ -251,7 +269,7 @@ public class WSClient
OutputStream os = postCon.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(os);
out.write(soapMsg);
out.close();
@ -380,5 +398,7 @@ public class WSClient
throws CertificateException
{
}
}
}

View File

@ -20,6 +20,7 @@
package org.jclouds.vi.compute;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_XML_NAMESPACE;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_IGNORE_CERTIFICATE;
import java.util.Properties;
@ -37,6 +38,7 @@ public class ViPropertiesBuilder extends PropertiesBuilder {
protected Properties defaultProperties() {
Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_VI_XML_NAMESPACE, ServiceInstance.VIM25_NAMESPACE);
properties.setProperty(PROPERTY_VI_IGNORE_CERTIFICATE, "true");
return properties;
}

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_XML_NAMESPACE;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_IGNORE_CERTIFICATE;
import java.io.File;
import java.io.FileInputStream;
@ -85,8 +86,9 @@ import com.vmware.vim25.ws.WSClient;
public class ViComputeServiceContextModule
extends
ComputeServiceAdapterContextModule<ServiceInstance, ServiceInstance, VirtualMachine, VirtualMachine, Image, Datacenter> {
public ViComputeServiceContextModule() {
public ViComputeServiceContextModule() {
super(ServiceInstance.class, ServiceInstance.class);
}
@ -110,27 +112,31 @@ public class ViComputeServiceContextModule
@Provides
@Singleton
protected ServiceInstance createConnection(JcloudsWSClient client,
@Named(Constants.PROPERTY_IDENTITY) String identity, @Named(Constants.PROPERTY_CREDENTIAL) String credential)
@Named(Constants.PROPERTY_IDENTITY) String identity, @Named(Constants.PROPERTY_CREDENTIAL) String credential,
@Named(PROPERTY_VI_IGNORE_CERTIFICATE) String ignoreCertificate)
throws RemoteException, MalformedURLException {
return new ServiceInstance(client, identity, credential);
return new ServiceInstance(client, identity, credential, ignoreCertificate);
}
@Singleton
public
static class JcloudsWSClient extends WSClient {
private final HttpClient client;
@Inject
public JcloudsWSClient(HttpClient client, @Provider URI baseUrl,
@Named(PROPERTY_VI_XML_NAMESPACE) String vimNameSpace) throws MalformedURLException {
super(baseUrl.toASCIIString(), false);
@Named(PROPERTY_VI_XML_NAMESPACE) String vimNameSpace,
@Named(PROPERTY_VI_IGNORE_CERTIFICATE) String ignoreCert) throws MalformedURLException {
super(baseUrl.toASCIIString(), ignoreCert.equals("true"));
this.setVimNameSpace(vimNameSpace);
this.client = client;
}
@Override
public InputStream post(String soapMsg) throws IOException {
Builder<String, String> headers = ImmutableMultimap.<String, String> builder();
headers.put("SOAPAction", "urn:vim25/4.0");
if (getCookie() != null)
@ -144,7 +150,6 @@ public class ViComputeServiceContextModule
Throwables.propagate(e);
return null;// unreachable as the above line will throw the exception.
}
HttpResponse response = client.invoke(request);
if (getCookie() != null && response.getFirstHeaderOrNull(HttpHeaders.SET_COOKIE) != null) {
@ -153,6 +158,7 @@ public class ViComputeServiceContextModule
return response.getPayload().getInput();
}
}
@Override

View File

@ -27,5 +27,7 @@ package org.jclouds.vi.reference;
public interface ViConstants {
public static final String PROPERTY_VI_XML_NAMESPACE = "jclouds.vi.xml.ns";
public static final String PROPERTY_VI_IGNORE_CERTIFICATE = "jclouds.ignore-certificate";
}

View File

@ -21,20 +21,24 @@ package org.jclouds.vi.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Resource;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import com.google.inject.name.Named;
/**
*
@ -42,12 +46,13 @@ import com.google.inject.Module;
*/
@Test(groups = "live", testName = "vsphere.ViExperimentLiveTest")
public class ViExperimentLiveTest {
protected String provider = "vsphere";
protected String provider = "vsphere";
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
@BeforeClass
protected void setupCredentials() {
identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
@ -59,14 +64,20 @@ public class ViExperimentLiveTest {
@Test
public void testAndExperiment() {
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory().createContext(new ViComputeServiceContextSpec(endpoint, identity,
credential), ImmutableSet.<Module>of(new Log4JLoggingModule()), new Properties());
credential), ImmutableSet.<Module>of(new Log4JLoggingModule()), new ViPropertiesBuilder().build());
Set<? extends Location> locations = context.getComputeService().listAssignableLocations();
for (Location location : locations) {
System.out.println("location id: " + location.getId() + " - desc: " + location.getDescription());
}
Set<? extends Image> images = context.getComputeService().listImages();
for (Image image : images) {
System.out.println("id: " + image.getId() + " - name:" + image.getName());
// Set<? extends ComputeMetadata> nodes = context.getComputeService().listNodes();
//
@ -75,9 +86,7 @@ public class ViExperimentLiveTest {
System.out.println("hardware id: " + hardware.getId() + " - name: " + hardware.getName());
}
//
Set<? extends Image> images = context.getComputeService().listImages();
for (Image image : images) {
System.out.println("id: " + image.getId() + " - name:" + image.getName());
}
//
// NodeMetadata node = context.getComputeService().getNodeMetadata("MyWinServer");