in cloudstack/resource_cloudstack_port_forward.go [215:329]
func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// First check if the IP address is still associated
_, count, err := cs.Address.GetPublicIpAddressByID(
d.Id(),
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
log.Printf(
"[DEBUG] IP address with ID %s is no longer associated", d.Id())
d.SetId("")
return nil
}
return err
}
// Get all the forwards from the running environment
p := cs.Firewall.NewListPortForwardingRulesParams()
p.SetIpaddressid(d.Id())
p.SetListall(true)
if err := setProjectid(p, cs, d); err != nil {
return err
}
l, err := cs.Firewall.ListPortForwardingRules(p)
if err != nil {
return err
}
// Make a map of all the forwards so we can easily find a forward
forwardMap := make(map[string]*cloudstack.PortForwardingRule, l.Count)
for _, f := range l.PortForwardingRules {
forwardMap[f.Id] = f
}
// Create an empty schema.Set to hold all forwards
forwards := resourceCloudStackPortForward().Schema["forward"].ZeroValue().(*schema.Set)
// Read all forwards that are configured
if rs := d.Get("forward").(*schema.Set); rs.Len() > 0 {
for _, forward := range rs.List() {
forward := forward.(map[string]interface{})
id, ok := forward["uuid"]
if !ok || id.(string) == "" {
continue
}
// Get the forward
f, ok := forwardMap[id.(string)]
if !ok {
forward["uuid"] = ""
continue
}
// Delete the known rule so only unknown rules remain in the ruleMap
delete(forwardMap, id.(string))
privPort, err := strconv.Atoi(f.Privateport)
if err != nil {
return err
}
pubPort, err := strconv.Atoi(f.Publicport)
if err != nil {
return err
}
// Update the values
forward["protocol"] = f.Protocol
forward["private_port"] = privPort
forward["public_port"] = pubPort
forward["virtual_machine_id"] = f.Virtualmachineid
// This one is a bit tricky. We only want to update this optional value
// if we've set one ourselves. If not this would become a computed value
// and that would mess up the calculated hash of the set item.
if forward["vm_guest_ip"].(string) != "" {
forward["vm_guest_ip"] = f.Vmguestip
}
forwards.Add(forward)
}
}
// If this is a managed resource, add all unknown forwards to dummy forwards
managed := d.Get("managed").(bool)
if managed && len(forwardMap) > 0 {
for uuid := range forwardMap {
// Make a dummy forward to hold the unknown UUID
forward := map[string]interface{}{
"protocol": uuid,
"private_port": 0,
"public_port": 0,
"virtual_machine_id": uuid,
"uuid": uuid,
}
// Add the dummy forward to the forwards set
forwards.Add(forward)
}
}
if forwards.Len() > 0 {
d.Set("forward", forwards)
} else if !managed {
d.SetId("")
}
return nil
}