|
40 | 40 | import com.cloud.user.Account; |
41 | 41 | import com.cloud.user.UserContext; |
42 | 42 | import com.cloud.utils.net.Ip; |
| 43 | +import com.cloud.utils.net.NetUtils; |
43 | 44 |
|
44 | 45 | @Implementation(description = "Creates a port forwarding rule", responseObject = FirewallRuleResponse.class) |
45 | 46 | public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements PortForwardingRule { |
@@ -108,20 +109,27 @@ public List<String> getSourceCidrList() { |
108 | 109 | public String getCommandName() { |
109 | 110 | return s_name; |
110 | 111 | } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void setSourceCidrList(List<String> cidrs){ |
| 115 | + cidrlist = cidrs; |
| 116 | + } |
111 | 117 |
|
112 | 118 | @Override |
113 | 119 | public void execute() throws ResourceUnavailableException { |
114 | 120 | UserContext callerContext = UserContext.current(); |
115 | | - |
116 | 121 | boolean success = false; |
117 | 122 | PortForwardingRule rule = _entityMgr.findById(PortForwardingRule.class, getEntityId()); |
| 123 | + // load cidrs if any |
| 124 | + rule.setSourceCidrList(_rulesService.getSourceCidrs(rule.getId())); |
| 125 | + |
118 | 126 | try { |
119 | 127 | UserContext.current().setEventDetails("Rule Id: " + getEntityId()); |
120 | 128 | success = _rulesService.applyPortForwardingRules(rule.getSourceIpAddressId(), callerContext.getCaller()); |
121 | 129 |
|
122 | 130 | // State is different after the rule is applied, so get new object here |
123 | 131 | rule = _entityMgr.findById(PortForwardingRule.class, getEntityId()); |
124 | | - FirewallRuleResponse fwResponse = new FirewallRuleResponse(); |
| 132 | + FirewallRuleResponse fwResponse = new FirewallRuleResponse(); |
125 | 133 | if (rule != null) { |
126 | 134 | fwResponse = _responseGenerator.createFirewallRuleResponse(rule); |
127 | 135 | setResponseObject(fwResponse); |
@@ -210,6 +218,12 @@ public int getDestinationPortEnd() { |
210 | 218 |
|
211 | 219 | @Override |
212 | 220 | public void create() { |
| 221 | + if (cidrlist != null) |
| 222 | + for (String cidr: cidrlist){ |
| 223 | + if (!NetUtils.isValidCIDR(cidr)){ |
| 224 | + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Source cidrs formatting error " + cidr); |
| 225 | + } |
| 226 | + } |
213 | 227 | try { |
214 | 228 | PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId); |
215 | 229 | setEntityId(result.getId()); |
|
0 commit comments