Search in sources :

Example 1 with ServiceInstance

use of com.vmware.vim.binding.vim.ServiceInstance in project flowgate by vmware.

the class AuthVcUser method authenticateUser.

public void authenticateUser(String name, String password) throws URISyntaxException, InvalidLogin, InvalidLocale, SSLException {
    Client vmomiClient = null;
    SessionManager sessionManager = null;
    try {
        URI uri = new URI(serviceUrl);
        HttpConfiguration httpConfig = new HttpConfigurationImpl();
        httpConfig.setThumbprintVerifier(getThumbprintVerifier());
        HttpClientConfiguration clientConfig = HttpClientConfiguration.Factory.newInstance();
        // set customized SSL protocols
        // TlsClientConfiguration tlsClientConfiguration = new TlsClientConfiguration();
        httpConfig.setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" });
        clientConfig.setHttpConfiguration(httpConfig);
        vmomiClient = Client.Factory.createClient(uri, version, clientConfig);
        ManagedObjectReference svcRef = new ManagedObjectReference();
        svcRef.setType("ServiceInstance");
        svcRef.setValue("ServiceInstance");
        if (name == null || name.isEmpty()) {
            // VC session token auth
            vmomiClient.getBinding().setSession(vmomiClient.getBinding().createSession(password));
        }
        ServiceInstance instance = vmomiClient.createStub(ServiceInstance.class, svcRef);
        ServiceInstanceContent instanceContent = instance.retrieveContent();
        sessionManager = vmomiClient.createStub(SessionManager.class, instanceContent.getSessionManager());
        if (name != null && !name.isEmpty()) {
            // username/passowrd auth
            sessionManager.login(name, password, sessionManager.getDefaultLocale());
            sessionManager.logout();
        } else {
            // VC session token auth
            UserSession session = sessionManager.getCurrentSession();
            if (session == null) {
                throw new WormholeException("invalid vc session.");
            } else {
                logger.info(session.getUserName() + " is authenticated");
            }
        }
    } finally {
        if (vmomiClient != null) {
            vmomiClient.shutdown();
        }
    }
}
Also used : ServiceInstanceContent(com.vmware.vim.binding.vim.ServiceInstanceContent) HttpConfigurationImpl(com.vmware.vim.vmomi.client.http.impl.HttpConfigurationImpl) SessionManager(com.vmware.vim.binding.vim.SessionManager) UserSession(com.vmware.vim.binding.vim.UserSession) ServiceInstance(com.vmware.vim.binding.vim.ServiceInstance) WormholeException(com.vmware.flowgate.common.exception.WormholeException) HttpConfiguration(com.vmware.vim.vmomi.client.http.HttpConfiguration) Client(com.vmware.vim.vmomi.client.Client) URI(java.net.URI) HttpClientConfiguration(com.vmware.vim.vmomi.client.http.HttpClientConfiguration) ManagedObjectReference(com.vmware.vim.binding.vmodl.ManagedObjectReference)

Aggregations

WormholeException (com.vmware.flowgate.common.exception.WormholeException)1 ServiceInstance (com.vmware.vim.binding.vim.ServiceInstance)1 ServiceInstanceContent (com.vmware.vim.binding.vim.ServiceInstanceContent)1 SessionManager (com.vmware.vim.binding.vim.SessionManager)1 UserSession (com.vmware.vim.binding.vim.UserSession)1 ManagedObjectReference (com.vmware.vim.binding.vmodl.ManagedObjectReference)1 Client (com.vmware.vim.vmomi.client.Client)1 HttpClientConfiguration (com.vmware.vim.vmomi.client.http.HttpClientConfiguration)1 HttpConfiguration (com.vmware.vim.vmomi.client.http.HttpConfiguration)1 HttpConfigurationImpl (com.vmware.vim.vmomi.client.http.impl.HttpConfigurationImpl)1 URI (java.net.URI)1