* '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; import com.vmware.vim25.ws.WSClient;
/** /**
*
* jclouds
*
* The managed object class corresponding to the one defined in VI SDK API reference. * The managed object class corresponding to the one defined in VI SDK API reference.
* @author Steve JIN (sjin@vmware.com) * @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; private ServiceContent serviceContent = null;
final static ManagedObjectReference SERVICE_INSTANCE_MOR; final static ManagedObjectReference SERVICE_INSTANCE_MOR;
public final static String VIM25_NAMESPACE = " xmlns=\"urn:vim25\">"; public final static String VIM25_NAMESPACE = " xmlns=\"urn:vim25\">";
@ -67,19 +74,23 @@ public class ServiceInstance extends ManagedObject
SERVICE_INSTANCE_MOR.setType("ServiceInstance"); SERVICE_INSTANCE_MOR.setType("ServiceInstance");
} }
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();
}
public ServiceInstance(WSClient wsc, String username, String password) if(username == null) {
throws RemoteException, MalformedURLException
{
if(username==null)
{
throw new NullPointerException("username cannot be null."); throw new NullPointerException("username cannot be null.");
} }
setMOR(SERVICE_INSTANCE_MOR); setMOR(SERVICE_INSTANCE_MOR);
VimPortType vimService_ = new VimPortType(wsc.getBaseUrl().toString(), ignoreCert.equals("true"));
VimPortType vimService = new VimPortType(wsc); VimPortType vimService = new VimPortType(wsc);
vimService.getWsc().setVimNameSpace(wsc.getVimNameSpace());
serviceContent = vimService.retrieveServiceContent(SERVICE_INSTANCE_MOR); serviceContent = vimService.retrieveServiceContent(SERVICE_INSTANCE_MOR);
vimService.getWsc().setSoapActionOnApiVersion(serviceContent.getAbout().getApiVersion()); vimService.getWsc().setSoapActionOnApiVersion(serviceContent.getAbout().getApiVersion());
@ -326,6 +337,12 @@ public class ServiceInstance extends ManagedObject
return MorUtil.createExactManagedObject(getServerConnection(), mor); 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 // 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.MalformedURLException;
import java.net.ProtocolException; import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.rmi.RemoteException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
@ -56,8 +57,9 @@ import org.dom4j.Element;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import com.vmware.vim25.ManagedObjectReference; import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ObjectSpec;
import java.rmi.RemoteException; import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertySpec;
/** /**
* *
@ -82,34 +84,36 @@ public class WSClient
private int connectTimeout = 0; private int connectTimeout = 0;
private int readTimeout = 0; private int readTimeout = 0;
public WSClient(String serverUrl) throws MalformedURLException
{
public WSClient(String serverUrl) throws MalformedURLException {
this(serverUrl, true); this(serverUrl, true);
} }
public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException {
{ if(serverUrl.endsWith("/")) {
if(serverUrl.endsWith("/"))
{
serverUrl = serverUrl.substring(0, serverUrl.length()-1); serverUrl = serverUrl.substring(0, serverUrl.length()-1);
} }
this.baseUrl = new URL(serverUrl); this.baseUrl = new URL(serverUrl);
if(ignoreCert) if(ignoreCert) {
{ ignoreCertificates();
try }
{ }
/**
*
*/
public void ignoreCertificates() {
try {
trustAllHttpsCertificates(); trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
( public boolean verify(String urlHostName, SSLSession session) {
new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
return true; return true;
} }
} });
); } catch (Exception e) {
} catch (Exception e) {} // TODO
} }
} }
@ -127,6 +131,20 @@ public class WSClient
Element body = (Element) root.elements().get(0); Element body = (Element) root.elements().get(0);
Element resp = (Element) body.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) if(resp.getName().indexOf("Fault")!=-1)
{ {
SoapFaultException sfe = null; 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); String soapMsg = createSoapMessage(methodName, paras);
Element root = null; 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(); StringBuffer sb = new StringBuffer();
sb.append(SOAP_HEADER); sb.append(SOAP_HEADER);
@ -223,6 +240,7 @@ public class WSClient
sb.append("</" + methodName + ">"); sb.append("</" + methodName + ">");
sb.append(SOAP_END); sb.append(SOAP_END);
// System.out.println("sb.tostring: " + sb.toString());
return sb.toString(); return sb.toString();
} }
@ -380,5 +398,7 @@ public class WSClient
throws CertificateException throws CertificateException
{ {
} }
} }
} }

View File

