Skip to content

Commit 4fcb406

Browse files
committed
bug 4706: allow to specify which ip address from the network to take when do vm deployment. The model is supported for multiple network case as well.
status 4706: resolved fixed
1 parent 2ed1afa commit 4fcb406

14 files changed

Lines changed: 157 additions & 48 deletions

‎api/src/com/cloud/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,5 @@ public class ApiConstants {
246246
public static final String SNAPSHOT_POLICY = "snapshotpolicy";
247247
public static final String SNAPSHOT_RESERVATION = "snapshotreservation";
248248
public static final String REDUNDANT_ROUTER = "redundantrouter";
249+
public static final String IP_NETWORK_LIST = "iptonetworklist";
249250
}

‎api/src/com/cloud/api/commands/DeployVMCmd.java‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
package com.cloud.api.commands;
2020

2121
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.HashMap;
24+
import java.util.Iterator;
2225
import java.util.List;
26+
import java.util.Map;
2327

2428
import org.apache.log4j.Logger;
2529

@@ -79,7 +83,7 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
7983
private Long domainId;
8084

8185
//Network information
82-
@Parameter(name=ApiConstants.NETWORK_IDS, type=CommandType.LIST, collectionType=CommandType.LONG, description="list of network ids used by virtual machine")
86+
@Parameter(name=ApiConstants.NETWORK_IDS, type=CommandType.LIST, collectionType=CommandType.LONG, description="list of network ids used by virtual machine. Can't be specified with ipToNetworkList parameter")
8387
private List<Long> networkIds;
8488

8589
//DataDisk information
@@ -109,6 +113,9 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
109113

110114
@Parameter(name=ApiConstants.SECURITY_GROUP_NAMES, type=CommandType.LIST, collectionType=CommandType.STRING, description="comma separated list of security groups names that going to be applied to the virtual machine. Should be passed only when vm is created from a zone with Basic Network support. Mutually exclusive with securitygroupids parameter")
111115
private List<String> securityGroupNameList;
116+
117+
@Parameter(name = ApiConstants.IP_NETWORK_LIST, type = CommandType.MAP, description = "ip to network mapping. Can't be specified with networkIds parameter. Example: iptonetworklist[0].ip=10.10.10.11&iptonetworklist[0].networkid=204 - requests to use ip 10.10.10.11 in network id=204")
118+
private Map ipToNetworkList;
112119

113120
/////////////////////////////////////////////////////
114121
/////////////////// Accessors ///////////////////////
@@ -187,6 +194,16 @@ public Long getZoneId() {
187194
}
188195

189196
public List<Long> getNetworkIds() {
197+
if (ipToNetworkList != null ) {
198+
if (networkIds != null) {
199+
throw new InvalidParameterValueException("NetworkIds can't be specified along with ipToNetworkMap");
200+
} else {
201+
List<Long> networks = new ArrayList<Long>();
202+
networks.addAll(getIpToNetworkMap().keySet());
203+
return networks;
204+
}
205+
}
206+
190207
return networkIds;
191208
}
192209

@@ -201,6 +218,27 @@ public String getSSHKeyPairName() {
201218
public Long getHostId() {
202219
return hostId;
203220
}
221+
222+
private Map<Long, String> getIpToNetworkMap() {
223+
if (networkIds != null && ipToNetworkList != null) {
224+
throw new InvalidParameterValueException("NetworkIds can't be specified along with ipToNetworkMap");
225+
}
226+
Map<Long, String> ipToNetworkMap = null;
227+
if (ipToNetworkList != null && !ipToNetworkList.isEmpty()) {
228+
ipToNetworkMap = new HashMap<Long, String>();
229+
Collection ipsCollection = ipToNetworkList.values();
230+
Iterator iter = ipsCollection.iterator();
231+
while (iter.hasNext()) {
232+
HashMap<String, String> ips = (HashMap<String, String>) iter.next();
233+
Long networkId = Long.valueOf(ips.get("networkid"));
234+
String requestedIp = (String) ips.get("ip");
235+
236+
ipToNetworkMap.put(networkId, requestedIp);
237+
}
238+
}
239+
240+
return ipToNetworkMap;
241+
}
204242

205243
/////////////////////////////////////////////////////
206244
/////////////// API Implementation///////////////////
@@ -322,18 +360,18 @@ public void create() throws ResourceAllocationException{
322360
throw new InvalidParameterValueException("Can't specify network Ids in Basic zone");
323361
} else {
324362
vm = _userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, getSecurityGroupIdList(), owner, name,
325-
displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName);
363+
displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap());
326364
}
327365
} else {
328366
if (zone.isSecurityGroupEnabled()) {
329367
vm = _userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, getNetworkIds(), getSecurityGroupIdList(),
330-
owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName);
368+
owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap());
331369
} else {
332370
if (getSecurityGroupIdList() != null && !getSecurityGroupIdList().isEmpty()) {
333371
throw new InvalidParameterValueException("Can't create vm with security groups; security group feature is not enabled per zone");
334372
}
335373
vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName,
336-
diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName);
374+
diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap());
337375
}
338376
}
339377
}

