bhouse-nexthop commented on code in PR #280:
URL:
https://github.com/apache/cloudstack-terraform-provider/pull/280#discussion_r3086956825
##########
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:
This is a best-effort inheritance — if the IP address lookup fails, it
simply means no project is inherited, and the resource continues without one
(same as if the user never specified a project). The err == nil && count > 0
guard is intentional: we don't want to fail resource creation just because we
couldn't look up project metadata for an optional field. The resource will
still work; it just won't have project context in state.
--
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]