Skip to content

Commit

Permalink
Merge pull request #3 from dhtech/support-aliases
Browse files Browse the repository at this point in the history
Add support for ALIAS to ipplan hosts
  • Loading branch information
rctl authored Nov 27, 2018
2 parents a318bc7 + 0c5fa63 commit a005549
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion enforcer/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (e *Enforcer) GetAllRecords() ([]*Record, error) {
if err != nil {
return nil, err
}
static, err := e.GetStaticRecords()
static, err := e.GetStaticRecords(hosts)
if err != nil {
return nil, err
}
Expand Down
40 changes: 33 additions & 7 deletions enforcer/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import (
)

// GetStaticRecords returns records that are specified in static YAML file
func (e *Enforcer) GetStaticRecords() ([]*Record, error) {
func (e *Enforcer) GetStaticRecords(hostr []*Record) ([]*Record, error) {
m := make(map[string][]*Record)
for _, r := range hostr {
if d, e := m[r.Name]; e {
m[r.Name] = append(d, r)
} else {
m[r.Name] = []*Record{r}
}
}
var records []*Record
reader := yaml.NewDecoder(e.static)
for {
Expand All @@ -29,12 +37,30 @@ func (e *Enforcer) GetStaticRecords() ([]*Record, error) {
continue
}
for _, d := range record.Data {
records = append(records, &Record{
Name: record.Name,
TTL: record.TTL,
Type: record.Type,
Data: []string{d},
})
if record.Type == "ALIAS" {
if dsts, e := m[d]; e {
for _, dst := range dsts {
for _, ds := range dst.Data {
records = append(records, &Record{
Name: record.Name,
TTL: record.TTL,
Type: dst.Type,
Data: []string{ds},
})
}
}
log.Infof("Added ALIAS %s for %s", record.Name, d)
} else {
log.Warningf("Found ALIAS %s pointing to non existing host %s", record.Name, d)
}
} else {
records = append(records, &Record{
Name: record.Name,
TTL: record.TTL,
Type: record.Type,
Data: []string{d},
})
}
}
}
}
Expand Down

0 comments on commit a005549

Please sign in to comment.