Skip to content

Commit

Permalink
Merge branch 'return-root-uuid' into '4.16.0.0-scclouds'
Browse files Browse the repository at this point in the history
Retornar UUID do volume na `listRouters` e na `listSystemVms`

Closes apache#553

See merge request scclouds/scclouds!295
  • Loading branch information
Daniel Augusto Veronezi Salvador committed Aug 5, 2022
2 parents 9658b99 + d115c24 commit 2d04307
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ public class DomainRouterResponse extends BaseResponseWithAnnotations implements
@Param(description = "Last executed health check result for the router", responseObject = RouterHealthCheckResultResponse.class, since = "4.14")
List<RouterHealthCheckResultResponse> healthCheckResults;

@SerializedName("rootvolumeid")
@Param(description = "the router's ROOT volume ID")
private String rootVolumeID;

public DomainRouterResponse() {
nics = new LinkedHashSet<NicResponse>();
}
Expand Down Expand Up @@ -490,4 +494,12 @@ public void setHealthChecksFailed(boolean healthChecksFailed) {
public void setHealthCheckResults(List<RouterHealthCheckResultResponse> healthCheckResults) {
this.healthCheckResults = healthCheckResults;
}

public String getRootVolumeID() {
return rootVolumeID;
}

public void setRootVolumeID(String rootVolumeID) {
this.rootVolumeID = rootVolumeID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public class SystemVmResponse extends BaseResponseWithAnnotations {
@Param(description = "true if vm contains XS/VMWare tools inorder to support dynamic scaling of VM cpu/memory.")
private Boolean isDynamicallyScalable;

@SerializedName("rootvolumeid")
@Param(description = "the system VM's ROOT volume ID")
private String rootVolumeID;

@Override
public String getObjectId() {
return this.getId();
Expand Down Expand Up @@ -454,4 +458,12 @@ public Boolean getDynamicallyScalable() {
public void setDynamicallyScalable(Boolean dynamicallyScalable) {
isDynamicallyScalable = dynamicallyScalable;
}

public String getRootVolumeID() {
return rootVolumeID;
}

public void setRootVolumeID(String rootVolumeID) {
this.rootVolumeID = rootVolumeID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S

List<VolumeVO> findReadyAndAllocatedRootVolumesByInstance(long instanceId);

List<VolumeVO> findRootVolumesByInstance(long instanceId);

VolumeVO getInstanceRootVolume(long instanceId, String instanceUuid);

List<Long> listPoolIdsByVolumeCount(long dcId, Long podId, Long clusterId, long accountId);

List<Long> listZoneWidePoolIdsByVolumeCount(long dcId, long accountId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

@Component
public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements VolumeDao {
private static final Logger s_logger = Logger.getLogger(VolumeDaoImpl.class);
protected Logger logger = Logger.getLogger(VolumeDaoImpl.class);
protected final SearchBuilder<VolumeVO> DetachedAccountIdSearch;
protected final SearchBuilder<VolumeVO> TemplateZoneSearch;
protected final GenericSearchBuilder<VolumeVO, SumCount> TotalSizeByPoolSearch;
Expand Down Expand Up @@ -235,6 +235,27 @@ public List<VolumeVO> findReadyAndAllocatedRootVolumesByInstance(long instanceId
return listBy(sc);
}

@Override
public List<VolumeVO> findRootVolumesByInstance(long instanceId) {
SearchCriteria<VolumeVO> sc = RootDiskStateSearch.create();
sc.setParameters("instanceId", instanceId);
sc.setParameters("vType", Volume.Type.ROOT);
return listBy(sc);
}

@Override
public VolumeVO getInstanceRootVolume(long instanceId, String instanceUuid) {
List<VolumeVO> rootVolumeVoList = findRootVolumesByInstance(instanceId);
if (rootVolumeVoList.size() > 1) {
logger.warn(String.format("More than one ROOT volume has been found for VM [%s], there is probably an inconsistency in the database.", instanceUuid));
} else if (rootVolumeVoList.isEmpty()) {
CloudRuntimeException exception = new CloudRuntimeException(String.format("No ROOT volume has been found for VM [%s].", instanceUuid));
logger.error(exception.getMessage(), exception);
throw exception;
}
return rootVolumeVoList.get(0);
}

@Override
public List<VolumeVO> findByPod(long podId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
Expand Down Expand Up @@ -338,7 +359,7 @@ public HypervisorType getHypervisorType(long volumeId) {
} else if (scope == ScopeType.ZONE) {
sql = SELECT_HYPERTYPE_FROM_ZONE_VOLUME;
} else {
s_logger.error("Unhandled scope type '" + scope + "' when running getHypervisorType on volume id " + volumeId);
logger.error("Unhandled scope type '" + scope + "' when running getHypervisorType on volume id " + volumeId);
}

pstmt = txn.prepareAutoCloseStatement(sql);
Expand Down Expand Up @@ -368,7 +389,7 @@ public ImageFormat getImageFormat(Long volumeId) {
} else if (type.equals(HypervisorType.VMware)) {
return ImageFormat.OVA;
} else {
s_logger.warn("Do not support hypervisor " + type.toString());
logger.warn("Do not support hypervisor " + type.toString());
return null;
}
}
Expand Down Expand Up @@ -579,7 +600,7 @@ public boolean updateState(com.cloud.storage.Volume.State currentState, Event ev
builder.set(vo, "updated", new Date());

int rows = update((VolumeVO)vo, sc);
if (rows == 0 && s_logger.isDebugEnabled()) {
if (rows == 0 && logger.isDebugEnabled()) {
VolumeVO dbVol = findByIdIncludingRemoved(vo.getId());
if (dbVol != null) {
StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString());
Expand All @@ -590,7 +611,7 @@ public boolean updateState(com.cloud.storage.Volume.State currentState, Event ev
str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated)
.append("; updatedTime=").append(oldUpdatedTime);
} else {
s_logger.debug("Unable to update volume: id=" + vo.getId() + ", as there is no such volume exists in the database anymore");
logger.debug("Unable to update volume: id=" + vo.getId() + ", as there is no such volume exists in the database anymore");
}
}
return rows > 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.storage.dao;

import com.cloud.storage.VolumeVO;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class VolumeDaoImplTest {

@Mock
VolumeVO volumeVoMock;

@Mock
VolumeVO alternateVolumeVoMock;

@Mock
Logger loggerMock;

@Spy
@InjectMocks
VolumeDaoImpl volumeDaoSpy;

@Test
public void getInstanceRootVolumeTestReturningOneVolume() {
List<VolumeVO> volumeVOList = Collections.singletonList(volumeVoMock);
Mockito.doReturn(volumeVOList).when(volumeDaoSpy).findRootVolumesByInstance(1234);
VolumeVO actualVo = volumeDaoSpy.getInstanceRootVolume(1234, "test-uuid");
Assert.assertEquals(volumeVoMock, actualVo);
}

@Test
public void getInstanceRootVolumeTestReturningMoreThanOneVolume() {
List<VolumeVO> volumeVOList = Arrays.asList(volumeVoMock, alternateVolumeVoMock);
Mockito.doReturn(volumeVOList).when(volumeDaoSpy).findRootVolumesByInstance(1234);
VolumeVO actualVo = volumeDaoSpy.getInstanceRootVolume(1234, "test-uuid");
Mockito.verify(loggerMock).warn("More than one ROOT volume has been found for VM [test-uuid], there is probably an inconsistency in the database.");
Assert.assertEquals(volumeVoMock, actualVo);
}

@Test (expected = CloudRuntimeException.class)
public void getInstanceRootVolumeTestReturningNoVolumes() {
List<VolumeVO> volumeVOList = new ArrayList<>();
Mockito.doReturn(volumeVOList).when(volumeDaoSpy).findRootVolumesByInstance(1234);
volumeDaoSpy.getInstanceRootVolume(1234, "test-uuid");
}
}
5 changes: 5 additions & 0 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,11 @@ public SystemVmResponse createSystemVmResponse(VirtualMachine vm) {
}
}
}

VolumeVO rootVolumeVo = _volumeDao.getInstanceRootVolume(vm.getId(), vm.getUuid());

vmResponse.setRootVolumeID(rootVolumeVo.getUuid());

vmResponse.setObjectName("systemvm");
return vmResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.inject.Inject;

import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VolumeDao;
import org.apache.cloudstack.annotation.AnnotationService;
import org.apache.cloudstack.annotation.dao.AnnotationDao;
import org.apache.cloudstack.context.CallContext;
Expand Down Expand Up @@ -58,6 +60,9 @@ public class DomainRouterJoinDaoImpl extends GenericDaoBase<DomainRouterJoinVO,
@Inject
private AnnotationDao annotationDao;

@Inject
private VolumeDao volumeDao;

private final SearchBuilder<DomainRouterJoinVO> vrSearch;

private final SearchBuilder<DomainRouterJoinVO> vrIdSearch;
Expand Down Expand Up @@ -206,6 +211,11 @@ public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, A
routerResponse.setObjectName("router");
}

VolumeVO rootVolumeVo = volumeDao.getInstanceRootVolume(router.getId(), router.getUuid());

routerResponse.setRootVolumeID(rootVolumeVo.getUuid());


return routerResponse;
}

Expand Down

0 comments on commit 2d04307

Please sign in to comment.