|
| 1 | +// Copyright (c) Platform9 Systems, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// Package dns implements the pcd_dns_* resources and data sources (Designate v2), |
| 5 | +// ported from terraform-provider-openstack v3.4.0. |
| 6 | +// |
| 7 | +// Designate zone and recordset operations are asynchronous: create/update return |
| 8 | +// the object in a PENDING status that settles to ACTIVE, and delete leaves the |
| 9 | +// object PENDING_DELETE until it is gone. Resources wait for the object to reach |
| 10 | +// ACTIVE after create/update and to 404 after delete. |
| 11 | +package dns |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "net/http" |
| 17 | + "strings" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/gophercloud/gophercloud/v2" |
| 21 | + "github.com/gophercloud/gophercloud/v2/openstack/dns/v2/recordsets" |
| 22 | + "github.com/gophercloud/gophercloud/v2/openstack/dns/v2/zones" |
| 23 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 24 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 25 | + |
| 26 | + "github.com/platform9/terraform-provider-pcd/internal/clients" |
| 27 | +) |
| 28 | + |
| 29 | +// listToStrings converts a list attribute to a Go slice (nil for null/unknown). |
| 30 | +func listToStrings(ctx context.Context, l types.List, diags *diag.Diagnostics) []string { |
| 31 | + if l.IsNull() || l.IsUnknown() { |
| 32 | + return nil |
| 33 | + } |
| 34 | + var out []string |
| 35 | + diags.Append(l.ElementsAs(ctx, &out, false)...) |
| 36 | + return out |
| 37 | +} |
| 38 | + |
| 39 | +// mapToStrings converts a map attribute to a Go map (nil for null/unknown). |
| 40 | +func mapToStrings(ctx context.Context, m types.Map, diags *diag.Diagnostics) map[string]string { |
| 41 | + if m.IsNull() || m.IsUnknown() { |
| 42 | + return nil |
| 43 | + } |
| 44 | + out := map[string]string{} |
| 45 | + diags.Append(m.ElementsAs(ctx, &out, false)...) |
| 46 | + return out |
| 47 | +} |
| 48 | + |
| 49 | +// setToStrings converts a set attribute to a Go slice (nil for null/unknown). |
| 50 | +func setToStrings(ctx context.Context, s types.Set, diags *diag.Diagnostics) []string { |
| 51 | + if s.IsNull() || s.IsUnknown() { |
| 52 | + return nil |
| 53 | + } |
| 54 | + var out []string |
| 55 | + diags.Append(s.ElementsAs(ctx, &out, false)...) |
| 56 | + return out |
| 57 | +} |
| 58 | + |
| 59 | +// splitZoneChildID parses a composite "<zone_id>/<recordset_id>" import ID. |
| 60 | +func splitZoneChildID(id string) (zoneID, child string, err error) { |
| 61 | + parts := strings.SplitN(id, "/", 2) |
| 62 | + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 63 | + return "", "", fmt.Errorf("expected import ID in the form <zone_id>/<recordset_id>, got %q", id) |
| 64 | + } |
| 65 | + return parts[0], parts[1], nil |
| 66 | +} |
| 67 | + |
| 68 | +// Designate status values (plain strings in the API; no exported constants). |
| 69 | +const ( |
| 70 | + dnsActive = "ACTIVE" |
| 71 | + dnsError = "ERROR" |
| 72 | +) |
| 73 | + |
| 74 | +// defaultDNSTimeout bounds each wait for a zone or recordset to settle. |
| 75 | +const defaultDNSTimeout = 10 * time.Minute |
| 76 | + |
| 77 | +// configureClient extracts the shared *clients.Config from ProviderData. |
| 78 | +func configureClient(providerData any, diags *diag.Diagnostics) *clients.Config { |
| 79 | + if providerData == nil { |
| 80 | + return nil |
| 81 | + } |
| 82 | + config, ok := providerData.(*clients.Config) |
| 83 | + if !ok { |
| 84 | + diags.AddError( |
| 85 | + "Unexpected provider data type", |
| 86 | + fmt.Sprintf("Expected *clients.Config, got %T. This is a bug in the provider.", providerData), |
| 87 | + ) |
| 88 | + return nil |
| 89 | + } |
| 90 | + return config |
| 91 | +} |
| 92 | + |
| 93 | +// waitForZoneActive blocks until the zone reaches ACTIVE, failing on ERROR/timeout. |
| 94 | +func waitForZoneActive(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, timeout time.Duration) error { |
| 95 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 96 | + defer cancel() |
| 97 | + err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) { |
| 98 | + z, err := zones.Get(ctx, client, zoneID).Extract() |
| 99 | + if err != nil { |
| 100 | + return false, err |
| 101 | + } |
| 102 | + switch z.Status { |
| 103 | + case dnsActive: |
| 104 | + return true, nil |
| 105 | + case dnsError: |
| 106 | + return false, fmt.Errorf("zone %s entered ERROR status", zoneID) |
| 107 | + default: |
| 108 | + return false, nil |
| 109 | + } |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("waiting for zone %s to become active: %w", zoneID, err) |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// waitForZoneDeleted blocks until the zone is gone (404). |
| 118 | +func waitForZoneDeleted(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, timeout time.Duration) error { |
| 119 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 120 | + defer cancel() |
| 121 | + err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) { |
| 122 | + z, err := zones.Get(ctx, client, zoneID).Extract() |
| 123 | + if err != nil { |
| 124 | + if gophercloud.ResponseCodeIs(err, http.StatusNotFound) { |
| 125 | + return true, nil |
| 126 | + } |
| 127 | + return false, err |
| 128 | + } |
| 129 | + if z.Status == dnsError { |
| 130 | + return false, fmt.Errorf("zone %s entered ERROR status during delete", zoneID) |
| 131 | + } |
| 132 | + return false, nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("waiting for zone %s to delete: %w", zoneID, err) |
| 136 | + } |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// waitForRecordSetActive blocks until the recordset reaches ACTIVE. |
| 141 | +func waitForRecordSetActive(ctx context.Context, client *gophercloud.ServiceClient, zoneID, rrID string, timeout time.Duration) error { |
| 142 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 143 | + defer cancel() |
| 144 | + err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) { |
| 145 | + rr, err := recordsets.Get(ctx, client, zoneID, rrID).Extract() |
| 146 | + if err != nil { |
| 147 | + return false, err |
| 148 | + } |
| 149 | + switch rr.Status { |
| 150 | + case dnsActive: |
| 151 | + return true, nil |
| 152 | + case dnsError: |
| 153 | + return false, fmt.Errorf("recordset %s entered ERROR status", rrID) |
| 154 | + default: |
| 155 | + return false, nil |
| 156 | + } |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("waiting for recordset %s to become active: %w", rrID, err) |
| 160 | + } |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +// waitForRecordSetDeleted blocks until the recordset is gone (404). |
| 165 | +func waitForRecordSetDeleted(ctx context.Context, client *gophercloud.ServiceClient, zoneID, rrID string, timeout time.Duration) error { |
| 166 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 167 | + defer cancel() |
| 168 | + err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) { |
| 169 | + _, err := recordsets.Get(ctx, client, zoneID, rrID).Extract() |
| 170 | + if err != nil { |
| 171 | + if gophercloud.ResponseCodeIs(err, http.StatusNotFound) { |
| 172 | + return true, nil |
| 173 | + } |
| 174 | + return false, err |
| 175 | + } |
| 176 | + return false, nil |
| 177 | + }) |
| 178 | + if err != nil { |
| 179 | + return fmt.Errorf("waiting for recordset %s to delete: %w", rrID, err) |
| 180 | + } |
| 181 | + return nil |
| 182 | +} |
0 commit comments