|
| 1 | +// Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package dns |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + oci_dns "github.com/oracle/oci-go-sdk/v65/dns" |
| 11 | + |
| 12 | + "github.com/oracle/terraform-provider-oci/internal/client" |
| 13 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 14 | +) |
| 15 | + |
| 16 | +func DnsRrsetsDataSource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: readDnsRrsets, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "filter": tfresource.DataSourceFiltersSchema(), |
| 21 | + "domain": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Optional: true, |
| 24 | + }, |
| 25 | + "domain_contains": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + }, |
| 29 | + "rtype": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + }, |
| 33 | + "scope": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "view_id": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + }, |
| 41 | + "zone_name_or_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + }, |
| 45 | + // Computed |
| 46 | + "rrsets": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "domain": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + DiffSuppressFunc: tfresource.EqualIgnoreCaseSuppressDiff, |
| 55 | + }, |
| 56 | + "rtype": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + "items": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Required: true, |
| 63 | + Elem: &schema.Resource{ |
| 64 | + Schema: map[string]*schema.Schema{ |
| 65 | + // Required |
| 66 | + "domain": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Required: true, |
| 69 | + DiffSuppressFunc: tfresource.EqualIgnoreCaseSuppressDiff, |
| 70 | + }, |
| 71 | + "rdata": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Required: true, |
| 74 | + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { |
| 75 | + rtype := d.Get("rtype").(string) |
| 76 | + return normalizeRData(rtype, new) == normalizeRData(rtype, old) |
| 77 | + }, |
| 78 | + }, |
| 79 | + "rtype": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Required: true, |
| 82 | + }, |
| 83 | + "ttl": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Required: true, |
| 86 | + }, |
| 87 | + |
| 88 | + // Optional |
| 89 | + |
| 90 | + // Computed |
| 91 | + "is_protected": { |
| 92 | + Type: schema.TypeBool, |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "record_hash": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "rrset_version": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func readDnsRrsets(d *schema.ResourceData, m interface{}) error { |
| 114 | + sync := &DnsRrsetsDataSourceCrud{} |
| 115 | + sync.D = d |
| 116 | + sync.Client = m.(*client.OracleClients).DnsClient() |
| 117 | + |
| 118 | + return tfresource.ReadResource(sync) |
| 119 | +} |
| 120 | + |
| 121 | +type DnsRrsetsDataSourceCrud struct { |
| 122 | + D *schema.ResourceData |
| 123 | + Client *oci_dns.DnsClient |
| 124 | + Res *oci_dns.GetZoneRecordsResponse |
| 125 | +} |
| 126 | + |
| 127 | +func (s *DnsRrsetsDataSourceCrud) VoidState() { |
| 128 | + s.D.SetId("") |
| 129 | +} |
| 130 | + |
| 131 | +func (s *DnsRrsetsDataSourceCrud) Get() error { |
| 132 | + request := oci_dns.GetZoneRecordsRequest{} |
| 133 | + |
| 134 | + if domain, ok := s.D.GetOkExists("domain"); ok { |
| 135 | + tmp := domain.(string) |
| 136 | + request.Domain = &tmp |
| 137 | + } |
| 138 | + |
| 139 | + if domainContains, ok := s.D.GetOkExists("domain_contains"); ok { |
| 140 | + tmp := domainContains.(string) |
| 141 | + request.DomainContains = &tmp |
| 142 | + } |
| 143 | + |
| 144 | + if rtype, ok := s.D.GetOkExists("rtype"); ok { |
| 145 | + tmp := rtype.(string) |
| 146 | + request.Rtype = &tmp |
| 147 | + } |
| 148 | + |
| 149 | + if scope, ok := s.D.GetOkExists("scope"); ok { |
| 150 | + request.Scope = oci_dns.GetZoneRecordsScopeEnum(scope.(string)) |
| 151 | + } |
| 152 | + |
| 153 | + if viewId, ok := s.D.GetOkExists("view_id"); ok { |
| 154 | + tmp := viewId.(string) |
| 155 | + request.ViewId = &tmp |
| 156 | + } |
| 157 | + |
| 158 | + if zoneNameOrId, ok := s.D.GetOkExists("zone_name_or_id"); ok { |
| 159 | + tmp := zoneNameOrId.(string) |
| 160 | + request.ZoneNameOrId = &tmp |
| 161 | + } |
| 162 | + |
| 163 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(false, "dns") |
| 164 | + |
| 165 | + response, err := s.Client.GetZoneRecords(context.Background(), request) |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + request.Page = response.OpcNextPage |
| 171 | + |
| 172 | + for request.Page != nil { |
| 173 | + listResponse, err := s.Client.GetZoneRecords(context.Background(), request) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + response.Items = append(response.Items, listResponse.Items...) |
| 179 | + request.Page = listResponse.OpcNextPage |
| 180 | + } |
| 181 | + |
| 182 | + s.Res = &response |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +func (s *DnsRrsetsDataSourceCrud) SetData() error { |
| 187 | + if s.Res == nil { |
| 188 | + return nil |
| 189 | + } |
| 190 | + |
| 191 | + s.D.SetId(tfresource.GenerateDataSourceHashID("DnsRrsetsDataSource-", DnsRrsetsDataSource(), s.D)) |
| 192 | + |
| 193 | + rrsetMap := map[string]map[string][]oci_dns.Record{} |
| 194 | + |
| 195 | + for _, record := range s.Res.Items { |
| 196 | + if _, ok := rrsetMap[*record.Domain]; !ok { |
| 197 | + rrsetMap[*record.Domain] = map[string][]oci_dns.Record{} |
| 198 | + } |
| 199 | + if _, ok := rrsetMap[*record.Domain][*record.Rtype]; !ok { |
| 200 | + rrsetMap[*record.Domain][*record.Rtype] = []oci_dns.Record{} |
| 201 | + } |
| 202 | + rrsetMap[*record.Domain][*record.Rtype] = append(rrsetMap[*record.Domain][*record.Rtype], record) |
| 203 | + } |
| 204 | + |
| 205 | + rrsets := []interface{}{} |
| 206 | + for domain, domainRrsets := range rrsetMap { |
| 207 | + for rtype, rrset := range domainRrsets { |
| 208 | + rrsetMap := map[string]interface{}{} |
| 209 | + rrsetMap["domain"] = domain |
| 210 | + rrsetMap["rtype"] = rtype |
| 211 | + records := []interface{}{} |
| 212 | + for _, record := range rrset { |
| 213 | + records = append(records, RecordToMap(record)) |
| 214 | + } |
| 215 | + rrsetMap["items"] = records |
| 216 | + rrsets = append(rrsets, rrsetMap) |
| 217 | + } |
| 218 | + } |
| 219 | + s.D.Set("rrsets", rrsets) |
| 220 | + |
| 221 | + return nil |
| 222 | +} |
0 commit comments