@ -20,6 +20,7 @@
package org.jclouds.vi.compute; 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_XML_NAMESPACE;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_IGNORE_CERTIFICATE;
import java.util.Properties; import java.util.Properties;
@ -37,6 +38,7 @@ public class ViPropertiesBuilder extends PropertiesBuilder {
protected Properties defaultProperties() { protected Properties defaultProperties() {
Properties properties = super.defaultProperties(); Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_VI_XML_NAMESPACE, ServiceInstance.VIM25_NAMESPACE); properties.setProperty(PROPERTY_VI_XML_NAMESPACE, ServiceInstance.VIM25_NAMESPACE);
properties.setProperty(PROPERTY_VI_IGNORE_CERTIFICATE, "true");
return properties; 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.filter;
import static com.google.common.collect.Iterables.transform; 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_XML_NAMESPACE;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_IGNORE_CERTIFICATE;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -86,6 +87,7 @@ public class ViComputeServiceContextModule
extends extends
ComputeServiceAdapterContextModule<ServiceInstance, ServiceInstance, VirtualMachine, VirtualMachine, Image, Datacenter> { ComputeServiceAdapterContextModule<ServiceInstance, ServiceInstance, VirtualMachine, VirtualMachine, Image, Datacenter> {
public ViComputeServiceContextModule() { public ViComputeServiceContextModule() {
super(ServiceInstance.class, ServiceInstance.class); super(ServiceInstance.class, ServiceInstance.class);
} }
@ -110,24 +112,28 @@ public class ViComputeServiceContextModule
@Provides @Provides
@Singleton @Singleton
protected ServiceInstance createConnection(JcloudsWSClient client, 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 { throws RemoteException, MalformedURLException {
return new ServiceInstance(client, identity, credential); return new ServiceInstance(client, identity, credential, ignoreCertificate);
} }
@Singleton @Singleton
public
static class JcloudsWSClient extends WSClient { static class JcloudsWSClient extends WSClient {
private final HttpClient client; private final HttpClient client;
@Inject @Inject
public JcloudsWSClient(HttpClient client, @Provider URI baseUrl, public JcloudsWSClient(HttpClient client, @Provider URI baseUrl,
@Named(PROPERTY_VI_XML_NAMESPACE) String vimNameSpace) throws MalformedURLException { @Named(PROPERTY_VI_XML_NAMESPACE) String vimNameSpace,
super(baseUrl.toASCIIString(), false); @Named(PROPERTY_VI_IGNORE_CERTIFICATE) String ignoreCert) throws MalformedURLException {
super(baseUrl.toASCIIString(), ignoreCert.equals("true"));
this.setVimNameSpace(vimNameSpace); this.setVimNameSpace(vimNameSpace);
this.client = client; this.client = client;
} }
@Override @Override
public InputStream post(String soapMsg) throws IOException { public InputStream post(String soapMsg) throws IOException {
@ -144,7 +150,6 @@ public class ViComputeServiceContextModule
Throwables.propagate(e); Throwables.propagate(e);
return null;// unreachable as the above line will throw the exception. return null;// unreachable as the above line will throw the exception.
} }
HttpResponse response = client.invoke(request); HttpResponse response = client.invoke(request);
if (getCookie() != null && response.getFirstHeaderOrNull(HttpHeaders.SET_COOKIE) != null) { if (getCookie() != null && response.getFirstHeaderOrNull(HttpHeaders.SET_COOKIE) != null) {
@ -153,6 +158,7 @@ public class ViComputeServiceContextModule
return response.getPayload().getInput(); return response.getPayload().getInput();
} }
} }
@Override @Override

View File

@ -28,4 +28,6 @@ public interface ViConstants {
public static final String PROPERTY_VI_XML_NAMESPACE = "jclouds.vi.xml.ns"; 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 static com.google.common.base.Preconditions.checkNotNull;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import javax.annotation.Resource;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.inject.Module; import com.google.inject.Module;
import com.google.inject.name.Named;
/** /**
* *
@ -42,6 +46,7 @@ import com.google.inject.Module;
*/ */
@Test(groups = "live", testName = "vsphere.ViExperimentLiveTest") @Test(groups = "live", testName = "vsphere.ViExperimentLiveTest")
public class ViExperimentLiveTest { public class ViExperimentLiveTest {
protected String provider = "vsphere"; protected String provider = "vsphere";
protected String identity; protected String identity;
protected String credential; protected String credential;
@ -59,15 +64,21 @@ public class ViExperimentLiveTest {
@Test @Test
public void testAndExperiment() { public void testAndExperiment() {
ComputeServiceContext context = null; ComputeServiceContext context = null;
try { try {
context = new ComputeServiceContextFactory().createContext(new ViComputeServiceContextSpec(endpoint, identity, 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(); Set<? extends Location> locations = context.getComputeService().listAssignableLocations();
for (Location location : locations) { for (Location location : locations) {
System.out.println("location id: " + location.getId() + " - desc: " + location.getDescription()); 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(); // Set<? extends ComputeMetadata> nodes = context.getComputeService().listNodes();
// //
Set<? extends Hardware> hardwares = context.getComputeService().listHardwareProfiles(); Set<? extends Hardware> hardwares = context.getComputeService().listHardwareProfiles();
@ -75,9 +86,7 @@ public class ViExperimentLiveTest {
System.out.println("hardware id: " + hardware.getId() + " - name: " + hardware.getName()); 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"); // NodeMetadata node = context.getComputeService().getNodeMetadata("MyWinServer");