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


##########
cloudstack/resource_cloudstack_nic.go:
##########
@@ -88,54 +88,64 @@ func resourceCloudStackNICCreate(d *schema.ResourceData, 
meta interface{}) error
                return fmt.Errorf("Error creating the new NIC: %s", err)
        }
 
-       found := false
-       for _, n := range r.(*cloudstack.AddNicToVirtualMachineResponse).Nic {
-               if n.Networkid == d.Get("network_id").(string) {
-                       d.SetId(n.Id)
-                       found = true
-                       break
-               }
-       }
-
-       if !found {
-               return fmt.Errorf("Could not find NIC ID for network ID: %s", 
d.Get("network_id").(string))
-       }
-
-       return resourceCloudStackNICRead(d, meta)
+       return setCloudStackNICStateFromCreateResponse(
+               d,
+               r.(*cloudstack.AddNicToVirtualMachineResponse),
+       )
 }
 
 func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error 
{
        cs := meta.(*cloudstack.CloudStackClient)
 
-       // Get the virtual machine details
-       vm, count, err := 
cs.VirtualMachine.GetVirtualMachineByID(d.Get("virtual_machine_id").(string))
-       if err != nil {
-               if count == 0 {
-                       log.Printf("[DEBUG] Instance %s does no longer exist", 
d.Get("virtual_machine_id").(string))
-                       d.SetId("")
-                       return nil
-               }
+       p := cs.Nic.NewListNicsParams(d.Get("virtual_machine_id").(string))
+       p.SetNicid(d.Id())
 
+       l, err := cs.Nic.ListNics(p)
+       if err != nil {
                return err

Review Comment:
   This removes the previous `GetVirtualMachineByID` check that converted a 
deleted instance into `d.SetId("")`. Because `listNics` is scoped by 
`virtual_machine_id`, a refresh after the instance is deleted can instead 
return a VM-not-found error here, causing Terraform refresh to fail while 
retaining the NIC in state rather than treating it as gone. Preserve the prior 
missing-instance handling (or translate the corresponding not-found error) 
before returning this error.



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