Merge pull request #2126 from Accelerite/CLOUDSTACK-9740

CLOUDSTACK-9740 : Search for secondary IP of NIC that is attached to an instance is not working
This commit is contained in:
Rajani Karuturi 2017-06-06 16:29:45 +05:30 committed by GitHub
commit 9fd0965087
12 changed files with 120 additions and 27 deletions

View File

@ -180,4 +180,6 @@ public interface NetworkService {
IpAddress updateIP(Long id, String customId, Boolean displayIp);
boolean configureNicSecondaryIp(NicSecondaryIp secIp, boolean isZoneSgEnabled);
List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd listNicsCmd);
}

View File

@ -18,7 +18,8 @@ package org.apache.cloudstack.api.command.user.vm;
import java.util.ArrayList;
import java.util.List;
import com.cloud.vm.NicSecondaryIp;
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandJobType;
@ -122,6 +123,28 @@ public class ListNicsCmd extends BaseListCmd {
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
try {
if (this.getKeyword() != null && !this.getKeyword().isEmpty() && this.getNicId() != null) {
List<? extends NicSecondaryIp> results = _networkService.listVmNicSecondaryIps(this);
ListResponse<NicSecondaryIpResponse> response = new ListResponse<NicSecondaryIpResponse>();
List<NicSecondaryIpResponse> resList = new ArrayList<NicSecondaryIpResponse>();
NicSecondaryIpResponse res = new NicSecondaryIpResponse();
List<NicSecondaryIpResponse> res_List = new ArrayList<NicSecondaryIpResponse>();
if (results != null) {
for (NicSecondaryIp r : results) {
NicSecondaryIpResponse ipRes = _responseGenerator.createSecondaryIPToNicResponse(r);
resList.add(ipRes);
res.setSecondaryIpsList(resList);
res.setObjectName("nic");
}
res_List.add(res);
response.setResponses(res_List);
}
response.setResponses(res_List);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
List<? extends Nic> results = _networkService.listNics(this);
ListResponse<NicResponse> response = new ListResponse<NicResponse>();
List<NicResponse> resList = null;
@ -134,10 +157,11 @@ public class ListNicsCmd extends BaseListCmd {
}
response.setResponses(resList);
}
response.setResponses(resList);
response.setResponseName(getCommandName());
this.setResponseObject(response);
}
} catch (Exception e) {
s_logger.warn("Failed to list secondary ip address per nic ");
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());

View File

@ -17,7 +17,7 @@
package org.apache.cloudstack.api.response;
import com.google.gson.annotations.SerializedName;
import java.util.List;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;
@ -33,6 +33,10 @@ public class NicSecondaryIpResponse extends BaseResponse {
@Param(description = "the ID of the secondary private IP addr")
private String id;
@SerializedName("secondaryip")
@Param(description = "the list of Secondary ipv4 addr of nic")
private List<NicSecondaryIpResponse> secondaryIpsList;
@SerializedName(ApiConstants.IP_ADDRESS)
@Param(description = "Secondary IP address")
private String ipAddr;
@ -94,4 +98,11 @@ public class NicSecondaryIpResponse extends BaseResponse {
this.id = id;
}
public List<NicSecondaryIpResponse> getSecondaryIpsList() {
return secondaryIpsList;
}
public void setSecondaryIpsList(List<NicSecondaryIpResponse> secondaryIpsList) {
this.secondaryIpsList = secondaryIpsList;
}
}

View File

@ -213,7 +213,7 @@ public interface NetworkOrchestrationService {
boolean isSecondaryIpSetForNic(long nicId);
List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId);
List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId, String keyword);
Nic savePlaceholderNic(Network network, String ip4Address, String ip6Address, Type vmType);

View File

