Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DNS record deletion issue in MAAS provider #196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions maas/resource_maas_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ func resourceDnsRecordDelete(ctx context.Context, d *schema.ResourceData, meta i
if err != nil {
return diag.FromErr(err)
}
if err := client.DNSResource.Delete(id); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+       "github.com/hashicorp/go-set"
        "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
        "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
        "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -191,7 +192,23 @@ func resourceDnsRecordDelete(ctx context.Context, d *schema.ResourceData, meta i
                if err := client.DNSResource.Delete(id); err != nil {
                        return diag.FromErr(err)
                }
+               allDNSResources, err := client.DNSResources.Get()
+               if err != nil {
+                       return diag.FromErr(err)
+               }
+               allOtherDNSResourcesIPAddresses := set.New[string](0)
+               for _, dnsResource := range allDNSResources {
+                       if dnsResource.ID == id {
+                               continue
+                       }
+                       for _, ipAddress := range dnsResource.IPAddresses {
+                               allOtherDNSResourcesIPAddresses.Insert(ipAddress.IP.String())
+                       }
+               }
                for _, ipAddress := range dnsResource.IPAddresses {
+                       if allOtherDNSResourcesIPAddresses.Contains(ipAddress.IP.String()) {
+                               continue
+                       }
                        if err := client.IPAddresses.Release(&entity.IPAddressesParams{IP: ipAddress.IP.String()}); err != nil {
                                return diag.FromErr(err)
                        }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will avoid deleting an IP address of the under deletion DNS Resource if that IP address is in use by at least another DNS Resource.

return diag.FromErr(err)
}
ip := d.Get("data").(string)
for _, ipAddress := range dnsResource.IPAddresses {
if err := client.IPAddresses.Release(&entity.IPAddressesParams{IP: ipAddress.IP.String()}); err != nil {
return diag.FromErr(err)
if ipAddress.IP.String() == ip {
if err := client.DNSResource.Delete(id); err != nil {
return diag.FromErr(err)
}
break
}
}
} else {
Expand Down
Loading