Skip to content

Commit b15431e

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-5810: addSecondaryIp to vm's nic - derive the ip owner from the vm instance account, not from the caller
1 parent 647ea6e commit b15431e

11 files changed

Lines changed: 163 additions & 276 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Network createPrivateNetwork(String networkName, String displayText, long physic
168168
InsufficientCapacityException;
169169

170170
/* Requests an IP address for the guest nic */
171-
NicSecondaryIp allocateSecondaryGuestIP(Account account, long zoneId, Long nicId, Long networkId, String ipaddress) throws InsufficientAddressCapacityException;
171+
NicSecondaryIp allocateSecondaryGuestIP(long nicId, String ipaddress) throws InsufficientAddressCapacityException;
172172

173173
boolean releaseSecondaryIpFromNic(long ipAddressId);
174174

api/src/com/cloud/network/security/SecurityGroupService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ public interface SecurityGroupService {
4747

4848
public List<? extends SecurityRule> authorizeSecurityGroupEgress(AuthorizeSecurityGroupEgressCmd cmd);
4949

50-
public boolean securityGroupRulesForVmSecIp(Long nicId, Long networkId, String secondaryIp, boolean ruleAction);
50+
public boolean securityGroupRulesForVmSecIp(long nicId, String secondaryIp, boolean ruleAction);
5151
}

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

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.vm;
1818

19-
import org.apache.log4j.Logger;
20-
2119
import org.apache.cloudstack.api.APICommand;
2220
import org.apache.cloudstack.api.ApiCommandJobType;
2321
import org.apache.cloudstack.api.ApiConstants;
@@ -28,6 +26,7 @@
2826
import org.apache.cloudstack.api.response.NicResponse;
2927
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
3028
import org.apache.cloudstack.context.CallContext;
29+
import org.apache.log4j.Logger;
3130

3231
import com.cloud.dc.DataCenter;
3332
import com.cloud.dc.DataCenter.NetworkType;
@@ -39,10 +38,10 @@
3938
import com.cloud.exception.ResourceAllocationException;
4039
import com.cloud.exception.ResourceUnavailableException;
4140
import com.cloud.network.Network;
42-
import com.cloud.user.Account;
4341
import com.cloud.utils.net.NetUtils;
4442
import com.cloud.vm.Nic;
4543
import com.cloud.vm.NicSecondaryIp;
44+
import com.cloud.vm.VirtualMachine;
4645

4746
@APICommand(name = "addIpToNic", description = "Assigns secondary IP to NIC", responseObject = NicSecondaryIpResponse.class)
4847
public class AddIpToVmNicCmd extends BaseAsyncCmd {
@@ -52,11 +51,7 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd {
5251
/////////////////////////////////////////////////////
5352
//////////////// API parameters /////////////////////
5453
/////////////////////////////////////////////////////
55-
@Parameter(name = ApiConstants.NIC_ID,
56-
type = CommandType.UUID,
57-
entityType = NicResponse.class,
58-
required = true,
59-
description = "the ID of the nic to which you want to assign private IP")
54+
@Parameter(name = ApiConstants.NIC_ID, type = CommandType.UUID, entityType = NicResponse.class, required = true, description = "the ID of the nic to which you want to assign private IP")
6055
private Long nicId;
6156

6257
@Parameter(name = ApiConstants.IP_ADDRESS, type = CommandType.STRING, required = false, description = "Secondary IP Address")
@@ -70,63 +65,40 @@ public String getEntityTable() {
7065
return "nic_secondary_ips";
7166
}
7267

73-
public String getAccountName() {
74-
return CallContext.current().getCallingAccount().getAccountName();
75-
}
76-
77-
public long getDomainId() {
78-
return CallContext.current().getCallingAccount().getDomainId();
79-
}
80-
81-
private long getZoneId() {
82-
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
83-
if (ntwk == null) {
84-
throw new InvalidParameterValueException("Can't find zone id for specified");
85-
}
86-
return ntwk.getDataCenterId();
87-
}
88-
89-
public Long getNetworkId() {
68+
private long getNetworkId() {
9069
Nic nic = _entityMgr.findById(Nic.class, nicId);
9170
if (nic == null) {
9271
throw new InvalidParameterValueException("Can't find network id for specified nic");
9372
}
94-
Long networkId = nic.getNetworkId();
95-
return networkId;
73+
return nic.getNetworkId();
9674
}
9775

98-
public Long getNicId() {
76+
public long getNicId() {
9977
return nicId;
10078
}
10179

102-
public String getIpaddress() {
80+
private String getIpaddress() {
10381
if (ipAddr != null) {
10482
return ipAddr;
10583
} else {
10684
return null;
10785
}
10886
}
10987

110-
public NetworkType getNetworkType() {
88+
private NetworkType getNetworkType() {
11189
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
11290
DataCenter dc = _entityMgr.findById(DataCenter.class, ntwk.getDataCenterId());
11391
return dc.getNetworkType();
11492
}
11593

116-
@Override
117-
public long getEntityOwnerId() {
118-
Account caller = CallContext.current().getCallingAccount();
119-
return caller.getAccountId();
120-
}
121-
12294
@Override
12395
public String getEventType() {
12496
return EventTypes.EVENT_NET_IP_ASSIGN;
12597
}
12698

12799
@Override
128100
public String getEventDescription() {
129-
return "associating ip to nic id: " + getNetworkId() + " in zone " + getZoneId();
101+
return "associating ip to nic id=" + getNicId() + " belonging to network id=" + getNetworkId();
130102
}
131103

132104
/////////////////////////////////////////////////////
@@ -156,7 +128,7 @@ public void execute() throws ResourceUnavailableException, ResourceAllocationExc
156128
}
157129

158130
try {
159-
result = _networkService.allocateSecondaryGuestIP(_accountService.getAccount(getEntityOwnerId()), getZoneId(), getNicId(), getNetworkId(), getIpaddress());
131+
result = _networkService.allocateSecondaryGuestIP(getNicId(), getIpaddress());
160132
} catch (InsufficientAddressCapacityException e) {
161133
throw new InvalidParameterValueException("Allocating guest ip for nic failed");
162134
}
@@ -166,7 +138,7 @@ public void execute() throws ResourceUnavailableException, ResourceAllocationExc
166138
if (getNetworkType() == NetworkType.Basic) {
167139
// add security group rules for the secondary ip addresses
168140
boolean success = false;
169-
success = _securityGroupService.securityGroupRulesForVmSecIp(getNicId(), getNetworkId(), secondaryIp, true);
141+
success = _securityGroupService.securityGroupRulesForVmSecIp(getNicId(), secondaryIp, true);
170142
if (success == false) {
171143
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
172144
}
@@ -197,4 +169,16 @@ public ApiCommandJobType getInstanceType() {
197169
return ApiCommandJobType.IpAddress;
198170
}
199171

172+
@Override
173+
public long getEntityOwnerId() {
174+
Nic nic = _entityMgr.findById(Nic.class, nicId);
175+
if (nic == null) {
176+
throw new InvalidParameterValueException("Can't find nic for id specified");
177+
}
178+
long vmId = nic.getInstanceId();
179+
VirtualMachine vm = _entityMgr.findById(VirtualMachine.class, vmId);
180+
181+
return vm.getAccountId();
182+
}
183+
200184
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void execute() throws InvalidParameterValueException {
142142
if (getNetworkType() == NetworkType.Basic) {
143143
//remove the security group rules for this secondary ip
144144
boolean success = false;
145-
success = _securityGroupService.securityGroupRulesForVmSecIp(nicSecIp.getNicId(), nicSecIp.getNetworkId(), nicSecIp.getIp4Address(), false);
145+
success = _securityGroupService.securityGroupRulesForVmSecIp(nicSecIp.getNicId(), nicSecIp.getIp4Address(), false);
146146
if (success == false) {
147147
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
148148
}

api/test/org/apache/cloudstack/api/command/test/AddIpToVmNicTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.rules.ExpectedException;
2626
import org.mockito.Matchers;
2727
import org.mockito.Mockito;
28-
2928
import org.apache.cloudstack.api.ResponseGenerator;
3029
import org.apache.cloudstack.api.command.user.vm.AddIpToVmNicCmd;
3130
import org.apache.cloudstack.api.command.user.vm.RemoveIpFromVmNicCmd;
@@ -39,7 +38,6 @@
3938
import com.cloud.exception.ResourceAllocationException;
4039
import com.cloud.exception.ResourceUnavailableException;
4140
import com.cloud.network.NetworkService;
42-
import com.cloud.user.Account;
4341
import com.cloud.vm.NicSecondaryIp;
4442

4543
public class AddIpToVmNicTest extends TestCase {
@@ -68,7 +66,7 @@ public void testCreateSuccess() throws ResourceAllocationException, ResourceUnav
6866
NicSecondaryIp secIp = Mockito.mock(NicSecondaryIp.class);
6967

7068
Mockito.when(
71-
networkService.allocateSecondaryGuestIP(Matchers.any(Account.class), Matchers.anyLong(), Matchers.anyLong(), Matchers.anyLong(), Matchers.anyString()))
69+
networkService.allocateSecondaryGuestIP(Matchers.anyLong(), Matchers.anyString()))
7270
.thenReturn(secIp);
7371

7472
ipTonicCmd._networkService = networkService;
@@ -88,7 +86,7 @@ public void testCreateFailure() throws ResourceAllocationException, ResourceUnav
8886
AddIpToVmNicCmd ipTonicCmd = Mockito.mock(AddIpToVmNicCmd.class);
8987

9088
Mockito.when(
91-
networkService.allocateSecondaryGuestIP(Matchers.any(Account.class), Matchers.anyLong(), Matchers.anyLong(), Matchers.anyLong(), Matchers.anyString()))
89+
networkService.allocateSecondaryGuestIP(Matchers.anyLong(), Matchers.anyString()))
9290
.thenReturn(null);
9391

9492
ipTonicCmd._networkService = networkService;

engine/components-api/src/com/cloud/network/IpAddressManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.cloudstack.framework.config.ConfigKey;
2222

2323
import com.cloud.dc.DataCenter;
24-
import com.cloud.dc.Pod;
2524
import com.cloud.dc.Vlan.VlanType;
2625
import com.cloud.exception.ConcurrentOperationException;
2726
import com.cloud.exception.InsufficientAddressCapacityException;
@@ -169,8 +168,8 @@ PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, Vl
169168

170169
int getRuleCountForIp(Long addressId, FirewallRule.Purpose purpose, FirewallRule.State state);
171170

172-
public String allocateGuestIP(Account ipOwner, boolean isSystem, long zoneId, Long networkId, String requestedIp) throws InsufficientAddressCapacityException;
171+
public String allocateGuestIP(Network network, String requestedIp) throws InsufficientAddressCapacityException;
173172

174-
String allocatePublicIpForGuestNic(Long networkId, DataCenter dc, Pod pod, Account caller, String requestedIp) throws InsufficientAddressCapacityException;
173+
String allocatePublicIpForGuestNic(Network network, Account ipOwner, String requestedIp) throws InsufficientAddressCapacityException;
175174

176175
}

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

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@Table(name = "nic_secondary_ips")
3434
public class NicSecondaryIpVO implements NicSecondaryIp {
3535

36-
public NicSecondaryIpVO(Long nicId, String ipaddr, Long vmId, Long accountId, Long domainId, Long networkId) {
36+
public NicSecondaryIpVO(long nicId, String ipaddr, long vmId, long accountId, long domainId, long networkId) {
3737
this.nicId = nicId;
3838
this.vmId = vmId;
3939
this.ip4Address = ipaddr;
@@ -57,7 +57,7 @@ protected NicSecondaryIpVO() {
5757
long domainId;
5858

5959
@Column(name = "account_id", updatable = false)
60-
private Long accountId;
60+
private long accountId;
6161

6262
@Column(name = "ip4_address")
6363
String ip4Address;
@@ -75,93 +75,53 @@ protected NicSecondaryIpVO() {
7575
String uuid = UUID.randomUUID().toString();
7676

7777
@Column(name = "vmId")
78-
Long vmId;
78+
long vmId;
7979

8080
@Override
8181
public long getId() {
8282
return id;
8383
}
8484

85-
public void setId(long id) {
86-
this.id = id;
87-
}
88-
8985
@Override
9086
public long getNicId() {
9187
return nicId;
9288
}
9389

94-
public void setNicId(long nicId) {
95-
this.nicId = nicId;
96-
}
97-
9890
@Override
9991
public long getDomainId() {
10092
return domainId;
10193
}
10294

103-
public void setDomainId(Long domainId) {
104-
this.domainId = domainId;
105-
}
106-
10795
@Override
10896
public long getAccountId() {
10997
return accountId;
11098
}
11199

112-
public void setAccountId(Long accountId) {
113-
this.accountId = accountId;
114-
}
115-
116100
@Override
117101
public String getIp4Address() {
118102
return ip4Address;
119103
}
120104

121-
public void setIp4Address(String ip4Address) {
122-
this.ip4Address = ip4Address;
123-
}
124-
125105
public String getIp6Address() {
126106
return ip6Address;
127107
}
128108

129-
public void setIp6Address(String ip6Address) {
130-
this.ip6Address = ip6Address;
131-
}
132-
133109
@Override
134110
public long getNetworkId() {
135111
return networkId;
136112
}
137113

138-
public void setNetworkId(long networkId) {
139-
this.networkId = networkId;
140-
}
141-
142114
public Date getCreated() {
143115
return created;
144116
}
145117

146-
public void setCreated(Date created) {
147-
this.created = created;
148-
}
149-
150118
@Override
151119
public String getUuid() {
152120
return uuid;
153121
}
154122

155-
public void setUuid(String uuid) {
156-
this.uuid = uuid;
157-
}
158-
159123
@Override
160124
public long getVmId() {
161125
return vmId;
162126
}
163-
164-
public void setVmId(Long vmId) {
165-
this.vmId = vmId;
166-
}
167127
}

0 commit comments

Comments
 (0)