Skip to content

Commit fc43f6c

Browse files
committed
Allow specifying private end port & public end port for port forward rules
1 parent 4eb6d4b commit fc43f6c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
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

0 commit comments

Comments
 (0)