Skip to content

Commit 9fd0965

Browse files
authored
Merge pull request apache#2126 from Accelerite/CLOUDSTACK-9740
CLOUDSTACK-9740 : Search for secondary IP of NIC that is attached to an instance is not working
2 parents b894cc4 + 5eed751 commit 9fd0965

12 files changed

Lines changed: 118 additions & 25 deletions

File tree

api/src/com/cloud/network/NetworkService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,6 @@ Network createPrivateNetwork(String networkName, String displayText, long physic
180180
IpAddress updateIP(Long id, String customId, Boolean displayIp);
181181

182182
boolean configureNicSecondaryIp(NicSecondaryIp secIp, boolean isZoneSgEnabled);
183+
184+
List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd listNicsCmd);
183185
}

api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21-
21+
import com.cloud.vm.NicSecondaryIp;
22+
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
2223
import org.apache.cloudstack.acl.RoleType;
2324
import org.apache.cloudstack.api.APICommand;
2425
import org.apache.cloudstack.api.ApiCommandJobType;
@@ -122,22 +123,45 @@ public static String getResultObjectName() {
122123
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
123124

124125
try {
125-
List<? extends Nic> results = _networkService.listNics(this);
126-
ListResponse<NicResponse> response = new ListResponse<NicResponse>();
127-
List<NicResponse> resList = null;
128-
if (results != null) {
129-
resList = new ArrayList<NicResponse>(results.size());
130-
for (Nic r : results) {
131-
NicResponse resp = _responseGenerator.createNicResponse(r);
132-
resp.setObjectName("nic");
133-
resList.add(resp);
126+
if (this.getKeyword() != null && !this.getKeyword().isEmpty() && this.getNicId() != null) {
127+
List<? extends NicSecondaryIp> results = _networkService.listVmNicSecondaryIps(this);
128+
ListResponse<NicSecondaryIpResponse> response = new ListResponse<NicSecondaryIpResponse>();
129+
List<NicSecondaryIpResponse> resList = new ArrayList<NicSecondaryIpResponse>();
130+
NicSecondaryIpResponse res = new NicSecondaryIpResponse();
131+
List<NicSecondaryIpResponse> res_List = new ArrayList<NicSecondaryIpResponse>();
132+
if (results != null) {
133+
for (NicSecondaryIp r : results) {
134+
NicSecondaryIpResponse ipRes = _responseGenerator.createSecondaryIPToNicResponse(r);
135+
resList.add(ipRes);
136+
res.setSecondaryIpsList(resList);
137+
res.setObjectName("nic");
138+
}
139+
140+
res_List.add(res);
141+
response.setResponses(res_List);
142+
}
143+
response.setResponses(res_List);
144+
response.setResponseName(getCommandName());
145+
this.setResponseObject(response);
146+
147+
} else {
148+
List<? extends Nic> results = _networkService.listNics(this);
149+
ListResponse<NicResponse> response = new ListResponse<NicResponse>();
150+
List<NicResponse> resList = null;
151+
if (results != null) {
152+
resList = new ArrayList<NicResponse>(results.size());
153+
for (Nic r : results) {
154+
NicResponse resp = _responseGenerator.createNicResponse(r);
155+
resp.setObjectName("nic");
156+
resList.add(resp);
157+
}
158+
response.setResponses(resList);
134159
}
160+
135161
response.setResponses(resList);
162+
response.setResponseName(getCommandName());
163+
this.setResponseObject(response);
136164
}
137-
response.setResponses(resList);
138-
response.setResponseName(getCommandName());
139-
this.setResponseObject(response);
140-
141165
} catch (Exception e) {
142166
s_logger.warn("Failed to list secondary ip address per nic ");
143167
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());

api/src/org/apache/cloudstack/api/response/NicSecondaryIpResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.cloudstack.api.response;
1818

1919
import com.google.gson.annotations.SerializedName;
20-
20+
import java.util.List;
2121
import org.apache.cloudstack.api.ApiConstants;
2222
import org.apache.cloudstack.api.BaseResponse;
2323
import org.apache.cloudstack.api.EntityReference;
@@ -33,6 +33,10 @@ public class NicSecondaryIpResponse extends BaseResponse {
3333
@Param(description = "the ID of the secondary private IP addr")
3434
private String id;
3535

36+
@SerializedName("secondaryip")
37+
@Param(description = "the list of Secondary ipv4 addr of nic")
38+
private List<NicSecondaryIpResponse> secondaryIpsList;
39+
3640
@SerializedName(ApiConstants.IP_ADDRESS)
3741
@Param(description = "Secondary IP address")
3842
private String ipAddr;
@@ -94,4 +98,11 @@ public void setId(String id) {
9498
this.id = id;
9599
}
96100

101+
public List<NicSecondaryIpResponse> getSecondaryIpsList() {
102+
return secondaryIpsList;
103+
}
104+
105+
public void setSecondaryIpsList(List<NicSecondaryIpResponse> secondaryIpsList) {
106+
this.secondaryIpsList = secondaryIpsList;
107+
}
97108
}

engine/api/src/org/apache/cloudstack/engine/orchestration/service/NetworkOrchestrationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void implementNetworkElementsAndResources(DeployDestination dest, ReservationCon
213213

214214
boolean isSecondaryIpSetForNic(long nicId);
215215

216-
List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId);
216+
List<? extends Nic> listVmNics(long vmId, Long nicId, Long networkId, String keyword);
217217

218218
Nic savePlaceholderNic(Network network, String ip4Address, String ip6Address, Type vmType);
219219

engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,12 +2894,17 @@ protected boolean isSharedNetworkOfferingWithServices(final long networkOffering
28942894
}
28952895

28962896
@Override
2897-
public List<? extends Nic> listVmNics(final long vmId, final Long nicId, final Long networkId) {
2897+
public List<? extends Nic> listVmNics(final long vmId, final Long nicId, final Long networkId, String keyword) {
28982898
List<NicVO> result = null;
2899-
if (nicId == null && networkId == null) {
2900-
result = _nicDao.listByVmId(vmId);
2899+
2900+
if (keyword == null || keyword.isEmpty()) {
2901+
if (nicId == null && networkId == null) {
2902+
result = _nicDao.listByVmId(vmId);
2903+
} else {
2904+
result = _nicDao.listByVmIdAndNicIdAndNtwkId(vmId, nicId, networkId);
2905+
}
29012906
} else {
2902-
result = _nicDao.listByVmIdAndNicIdAndNtwkId(vmId, nicId, networkId);
2907+
result = _nicDao.listByVmIdAndKeyword(vmId, keyword);
29032908
}
29042909

29052910
for (final NicVO nic : result) {

engine/schema/src/com/cloud/vm/dao/NicDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
7676
int countNicsForStartingVms(long networkId);
7777

7878
NicVO getControlNicForVM(long vmId);
79+
80+
List<NicVO> listByVmIdAndKeyword(long instanceId, String keyword);
7981
}

engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected void init() {
6060
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
6161
AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getIPv4Gateway(), Op.EQ);
6262
AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ);
63-
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIPv4Address(), Op.EQ);
63+
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIPv4Address(), Op.LIKE);
6464
AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ);
6565
AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
6666
AllFieldsSearch.and("secondaryip", AllFieldsSearch.entity().getSecondaryIp(), Op.EQ);
@@ -311,4 +311,12 @@ public int countNicsForStartingVms(long networkId) {
311311
List<Integer> results = customSearch(sc, null);
312312
return results.get(0);
313313
}
314+
315+
@Override
316+
public List<NicVO> listByVmIdAndKeyword(long instanceId, String keyword) {
317+
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
318+
sc.setParameters("instance", instanceId);
319+
sc.setParameters("address", "%" + keyword + "%");
320+
return listBy(sc);
321+
}
314322
}

engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ public interface NicSecondaryIpDao extends GenericDao<NicSecondaryIpVO, Long> {
5353
List<String> getSecondaryIpAddressesForNic(long nicId);
5454

5555
Long countByNicId(long nicId);
56+
57+
List<NicSecondaryIpVO> listSecondaryIpUsingKeyword(long nicId, String keyword);
5658
}

engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected NicSecondaryIpDaoImpl() {
4040
AllFieldsSearch = createSearchBuilder();
4141
AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getVmId(), Op.EQ);
4242
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
43-
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ);
43+
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.LIKE);
4444
AllFieldsSearch.and("nicId", AllFieldsSearch.entity().getNicId(), Op.EQ);
4545
AllFieldsSearch.done();
4646

