Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions aws_utils/hz_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws_utils

import (
"fmt"
)

const (
HZNameCol = "Hosted Zone"
HZIdCol = "Id"
HZTotalRecordsCol = "Total records"
HZPrivateCol = "Private"
)

var supportedCols = []string{HZNameCol, HZIdCol, HZTotalRecordsCol, HZPrivateCol}

type HostedZoneR53ResultTableOutput struct {
Outputs map[string]string
VerifiedNameservers bool
}

func (o *HostedZoneR53ResultTableOutput) GetHZTableCols() []string {
return supportedCols
}

func (r *GetRecordAliasesResult) GetHostedZoneR53AsTableOutput() *HostedZoneR53ResultTableOutput {

if r.HostedZone == nil {
return nil
}

hz := r.HostedZone
name := ""
id := ""
count := ""
privateZone := ""

if hz.Name != nil {
name = *hz.Name
}

if hz.Id != nil {
id = *hz.Id
}

if hz.ResourceRecordSetCount != nil {
count = fmt.Sprint(*hz.ResourceRecordSetCount)
}

if hz.Config != nil && hz.Config.PrivateZone != nil {
privateZone = fmt.Sprintf("%t", *hz.Config.PrivateZone)
}

hzTableoutput := map[string]string{
HZNameCol: name,
HZIdCol: id,
HZTotalRecordsCol: count,
HZPrivateCol: privateZone,
}

return &HostedZoneR53ResultTableOutput{
Outputs: hzTableoutput,
VerifiedNameservers: r.VerifiedHostedZone,
}

}
1 change: 1 addition & 0 deletions aws_utils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (r *GetRecordAliasesResult) PrintTable(opts *PrintOptions) {
r.printRecordsTable(opts)
}


func (r *GetRecordAliasesResult) printRecordsTable(opts *PrintOptions) {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
t := table.NewWriter()
Expand Down
163 changes: 163 additions & 0 deletions aws_utils/records_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package aws_utils

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/service/route53"
log "github.com/sirupsen/logrus"
)

const (
RecordCol = "Record"
TypeCol = "Type"
NumCol = "#"
TTLCol = "TTL"
CountryCol = "Country"
AliasCol = "Alias"
ResourcesCol = "Resources"
WebURLCol = "URL"
)

type R53ResultTableOutput struct {
HzOutput *HostedZoneR53ResultTableOutput
Outputs []map[string]string
NonEmptyCols map[string]bool
}

func (o *R53ResultTableOutput) GetNonEmptyCols() []string {
var cols []string
for colName, _ := range o.NonEmptyCols {
cols = append(cols, colName)
}
return cols
}

func (o *R53ResultTableOutput) IsEmptyCol(colName string) bool {
_, found := o.NonEmptyCols[colName]
return !found
}

type R53ResultTableInput struct {
PrintOpts *PrintOptions
Columns []string
}

func (r *GetRecordAliasesResult) getSupportedParsers() map[string]func(*route53.ResourceRecordSet) (string, bool) {

m := map[string]func(*route53.ResourceRecordSet) (string, bool){
RecordCol: r.recordCol,
TypeCol: r.recordType,
TTLCol: r.recordTTL,
CountryCol: r.recordCountry,
AliasCol: r.recordAlias,
ResourcesCol: r.recordResources,
WebURLCol: r.recordURL,
}

return m
}

func (r *GetRecordAliasesResult) GetR53AsTableOutput(input *R53ResultTableInput) *R53ResultTableOutput {

// parse r53 recourds results into a dynamic table

output := &R53ResultTableOutput{}

nonEmptyCols := map[string]bool{}

supportedCols := r.getSupportedParsers()

for _, recSet := range r.Records {

outputRow := map[string]string{}

if recSet.Region == nil {
recSet.Region = &r.Region
}

for _, col := range input.Columns {
if parser, supported := supportedCols[col]; supported {
if val, found := parser(recSet); found {
nonEmptyCols[col] = true
outputRow[col] = val
}

}
if url, found := r.recordURL(recSet); found {
outputRow[WebURLCol] = url
}
}

output.Outputs = append(output.Outputs, outputRow)
}

output.NonEmptyCols = nonEmptyCols

// parse hostedZone table

output.HzOutput = r.GetHostedZoneR53AsTableOutput()

return output
}

func (r *GetRecordAliasesResult) recordCol(recSet *route53.ResourceRecordSet) (string, bool) {
recordStr := ""
found := false
if recSet.Name != nil {
found = true
recordStr = *recSet.Name
if strings.HasPrefix(recordStr, WildCard) {
recordStr = strings.Replace(recordStr, WildCard, "*", 1)
}
}
return recordStr, found
}
func (r *GetRecordAliasesResult) recordType(recSet *route53.ResourceRecordSet) (string, bool) {
return *recSet.Type, recSet.Type != nil
}

func (r *GetRecordAliasesResult) recordTTL(recSet *route53.ResourceRecordSet) (string, bool) {
ttl := int64(0)
if recSet.TTL != nil {
ttl = *recSet.TTL
}
return fmt.Sprint(ttl), ttl != int64(0)
}

func (r *GetRecordAliasesResult) recordCountry(recSet *route53.ResourceRecordSet) (string, bool) {
countryCode := ""
if recSet.GeoLocation != nil && recSet.GeoLocation.SubdivisionCode != nil {
countryCode = *recSet.GeoLocation.SubdivisionCode
}
return countryCode, countryCode != ""
}

func (r *GetRecordAliasesResult) recordAlias(recSet *route53.ResourceRecordSet) (string, bool) {
dnsName := ""
if recSet.AliasTarget != nil && recSet.AliasTarget.DNSName != nil {
dnsName = *recSet.AliasTarget.DNSName
}
return dnsName, dnsName != ""
}

func (r *GetRecordAliasesResult) recordURL(recSet *route53.ResourceRecordSet) (string, bool) {

url, err := GenerateWebURL(recSet)

if err != nil {
log.WithField("record", *recSet.Name).WithError(err).Debug("failed getting web url for record")
}

return url, err == nil && url != ""
}

func (r *GetRecordAliasesResult) recordResources(recSet *route53.ResourceRecordSet) (string, bool) {
resourcesRow := ""
for _, resources := range recSet.ResourceRecords {
if resources != nil {
resourcesRow += *resources.Value + "\n"
}
}
return resourcesRow, resourcesRow != ""
}
2 changes: 1 addition & 1 deletion aws_utils/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (r53m *Route53Manager) GetRecordSetAliases(recordName string, skipNSVerific
continue
}
}
return &GetRecordAliasesResult{Region: r53m.GetRegion(), Records: recordSets, HostedZone: hosedZone, Stream: recordStream}, nil
return &GetRecordAliasesResult{Region: r53m.GetRegion(), Records: recordSets, HostedZone: hosedZone, Stream: recordStream, VerifiedHostedZone: skipNSVerification}, nil
}
return nil, errors.New("NoRecordMatchFound")
}
Expand Down
9 changes: 5 additions & 4 deletions aws_utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type GroupManager interface {
}

type GetRecordAliasesResult struct {
Records []*route53.ResourceRecordSet
HostedZone *route53.HostedZone
Stream RecordStream
Region string
VerifiedHostedZone bool
Records []*route53.ResourceRecordSet
HostedZone *route53.HostedZone
Stream RecordStream
Region string
}

type Route53Api interface {
Expand Down
Loading