use of com.emc.storageos.vcentercontroller.VcenterController in project coprhd-controller by CoprHD.
the class VcenterDataCenterService method createOrUpdateVcenterCluster.
/*
* @param create - Whether to create a new cluster vs update an existing
*/
private TaskResourceRep createOrUpdateVcenterCluster(boolean createCluster, URI id, URI clusterId, List<URI> addHosts, List<URI> removeHosts) {
_log.info("createOrUpdateVcenterCluster " + createCluster + " " + id + " " + clusterId);
ArgValidator.checkFieldUriType(id, VcenterDataCenter.class, "id");
VcenterDataCenter vcenterDataCenter = queryObject(VcenterDataCenter.class, id, false);
ArgValidator.checkEntity(vcenterDataCenter, id, isIdEmbeddedInURL(id));
ArgValidator.checkFieldUriType(clusterId, Cluster.class, "clusterId");
Cluster cluster = queryObject(Cluster.class, clusterId, false);
ArgValidator.checkEntity(cluster, clusterId, isIdEmbeddedInURL(clusterId));
verifyAuthorizedInTenantOrg(cluster.getTenant(), getUserFromContext());
/*
* Check if explicit add host or remove hosts are specified
* If one or both are, only execute whats specified
* If nothing is specified, do automatic host selection and import all hosts in cluster
*/
Collection<URI> addHostUris = new ArrayList<URI>();
Collection<URI> removeHostUris = new ArrayList<URI>();
boolean manualHostSpecification = false;
if (addHosts != null && !addHosts.isEmpty()) {
_log.info("Request to explicitly add hosts " + addHosts);
manualHostSpecification = true;
}
if (removeHosts != null && !removeHosts.isEmpty()) {
_log.info("Request to explicitly remove hosts " + removeHosts);
manualHostSpecification = true;
}
if (manualHostSpecification) {
for (URI uri : addHosts) {
Host host = queryObject(Host.class, uri, false);
if (isHostCompatibleForVcenterCluster(host)) {
addHostUris.add(uri);
}
}
for (URI uri : removeHosts) {
Host host = queryObject(Host.class, uri, false);
if (isHostCompatibleForVcenterCluster(host)) {
removeHostUris.add(uri);
}
}
} else {
// If no hosts specified by default add all hosts within cluster
Collection<URI> hostUris = new ArrayList<URI>();
List<NamedElementQueryResultList.NamedElement> hostNamedElements = listChildren(clusterId, Host.class, "label", "cluster");
for (NamedElementQueryResultList.NamedElement hostNamedElement : hostNamedElements) {
Host host = queryObject(Host.class, hostNamedElement.getId(), false);
if (isHostCompatibleForVcenterCluster(host)) {
addHostUris.add(host.getId());
}
}
if (addHostUris.isEmpty()) {
// Require at least a single compatible host for automatic mode, do not create empty cluster
_log.error("Cluster " + cluster.getLabel() + " does not contain any ESX/ESXi hosts and is thus incompatible for vCenter operations");
throw APIException.badRequests.clusterContainsNoCompatibleHostsForVcenter();
}
}
// Find all shared volumes in the cluster
List<URI> volumeUris = new ArrayList<URI>();
try {
List<ExportGroup> exportGroups = CustomQueryUtility.queryActiveResourcesByConstraint(_dbClient, ExportGroup.class, AlternateIdConstraint.Factory.getConstraint(ExportGroup.class, "clusters", cluster.getId().toString()));
_log.info("Found " + exportGroups.size() + " export groups for cluster " + cluster.getLabel());
for (ExportGroup exportGroup : exportGroups) {
_log.info("Cluster " + cluster.getLabel() + " has export group " + exportGroup.getLabel() + " of type " + exportGroup.getType());
if (exportGroup.forCluster()) {
_log.info("Export group " + exportGroup.getLabel() + " is cluster/shared type");
StringMap volumes = exportGroup.getVolumes();
_log.info("Export group " + exportGroup.getLabel() + " has " + volumes.size() + " volumes");
for (String volumeUriString : volumes.keySet()) {
_log.info("Volume URI " + volumeUriString + " found in export group " + exportGroup.getLabel());
URI uri = URI.create(volumeUriString);
volumeUris.add(uri);
}
}
}
} catch (Exception e) {
_log.error("Exception navigating cluster export groups for shared volumes " + e);
// for time being just ignore
}
Collection<Volume> volumes = _dbClient.queryObject(Volume.class, volumeUris);
for (Volume volume : volumes) {
_log.info("Volume " + volume.getLabel() + " " + volume.getWWN() + " is shared and compatible for VMFS datastore");
}
String taskId = UUID.randomUUID().toString();
Operation op = new Operation();
op.setResourceType(createCluster ? ResourceOperationTypeEnum.CREATE_VCENTER_CLUSTER : ResourceOperationTypeEnum.UPDATE_VCENTER_CLUSTER);
_dbClient.createTaskOpStatus(VcenterDataCenter.class, vcenterDataCenter.getId(), taskId, op);
AsyncTask task = new AsyncTask(VcenterDataCenter.class, vcenterDataCenter.getId(), taskId);
VcenterController vcenterController = getController(VcenterController.class, null);
if (createCluster) {
vcenterController.createVcenterCluster(task, clusterId, addHostUris.toArray(new URI[0]), volumeUris.toArray(new URI[0]));
} else {
vcenterController.updateVcenterCluster(task, clusterId, addHostUris.toArray(new URI[0]), removeHostUris.toArray(new URI[0]), volumeUris.toArray(new URI[0]));
}
auditOp(OperationTypeEnum.CREATE_UPDATE_VCENTER_CLUSTER, true, null, vcenterDataCenter.auditParameters());
return toTask(vcenterDataCenter, taskId, op);
}
Aggregations