@@ -146,4 +146,13 @@ public Long countByNicId(long nicId) {
146146
sc.setParameters("nic", nicId);
147147
return customSearch(sc, null).get(0);
148148
}
149+
150+
@Override
151+
public List<NicSecondaryIpVO> listSecondaryIpUsingKeyword(long nicId, String keyword)
152+
{
153+
SearchCriteria<NicSecondaryIpVO> sc = AllFieldsSearch.create();
154+
sc.setParameters("nicId", nicId);
155+
sc.setParameters("address", "%" + keyword + "%");
156+
return listBy(sc);
157+
}
149158
}

server/src/com/cloud/network/NetworkServiceImpl.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4140,6 +4140,7 @@ public List<? extends Nic> listNics(ListNicsCmd cmd) {
41404140
Account caller = CallContext.current().getCallingAccount();
41414141
Long nicId = cmd.getNicId();
41424142
long vmId = cmd.getVmId();
4143+
String keyword = cmd.getKeyword();
41434144
Long networkId = cmd.getNetworkId();
41444145
UserVmVO userVm = _userVmDao.findById(vmId);
41454146

@@ -4150,7 +4151,26 @@ public List<? extends Nic> listNics(ListNicsCmd cmd) {
41504151
}
41514152

41524153
_accountMgr.checkAccess(caller, null, true, userVm);
4153-
return _networkMgr.listVmNics(vmId, nicId, networkId);
4154+
return _networkMgr.listVmNics(vmId, nicId, networkId, keyword);
4155+
}
4156+
4157+
@Override
4158+
public List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd cmd)
4159+
{
4160+
Account caller = CallContext.current().getCallingAccount();
4161+
Long nicId = cmd.getNicId();
4162+
long vmId = cmd.getVmId();
4163+
String keyword = cmd.getKeyword();
4164+
UserVmVO userVm = _userVmDao.findById(vmId);
4165+
4166+
if (userVm == null || (!userVm.isDisplayVm() && caller.getType() == Account.ACCOUNT_TYPE_NORMAL)) {
4167+
InvalidParameterValueException ex = new InvalidParameterValueException("Virtual mahine id does not exist");
4168+
ex.addProxyObject(Long.valueOf(vmId).toString(), "vmId");
4169+
throw ex;
4170+
}
4171+
4172+
_accountMgr.checkAccess(caller, null, true, userVm);
4173+
return _nicSecondaryIpDao.listSecondaryIpUsingKeyword(nicId, keyword);
41544174
}
41554175

41564176
public List<NetworkGuru> getNetworkGurus() {

0 commit comments

Comments
 (0)