Skip to content

Commit bb87f11

Browse files
BryanMLimaneogismm
authored andcommitted
Validate host tags on VM live scale (apache#6409)
* Validate host tags on VM live scale * Remove extra spaces
1 parent b6ae266 commit bb87f11

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

engine/schema/src/main/java/com/cloud/host/HostVO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.cloud.utils.NumbersUtil;
4747
import com.cloud.utils.db.GenericDao;
4848
import java.util.Arrays;
49+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
4950
import org.apache.commons.lang3.StringUtils;
5051

5152
@Entity
@@ -680,7 +681,7 @@ public boolean equals(Object obj) {
680681

681682
@Override
682683
public String toString() {
683-
return String.format("Host {\"id\": \"%s\", \"name\": \"%s\", \"uuid\": \"%s\", \"type\"=\"%s\"}", id, name, uuid, type);
684+
return String.format("Host %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "name", "uuid", "type"));
684685
}
685686

686687
public void setHypervisorType(HypervisorType hypervisorType) {

engine/schema/src/main/java/com/cloud/service/ServiceOfferingVO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.cloud.offering.ServiceOffering;
3636
import com.cloud.utils.db.GenericDao;
3737
import com.cloud.vm.VirtualMachine;
38+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
3839

3940
@Entity
4041
@Table(name = "service_offering")
@@ -424,7 +425,7 @@ public String getUuid() {
424425

425426
@Override
426427
public String toString() {
427-
return String.format("Service offering {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\"}", getId(), getName(), getUuid());
428+
return String.format("Service offering %s.", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "name", "uuid"));
428429
}
429430

430431
public boolean isDynamicScalingEnabled() {

engine/schema/src/main/java/com/cloud/vm/VMInstanceVO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import javax.persistence.Transient;
4242

4343
import org.apache.cloudstack.backup.Backup;
44+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
4445
import org.apache.commons.codec.binary.Base64;
4546
import org.apache.log4j.Logger;
4647

@@ -501,7 +502,7 @@ public void setRemoved(Date removed) {
501502

502503
@Override
503504
public String toString() {
504-
return String.format("VM instance {id: \"%s\", name: \"%s\", uuid: \"%s\", type=\"%s\"}", id, getInstanceName(), uuid, type);
505+
return String.format("VM instance %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "instanceName", "uuid", "type"));
505506
}
506507

507508
@Override

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,9 @@ public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
18291829
}
18301830
final List<String> hostTags = cmd.getHostTags();
18311831
if (hostTags != null) {
1832+
List<VMInstanceVO> activeVMs = _vmDao.listByHostId(hostId);
1833+
s_logger.warn(String.format("The following active VMs [%s] are using the host [%s]. Updating the host tags will not affect them.", activeVMs, host));
1834+
18321835
if (s_logger.isDebugEnabled()) {
18331836
s_logger.debug("Updating Host Tags to :" + hostTags);
18341837
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,20 +1847,31 @@ public HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, St
18471847
public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException,
18481848
ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
18491849

1850-
// Verify input parameters
18511850
VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
18521851
Account caller = CallContext.current().getCallingAccount();
18531852
_accountMgr.checkAccess(caller, null, true, vmInstance);
1854-
if (vmInstance != null) {
1855-
if (vmInstance.getState().equals(State.Stopped)) {
1856-
upgradeStoppedVirtualMachine(vmId, newServiceOfferingId, customParameters);
1857-
return true;
1858-
}
1859-
if (vmInstance.getState().equals(State.Running)) {
1860-
return upgradeRunningVirtualMachine(vmId, newServiceOfferingId, customParameters);
1853+
if (vmInstance == null) {
1854+
s_logger.error(String.format("VM instance with id [%s] is null, it is not possible to upgrade a null VM.", vmId));
1855+
return false;
1856+
}
1857+
1858+
if (State.Stopped.equals(vmInstance.getState())) {
1859+
upgradeStoppedVirtualMachine(vmId, newServiceOfferingId, customParameters);
1860+
return true;
1861+
}
1862+
1863+
if (State.Running.equals(vmInstance.getState())) {
1864+
ServiceOfferingVO newServiceOfferingVO = _serviceOfferingDao.findById(newServiceOfferingId);
1865+
HostVO instanceHost = _hostDao.findById(vmInstance.getHostId());
1866+
_hostDao.loadHostTags(instanceHost);
1867+
1868+
if (!instanceHost.checkHostServiceOfferingTags(newServiceOfferingVO)) {
1869+
s_logger.error(String.format("Cannot upgrade VM [%s] as the new service offering [%s] does not have the required host tags %s.", vmInstance, newServiceOfferingVO,
1870+
instanceHost.getHostTags()));
1871+
return false;
18611872
}
18621873
}
1863-
return false;
1874+
return upgradeRunningVirtualMachine(vmId, newServiceOfferingId, customParameters);
18641875
}
18651876

18661877
private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException,

0 commit comments

Comments
 (0)