Skip to content

Commit ae10263

Browse files
DaanHooglandNuxRoGutoVeronezi
authored
Possibility to choose the source NAT IP address on a isolated network or VPC (apache#6442)
Co-authored-by: NuxRo <nux@li.nux.ro> Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com>
1 parent 41e8ad7 commit ae10263

41 files changed

Lines changed: 1398 additions & 347 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ jobs:
8080
smoke/test_metrics_api
8181
smoke/test_migration
8282
smoke/test_multipleips_per_nic
83-
smoke/test_nested_virtualization",
83+
smoke/test_nested_virtualization
84+
smoke/test_set_sourcenat",
8485
"smoke/test_network
8586
smoke/test_network_acl
8687
smoke/test_network_ipv6

api/src/main/java/com/cloud/network/Network.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static Provider getProvider(String providerName) {
256256

257257
public static class Capability {
258258

259-
private static List<Capability> supportedCapabilities = new ArrayList<Capability>();
259+
private static List<Capability> supportedCapabilities = new ArrayList<>();
260260

261261
public static final Capability SupportedProtocols = new Capability("SupportedProtocols");
262262
public static final Capability SupportedLBAlgorithms = new Capability("SupportedLbAlgorithms");

api/src/main/java/com/cloud/network/vpc/VpcService.java

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.apache.cloudstack.api.command.user.vpc.CreateVPCCmd;
2424
import org.apache.cloudstack.api.command.user.vpc.ListPrivateGatewaysCmd;
2525
import org.apache.cloudstack.api.command.user.vpc.ListStaticRoutesCmd;
26+
import org.apache.cloudstack.api.command.user.vpc.ListVPCsCmd;
2627
import org.apache.cloudstack.api.command.user.vpc.RestartVPCCmd;
28+
import org.apache.cloudstack.api.command.user.vpc.UpdateVPCCmd;
2729

2830
import com.cloud.exception.ConcurrentOperationException;
2931
import com.cloud.exception.InsufficientAddressCapacityException;
@@ -37,7 +39,6 @@
3739

3840
public interface VpcService {
3941

40-
public Vpc createVpc(CreateVPCCmd cmd) throws ResourceAllocationException;
4142
/**
4243
* Persists VPC record in the database
4344
*
@@ -48,14 +49,25 @@ public interface VpcService {
4849
* @param displayText
4950
* @param cidr
5051
* @param networkDomain TODO
52+
* @param ip4Dns1
53+
* @param ip4Dns2
5154
* @param displayVpc TODO
5255
* @return
5356
* @throws ResourceAllocationException TODO
5457
*/
55-
public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain,
56-
String dns1, String dns2, String ip6Dns1, String ip6Dns2, Boolean displayVpc, Integer publicMtu)
58+
Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain,
59+
String ip4Dns1, String ip4Dns2, String ip6Dns1, String ip6Dns2, Boolean displayVpc, Integer publicMtu)
5760
throws ResourceAllocationException;
5861

62+
/**
63+
* Persists VPC record in the database
64+
*
65+
* @param cmd the command with specification data for the new vpc
66+
* @return a data object describing the new vpc
67+
* @throws ResourceAllocationException the resources for this VPC cannot be allocated
68+
*/
69+
Vpc createVpc(CreateVPCCmd cmd) throws ResourceAllocationException;
70+
5971
/**
6072
* Deletes a VPC
6173
*
@@ -65,48 +77,48 @@ public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName
6577
* @throws ResourceUnavailableException
6678
* @throws ConcurrentOperationException
6779
*/
68-
public boolean deleteVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException;
80+
boolean deleteVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException;
81+
82+
/**
83+
* Persists VPC record in the database
84+
*
85+
* @param cmd the command with specification data for updating the vpc
86+
* @return a data object describing the new vpc state
87+
* @throws ResourceUnavailableException if during restart some resources may not be available
88+
* @throws InsufficientCapacityException if for instance no address space, compute or storage is sufficiently available
89+
*/
90+
Vpc updateVpc(UpdateVPCCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException;
6991

7092
/**
7193
* Updates VPC with new name/displayText
7294
*
73-
* @param vpcId
74-
* @param vpcName
75-
* @param displayText
76-
* @param customId TODO
77-
* @param displayVpc TODO
78-
* @param mtu
79-
* @return
95+
* @param vpcId the ID of the Vpc to update
96+
* @param vpcName The new name to give the vpc
97+
* @param displayText the new display text to use for describing the VPC
98+
* @param customId A new custom (external) ID to associate this VPC with
99+
* @param displayVpc should this VPC be displayed on public lists
100+
* @param mtu what maximal transfer unit to us in this VPCs networks
101+
* @param sourceNatIp the source NAT address to use for this VPC (must already be associated with the VPC)
102+
* @return an object describing the current state of the VPC
103+
* @throws ResourceUnavailableException if during restart some resources may not be available
104+
* @throws InsufficientCapacityException if for instance no address space, compute or storage is sufficiently available
80105
*/
81-
public Vpc updateVpc(long vpcId, String vpcName, String displayText, String customId, Boolean displayVpc, Integer mtu);
106+
Vpc updateVpc(long vpcId, String vpcName, String displayText, String customId, Boolean displayVpc, Integer mtu, String sourceNatIp) throws ResourceUnavailableException, InsufficientCapacityException;
82107

83108
/**
84-
* Lists VPC(s) based on the parameters passed to the method call
109+
* Lists VPC(s) based on the parameters passed to the API call
85110
*
86-
* @param id
87-
* @param vpcName
88-
* @param displayText
89-
* @param supportedServicesStr
90-
* @param cidr
91-
* @param state TODO
92-
* @param accountName
93-
* @param domainId
94-
* @param keyword
95-
* @param startIndex
96-
* @param pageSizeVal
97-
* @param zoneId TODO
98-
* @param isRecursive TODO
99-
* @param listAll TODO
100-
* @param restartRequired TODO
101-
* @param tags TODO
102-
* @param projectId TODO
103-
* @param display TODO
104-
* @param vpc
105-
* @return
111+
* @param cmd object containing the search specs
112+
* @return the List of VPCs
106113
*/
107-
public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, String displayText, List<String> supportedServicesStr, String cidr, Long vpcOffId, String state,
108-
String accountName, Long domainId, String keyword, Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, Boolean restartRequired,
109-
Map<String, String> tags, Long projectId, Boolean display);
114+
Pair<List<? extends Vpc>, Integer> listVpcs(ListVPCsCmd cmd);
115+
116+
/**
117+
* Lists VPC(s) based on the parameters passed to the method call
118+
*/
119+
Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, String displayText, List<String> supportedServicesStr, String cidr, Long vpcOffId, String state,
120+
String accountName, Long domainId, String keyword, Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, Boolean restartRequired,
121+
Map<String, String> tags, Long projectId, Boolean display);
110122

111123
/**
112124
* Starts VPC which includes starting VPC provider and applying all the networking rules on the backend
@@ -130,17 +142,17 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
130142
*/
131143
boolean shutdownVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException;
132144

145+
boolean restartVpc(RestartVPCCmd cmd) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException;
146+
133147
/**
134148
* Restarts the VPC. VPC gets shutdown and started as a part of it
135149
*
136-
* @param id
137-
* @param cleanUp
138-
* @param makeredundant
139-
* @return
140-
* @throws InsufficientCapacityException
150+
* @param networkId the network to restart
151+
* @param cleanup throw away the existing VR and rebuild a new one?
152+
* @param makeRedundant create two VRs for this network
153+
* @return success or not
154+
* @throws InsufficientCapacityException when there is no suitable deployment plan possible
141155
*/
142-
boolean restartVpc(RestartVPCCmd cmd) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException;
143-
144156
boolean restartVpc(Long networkId, boolean cleanup, boolean makeRedundant, boolean livePatch, User user) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException;
145157

146158
/**
@@ -154,23 +166,12 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
154166
/**
155167
* Persists VPC private gateway in the Database.
156168
*
157-
*
158-
* @param vpcId TODO
159-
* @param physicalNetworkId
160-
* @param vlan
161-
* @param ipAddress
162-
* @param gateway
163-
* @param netmask
164-
* @param gatewayOwnerId
165-
* @param networkOfferingId
166-
* @param isSourceNat
167-
* @param aclId
168-
* @return
169+
* @return data object describing the private gateway
169170
* @throws InsufficientCapacityException
170171
* @throws ConcurrentOperationException
171172
* @throws ResourceAllocationException
172173
*/
173-
public PrivateGateway createVpcPrivateGateway(CreatePrivateGatewayCmd command) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException;
174+
PrivateGateway createVpcPrivateGateway(CreatePrivateGatewayCmd command) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException;
174175

175176
/**
176177
* Applies VPC private gateway on the backend, so it becomes functional
@@ -181,12 +182,12 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
181182
* @throws ResourceUnavailableException
182183
* @throws ConcurrentOperationException
183184
*/
184-
public PrivateGateway applyVpcPrivateGateway(long gatewayId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException;
185+
PrivateGateway applyVpcPrivateGateway(long gatewayId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException;
185186

186187
/**
187188
* Deletes VPC private gateway
188189
*
189-
* @param id
190+
* @param gatewayId
190191
* @return
191192
* @throws ResourceUnavailableException
192193
* @throws ConcurrentOperationException
@@ -199,7 +200,7 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
199200
* @param listPrivateGatewaysCmd
200201
* @return
201202
*/
202-
public Pair<List<PrivateGateway>, Integer> listPrivateGateway(ListPrivateGatewaysCmd listPrivateGatewaysCmd);
203+
Pair<List<PrivateGateway>, Integer> listPrivateGateway(ListPrivateGatewaysCmd listPrivateGatewaysCmd);
203204

204205
/**
205206
* Returns Static Route found by Id
@@ -216,7 +217,7 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
216217
* @return
217218
* @throws ResourceUnavailableException
218219
*/
219-
public boolean applyStaticRoutesForVpc(long vpcId) throws ResourceUnavailableException;
220+
boolean applyStaticRoutesForVpc(long vpcId) throws ResourceUnavailableException;
220221

221222
/**
222223
* Deletes static route from the backend and the database
@@ -225,7 +226,7 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
225226
* @return TODO
226227
* @throws ResourceUnavailableException
227228
*/
228-
public boolean revokeStaticRoute(long routeId) throws ResourceUnavailableException;
229+
boolean revokeStaticRoute(long routeId) throws ResourceUnavailableException;
229230

230231
/**
231232
* Persists static route entry in the Database
@@ -234,15 +235,15 @@ public Pair<List<? extends Vpc>, Integer> listVpcs(Long id, String vpcName, Stri
234235
* @param cidr
235236
* @return
236237
*/
237-
public StaticRoute createStaticRoute(long gatewayId, String cidr) throws NetworkRuleConflictException;
238+
StaticRoute createStaticRoute(long gatewayId, String cidr) throws NetworkRuleConflictException;
238239

239240
/**
240241
* Lists static routes based on parameters passed to the call
241242
*
242-
* @param listStaticRoutesCmd
243+
* @param cmd Command object with parameters for { @see ListStaticRoutesCmd }
243244
* @return
244245
*/
245-
public Pair<List<? extends StaticRoute>, Integer> listStaticRoutes(ListStaticRoutesCmd cmd);
246+
Pair<List<? extends StaticRoute>, Integer> listStaticRoutes(ListStaticRoutesCmd cmd);
246247

247248
/**
248249
* Associates IP address from the Public network, to the VPC
@@ -262,6 +263,5 @@ IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationExcep
262263
* @param routeId
263264
* @return
264265
*/
265-
public boolean applyStaticRoute(long routeId) throws ResourceUnavailableException;
266-
266+
boolean applyStaticRoute(long routeId) throws ResourceUnavailableException;
267267
}

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ public class ApiConstants {
10321032
public static final String AUTO_ENABLE_KVM_HOST = "autoenablekvmhost";
10331033
public static final String LIST_APIS = "listApis";
10341034

1035+
public static final String SOURCE_NAT_IP = "sourcenatipaddress";
1036+
public static final String SOURCE_NAT_IP_ID = "sourcenatipaddressid";
1037+
public static final String HAS_RULES = "hasrules";
1038+
10351039
/**
10361040
* This enum specifies IO Drivers, each option controls specific policies on I/O.
10371041
* Qemu guests support "threads" and "native" options Since 0.8.8 ; "io_uring" is supported Since 6.3.0 (QEMU 5.0).

api/src/main/java/org/apache/cloudstack/api/command/user/address/ListPublicIpAddressesCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ public String getCommandName() {
192192
@Override
193193
public void execute() {
194194
Pair<List<? extends IpAddress>, Integer> result = _mgr.searchForIPAddresses(this);
195-
ListResponse<IPAddressResponse> response = new ListResponse<IPAddressResponse>();
196-
List<IPAddressResponse> ipAddrResponses = new ArrayList<IPAddressResponse>();
195+
ListResponse<IPAddressResponse> response = new ListResponse<>();
196+
List<IPAddressResponse> ipAddrResponses = new ArrayList<>();
197197
for (IpAddress ipAddress : result.first()) {
198198
IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(getResponseView(), ipAddress);
199199
ipResponse.setObjectName("publicipaddress");

api/src/main/java/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.network;
1818

19-
import com.cloud.network.NetworkService;
19+
import org.apache.commons.lang3.StringUtils;
2020
import org.apache.log4j.Logger;
2121

2222
import org.apache.cloudstack.acl.RoleType;
@@ -43,10 +43,10 @@
4343
import com.cloud.exception.InvalidParameterValueException;
4444
import com.cloud.exception.ResourceAllocationException;
4545
import com.cloud.network.Network;
46+
import com.cloud.network.NetworkService;
4647
import com.cloud.network.Network.GuestType;
4748
import com.cloud.offering.NetworkOffering;
4849
import com.cloud.utils.net.NetUtils;
49-
import org.apache.commons.lang3.StringUtils;
5050

5151
@APICommand(name = "createNetwork", description = "Creates a network", responseObject = NetworkResponse.class, responseView = ResponseView.Restricted, entityType = {Network.class},
5252
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
@@ -183,6 +183,14 @@ public class CreateNetworkCmd extends BaseCmd implements UserCmd {
183183
@Parameter(name = ApiConstants.IP6_DNS2, type = CommandType.STRING, description = "the second IPv6 DNS for the network", since = "4.18.0")
184184
private String ip6Dns2;
185185

186+
@Parameter(name = ApiConstants.SOURCE_NAT_IP,
187+
type = CommandType.STRING,
188+
description = "IPV4 address to be assigned to the public interface of the network router. " +
189+
"This address will be used as source NAT address for the network. " +
190+
"\nIf an address is given and it cannot be acquired, an error will be returned and the network won´t be implemented,",
191+
since = "4.19")
192+
private String sourceNatIP;
193+
186194
/////////////////////////////////////////////////////
187195
/////////////////// Accessors ///////////////////////
188196
/////////////////////////////////////////////////////
@@ -266,6 +274,10 @@ public String getTungstenVirtualRouterUuid() {
266274
return tungstenVirtualRouterUuid;
267275
}
268276

277+
public String getSourceNatIP() {
278+
return sourceNatIP;
279+
}
280+
269281
@Override
270282
public boolean isDisplay() {
271283
if(displayNetwork == null)

api/src/main/java/org/apache/cloudstack/api/command/user/network/UpdateNetworkCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public class UpdateNetworkCmd extends BaseAsyncCustomIdCmd implements UserCmd {
104104
@Parameter(name = ApiConstants.IP6_DNS2, type = CommandType.STRING, description = "the second IPv6 DNS for the network. Empty string will update the second IPv6 DNS with the value from the zone", since = "4.18.0")
105105
private String ip6Dns2;
106106

107+
@Parameter(name = ApiConstants.SOURCE_NAT_IP, type = CommandType.STRING, description = "IPV4 address to be assigned to the public interface of the network router. This address must already be acquired for this network", since = "4.19")
108+
private String sourceNatIP;
109+
107110
/////////////////////////////////////////////////////
108111
/////////////////// Accessors ///////////////////////
109112
/////////////////////////////////////////////////////
@@ -181,6 +184,10 @@ public String getIp6Dns2() {
181184
return ip6Dns2;
182185
}
183186

187+
public String getSourceNatIP() {
188+
return sourceNatIP;
189+
}
190+
184191
/////////////////////////////////////////////////////
185192
/////////////// API Implementation///////////////////
186193
/////////////////////////////////////////////////////

0 commit comments

Comments
 (0)