‎api/src/com/cloud/vm/NicProfile.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class NicProfile {
5555
Integer networkRate;
5656
boolean isSecurityGroupEnabled;
5757
List<String> tags;
58+
String requestedIp;
5859

5960
public String getDns1() {
6061
return dns1;
@@ -254,6 +255,10 @@ public NicProfile(ReservationStrategy strategy, String ip4Address, String macAdd
254255
this.netmask = netmask;
255256
this.strategy = strategy;
256257
}
258+
259+
public NicProfile(String requestedIp) {
260+
this.requestedIp = requestedIp;
261+
}
257262

258263
public NicProfile() {
259264
}
@@ -278,6 +283,10 @@ public void setSecurityGroupEnabled(boolean enabled) {
278283
this.isSecurityGroupEnabled = enabled;
279284
}
280285

286+
public String getRequestedIp() {
287+
return requestedIp;
288+
}
289+
281290
public void deallocate() {
282291
this.gateway = null;
283292
this.mode = null;

‎api/src/com/cloud/vm/UserVmService.java‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.cloud.vm;
1919

2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import javax.naming.InsufficientResourcesException;
2324

@@ -145,10 +146,6 @@ UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, E
145146
* - the template for the virtual machine
146147
* @param securityGroupIdList
147148
* - comma separated list of security groups id that going to be applied to the virtual machine
148-
* @param accountName
149-
* - an optional account for the virtual machine. Must be used with domainId
150-
* @param domainId
151-
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
152149
* @param hostName
153150
* - host name for the virtual machine
154151
* @param displayName
@@ -171,7 +168,11 @@ UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, E
171168
* GET (via querystring), you can send up to 2KB of data after base64 encoding
172169
* @param sshKeyPair
173170
* - name of the ssh key pair used to login to the virtual machine
174-
*
171+
* @param requestedIps TODO
172+
* @param accountName
173+
* - an optional account for the virtual machine. Must be used with domainId
174+
* @param domainId
175+
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
175176
* @return UserVm object if successful.
176177
*
177178
* @throws InsufficientCapacityException
@@ -183,7 +184,7 @@ UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, E
183184
* @throws InsufficientResourcesException
184185
*/
185186
UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List<Long> securityGroupIdList, Account owner, String hostName,
186-
String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair)
187+
String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Map<Long, String> requestedIps)
187188
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException;
188189

189190
/**
@@ -199,10 +200,6 @@ UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering s
199200
* - list of network ids used by virtual machine
200201
* @param securityGroupIdList
201202
* - comma separated list of security groups id that going to be applied to the virtual machine
202-
* @param accountName
203-
* - an optional account for the virtual machine. Must be used with domainId
204-
* @param domainId
205-
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
206203
* @param hostName
207204
* - host name for the virtual machine
208205
* @param displayName
@@ -225,7 +222,11 @@ UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering s
225222
* GET (via querystring), you can send up to 2KB of data after base64 encoding
226223
* @param sshKeyPair
227224
* - name of the ssh key pair used to login to the virtual machine
228-
*
225+
* @param requestedIps TODO
226+
* @param accountName
227+
* - an optional account for the virtual machine. Must be used with domainId
228+
* @param domainId
229+
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
229230
* @return UserVm object if successful.
230231
*
231232
* @throws InsufficientCapacityException
@@ -237,7 +238,7 @@ UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering s
237238
* @throws InsufficientResourcesException
238239
*/
239240
UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List<Long> networkIdList, List<Long> securityGroupIdList,
240-
Account owner, String hostName, String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair)
241+
Account owner, String hostName, String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Map<Long, String> requestedIps)
241242
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException;
242243

243244
/**
@@ -251,10 +252,6 @@ UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, ServiceOfferin
251252
* - the template for the virtual machine
252253
* @param networkIdList
253254
* - list of network ids used by virtual machine
254-
* @param accountName
255-
* - an optional account for the virtual machine. Must be used with domainId
256-
* @param domainId
257-
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
258255
* @param hostName
259256
* - host name for the virtual machine
260257
* @param displayName
@@ -277,7 +274,11 @@ UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, ServiceOfferin
277274
* GET (via querystring), you can send up to 2KB of data after base64 encoding
278275
* @param sshKeyPair
279276
* - name of the ssh key pair used to login to the virtual machine
280-
*
277+
* @param requestedIps TODO
278+
* @param accountName
279+
* - an optional account for the virtual machine. Must be used with domainId
280+
* @param domainId
281+
* - an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used
281282
* @return UserVm object if successful.
282283
*
283284
* @throws InsufficientCapacityException
@@ -289,7 +290,7 @@ UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, ServiceOfferin
289290
* @throws InsufficientResourcesException
290291
*/
291292
UserVm createAdvancedVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List<Long> networkIdList, Account owner, String hostName,
292-
String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair)
293+
String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Map<Long, String> requestedIps)
293294
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException;
294295

295296
/**

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ public interface NetworkManager extends NetworkService {
6262
* @param owner
6363
* @param type
6464
* @param networkId
65+
* @param requestedIp TODO
6566
* @return
6667
* @throws InsufficientAddressCapacityException
6768
*/
68-
PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId) throws InsufficientAddressCapacityException;
69+
PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp) throws InsufficientAddressCapacityException;
6970

7071
/**
7172
* assigns a source nat ip address to an account within a network.
@@ -204,5 +205,5 @@ boolean associateIpAddressListToAccount(long userId, long accountId, long zoneId
204205

205206
IPAddressVO markIpAsUnavailable(long addrId);
206207

207-
public String acquireGuestIpAddress(Network network);
208+
public String acquireGuestIpAddress(Network network, String requestedIp);
208209
}

0 commit comments

Comments
 (0)