Skip to content

Commit

Permalink
Implement Get Zone for Azure DNS (StackExchange#631)
Browse files Browse the repository at this point in the history
* Implement Get Zone for Azure DNS

* Internalize getExistingRecord
  • Loading branch information
vatsalyagoel committed Feb 18, 2020
1 parent a5558b3 commit 155b0bc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/_includes/matrix.html
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,8 @@
<td class="info">
<i class="fa fa-circle-o text-info" aria-hidden="true"></i>
</td>
<td class="info">
<i class="fa fa-circle-o text-info" aria-hidden="true"></i>
<td class="success">
<i class="fa fa-check text-success" aria-hidden="true"></i>
</td>
<td class="success">
<i class="fa fa-check text-success" aria-hidden="true"></i>
Expand Down
77 changes: 57 additions & 20 deletions providers/azuredns/azureDnsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,33 @@ var features = providers.DocumentationNotes{
providers.CanUseNAPTR: providers.Cannot(),
providers.CanUseSSHFP: providers.Cannot(),
providers.CanUseTLSA: providers.Cannot(),
providers.CanGetZones: providers.Unimplemented(),
providers.CanGetZones: providers.Can(),
}

func init() {
providers.RegisterDomainServiceProviderType("AZURE_DNS", newAzureDnsDsp, features)
}

func (a *azureDnsProvider) getZones() error {
a.zones = make(map[string]*adns.Zone)

func (a *azureDnsProvider) getExistingZones() (*adns.ZoneListResult, error) {
ctx, cancel := context.WithTimeout(context.Background(), 6000*time.Second)
defer cancel()
zonesIterator, zonesErr := a.zonesClient.ListByResourceGroupComplete(ctx, *a.resourceGroup, to.Int32Ptr(100))
if zonesErr != nil {
return zonesErr
return nil, zonesErr
}
zonesResult := zonesIterator.Response()
return &zonesResult, nil
}

func (a *azureDnsProvider) getZones() error {
a.zones = make(map[string]*adns.Zone)

zonesResult, err := a.getExistingZones()

if err != nil {
return err
}

for _, z := range *zonesResult.Value {
zone := z
domain := strings.TrimSuffix(*z.Name, ".")
Expand Down Expand Up @@ -111,39 +121,66 @@ func (a *azureDnsProvider) GetNameservers(domain string) ([]*models.Nameserver,
return ns, nil
}

// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
func (client *azureDnsProvider) GetZoneRecords(domain string) (models.Records, error) {
return nil, fmt.Errorf("not implemented")
// This enables the get-zones subcommand.
// Implement this by extracting the code from GetDomainCorrections into
// a single function. For most providers this should be relatively easy.
}
func (a *azureDnsProvider) ListZones() ([]string, error) {
zonesResult, err := a.getExistingZones()

func (a *azureDnsProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
err := dc.Punycode()
if err != nil {
return nil, err
}

var zones []string

for _, z := range *zonesResult.Value {
domain := strings.TrimSuffix(*z.Name, ".")
zones = append(zones, domain)
}

return zones, nil
}

// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
func (a *azureDnsProvider) GetZoneRecords(domain string) (models.Records, error) {
existingRecords, _, _, err := a.getExistingRecords(domain)
if err != nil {
return nil, err
}
return existingRecords, nil
}

var corrections []*models.Correction
zone, ok := a.zones[dc.Name]
func (a *azureDnsProvider) getExistingRecords(domain string) (models.Records, []*adns.RecordSet, string, error) {
zone, ok := a.zones[domain]
if !ok {
return nil, errNoExist{dc.Name}
return nil, nil, "", errNoExist{domain}
}
var zoneName string
zoneName = *zone.Name
records, err := a.fetchRecordSets(zoneName)
if err != nil {
return nil, err
return nil, nil, "", err
}

var existingRecords []*models.RecordConfig
var existingRecords models.Records
for _, set := range records {
existingRecords = append(existingRecords, nativeToRecords(set, dc.Name)...)
existingRecords = append(existingRecords, nativeToRecords(set, zoneName)...)
}

models.PostProcessRecords(existingRecords)
return existingRecords, records, zoneName, nil
}

func (a *azureDnsProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
err := dc.Punycode()

if err != nil {
return nil, err
}

var corrections []*models.Correction

existingRecords, records, zoneName, err := a.getExistingRecords(dc.Name)
if err != nil {
return nil, err
}

differ := diff.New(dc)
namesToUpdate := differ.ChangedGroups(existingRecords)
Expand Down

0 comments on commit 155b0bc

Please sign in to comment.