* '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,19 +74,23 @@ public class ServiceInstance extends ManagedObject
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)
throws RemoteException, MalformedURLException
{
if(username==null)
{
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());
@ -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,34 +84,36 @@ 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
{
if(ignoreCert) {
ignoreCertificates();
}
}
/**
*
*/
public void ignoreCertificates() {
try {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier
(
new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
}
);
} catch (Exception e) {}
});
} catch (Exception e) {
// TODO
}
}
@ -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,6 +240,7 @@ public class WSClient
sb.append("</" + methodName + ">");
sb.append(SOAP_END);
// System.out.println("sb.tostring: " + sb.toString());
return sb.toString();
}
@ -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;
@ -86,6 +87,7 @@ public class ViComputeServiceContextModule
extends
ComputeServiceAdapterContextModule<ServiceInstance, ServiceInstance, VirtualMachine, VirtualMachine, Image, Datacenter> {
public ViComputeServiceContextModule() {
super(ServiceInstance.class, ServiceInstance.class);
}
@ -110,24 +112,28 @@ 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 {
@ -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

@ -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_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,6 +46,7 @@ import com.google.inject.Module;
*/
@Test(groups = "live", testName = "vsphere.ViExperimentLiveTest")
public class ViExperimentLiveTest {
protected String provider = "vsphere";
protected String identity;
protected String credential;
@ -59,15 +64,21 @@ 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();
//
Set<? extends Hardware> hardwares = context.getComputeService().listHardwareProfiles();
@ -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");