bhouse-nexthop commented on code in PR #280:
URL: 
https://github.com/apache/cloudstack-terraform-provider/pull/280#discussion_r3570058268


##########
cloudstack/resource_cloudstack_ipaddress.go:
##########
@@ -102,11 +102,33 @@ func resourceCloudStackIPAddressCreate(d 
*schema.ResourceData, meta interface{})
                if vpcid, ok := d.GetOk("vpc_id"); ok && vpcid.(string) != "" {
                        return fmt.Errorf("set only network_id or vpc_id")
                }
+
+               // If no project is explicitly set, try to inherit it from the 
network
+               if _, ok := d.GetOk("project"); !ok {
+                       // Get the network to retrieve its project
+                       // Use projectid=-1 to search across all projects
+                       network, count, err := 
cs.Network.GetNetworkByID(networkid.(string), cloudstack.WithProject("-1"))
+                       if err == nil && count > 0 && network.Projectid != "" {
+                               log.Printf("[DEBUG] Inheriting project %s from 
network %s", network.Projectid, networkid.(string))
+                               p.SetProjectid(network.Projectid)
+                       }
+               }
        }
 
        if vpcid, ok := d.GetOk("vpc_id"); ok {
                // Set the vpcid
                p.SetVpcid(vpcid.(string))
+
+               // If no project is explicitly set, try to inherit it from the 
VPC
+               if _, ok := d.GetOk("project"); !ok {
+                       // Get the VPC to retrieve its project
+                       // Use projectid=-1 to search across all projects
+                       vpc, count, err := cs.VPC.GetVPCByID(vpcid.(string), 
cloudstack.WithProject("-1"))
+                       if err == nil && count > 0 && vpc.Projectid != "" {
+                               log.Printf("[DEBUG] Inheriting project %s from 
VPC %s", vpc.Projectid, vpcid.(string))
+                               p.SetProjectid(vpc.Projectid)
+                       }
+               }
        }

Review Comment:
   Good catch. Fixed in ed6f474 by adding the same `projectid=-1` read fallback 
already used by the network and instance resources: if the initial lookup 
returns count==0 and no explicit project is configured, Read retries with 
`WithProject("-1")` so inherited-project IPs are found instead of being cleared 
from state. Chose the read fallback over setting `project` in Create to stay 
consistent with the sibling resources.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to