This is an automated email from the ASF dual-hosted git repository.

harikrishna pushed a commit to branch FixTerraformDestroyForIpAddress
in repository 
https://gitbox.apache.org/repos/asf/cloudstack-terraform-provider.git


The following commit(s) were added to 
refs/heads/FixTerraformDestroyForIpAddress by this push:
     new f6386b0  Fix ip address deletion during "terraform destory" command. 
All created IP addresses will be destroyed but the IP having source nat, does 
not have to be deassociated.
f6386b0 is described below

commit f6386b0951ca7e6c987c3129d824c03e5ce848f3
Author: Harikrishna Patnala <[email protected]>
AuthorDate: Tue Jan 11 13:17:02 2022 +0530

    Fix ip address deletion during "terraform destory" command. All created IP 
addresses will be destroyed but the IP having source nat, does not have to be 
deassociated.
---
 cloudstack/resource_cloudstack_ipaddress.go | 36 ++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/cloudstack/resource_cloudstack_ipaddress.go 
b/cloudstack/resource_cloudstack_ipaddress.go
index 743cd10..98ad47e 100644
--- a/cloudstack/resource_cloudstack_ipaddress.go
+++ b/cloudstack/resource_cloudstack_ipaddress.go
@@ -72,6 +72,11 @@ func resourceCloudStackIPAddress() *schema.Resource {
                                Computed: true,
                        },
 
+                       "is_source_nat": {
+                               Type:     schema.TypeBool,
+                               Computed: true,
+                       },
+
                        "tags": tagsSchema(),
                },
        }
@@ -154,6 +159,7 @@ func resourceCloudStackIPAddressRead(d 
*schema.ResourceData, meta interface{}) e
        }
 
        d.Set("is_portable", ip.Isportable)
+       d.Set("is_source_nat", ip.Issourcenat)
 
        // Updated the IP address
        d.Set("ip_address", ip.Ipaddress)
@@ -182,21 +188,23 @@ func resourceCloudStackIPAddressRead(d 
*schema.ResourceData, meta interface{}) e
 }
 
 func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta 
interface{}) error {
-       cs := meta.(*cloudstack.CloudStackClient)
-
-       // Create a new parameter struct
-       p := cs.Address.NewDisassociateIpAddressParams(d.Id())
-
-       // Disassociate the IP address
-       if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
-               // This is a very poor way to be told the ID does no longer 
exist :(
-               if strings.Contains(err.Error(), fmt.Sprintf(
-                       "Invalid parameter id value=%s due to incorrect long 
value format, "+
-                               "or entity does not exist", d.Id())) {
-                       return nil
+       if !d.Get("is_source_nat").(bool) {
+               cs := meta.(*cloudstack.CloudStackClient)
+
+               // Create a new parameter struct
+               p := cs.Address.NewDisassociateIpAddressParams(d.Id())
+
+               // Disassociate the IP address
+               if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
+                       // This is a very poor way to be told the ID does no 
longer exist :(
+                       if strings.Contains(err.Error(), fmt.Sprintf(
+                               "Invalid parameter id value=%s due to incorrect 
long value format, "+
+                                       "or entity does not exist", d.Id())) {
+                               return nil
+                       }
+
+                       return fmt.Errorf("Error disassociating IP address %s: 
%s", d.Id(), err)
                }
-
-               return fmt.Errorf("Error disassociating IP address %s: %s", 
d.Id(), err)
        }
 
        return nil

Reply via email to