Search in sources :

Example 6 with BasicConnection

use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection in project photon-model by vmware.

the class SanityCheckTest method example.

@Test
public void example() throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    BasicConnection conn = new BasicConnection();
    String url = System.getProperty("vsphere.url");
    String username = System.getProperty("vsphere.username");
    String password = System.getProperty("vsphere.password");
    conn.setURI(URI.create(url));
    conn.setUsername(username);
    conn.setPassword(password);
    conn.setIgnoreSslErrors(true);
    conn.setRequestTimeout(30, TimeUnit.SECONDS);
    conn.connect();
    AboutInfo about = conn.getServiceContent().getAbout();
    System.out.println(Utils.toJsonHtml(about));
    ManagedObjectReference rootFolder = conn.getServiceContent().getRootFolder();
    GetMoRef getMoRef = new GetMoRef(conn);
    String name = getMoRef.entityProp(rootFolder, "name");
    System.out.println("Root folder is called \'" + name + "\'");
}
Also used : GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) AboutInfo(com.vmware.vim25.AboutInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test)

Example 7 with BasicConnection

use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection in project photon-model by vmware.

the class ConnectionRule method createConnection.

private static BasicConnection createConnection() {
    if (!shouldRun()) {
        return null;
    }
    BasicConnection c = new BasicConnection();
    c.setIgnoreSslErrors(true);
    c.setURI(URI.create(System.getProperty(VC_URL)));
    c.setUsername(System.getProperty(VC_USERNAME));
    c.setPassword(System.getProperty(VC_PASSWORD));
    return c;
}
Also used : BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection)

Example 8 with BasicConnection

use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection in project photon-model by vmware.

the class DatacenterEnumeratorService method handlePatch.

@Override
public void handlePatch(Operation patch) {
    if (!patch.hasBody()) {
        patch.fail(new IllegalArgumentException("body is required"));
        return;
    }
    VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
        EnumerateDatacentersRequest req = patch.getBody(EnumerateDatacentersRequest.class);
        BasicConnection connection = new BasicConnection();
        try {
            EnumerateDatacentersResponse res = new EnumerateDatacentersResponse();
            if (req.isMock) {
                res.datacenters = Collections.singletonList("dc-1");
                res.moRefs = Collections.singletonList("Datacenter:dc-1");
            } else {
                connection.setURI(URI.create("https://" + req.host + "/sdk"));
                connection.setUsername(req.username);
                connection.setPassword(req.password);
                connection.setIgnoreSslErrors(true);
                connection.connect();
                DatacenterLister lister = new DatacenterLister(connection);
                res.moRefs = new ArrayList<>();
                res.datacenters = new ArrayList<>();
                lister.listAllDatacenters().stream().forEach(el -> {
                    res.datacenters.add(DatacenterLister.prettifyPath(el.path));
                    res.moRefs.add(VimUtils.convertMoRefToString(el.object));
                });
            }
            patch.setBody(res);
            patch.complete();
        } catch (Exception e) {
            patch.fail(e);
        } finally {
            connection.closeQuietly();
        }
    });
}
Also used : BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)

Example 9 with BasicConnection

use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection in project photon-model by vmware.

the class VSphereEndpointAdapterService method createConnection.

private BasicConnection createConnection(URI adapterReference, AuthCredentialsServiceState auth) {
    BasicConnection connection = new BasicConnection();
    // ignores the certificate for testing purposes
    if (VSPHERE_IGNORE_CERTIFICATE_WARNINGS) {
        connection.setIgnoreSslErrors(true);
    } else {
        ServerX509TrustManager trustManager = ServerX509TrustManager.getInstance();
        connection.setTrustManager(trustManager);
    }
    connection.setUsername(auth.privateKeyId);
    connection.setPassword(EncryptionUtils.decrypt(auth.privateKey));
    connection.setURI(adapterReference);
    return connection;
}
Also used : ServerX509TrustManager(com.vmware.photon.controller.model.security.ssl.ServerX509TrustManager) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection)

Example 10 with BasicConnection

use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection in project photon-model by vmware.

the class VSphereEndpointAdapterService method doValidate.

private void doValidate(AuthCredentialsServiceState credentials, BiConsumer<ServiceErrorResponse, Throwable> callback, URI adapterManagementUri, String id) {
    BasicConnection connection = createConnection(adapterManagementUri, credentials);
    try {
        // login and session creation
        connection.connect();
        if (id != null && !id.isEmpty()) {
            // if a datacenter is configured also validate moref is OK
            new GetMoRef(connection).entityProp(VimUtils.convertStringToMoRef(id), VimNames.PROPERTY_NAME);
        }
        callback.accept(null, null);
    } catch (RuntimeFaultFaultMsg | InvalidPropertyFaultMsg | IllegalArgumentException e) {
        ServiceErrorResponse r = Utils.toServiceErrorResponse(e);
        r.statusCode = STATUS_CODE_BAD_REQUEST;
        r.message = String.format("Error looking for datacenter for id '%s'", id);
        callback.accept(r, e);
    } catch (ConnectionException e) {
        String msg = String.format("Cannot establish connection to %s", adapterManagementUri);
        logWarning(msg);
        callback.accept(null, e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) ConnectionException(com.vmware.photon.controller.model.adapters.vsphere.util.connection.ConnectionException)

Aggregations

BasicConnection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection)20 GetMoRef (com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef)11 Test (org.junit.Test)10 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)9 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)6 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)4 ProvisionComputeTaskService (com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService)4 VirtualDisk (com.vmware.vim25.VirtualDisk)3 Operation (com.vmware.xenon.common.Operation)3 DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)2 DiskService (com.vmware.photon.controller.model.resources.DiskService)2 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)2 RegionEnumerationResponse (com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse)1 EndpointAdapterUtils (com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils)1 PROVIDER_DISK_UNIQUE_ID (com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.PROVIDER_DISK_UNIQUE_ID)1 EnumerationClientTest (com.vmware.photon.controller.model.adapters.vsphere.EnumerationClientTest)1 ImportOvfRequest (com.vmware.photon.controller.model.adapters.vsphere.ovf.ImportOvfRequest)1 ConnectionException (com.vmware.photon.controller.model.adapters.vsphere.util.connection.ConnectionException)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)1