Issue 167: revised vi implementation wrt specifying http client

This commit is contained in:
Adrian Cole 2011-01-16 10:52:20 -08:00
parent 2bc2e23d9c
commit 04aa3f6f94
4 changed files with 349 additions and 41 deletions

View File

@ -0,0 +1,331 @@
/*================================================================================
Copyright (c) 2008 VMware, Inc. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of VMware, Inc. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
================================================================================*/
package com.vmware.vim25.mo;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Calendar;
import com.vmware.vim25.AboutInfo;
import com.vmware.vim25.Capability;
import com.vmware.vim25.Event;
import com.vmware.vim25.HostVMotionCompatibility;
import com.vmware.vim25.InvalidState;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ProductComponentInfo;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.UserSession;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VirtualMachinePowerState;
import com.vmware.vim25.mo.util.MorUtil;
import com.vmware.vim25.ws.WSClient;
/**
* 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
{
private ServiceContent serviceContent = null;
final static ManagedObjectReference SERVICE_INSTANCE_MOR;
public final static String VIM25_NAMESPACE = " xmlns=\"urn:vim25\">";
public final static String VIM20_NAMESPACE = " xmlns=\"urn:vim2\">";
static
{
SERVICE_INSTANCE_MOR = new ManagedObjectReference();
SERVICE_INSTANCE_MOR.set_value("ServiceInstance");
SERVICE_INSTANCE_MOR.setType("ServiceInstance");
}
public ServiceInstance(WSClient wsc, String username, String password)
throws RemoteException, MalformedURLException
{
if(username==null)
{
throw new NullPointerException("username cannot be null.");
}
setMOR(SERVICE_INSTANCE_MOR);
VimPortType vimService = new VimPortType(wsc);
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("&", "&")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&apos;");
UserSession userSession = getSessionManager().login(username, password, null);
// it appears that this data is never used anywhere via reference checks
// getServerConnection().setUserSession(userSession);
}
public Calendar getServerClock()
{
return (Calendar) getCurrentProperty("serverClock");
}
public Capability getCapability()
{
return (Capability) getCurrentProperty("capability");
}
public ClusterProfileManager getClusterProfileManager()
{
return (ClusterProfileManager) createMO(getServiceContent().getClusterProfileManager());
}
public Calendar currentTime() throws RuntimeFault, RemoteException
{
return getVimService().currentTime(getMOR());
}
public Folder getRootFolder()
{
return new Folder(this.getServerConnection(), this.getServiceContent().getRootFolder());
}
public HostVMotionCompatibility[] queryVMotionCompatibility(VirtualMachine vm, HostSystem[] hosts, String[] compatibility) throws RuntimeFault, RemoteException
{
if(vm==null || hosts==null)
{
throw new IllegalArgumentException("Neither vm or hosts can be null.");
}
return getVimService().queryVMotionCompatibility(getMOR(), vm.getMOR(), MorUtil.createMORs(hosts), compatibility);
}
public ProductComponentInfo[] retrieveProductComponents() throws RuntimeFault, RemoteException
{
return getVimService().retrieveProductComponents(getMOR());
}
private ServiceContent retrieveServiceContent() throws RuntimeFault, RemoteException
{
return getVimService().retrieveServiceContent(getMOR());
}
public Event[] validateMigration(VirtualMachine[] vms, VirtualMachinePowerState state, String[] testType
, ResourcePool pool, HostSystem host) throws InvalidState, RuntimeFault, RemoteException
{
if(vms==null)
{
throw new IllegalArgumentException("vms must not be null.");
}
return getVimService().validateMigration(getMOR(), MorUtil.createMORs(vms), state, testType,
pool==null? null: pool.getMOR(), host==null? null : host.getMOR());
}
public ServiceContent getServiceContent()
{
if(serviceContent == null)
{
try
{
serviceContent = retrieveServiceContent();
} catch(Exception e)
{
System.out.println("Exceptoin: " + e);
}
}
return serviceContent;
}
public AboutInfo getAboutInfo()
{
return getServiceContent().getAbout();
}
public AlarmManager getAlarmManager()
{
return (AlarmManager) createMO(getServiceContent().getAlarmManager());
}
public AuthorizationManager getAuthorizationManager()
{
return (AuthorizationManager) createMO(getServiceContent().getAuthorizationManager());
}
public CustomFieldsManager getCustomFieldsManager()
{
return (CustomFieldsManager) createMO(getServiceContent().getCustomFieldsManager());
}
public CustomizationSpecManager getCustomizationSpecManager()
{
return (CustomizationSpecManager) createMO(getServiceContent().getCustomizationSpecManager());
}
public EventManager getEventManager()
{
return (EventManager) createMO(getServiceContent().getEventManager());
}
public DiagnosticManager getDiagnosticManager()
{
return (DiagnosticManager) createMO(getServiceContent().getDiagnosticManager());
}
public DistributedVirtualSwitchManager getDistributedVirtualSwitchManager()
{
return (DistributedVirtualSwitchManager) createMO(getServiceContent().getDvSwitchManager());
}
public ExtensionManager getExtensionManager()
{
return (ExtensionManager) createMO(getServiceContent().getExtensionManager());
}
public FileManager getFileManager()
{
return (FileManager) createMO(getServiceContent().getFileManager());
}
public HostLocalAccountManager getAccountManager()
{
return (HostLocalAccountManager) createMO(getServiceContent().getAccountManager());
}
public LicenseManager getLicenseManager()
{
return (LicenseManager) createMO(getServiceContent().getLicenseManager());
}
public LocalizationManager getLocalizationManager()
{
return (LocalizationManager) createMO(getServiceContent().getLocalizationManager());
}
public PerformanceManager getPerformanceManager()
{
return (PerformanceManager) createMO(getServiceContent().getPerfManager());
}
public ProfileComplianceManager getProfileComplianceManager()
{
return (ProfileComplianceManager) createMO(getServiceContent().getComplianceManager());
}
public PropertyCollector getPropertyCollector()
{
return (PropertyCollector) createMO(getServiceContent().getPropertyCollector());
}
public ScheduledTaskManager getScheduledTaskManager()
{
return (ScheduledTaskManager) createMO(getServiceContent().getScheduledTaskManager());
}
public SearchIndex getSearchIndex()
{
return (SearchIndex) createMO(getServiceContent().getSearchIndex());
}
public SessionManager getSessionManager()
{
return (SessionManager) createMO(getServiceContent().getSessionManager());
}
public HostSnmpSystem getHostSnmpSystem()
{
return (HostSnmpSystem) createMO(getServiceContent().getSnmpSystem());
}
public HostProfileManager getHostProfileManager()
{
return (HostProfileManager) createMO(getServiceContent().getHostProfileManager());
}
public IpPoolManager getIpPoolManager()
{
return (IpPoolManager) createMO(getServiceContent().getIpPoolManager());
}
public VirtualMachineProvisioningChecker getVirtualMachineProvisioningChecker()
{
return (VirtualMachineProvisioningChecker) createMO(getServiceContent().getVmProvisioningChecker());
}
public VirtualMachineCompatibilityChecker getVirtualMachineCompatibilityChecker()
{
return (VirtualMachineCompatibilityChecker) createMO(getServiceContent().getVmCompatibilityChecker());
}
public TaskManager getTaskManager()
{
return (TaskManager) createMO(getServiceContent().getTaskManager());
}
public UserDirectory getUserDirectory()
{
return (UserDirectory) createMO(getServiceContent().getUserDirectory());
}
public ViewManager getViewManager()
{
return (ViewManager) createMO(getServiceContent().getViewManager());
}
public VirtualDiskManager getVirtualDiskManager()
{
return (VirtualDiskManager) createMO(getServiceContent().getVirtualDiskManager());
}
public OptionManager getOptionManager()
{
return (OptionManager) createMO(getServiceContent().getSetting());
}
public OvfManager getOvfManager()
{
return (OvfManager) createMO(getServiceContent().getOvfManager());
}
private ManagedObject createMO(ManagedObjectReference mor)
{
return MorUtil.createExactManagedObject(getServerConnection(), mor);
}
// TODO vim.VirtualizationManager is defined in servicecontent but no documentation there. Filed a bug already
}

View File

@ -19,15 +19,14 @@
package org.jclouds.vi.compute;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.vi.ViConstants.PROPERTY_LIBVIRT_DOMAIN_DIR;
import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_SUSPENDED;
import static org.jclouds.vi.reference.ViConstants.PROPERTY_VI_XML_NAMESPACE;
import java.util.Properties;
import org.jclouds.PropertiesBuilder;
import com.vmware.vim25.mo.ServiceInstance;
/**
* Builds properties used in vi Clients
*
@ -37,14 +36,7 @@ public class ViPropertiesBuilder extends PropertiesBuilder {
@Override
protected Properties defaultProperties() {
Properties properties = super.defaultProperties();
// properties.setProperty(PROPERTY_LIBVIRT_DOMAIN_DIR, "/etc/libvirt/qemu");
properties.setProperty(PROPERTY_TIMEOUT_NODE_SUSPENDED, 120 * 1000 + "");
// auth fail sometimes happens in EC2, as the rc.local script that injects the
// authorized key executes after ssh has started
properties.setProperty("jclouds.ssh.max_retries", "7");
properties.setProperty("jclouds.ssh.retryable_messages",
"Auth fail,invalid data,End of IO Stream Read,Connection reset,socket is not established");
properties.setProperty(PROPERTY_VI_XML_NAMESPACE, ServiceInstance.VIM25_NAMESPACE);
return properties;
}

View File

@ -22,18 +22,19 @@ package org.jclouds.vi.compute.config;
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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.rmi.RemoteException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.core.HttpHeaders;
@ -72,9 +73,7 @@ import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.ServerConnection;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.ws.WSClient;
@ -110,33 +109,21 @@ public class ViComputeServiceContextModule
@Provides
@Singleton
protected ServiceInstance createConnection(HttpClient client, @Provider URI endpoint,
protected ServiceInstance createConnection(JcloudsWSClient client,
@Named(Constants.PROPERTY_IDENTITY) String identity, @Named(Constants.PROPERTY_CREDENTIAL) String credential)
throws RemoteException, MalformedURLException, SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException, URISyntaxException {
ServiceInstance foo = new ServiceInstance(endpoint.toURL(), identity, credential, true);
Field serverConnectionField = ServiceInstance.class.getDeclaredField("serverConnection");
serverConnectionField.setAccessible(true);
ServerConnection connection = (ServerConnection) serverConnectionField.get(foo);
Field vimServiceField = ServerConnection.class.getDeclaredField("vimService");
vimServiceField.setAccessible(true);
VimPortType vim = (VimPortType) vimServiceField.get(connection);
Field wscField = VimPortType.class.getDeclaredField("wsc");
wscField.setAccessible(true);
WSClient oldClient = (WSClient) wscField.get(vim);
wscField.set(vim, new JcloudsWSClient(client, oldClient.getBaseUrl().toURI(), oldClient.getCookie(), oldClient
.getVimNameSpace()));
return foo;
throws RemoteException, MalformedURLException {
return new ServiceInstance(client, identity, credential);
}
@Singleton
static class JcloudsWSClient extends WSClient {
private final HttpClient client;
public JcloudsWSClient(HttpClient client, URI baseUrl, String cookie, String vimNameSpace)
throws MalformedURLException {
@Inject
public JcloudsWSClient(HttpClient client, @Provider URI baseUrl,
@Named(PROPERTY_VI_XML_NAMESPACE) String vimNameSpace) throws MalformedURLException {
super(baseUrl.toASCIIString(), false);
this.setCookie(cookie);
this.setVimNameSpace(vimNameSpace);
this.client = client;
}

View File

@ -17,17 +17,15 @@
* ====================================================================
*/
package org.jclouds.vi;
package org.jclouds.vi.reference;
/**
* Configuration properties and constants used in libvirt local connections.
* Configuration properties and constants used in VI connections.
*
* @author Adrian Cole
*/
public interface ViConstants {
public static final String PROPERTY_LIBVIRT_DOMAIN_DIR = "jclouds.libvirt.domain.dir";
public static final String PROPERTY_VI_XML_NAMESPACE = "jclouds.vi.xml.ns";
}
//public static final String PROPERTY_AUTH_TAG = "jclouds.aws.auth.tag";
//public static final String PROPERTY_HEADER_TAG = "jclouds.aws.header.tag";