use of com.vmware.vim25.mo.Datacenter in project cloudstack by apache.
the class VmwareClient method validate.
public boolean validate() {
//
// There is no official API to validate an open vCenter API session. This is hacking way to tell if
// an open vCenter API session is still valid for making calls.
//
// It will give false result if there really does not exist data-center in the inventory, however, I consider
// this really is not possible in production deployment
//
// Create PropertySpecs
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datacenter");
pSpec.setAll(false);
pSpec.getPathSet().add("name");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(getRootFolder());
oSpec.setSkip(false);
oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.getPropSet().add(pSpec);
spec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
specArr.add(spec);
try {
List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
if (ocary != null && ocary.size() > 0)
return true;
} catch (Exception e) {
return false;
}
return false;
}
use of com.vmware.vim25.mo.Datacenter in project cloudstack by apache.
the class DatastoreMO method getOwnerDatacenter.
public Pair<DatacenterMO, String> getOwnerDatacenter() throws Exception {
if (_ownerDc != null)
return _ownerDc;
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datacenter");
pSpec.getPathSet().add("name");
TraversalSpec folderParentTraversal = new TraversalSpec();
folderParentTraversal.setType("Folder");
folderParentTraversal.setPath("parent");
folderParentTraversal.setName("folderParentTraversal");
SelectionSpec sSpec = new SelectionSpec();
sSpec.setName("folderParentTraversal");
folderParentTraversal.getSelectSet().add(sSpec);
TraversalSpec dsParentTraversal = new TraversalSpec();
dsParentTraversal.setType("Datastore");
dsParentTraversal.setPath("parent");
dsParentTraversal.setName("dsParentTraversal");
dsParentTraversal.getSelectSet().add(folderParentTraversal);
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(getMor());
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(dsParentTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
assert (ocs != null && ocs.size() > 0);
assert (ocs.get(0).getObj() != null);
assert (ocs.get(0).getPropSet() != null);
String dcName = ocs.get(0).getPropSet().get(0).getVal().toString();
_ownerDc = new Pair<DatacenterMO, String>(new DatacenterMO(_context, ocs.get(0).getObj()), dcName);
return _ownerDc;
}
use of com.vmware.vim25.mo.Datacenter in project cloudstack by apache.
the class DatacenterMO method getDvSwitchMor.
public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupMor) throws Exception {
String dvPortGroupKey = null;
ManagedObjectReference dvSwitchMor = null;
PropertySpec pSpec = new PropertySpec();
pSpec.setType("DistributedVirtualPortgroup");
pSpec.getPathSet().add("key");
pSpec.getPathSet().add("config.distributedVirtualSwitch");
TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
datacenter2DvPortGroupTraversal.setType("Datacenter");
datacenter2DvPortGroupTraversal.setPath("network");
datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
if (ocs != null) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
assert (props.size() == 2);
for (DynamicProperty prop : props) {
if (prop.getName().equals("key")) {
dvPortGroupKey = (String) prop.getVal();
} else {
dvSwitchMor = (ManagedObjectReference) prop.getVal();
}
}
if ((dvPortGroupKey != null) && dvPortGroupKey.equals(dvPortGroupMor.getValue())) {
return dvSwitchMor;
}
}
}
}
return null;
}
use of com.vmware.vim25.mo.Datacenter in project cloudstack by apache.
the class VmwareManagerImpl method addVmwareDatacenter.
@Override
@DB
public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws ResourceInUseException {
VmwareDatacenterVO vmwareDc = null;
Long zoneId = cmd.getZoneId();
String userName = cmd.getUsername();
String password = cmd.getPassword();
String vCenterHost = cmd.getVcenter();
String vmwareDcName = cmd.getName();
// Validate username, password, VMware DC name and vCenter
if (userName == null) {
throw new InvalidParameterValueException("Missing or invalid parameter username.");
}
if (password == null) {
throw new InvalidParameterValueException("Missing or invalid parameter username.");
}
if (vmwareDcName == null) {
throw new InvalidParameterValueException("Missing or invalid parameter name. Please provide valid VMware datacenter name.");
}
if (vCenterHost == null) {
throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid VMware vCenter server's IP address or fully qualified domain name.");
}
if (zoneId == null) {
throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid zone id.");
}
// Zone validation
validateZone(zoneId);
VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao.findByZoneId(zoneId);
// Check if zone is associated with VMware DC
if (vmwareDcZoneMap != null) {
// Check if the associated VMware DC matches the one specified in API params
// This check would yield success as the association exists between same entities (zone and VMware DC)
// This scenario would result in if the API addVmwareDc is called more than once with same parameters.
Long associatedVmwareDcId = vmwareDcZoneMap.getVmwareDcId();
VmwareDatacenterVO associatedVmwareDc = _vmwareDcDao.findById(associatedVmwareDcId);
if (associatedVmwareDc.getVcenterHost().equalsIgnoreCase(vCenterHost) && associatedVmwareDc.getVmwareDatacenterName().equalsIgnoreCase(vmwareDcName)) {
s_logger.info("Ignoring API call addVmwareDc, because VMware DC " + vCenterHost + "/" + vmwareDcName + " is already associated with specified zone with id " + zoneId);
return associatedVmwareDc;
} else {
throw new CloudRuntimeException("Zone " + zoneId + " is already associated with a VMware datacenter. " + "Only 1 VMware DC can be associated with a zone.");
}
}
// Zone validation to check if the zone already has resources.
// Association of VMware DC to zone is not allowed if zone already has resources added.
validateZoneWithResources(zoneId, "add VMware datacenter to zone");
// Check if DC is already part of zone
// In that case vmware_data_center table should have the DC
vmwareDc = _vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost);
if (vmwareDc != null) {
throw new ResourceInUseException("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones.");
}
VmwareContext context = null;
DatacenterMO dcMo = null;
String dcCustomFieldValue;
boolean addDcCustomFieldDef = false;
boolean dcInUse = false;
String guid;
ManagedObjectReference dcMor;
try {
context = VmwareContextFactory.create(vCenterHost, userName, password);
// Check if DC exists on vCenter
dcMo = new DatacenterMO(context, vmwareDcName);
dcMor = dcMo.getMor();
if (dcMor == null) {
String msg = "Unable to find VMware DC " + vmwareDcName + " in vCenter " + vCenterHost + ". ";
s_logger.error(msg);
throw new InvalidParameterValueException(msg);
}
// Check if DC is already associated with another cloudstack deployment
// Get custom field property cloud.zone over this DC
guid = vmwareDcName + "@" + vCenterHost;
dcCustomFieldValue = dcMo.getCustomFieldValue(CustomFieldConstants.CLOUD_ZONE);
if (dcCustomFieldValue == null) {
addDcCustomFieldDef = true;
}
dcInUse = Boolean.parseBoolean(dcCustomFieldValue);
if (dcInUse) {
throw new ResourceInUseException("This DC is being managed by other CloudStack deployment. Cannot add this DC to zone.");
}
// Add DC to database into vmware_data_center table
vmwareDc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, userName, password);
vmwareDc = _vmwareDcDao.persist(vmwareDc);
// Map zone with vmware datacenter
vmwareDcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDc.getId());
vmwareDcZoneMap = _vmwareDcZoneMapDao.persist(vmwareDcZoneMap);
// Set custom field for this DC
if (addDcCustomFieldDef) {
dcMo.ensureCustomFieldDef(CustomFieldConstants.CLOUD_ZONE);
}
dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "true");
} catch (Throwable e) {
String msg = "Failed to add VMware DC to zone ";
if (e instanceof RemoteException) {
msg = "Encountered remote exception at vCenter. " + VmwareHelper.getExceptionMessage(e);
} else {
msg += "due to : " + e.getMessage();
}
throw new CloudRuntimeException(msg);
} finally {
if (context != null) {
context.close();
}
context = null;
}
return vmwareDc;
}
use of com.vmware.vim25.mo.Datacenter in project cloudstack by apache.
the class VmwareStorageProcessor method createTemplateFromVolume.
@Override
public Answer createTemplateFromVolume(CopyCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
DataStoreTO imageStore = template.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO) imageStore;
String secondaryStoragePoolURL = nfsImageStore.getUrl();
String volumePath = volume.getPath();
String details = null;
VmwareContext context = hostService.getServiceContext(cmd);
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(volume.getVmName());
if (vmMo == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to find the owner VM for CreatePrivateTemplateFromVolumeCommand on host " + hyperHost.getHyperHostName() + ", try within datacenter");
}
vmMo = hyperHost.findVmOnPeerHyperHost(volume.getVmName());
if (vmMo == null) {
// This means either the volume is on a zone wide storage pool or VM is deleted by external entity.
// Look for the VM in the datacenter.
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dcMo = new DatacenterMO(context, dcMor);
vmMo = dcMo.findVm(volume.getVmName());
}
if (vmMo == null) {
String msg = "Unable to find the owner VM for volume operation. vm: " + volume.getVmName();
s_logger.error(msg);
throw new Exception(msg);
}
}
Ternary<String, Long, Long> result = createTemplateFromVolume(vmMo, template.getPath(), template.getId(), template.getName(), secondaryStoragePoolURL, volumePath, hostService.getWorkerName(context, cmd, 0), _nfsVersion);
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(result.first());
newTemplate.setFormat(ImageFormat.OVA);
newTemplate.setSize(result.third());
newTemplate.setPhysicalSize(result.second());
return new CopyCmdAnswer(newTemplate);
} catch (Throwable e) {
if (e instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
s_logger.error("Unexpecpted exception ", e);
details = "create template from volume exception: " + VmwareHelper.getExceptionMessage(e);
return new CopyCmdAnswer(details);
}
}
Aggregations