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 + "\'");
}
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;
}
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();
}
});
}
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;
}
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);
}
}
Aggregations