in cloudstack/resource_cloudstack_port_forward.go [159:213]
func createPortForward(d *schema.ResourceData, meta interface{}, forward map[string]interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Make sure all required parameters are there
if err := verifyPortForwardParams(d, forward); err != nil {
return err
}
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
forward["virtual_machine_id"].(string),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
return err
}
// Create a new parameter struct
p := cs.Firewall.NewCreatePortForwardingRuleParams(d.Id(), forward["private_port"].(int),
forward["protocol"].(string), forward["public_port"].(int), vm.Id)
if vmGuestIP, ok := forward["vm_guest_ip"]; ok && vmGuestIP.(string) != "" {
p.SetVmguestip(vmGuestIP.(string))
// Set the network ID based on the guest IP, needed when the public IP address
// is not associated with any network yet
NICS:
for _, nic := range vm.Nic {
if vmGuestIP.(string) == nic.Ipaddress {
p.SetNetworkid(nic.Networkid)
break NICS
}
for _, ip := range nic.Secondaryip {
if vmGuestIP.(string) == ip.Ipaddress {
p.SetNetworkid(nic.Networkid)
break NICS
}
}
}
} else {
// If no guest IP is configured, use the primary NIC
p.SetNetworkid(vm.Nic[0].Networkid)
}
// Do not open the firewall automatically in any case
p.SetOpenfirewall(false)
r, err := cs.Firewall.CreatePortForwardingRule(p)
if err != nil {
return err
}
forward["uuid"] = r.Id
return nil
}