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


##########
cloudstack/resource_cloudstack_network.go:
##########
@@ -359,6 +375,14 @@ func resourceCloudStackNetworkCreate(d 
*schema.ResourceData, meta interface{}) e
                return err
        }
 
+       if account, ok := d.GetOk("account"); ok {
+               p.SetAccount(account.(string))
+       }

Review Comment:
   Please reject `account` without `domain_id` before creating the network, as 
required by the [CloudStack API 
contract](https://github.com/apache/cloudstack/blob/f9c83c19424a4bb6f5b054722059368dfeff3627/api/src/main/java/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java#L112-L112).
 CloudStack only selects the requested account when both values are present; 
with root credentials, omitting `domain_id` can create a root-owned network 
instead of the requested tenant-owned network.



##########
cloudstack/resource_cloudstack_network.go:
##########
@@ -407,31 +431,58 @@ func resourceCloudStackNetworkCreate(d 
*schema.ResourceData, meta interface{}) e
 func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) 
error {
        cs := meta.(*cloudstack.CloudStackClient)
 
-       // Get the network details
-       // First try with the project from state (if any)
-       project := d.Get("project").(string)
-       n, count, err := cs.Network.GetNetworkByID(
-               d.Id(),
-               cloudstack.WithProject(project),
-       )
-
-       // If not found and no explicit project was set, try with projectid=-1
-       // This handles the case where the project was inherited from VPC
-       if count == 0 && project == "" {
+       var n *cloudstack.Network
+       var count int
+       var err error
+
+       if account, ok := d.GetOk("account"); ok {
+               // GetNetworkByID's plain listNetworks call only returns 
resources
+               // visible to the caller's own default scope - a network created
+               // (e.g. by a root admin) under a different account is 
invisible to
+               // it unless account/domainid/listall are set explicitly.
+               p := cs.Network.NewListNetworksParams()
+               p.SetId(d.Id())
+               p.SetAccount(account.(string))
+               if domainID, ok := d.GetOk("domain_id"); ok {
+                       p.SetDomainid(domainID.(string))
+               }
+               p.SetListall(true)
+
+               l, listErr := cs.Network.ListNetworks(p)
+               if listErr != nil {
+                       return listErr
+               }

Review Comment:
   For account-scoped networks, an out-of-band deletion can return CloudStack's 
`Invalid parameter id ... or entity does not exist` error. This immediate 
return prevents the later `count == 0` block from clearing the ID, so 
refresh/plan fails instead of detecting that the network needs recreation. 
Preserve the SDK helper's [not-found 
normalization](https://github.com/apache/cloudstack-go/blob/e463cd1c0db2582865a310ad8053653b74ce1f22/cloudstack/NetworkService.go#L4916-L4922),
 also verified in the pinned v2.19.1 SDK, while still returning other API 
errors.



##########
cloudstack/resource_cloudstack_network.go:
##########
@@ -407,31 +431,58 @@ func resourceCloudStackNetworkCreate(d 
*schema.ResourceData, meta interface{}) e
 func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) 
error {
        cs := meta.(*cloudstack.CloudStackClient)
 
-       // Get the network details
-       // First try with the project from state (if any)
-       project := d.Get("project").(string)
-       n, count, err := cs.Network.GetNetworkByID(
-               d.Id(),
-               cloudstack.WithProject(project),
-       )
-
-       // If not found and no explicit project was set, try with projectid=-1
-       // This handles the case where the project was inherited from VPC
-       if count == 0 && project == "" {
+       var n *cloudstack.Network
+       var count int
+       var err error
+
+       if account, ok := d.GetOk("account"); ok {
+               // GetNetworkByID's plain listNetworks call only returns 
resources
+               // visible to the caller's own default scope - a network created
+               // (e.g. by a root admin) under a different account is 
invisible to
+               // it unless account/domainid/listall are set explicitly.
+               p := cs.Network.NewListNetworksParams()
+               p.SetId(d.Id())
+               p.SetAccount(account.(string))
+               if domainID, ok := d.GetOk("domain_id"); ok {
+                       p.SetDomainid(domainID.(string))
+               }
+               p.SetListall(true)
+
+               l, listErr := cs.Network.ListNetworks(p)
+               if listErr != nil {
+                       return listErr
+               }
+               count = l.Count
+               if count > 0 {
+                       n = l.Networks[0]
+               }
+       } else {
+               // Get the network details
+               // First try with the project from state (if any)
+               project := d.Get("project").(string)
                n, count, err = cs.Network.GetNetworkByID(
                        d.Id(),
-                       cloudstack.WithProject("-1"),
+                       cloudstack.WithProject(project),

Review Comment:
   During import, `importStatePassthrough` restores only the ID and optional 
project, so the first read lacks `account` and takes this branch. For networks 
in a tenant's child domain, neither lookup enables cross-domain listing, so the 
read can miss an existing network and clear its ID. Add 
[`WithListall(true)`](https://github.com/apache/cloudstack-go/blob/e463cd1c0db2582865a310ad8053653b74ce1f22/cloudstack/cloudstack.go#L733-L739)
 to both attempts while retaining their project options.



##########
cloudstack/resource_cloudstack_network.go:
##########
@@ -454,6 +505,13 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, 
meta interface{}) err
        }
        d.Set("acl_id", n.Aclid)
 
+       if _, ok := d.GetOk("account"); ok {
+               d.Set("account", n.Account)
+       }
+       if _, ok := d.GetOk("domain_id"); ok {
+               d.Set("domain_id", n.Domainid)

Review Comment:
   These guards leave the Optional+Computed ownership fields empty when they 
were omitted, rather than populating them from the API as described in the PR. 
This leaves attribute references without the actual owner and can force 
unnecessary replacement when the same owner is later configured. Populate both 
fields from the response, and preserve project-scoped lookup when those newly 
populated values select the account branch on subsequent refreshes.



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