Skip to content

Commit eaf7578

Browse files
vishesh92ianc769
authored andcommitted
Allow specifying private end port & public end port for port forward rules
1 parent 27cdc1b commit eaf7578

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

cloudstack/resource_cloudstack_port_forward.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,23 @@ func resourceCloudStackPortForward() *schema.Resource {
7373
Required: true,
7474
},
7575

76+
"private_end_port": {
77+
Type: schema.TypeInt,
78+
Optional: true,
79+
Computed: true,
80+
},
81+
7682
"public_port": {
7783
Type: schema.TypeInt,
7884
Required: true,
7985
},
8086

87+
"public_end_port": {
88+
Type: schema.TypeInt,
89+
Optional: true,
90+
Computed: true,
91+
},
92+
8193
"virtual_machine_id": {
8294
Type: schema.TypeString,
8395
Required: true,
@@ -175,6 +187,12 @@ func createPortForward(d *schema.ResourceData, meta interface{}, forward map[str
175187
// Create a new parameter struct
176188
p := cs.Firewall.NewCreatePortForwardingRuleParams(d.Id(), forward["private_port"].(int),
177189
forward["protocol"].(string), forward["public_port"].(int), vm.Id)
190+
if val, ok := forward["private_end_port"]; ok && val != nil && val.(int) != 0 {
191+
p.SetPrivateendport(val.(int))
192+
}
193+
if val, ok := forward["public_end_port"]; ok && val != nil && val.(int) != 0 {
194+
p.SetPublicendport(val.(int))
195+
}
178196

179197
if vmGuestIP, ok := forward["vm_guest_ip"]; ok && vmGuestIP.(string) != "" {
180198
p.SetVmguestip(vmGuestIP.(string))
@@ -288,6 +306,21 @@ func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{})
288306
forward["protocol"] = f.Protocol
289307
forward["private_port"] = privPort
290308
forward["public_port"] = pubPort
309+
// Only set end ports if they differ from start ports (indicating a range)
310+
if f.Privateendport != "" && f.Privateendport != f.Privateport {
311+
privEndPort, err := strconv.Atoi(f.Privateendport)
312+
if err != nil {
313+
return err
314+
}
315+
forward["private_end_port"] = privEndPort
316+
}
317+
if f.Publicendport != "" && f.Publicendport != f.Publicport {
318+
pubEndPort, err := strconv.Atoi(f.Publicendport)
319+
if err != nil {
320+
return err
321+
}
322+
forward["public_end_port"] = pubEndPort
323+
}
291324
forward["virtual_machine_id"] = f.Virtualmachineid
292325

293326
// This one is a bit tricky. We only want to update this optional value

website/docs/r/port_forward.html.markdown

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ The `forward` block supports:
4747
* `protocol` - (Required) The name of the protocol to allow. Valid options are:
4848
`tcp` and `udp`.
4949

50-
* `private_port` - (Required) The private port to forward to.
50+
* `private_port` - (Required) The starting port of port forwarding rule's private port range.
5151

52-
* `public_port` - (Required) The public port to forward from.
52+
* `private_end_port` - (Optional) The ending port of port forwarding rule's private port range.
53+
If not specified, the private port will be used as the end port.
54+
55+
* `public_port` - (Required) The starting port of port forwarding rule's public port range.
56+
57+
* `public_end_port` - (Optional) The ending port of port forwarding rule's public port range.
58+
If not specified, the public port will be used as the end port.
5359

5460
* `virtual_machine_id` - (Required) The ID of the virtual machine to forward to.
5561

0 commit comments

Comments
 (0)