com.vmware.vim25.VimService Java Examples

The following examples show how to use com.vmware.vim25.VimService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicConnection.java    From cs-actions with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("rawtypes")
private void makeConnection(String url, String username, String password, boolean trustEveryone)
        throws RuntimeFaultFaultMsg,
        InvalidLocaleFaultMsg,
        InvalidLoginFaultMsg,
        KeyManagementException,
        NoSuchAlgorithmException {

    vimService = new VimService();
    vimPort = vimService.getVimPort();

    populateContextMap(url, username, password);

    if (Boolean.TRUE.equals(trustEveryone)) {
        DisableSecurity.trustEveryone();
    }

    serviceContent = vimPort.retrieveServiceContent(this.getServiceInstanceReference());
    userSession = vimPort.login(serviceContent.getSessionManager(), username, password, null);
}
 
Example #2
Source File: VimAuthenticationHelper.java    From vsphere-automation-sdk-java with MIT License 5 votes vote down vote up
/**
 * Creates a session with the server using username and password
 *
 * @param server hostname or ip address of the server to log in to
 * @param username username for login
 * @param password password for login
 */
public void loginByUsernameAndPassword(
    String server, String username, String password) {
    try {
        String vimSdkUrl = "https://" + server + VIM_PATH;

        /*
         * Create a VimService object to obtain a VimPort binding provider.
         * The BindingProvider provides access to the protocol fields
         * in request/response messages. Retrieve the request context
         * which will be used for processing message requests.
         */
        this.vimService = new VimService();
        this.vimPort = vimService.getVimPort();
        Map<String, Object> ctxt =
                ((BindingProvider) vimPort).getRequestContext();

         /*
          * Store the Server URL in the request context and specify true
          * to maintain the connection between the client and server.
          * The client API will include the Server's HTTP cookie in its
          * requests to maintain the session. If you do not set this to
          * true, the Server will start a new session with each request.
          */
        ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vimSdkUrl);
        ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

        // Retrieve the ServiceContent object and login
        this.serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);

        this.vimPort.login(
            serviceContent.getSessionManager(), username, password, null);

    } catch (InvalidLocaleFaultMsg | InvalidLoginFaultMsg | RuntimeFaultFaultMsg e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: VimAuthenticationHelper.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
public VimService getVimService() {
    return this.vimService;
}
 
Example #4
Source File: VMwareClient.java    From development with Apache License 2.0 4 votes vote down vote up
/**
 * Establish a connection to the vCenter.
 */
public void connect() throws Exception {
    // FIXME what to do?
    HostnameVerifier hv = new HostnameVerifier() {
        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };

    int numFailedLogins = 0;
    boolean repeatLogin = true;

    while (repeatLogin) {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(hv);

            VimService vimService = new VimService();
            VimPortType vimPort = vimService.getVimPort();
            Map<String, Object> ctxt = ((BindingProvider) vimPort)
                    .getRequestContext();

            ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
            ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY,
                    Boolean.TRUE);

            ManagedObjectReference morSvcInstance = new ManagedObjectReference();
            morSvcInstance.setType("ServiceInstance");
            morSvcInstance.setValue("ServiceInstance");
            ServiceContent serviceContent = vimPort
                    .retrieveServiceContent(morSvcInstance);
            vimPort.login(serviceContent.getSessionManager(), user,
                    password, null);
            connection = new ServiceConnection(vimPort, serviceContent);
            LOG.debug("Established connection to vSphere. URL: " + url
                    + ", UserId: " + user);

            repeatLogin = false;
        } catch (Exception e) {
            LOG.error("Failed to establish connection to vSphere. URL: "
                    + url + ", UserId: " + user, e);
            if (numFailedLogins > 2) {
                throw e;
            }
            numFailedLogins++;
            repeatLogin = true;
            try {
                Thread.sleep(3000);
            } catch (@SuppressWarnings("unused") InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
 
Example #5
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 3 votes vote down vote up
public static VMwareConnection getVMwareConnection(LoginInfo loginInfo) throws Exception {
    trustAllHttpsCertificates();

    HostnameVerifier hv = new HostnameVerifier() {
        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };

    HttpsURLConnection.setDefaultHostnameVerifier(hv);

    ManagedObjectReference serviceInstanceRef = new ManagedObjectReference();

    final String serviceInstanceName = "ServiceInstance";

    serviceInstanceRef.setType(serviceInstanceName);
    serviceInstanceRef.setValue(serviceInstanceName);

    VimService vimService = new VimService();

    VimPortType vimPortType = vimService.getVimPort();

    Map<String, Object> ctxt = ((BindingProvider)vimPortType).getRequestContext();

    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://" + loginInfo.getHost() + "/sdk");
    ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

    ServiceContent serviceContent = vimPortType.retrieveServiceContent(serviceInstanceRef);

    vimPortType.login(serviceContent.getSessionManager(), loginInfo.getUsername(), loginInfo.getPassword(), null);

    return new VMwareConnection(vimPortType, serviceContent);
}