Skip to content

Commit f6386b0

Browse files
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.
1 parent fe012a2 commit f6386b0

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

cloudstack/resource_cloudstack_ipaddress.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func resourceCloudStackIPAddress() *schema.Resource {
7272
Computed: true,
7373
},
7474

75+
"is_source_nat": {
76+
Type: schema.TypeBool,
77+
Computed: true,
78+
},
79+
7580
"tags": tagsSchema(),
7681
},
7782
}
@@ -154,6 +159,7 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
154159
}
155160

156161
d.Set("is_portable", ip.Isportable)
162+
d.Set("is_source_nat", ip.Issourcenat)
157163

158164
// Updated the IP address
159165
d.Set("ip_address", ip.Ipaddress)
@@ -182,21 +188,23 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
182188
}
183189

184190
func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
185-
cs := meta.(*cloudstack.CloudStackClient)
186-
187-
// Create a new parameter struct
188-
p := cs.Address.NewDisassociateIpAddressParams(d.Id())
189-
190-
// Disassociate the IP address
191-
if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
192-
// This is a very poor way to be told the ID does no longer exist :(
193-
if strings.Contains(err.Error(), fmt.Sprintf(
194-
"Invalid parameter id value=%s due to incorrect long value format, "+
195-
"or entity does not exist", d.Id())) {
196-
return nil
191+
if !d.Get("is_source_nat").(bool) {
192+
cs := meta.(*cloudstack.CloudStackClient)
193+
194+
// Create a new parameter struct
195+
p := cs.Address.NewDisassociateIpAddressParams(d.Id())
196+
197+
// Disassociate the IP address
198+
if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
199+
// This is a very poor way to be told the ID does no longer exist :(
200+
if strings.Contains(err.Error(), fmt.Sprintf(
201+
"Invalid parameter id value=%s due to incorrect long value format, "+
202+
"or entity does not exist", d.Id())) {
203+
return nil
204+
}
205+
206+
return fmt.Errorf("Error disassociating IP address %s: %s", d.Id(), err)
197207
}
198-
199-
return fmt.Errorf("Error disassociating IP address %s: %s", d.Id(), err)
200208
}
201209

202210
return nil

0 commit comments

Comments
 (0)