From 162a4a2ee1a64b3bd8140650205dea4474006f35 Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Wed, 28 Sep 2016 13:35:44 -0600 Subject: [PATCH] active directory delete command (#6) * commented out ad delete command * compiles * newline * change order * echo, and specify content * echo all fields * Purge checking for AD. * whatif * whatif and comment --- providers/activedir/domains.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/providers/activedir/domains.go b/providers/activedir/domains.go index 36ae9260c..9fd076775 100644 --- a/providers/activedir/domains.go +++ b/providers/activedir/domains.go @@ -49,17 +49,23 @@ func (c *adProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Co foundDiffRecords = append(foundDiffRecords, rec) } - _, create, _, mod := diff.IncrementalDiff(foundDiffRecords, expectedRecords) + _, creates, dels, modifications := diff.IncrementalDiff(foundDiffRecords, expectedRecords) // NOTE(tlim): This provider does not delete records. If // you need to delete a record, either delete it manually // or see providers/activedir/doc.md for implementation tips. // Generate changes. corrections := []*models.Correction{} - for _, d := range create { - corrections = append(corrections, c.createRec(dc.Name, d.Desired.(*models.RecordConfig))...) + for _, del := range dels { + if dc.KeepUnknown { + break + } + corrections = append(corrections, c.deleteRec(dc.Name, del.Existing.(*models.RecordConfig))) } - for _, m := range mod { + for _, cre := range creates { + corrections = append(corrections, c.createRec(dc.Name, cre.Desired.(*models.RecordConfig))...) + } + for _, m := range modifications { corrections = append(corrections, c.modifyRec(dc.Name, m)) } return corrections, nil @@ -267,6 +273,14 @@ func (c *adProvider) generatePowerShellModify(domainname, recName, recType, oldC return text } +func (c *adProvider) generatePowerShellDelete(domainname, recName, recType, content string) string { + text := fmt.Sprintf(`echo "DELETE %s %s %s"`, recType, recName, content) + text += "\r\n" + text += `# Remove-DnsServerResourceRecord -Force -ComputerName "%s" -ZoneName "%s" -Name "%s" -RRType "%s" -RecordData "%s" -WhatIf` + text += "\r\n" + return fmt.Sprintf(text, c.adServer, domainname, recName, recType, content) +} + func (c *adProvider) createRec(domainname string, rec *models.RecordConfig) []*models.Correction { arr := []*models.Correction{ { @@ -291,3 +305,12 @@ func (c *adProvider) modifyRec(domainname string, m diff.Correlation) *models.Co }, } } + +func (c *adProvider) deleteRec(domainname string, rec *models.RecordConfig) *models.Correction { + return &models.Correction{ + Msg: fmt.Sprintf("DELETE record: %s %s ttl(%d) %s", rec.Name, rec.Type, rec.TTL, rec.Target), + F: func() error { + return powerShellDoCommand(c.generatePowerShellDelete(domainname, rec.Name, rec.Type, rec.Target)) + }, + } +}