@ -2894,13 +2894,18 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra
}
@Override
public List<? extends Nic> listVmNics(final long vmId, final Long nicId, final Long networkId) {
public List<? extends Nic> listVmNics(final long vmId, final Long nicId, final Long networkId, String keyword) {
List<NicVO> result = null;
if (keyword == null || keyword.isEmpty()) {
if (nicId == null && networkId == null) {
result = _nicDao.listByVmId(vmId);
} else {
result = _nicDao.listByVmIdAndNicIdAndNtwkId(vmId, nicId, networkId);
}
} else {
result = _nicDao.listByVmIdAndKeyword(vmId, keyword);
}
for (final NicVO nic : result) {
if (_networkModel.isProviderForNetwork(Provider.NiciraNvp, nic.getNetworkId())) {

View File

@ -76,4 +76,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
int countNicsForStartingVms(long networkId);
NicVO getControlNicForVM(long vmId);
List<NicVO> listByVmIdAndKeyword(long instanceId, String keyword);
}

View File

@ -60,7 +60,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getIPv4Gateway(), Op.EQ);
AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ);
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIPv4Address(), Op.EQ);
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIPv4Address(), Op.LIKE);
AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ);
AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
AllFieldsSearch.and("secondaryip", AllFieldsSearch.entity().getSecondaryIp(), Op.EQ);
@ -311,4 +311,12 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
List<Integer> results = customSearch(sc, null);
return results.get(0);
}
@Override
public List<NicVO> listByVmIdAndKeyword(long instanceId, String keyword) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", instanceId);
sc.setParameters("address", "%" + keyword + "%");
return listBy(sc);
}
}

View File

@ -53,4 +53,6 @@ public interface NicSecondaryIpDao extends GenericDao<NicSecondaryIpVO, Long> {
List<String> getSecondaryIpAddressesForNic(long nicId);
Long countByNicId(long nicId);
List<NicSecondaryIpVO> listSecondaryIpUsingKeyword(long nicId, String keyword);
}

View File

@ -40,7 +40,7 @@ public class NicSecondaryIpDaoImpl extends GenericDaoBase<NicSecondaryIpVO, Long
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getVmId(), Op.EQ);
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ);
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.LIKE);
AllFieldsSearch.and("nicId", AllFieldsSearch.entity().getNicId(), Op.EQ);
AllFieldsSearch.done();
@ -146,4 +146,13 @@ public class NicSecondaryIpDaoImpl extends GenericDaoBase<NicSecondaryIpVO, Long
sc.setParameters("nic", nicId);
return customSearch(sc, null).get(0);
}
@Override
public List<NicSecondaryIpVO> listSecondaryIpUsingKeyword(long nicId, String keyword)
{
SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
sc.setParameters("nicId", nicId);
sc.setParameters("address", "%" + keyword + "%");
return listBy(sc);
}
}

View File

@ -4140,6 +4140,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
Account caller = CallContext.current().getCallingAccount();
Long nicId = cmd.getNicId();
long vmId = cmd.getVmId();
String keyword = cmd.getKeyword();
Long networkId = cmd.getNetworkId();
UserVmVO userVm = _userVmDao.findById(vmId);
@ -4150,7 +4151,26 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
}
_accountMgr.checkAccess(caller, null, true, userVm);
return _networkMgr.listVmNics(vmId, nicId, networkId);
return _networkMgr.listVmNics(vmId, nicId, networkId, keyword);
}
@Override
public List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd cmd)
{
Account caller = CallContext.current().getCallingAccount();
Long nicId = cmd.getNicId();
long vmId = cmd.getVmId();
String keyword = cmd.getKeyword();
UserVmVO userVm = _userVmDao.findById(vmId);
if (userVm == null || (!userVm.isDisplayVm() && caller.getType() == Account.ACCOUNT_TYPE_NORMAL)) {
InvalidParameterValueException ex = new InvalidParameterValueException("Virtual mahine id does not exist");
ex.addProxyObject(Long.valueOf(vmId).toString(), "vmId");
throw ex;
}
_accountMgr.checkAccess(caller, null, true, userVm);
return _nicSecondaryIpDao.listSecondaryIpUsingKeyword(nicId, keyword);
}
public List<NetworkGuru> getNetworkGurus() {

View File

@ -800,7 +800,7 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkOrches
}
@Override
public List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId) {
public List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId, String keyword) {
// TODO Auto-generated method stub
return null;
}
@ -906,4 +906,9 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkOrches
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd listNicsCmd) {
return null;
}
}

View File

@ -1710,11 +1710,16 @@
dataProvider: function(args) {
var data = {};
if (args.filterBy.search.value != null) {
data.keyword = args.filterBy.search.value;
}
$.ajax({
url: createURL('listNics'),
data: {
nicId: args.context.nics[0].id,
virtualmachineid: args.context.instances[0].id
virtualmachineid: args.context.instances[0].id,
keyword: args.filterBy.search.value
},
success: function(json) {
var ips = json.listnicsresponse.nic ? json.listnicsresponse.nic[0].secondaryip : [];