Skip to content

Commit 6325d21

Browse files
Alena ProkharchykAlena Prokharchyk
authored andcommitted
Get new elasticIp when releaseIpAddress is called for elasticIP of the vm
1 parent 6deeb7d commit 6325d21

21 files changed

Lines changed: 145 additions & 84 deletions

api/src/com/cloud/api/commands/AssociateIPAddrCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.cloud.exception.ResourceUnavailableException;
4343
import com.cloud.network.IpAddress;
4444
import com.cloud.network.Network;
45+
import com.cloud.network.IpAddress.AllocatedBy;
4546
import com.cloud.user.Account;
4647
import com.cloud.user.UserContext;
4748

@@ -173,7 +174,7 @@ public static String getResultObjectName() {
173174
@Override
174175
public void create() throws ResourceAllocationException{
175176
try {
176-
IpAddress ip = _networkService.allocateIP(getNetworkId(), _accountService.getAccount(getEntityOwnerId()));
177+
IpAddress ip = _networkService.allocateIP(getNetworkId(), _accountService.getAccount(getEntityOwnerId()), AllocatedBy.ipassoc);
177178
if (ip != null) {
178179
this.setEntityId(ip.getId());
179180
} else {

api/src/com/cloud/api/commands/DisableStaticNatCmd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.exception.InvalidParameterValueException;
3232
import com.cloud.exception.ResourceUnavailableException;
3333
import com.cloud.network.IpAddress;
34+
import com.cloud.network.IpAddress.AllocatedBy;
3435

3536
@Implementation(description="Disables static rule for given ip address", responseObject=SuccessResponse.class)
3637
public class DisableStaticNatCmd extends BaseAsyncCmd {

api/src/com/cloud/api/commands/EnableStaticNatCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.cloud.api.ServerApiException;
2929
import com.cloud.api.response.SuccessResponse;
3030
import com.cloud.exception.NetworkRuleConflictException;
31+
import com.cloud.exception.ResourceUnavailableException;
3132
import com.cloud.user.Account;
3233
import com.cloud.uservm.UserVm;
3334

@@ -81,7 +82,7 @@ public long getEntityOwnerId() {
8182
}
8283

8384
@Override
84-
public void execute(){
85+
public void execute() throws ResourceUnavailableException{
8586
try {
8687
boolean result = _rulesService.enableStaticNat(ipAddressId, virtualMachineId);
8788
if (result) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ enum State {
4545
Free // The IP address is ready to be allocated.
4646
}
4747

48+
enum AllocatedBy {
49+
ipassoc,
50+
elasticip
51+
}
52+
4853
long getDataCenterId();
4954

5055
Ip getAddress();
@@ -77,4 +82,7 @@ enum State {
7782
Long getAllocatedToAccountId();
7883

7984
Long getAllocatedInDomainId();
85+
86+
AllocatedBy getAllocatedBy();
87+
8088
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.cloud.exception.InsufficientCapacityException;
3131
import com.cloud.exception.ResourceAllocationException;
3232
import com.cloud.exception.ResourceUnavailableException;
33+
import com.cloud.network.IpAddress.AllocatedBy;
3334
import com.cloud.network.Network.Capability;
3435
import com.cloud.network.Network.Provider;
3536
import com.cloud.network.Network.Service;
@@ -42,7 +43,7 @@ public interface NetworkService {
4243

4344
List<? extends Network> getIsolatedNetworksOwnedByAccountInZone(long zoneId, Account owner);
4445

45-
IpAddress allocateIP(long networkId, Account ipOwner) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException;
46+
IpAddress allocateIP(long networkId, Account ipOwner, AllocatedBy allocatedBy) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException;
4647

4748
/**
4849
* Associates a public IP address for a router.

api/src/com/cloud/network/rules/RulesService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.cloud.api.commands.ListPortForwardingRulesCmd;
2323
import com.cloud.exception.NetworkRuleConflictException;
2424
import com.cloud.exception.ResourceUnavailableException;
25+
import com.cloud.network.IpAddress.AllocatedBy;
2526
import com.cloud.user.Account;
2627

2728
public interface RulesService {
@@ -54,10 +55,8 @@ public interface RulesService {
5455

5556
boolean applyPortForwardingRules(long ipAdddressId, Account caller) throws ResourceUnavailableException;
5657

57-
boolean enableStaticNat(long ipAddressId, long vmId) throws NetworkRuleConflictException;
58-
59-
boolean disableStaticNat(long ipAddressId) throws ResourceUnavailableException;
60-
58+
boolean enableStaticNat(long ipAddressId, long vmId) throws NetworkRuleConflictException, ResourceUnavailableException;
59+
6160
PortForwardingRule getPortForwardigRule(long ruleId);
6261
FirewallRule getFirewallRule(long ruleId);
6362

@@ -70,5 +69,7 @@ public interface RulesService {
7069
StaticNatRule buildStaticNatRule(FirewallRule rule);
7170

7271
List<String> getSourceCidrs(long ruleId);
72+
73+
boolean disableStaticNat(long ipId) throws ResourceUnavailableException;
7374

7475
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.cloud.network.ExternalLoadBalancerDeviceVO.LBDeviceAllocationState;
7474
import com.cloud.network.ExternalLoadBalancerDeviceVO.LBDeviceState;
7575
import com.cloud.network.ExternalNetworkDeviceManager.NetworkDevice;
76+
import com.cloud.network.IpAddress.AllocatedBy;
7677
import com.cloud.network.Network.Service;
7778
import com.cloud.network.Networks.TrafficType;
7879
import com.cloud.network.addr.PublicIp;
@@ -476,7 +477,7 @@ protected ExternalLoadBalancerDeviceVO allocateLoadBalancerForNetwork(Network gu
476477
String dedicatedLb = offering.getDedicatedLB()?"true":"false";
477478

478479
//acquire a public IP to associate with lb appliance (used as subnet IP to make the appliance part of private network)
479-
PublicIp publicIp = _networkMgr.assignPublicIpAddress(guestConfig.getDataCenterId(), null, _accountMgr.getSystemAccount(), VlanType.VirtualNetwork, null, null);
480+
PublicIp publicIp = _networkMgr.assignPublicIpAddress(guestConfig.getDataCenterId(), null, _accountMgr.getSystemAccount(), VlanType.VirtualNetwork, null, null, AllocatedBy.ipassoc);
480481
String publicIPNetmask = publicIp.getVlanNetmask();
481482
String publicIPgateway = publicIp.getVlanGateway();
482483
String publicIPVlanTag = publicIp.getVlanTag();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public class IPAddressVO implements IpAddress, Identity {
9696
@Column(name="physical_network_id")
9797
private Long physicalNetworkId;
9898

99+
@Column(name="allocated_by")
100+
@Enumerated(value=EnumType.STRING)
101+
private AllocatedBy allocatedBy;
102+
99103
@Column(name="account_id")
100104
@Transient
101105
private Long accountId = null;
@@ -265,4 +269,13 @@ public Long getPhysicalNetworkId() {
265269
public void setPhysicalNetworkId(Long physicalNetworkId) {
266270
this.physicalNetworkId = physicalNetworkId;
267271
}
272+
273+
@Override
274+
public AllocatedBy getAllocatedBy() {
275+
return allocatedBy;
276+
}
277+
278+
public void setAllocatedBy(AllocatedBy allocatedBy) {
279+
this.allocatedBy = allocatedBy;
280+
}
268281
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.cloud.exception.InsufficientCapacityException;
3636
import com.cloud.exception.ResourceUnavailableException;
3737
import com.cloud.hypervisor.Hypervisor.HypervisorType;
38+
import com.cloud.network.IpAddress.AllocatedBy;
3839
import com.cloud.network.Network.Capability;
3940
import com.cloud.network.Network.Provider;
4041
import com.cloud.network.Network.Service;
@@ -71,10 +72,11 @@ public interface NetworkManager extends NetworkService {
7172
* @param type
7273
* @param networkId
7374
* @param requestedIp TODO
75+
* @param allocatedBy TODO
7476
* @return
7577
* @throws InsufficientAddressCapacityException
7678
*/
77-
PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp) throws InsufficientAddressCapacityException;
79+
PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp, AllocatedBy allocatedBy) throws InsufficientAddressCapacityException;
7880

7981
/**
8082
* assigns a source nat ip address to an account within a network.

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
import com.cloud.host.Status;
104104
import com.cloud.host.dao.HostDao;
105105
import com.cloud.hypervisor.Hypervisor.HypervisorType;
106+
import com.cloud.network.IpAddress.AllocatedBy;
106107
import com.cloud.network.IpAddress.State;
107108
import com.cloud.network.Network.Capability;
108109
import com.cloud.network.Network.GuestType;
@@ -340,12 +341,12 @@ public boolean canElementEnableIndividualServices(Provider provider){
340341
}
341342

342343
@Override
343-
public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp) throws InsufficientAddressCapacityException {
344-
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp);
344+
public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp, AllocatedBy allocatedBy) throws InsufficientAddressCapacityException {
345+
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, allocatedBy);
345346
}
346347

347348
@DB
348-
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse, Long networkId, boolean sourceNat, boolean assign, String requestedIp)
349+
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse, Long networkId, boolean sourceNat, boolean assign, String requestedIp, AllocatedBy allocatedBy)
349350
throws InsufficientAddressCapacityException {
350351
StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in ");
351352
Transaction txn = Transaction.currentTxn();
@@ -401,6 +402,7 @@ public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account o
401402
addr.setAllocatedTime(new Date());
402403
addr.setAllocatedInDomainId(owner.getDomainId());
403404
addr.setAllocatedToAccountId(owner.getId());
405+
addr.setAllocatedBy(allocatedBy);
404406

405407
if (assign) {
406408
markPublicIpAsAllocated(addr);
@@ -505,7 +507,7 @@ public PublicIp assignSourceNatIpAddress(Account owner, Network network, long ca
505507
vlanId = maps.get(0).getVlanDbId();
506508
}
507509

508-
ip = fetchNewPublicIp(dcId, null, vlanId, owner, VlanType.VirtualNetwork, network.getId(), true, false, null);
510+
ip = fetchNewPublicIp(dcId, null, vlanId, owner, VlanType.VirtualNetwork, network.getId(), true, false, null, AllocatedBy.ipassoc);
509511
sourceNat = ip.ip();
510512

511513
markPublicIpAsAllocated(sourceNat);
@@ -920,7 +922,7 @@ public List<? extends Network> getIsolatedNetworksOwnedByAccountInZone(long zone
920922
@Override
921923
@DB
922924
@ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "allocating Ip", create = true)
923-
public IpAddress allocateIP(long networkId, Account ipOwner) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException {
925+
public IpAddress allocateIP(long networkId, Account ipOwner, AllocatedBy allocatedBy) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException {
924926
Account caller = UserContext.current().getCaller();
925927
long userId = UserContext.current().getCallerUserId();
926928

@@ -1007,7 +1009,7 @@ public IpAddress allocateIP(long networkId, Account ipOwner) throws ResourceAllo
10071009
}
10081010
}
10091011

1010-
ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, network.getId(), isSourceNat, assign, null);
1012+
ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, network.getId(), isSourceNat, assign, null, allocatedBy);
10111013

10121014
if (ip == null) {
10131015
throw new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId());
@@ -3461,6 +3463,7 @@ public boolean associateIpAddressListToAccount(long userId, long accountId, long
34613463
addr.setAllocatedTime(new Date());
34623464
addr.setAllocatedInDomainId(owner.getDomainId());
34633465
addr.setAllocatedToAccountId(owner.getId());
3466+
addr.setAllocatedBy(AllocatedBy.ipassoc);
34643467
addr.setState(IpAddress.State.Allocating);
34653468
markPublicIpAsAllocated(addr);
34663469
}
@@ -5861,7 +5864,7 @@ public IpAddress assignElasticIp(long networkId, Account owner, boolean forElast
58615864
try {
58625865
s_logger.debug("Allocating elastic IP address for load balancer rule...");
58635866
//allocate ip
5864-
ip = allocateIP(networkId, owner);
5867+
ip = allocateIP(networkId, owner, AllocatedBy.elasticip);
58655868
//apply ip associations
58665869
ip = associateIP(ip.getId());
58675870
} catch (ResourceAllocationException ex) {
@@ -5887,7 +5890,7 @@ public boolean handleElasticIpRelease(IpAddress ip) {
58875890
if (networkId != null) {
58885891
Network guestNetwork = getNetwork(networkId);
58895892
NetworkOffering offering = _configMgr.getNetworkOffering(guestNetwork.getNetworkOfferingId());
5890-
if (offering.getElasticIp()) {
5893+
if (offering.getElasticIp() && ip.getAllocatedBy() == AllocatedBy.elasticip) {
58915894
UserContext ctx = UserContext.current();
58925895
if (!releasePublicIpAddress(ip.getId(), ctx.getCallerUserId(), ctx.getCaller())) {
58935896
s_logger.warn("Unable to release elastic ip address id=" + ip.getId());

0 commit comments

Comments
 (0)