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


##########
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:
   When `project` is inherited from a VPC or network in Create (lines 110-114 
and 126-130), `p.SetProjectid` is set on the API parameters but 
`d.Set("project", ...)` is never called on the resource data. As a result, 
after `AssociateIpAddress` succeeds, `resourceCloudStackIPAddressRead` runs 
with `d.Get("project") == ""` and calls `GetPublicIpAddressByID(..., 
cloudstack.WithProject(""))` (line 174). For project-scoped IPs this lookup 
will typically return count==0, and the resource will be cleared from state 
(SetId("")) — reproducing the same "inconsistent result after apply" class of 
bug this PR is intended to fix. Either set `d.Set("project", ...)` in Create 
when inheritance happens (as done in `resource_cloudstack_port_forward.go` and 
`resource_cloudstack_network_acl.go`), or add a fallback in Read that retries 
with `WithProject("-1")` when the initial lookup returns 0 and no explicit 
project is configured (as done in `resource_cloudstack_network.go` and 
`resource_cloudstack_ins
 tance.go`).



-- 
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