use of org.apache.cloudstack.api.ResourceDetail in project cloudstack by apache.
the class QueryManagerImpl method listResourceDetails.
@Override
public List<ResourceDetailResponse> listResourceDetails(ListResourceDetailsCmd cmd) {
String key = cmd.getKey();
Boolean forDisplay = cmd.getDisplay();
ResourceTag.ResourceObjectType resourceType = cmd.getResourceType();
String resourceIdStr = cmd.getResourceId();
String value = cmd.getValue();
Long resourceId = null;
//Validation - 1.1 - resourceId and value cant be null.
if (resourceIdStr == null && value == null) {
throw new InvalidParameterValueException("Insufficient parameters passed for listing by resourceId OR key,value pair. Please check your params and try again.");
}
//Validation - 1.2 - Value has to be passed along with key.
if (value != null && key == null) {
throw new InvalidParameterValueException("Listing by (key, value) but key is null. Please check the params and try again");
}
//Validation - 1.3
if (resourceIdStr != null) {
resourceId = _taggedResourceMgr.getResourceId(resourceIdStr, resourceType);
if (resourceId == null) {
throw new InvalidParameterValueException("Cannot find resource with resourceId " + resourceIdStr + " and of resource type " + resourceType);
}
}
List<? extends ResourceDetail> detailList = new ArrayList<ResourceDetail>();
ResourceDetail requestedDetail = null;
if (key == null) {
detailList = _resourceMetaDataMgr.getDetailsList(resourceId, resourceType, forDisplay);
} else if (value == null) {
requestedDetail = _resourceMetaDataMgr.getDetail(resourceId, resourceType, key);
if (requestedDetail != null && forDisplay != null && requestedDetail.isDisplay() != forDisplay) {
requestedDetail = null;
}
} else {
detailList = _resourceMetaDataMgr.getDetails(resourceType, key, value, forDisplay);
}
List<ResourceDetailResponse> responseList = new ArrayList<ResourceDetailResponse>();
if (requestedDetail != null) {
ResourceDetailResponse detailResponse = createResourceDetailsResponse(requestedDetail, resourceType);
responseList.add(detailResponse);
} else {
for (ResourceDetail detail : detailList) {
ResourceDetailResponse detailResponse = createResourceDetailsResponse(detail, resourceType);
responseList.add(detailResponse);
}
}
return responseList;
}
use of org.apache.cloudstack.api.ResourceDetail in project cloudstack by apache.
the class VMSnapshotManagerTest method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
doReturn(admin).when(_vmSnapshotMgr).getCaller();
_vmSnapshotMgr._accountDao = _accountDao;
_vmSnapshotMgr._userVMDao = _userVMDao;
_vmSnapshotMgr._vmSnapshotDao = _vmSnapshotDao;
_vmSnapshotMgr._volumeDao = _volumeDao;
_vmSnapshotMgr._accountMgr = _accountMgr;
_vmSnapshotMgr._snapshotDao = _snapshotDao;
_vmSnapshotMgr._guestOSDao = _guestOSDao;
_vmSnapshotMgr._hypervisorCapabilitiesDao = _hypervisorCapabilitiesDao;
_vmSnapshotMgr._serviceOfferingDetailsDao = _serviceOfferingDetailsDao;
doNothing().when(_accountMgr).checkAccess(any(Account.class), any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
_vmSnapshotMgr._vmSnapshotMax = _vmSnapshotMax;
_vmSnapshotMgr._serviceOfferingDao = _serviceOfferingDao;
_vmSnapshotMgr._userVmDetailsDao = _userVmDetailsDao;
_vmSnapshotMgr._vmSnapshotDetailsDao = _vmSnapshotDetailsDao;
_vmSnapshotMgr._userVmManager = _userVmManager;
when(_userVMDao.findById(anyLong())).thenReturn(vmMock);
when(_vmSnapshotDao.findByName(anyLong(), anyString())).thenReturn(null);
when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(new ArrayList<VMSnapshotVO>());
when(_hypervisorCapabilitiesDao.isVmSnapshotEnabled(Hypervisor.HypervisorType.XenServer, "default")).thenReturn(true);
when(_serviceOfferingDetailsDao.findDetail(anyLong(), anyString())).thenReturn(null);
List<VolumeVO> mockVolumeList = new ArrayList<VolumeVO>();
mockVolumeList.add(volumeMock);
when(volumeMock.getInstanceId()).thenReturn(TEST_VM_ID);
when(_volumeDao.findByInstance(anyLong())).thenReturn(mockVolumeList);
when(vmMock.getId()).thenReturn(TEST_VM_ID);
when(vmMock.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
when(vmMock.getAccountId()).thenReturn(1L);
when(vmMock.getDomainId()).thenReturn(1L);
when(vmMock.getInstanceName()).thenReturn("i-3-VM-TEST");
when(vmMock.getState()).thenReturn(State.Running);
when(vmMock.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.XenServer);
when(_guestOSDao.findById(anyLong())).thenReturn(mock(GuestOSVO.class));
when(vmSnapshotVO.getId()).thenReturn(VM_SNAPSHOT_ID);
when(serviceOffering.isDynamic()).thenReturn(false);
when(_serviceOfferingDao.findById(SERVICE_OFFERING_ID)).thenReturn(serviceOffering);
for (ResourceDetail detail : Arrays.asList(userVmDetailCpuNumber, vmSnapshotDetailCpuNumber)) {
when(detail.getName()).thenReturn("cpuNumber");
when(detail.getValue()).thenReturn("2");
when(detail.isDisplay()).thenReturn(true);
}
for (ResourceDetail detail : Arrays.asList(userVmDetailMemory, vmSnapshotDetailMemory)) {
when(detail.getName()).thenReturn("memory");
when(detail.getValue()).thenReturn("2048");
when(detail.isDisplay()).thenReturn(true);
}
userVmDetails = Arrays.asList(userVmDetailCpuNumber, userVmDetailMemory);
vmSnapshotDetails = Arrays.asList(vmSnapshotDetailCpuNumber, vmSnapshotDetailMemory);
when(_userVmDetailsDao.listDetails(TEST_VM_ID)).thenReturn(userVmDetails);
when(_vmSnapshotDetailsDao.listDetails(VM_SNAPSHOT_ID)).thenReturn(vmSnapshotDetails);
when(userVm.getId()).thenReturn(TEST_VM_ID);
when(userVm.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
when(vmSnapshotVO.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
}
use of org.apache.cloudstack.api.ResourceDetail in project cloudstack by apache.
the class CloudZonesStartupProcessor method updateComputeHost.
protected void updateComputeHost(final HostVO host, final StartupCommand startup, final Host.Type type) throws AgentAuthnException {
String zoneToken = startup.getDataCenter();
if (zoneToken == null) {
s_logger.warn("No Zone Token passed in, cannot not find zone for the agent");
throw new AgentAuthnException("No Zone Token passed in, cannot not find zone for agent");
}
DataCenterVO zone = _zoneDao.findByToken(zoneToken);
if (zone == null) {
zone = _zoneDao.findByName(zoneToken);
if (zone == null) {
try {
long zoneId = Long.parseLong(zoneToken);
zone = _zoneDao.findById(zoneId);
if (zone == null) {
throw new AgentAuthnException("Could not find zone for agent with token " + zoneToken);
}
} catch (NumberFormatException nfe) {
throw new AgentAuthnException("Could not find zone for agent with token " + zoneToken);
}
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully loaded the DataCenter from the zone token passed in ");
}
long zoneId = zone.getId();
ResourceDetail maxHostsInZone = _zoneDetailsDao.findDetail(zoneId, ZoneConfig.MaxHosts.key());
if (maxHostsInZone != null) {
long maxHosts = Long.parseLong(maxHostsInZone.getValue());
long currentCountOfHosts = _hostDao.countRoutingHostsByDataCenter(zoneId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Number of hosts in Zone:" + currentCountOfHosts + ", max hosts limit: " + maxHosts);
}
if (currentCountOfHosts >= maxHosts) {
throw new AgentAuthnException("Number of running Routing hosts in the Zone:" + zone.getName() + " is already at the max limit:" + maxHosts + ", cannot start one more host");
}
}
HostPodVO pod = null;
if (startup.getPrivateIpAddress() == null) {
s_logger.warn("No private IP address passed in for the agent, cannot not find pod for agent");
throw new AgentAuthnException("No private IP address passed in for the agent, cannot not find pod for agent");
}
if (startup.getPrivateNetmask() == null) {
s_logger.warn("No netmask passed in for the agent, cannot not find pod for agent");
throw new AgentAuthnException("No netmask passed in for the agent, cannot not find pod for agent");
}
if (host.getPodId() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Pod is already created for this agent, looks like agent is reconnecting...");
}
pod = _podDao.findById(host.getPodId());
if (!checkCIDR(type, pod, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
pod = null;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Subnet of Pod does not match the subnet of the agent, not using this Pod: " + host.getPodId());
}
} else {
updatePodNetmaskIfNeeded(pod, startup.getPrivateNetmask());
}
}
if (pod == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Trying to detect the Pod to use from the agent's ip address and netmask passed in ");
}
//deduce pod
boolean podFound = false;
List<HostPodVO> podsInZone = _podDao.listByDataCenterId(zoneId);
for (HostPodVO hostPod : podsInZone) {
if (checkCIDR(type, hostPod, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
pod = hostPod;
//found the default POD having the same subnet.
updatePodNetmaskIfNeeded(pod, startup.getPrivateNetmask());
podFound = true;
break;
}
}
if (!podFound) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating a new Pod since no default Pod found that matches the agent's ip address and netmask passed in ");
}
if (startup.getGatewayIpAddress() == null) {
s_logger.warn("No Gateway IP address passed in for the agent, cannot create a new pod for the agent");
throw new AgentAuthnException("No Gateway IP address passed in for the agent, cannot create a new pod for the agent");
}
//auto-create a new pod, since pod matching the agent's ip is not found
String podName = "POD-" + (podsInZone.size() + 1);
try {
String gateway = startup.getGatewayIpAddress();
String cidr = NetUtils.getCidrFromGatewayAndNetmask(gateway, startup.getPrivateNetmask());
String[] cidrPair = cidr.split("\\/");
String cidrAddress = cidrPair[0];
long cidrSize = Long.parseLong(cidrPair[1]);
String startIp = NetUtils.getIpRangeStartIpFromCidr(cidrAddress, cidrSize);
String endIp = NetUtils.getIpRangeEndIpFromCidr(cidrAddress, cidrSize);
pod = _configurationManager.createPod(-1, podName, zoneId, gateway, cidr, startIp, endIp, null, true);
} catch (Exception e) {
// no longer tolerate exception during the cluster creation phase
throw new CloudRuntimeException("Unable to create new Pod " + podName + " in Zone: " + zoneId, e);
}
}
}
final StartupRoutingCommand scc = (StartupRoutingCommand) startup;
ClusterVO cluster = null;
if (host.getClusterId() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Cluster is already created for this agent, looks like agent is reconnecting...");
}
cluster = _clusterDao.findById(host.getClusterId());
}
if (cluster == null) {
//auto-create cluster - assume one host per cluster
String clusterName = "Cluster-" + startup.getPrivateIpAddress();
ClusterVO existingCluster = _clusterDao.findBy(clusterName, pod.getId());
if (existingCluster != null) {
cluster = existingCluster;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating a new Cluster for this agent with name: " + clusterName + " in Pod: " + pod.getId() + ", in Zone:" + zoneId);
}
cluster = new ClusterVO(zoneId, pod.getId(), clusterName);
cluster.setHypervisorType(scc.getHypervisorType().toString());
try {
cluster = _clusterDao.persist(cluster);
} catch (Exception e) {
// no longer tolerate exception during the cluster creation phase
throw new CloudRuntimeException("Unable to create cluster " + clusterName + " in pod " + pod.getId() + " and data center " + zoneId, e);
}
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Detected Zone: " + zoneId + ", Pod: " + pod.getId() + ", Cluster:" + cluster.getId());
}
host.setDataCenterId(zone.getId());
host.setPodId(pod.getId());
host.setClusterId(cluster.getId());
host.setPrivateIpAddress(startup.getPrivateIpAddress());
host.setPrivateNetmask(startup.getPrivateNetmask());
host.setPrivateMacAddress(startup.getPrivateMacAddress());
host.setPublicIpAddress(startup.getPublicIpAddress());
host.setPublicMacAddress(startup.getPublicMacAddress());
host.setPublicNetmask(startup.getPublicNetmask());
host.setStorageIpAddress(startup.getStorageIpAddress());
host.setStorageMacAddress(startup.getStorageMacAddress());
host.setStorageNetmask(startup.getStorageNetmask());
host.setVersion(startup.getVersion());
host.setName(startup.getName());
host.setType(type);
host.setStorageUrl(startup.getIqn());
host.setLastPinged(System.currentTimeMillis() >> 10);
host.setCaps(scc.getCapabilities());
host.setCpus(scc.getCpus());
host.setTotalMemory(scc.getMemory());
host.setSpeed(scc.getSpeed());
HypervisorType hyType = scc.getHypervisorType();
host.setHypervisorType(hyType);
host.setHypervisorVersion(scc.getHypervisorVersion());
updateHostDetails(host, scc);
}
Aggregations