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


##########
cloudstack/resource_cloudstack_port_forward.go:
##########
@@ -112,9 +113,23 @@ func resourceCloudStackPortForward() *schema.Resource {
 }
 
 func resourceCloudStackPortForwardCreate(d *schema.ResourceData, meta 
interface{}) error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
        // We need to set this upfront in order to be able to save a partial 
state
        d.SetId(d.Get("ip_address_id").(string))
 
+       // If no project is explicitly set, try to inherit it from the IP 
address
+       if _, ok := d.GetOk("project"); !ok {
+               // Get the IP address to retrieve its project
+               // Use projectid=-1 to search across all projects
+               ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id(), 
cloudstack.WithProject("-1"))
+               if err == nil && count > 0 && ip.Projectid != "" {
+                       log.Printf("[DEBUG] Inheriting project %s from IP 
address %s", ip.Projectid, d.Id())
+                       // Set the project in the resource data for state 
management
+                       d.Set("project", ip.Project)

Review Comment:
   In the project-inheritance block, errors/zero-count from 
`GetPublicIpAddressByID` are silently ignored. If this lookup fails, `project` 
remains unset and subsequent reads/listing may still miss project-scoped 
port-forward rules (reintroducing the inconsistent post-apply state). Consider 
handling the error explicitly (return a diagnostic, or at least log a warning) 
and setting `project` using `setValueOrID(d, "project", ip.Project, 
ip.Projectid)` (or fallback to `ip.Projectid` when `ip.Project` is empty) so 
state is consistent.
   ```suggestion
                if err != nil {
                        return fmt.Errorf("failed to retrieve IP address %s for 
project inheritance: %w", d.Id(), err)
                }
                if count == 0 {
                        return fmt.Errorf("failed to retrieve IP address %s for 
project inheritance: no IP address found", d.Id())
                }
                if ip.Projectid != "" {
                        project := ip.Project
                        if project == "" {
                                project = ip.Projectid
                        }
                        log.Printf("[DEBUG] Inheriting project %s from IP 
address %s", ip.Projectid, d.Id())
                        // Set the project in the resource data for state 
management
                        d.Set("project", project)
   ```



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