Skip to content

Commit

Permalink
Allowing parent properties in listing & sync \o/!
Browse files Browse the repository at this point in the history
Improving on #144 by adding zone in listing records
  • Loading branch information
simcap committed Feb 2, 2018
1 parent 3aad001 commit 4c3751d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
13 changes: 12 additions & 1 deletion aws/conv/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,24 @@ func InitResource(source interface{}) (*graph.Resource, error) {
return res, nil
}

func NewResource(source interface{}) (*graph.Resource, error) {
func NewResource(source interface{}, pairOfExtraProperties ...interface{}) (*graph.Resource, error) {
res, err := InitResource(source)
if err != nil {
return res, err
}

res.Properties()[properties.ID] = res.Id()

if count := len(pairOfExtraProperties); count > 1 && count%2 == 0 {
for i := 0; i < count; i = i + 2 {
key, isString := pairOfExtraProperties[i].(string)
if isString {
value := pairOfExtraProperties[i+1]
res.Properties()[key] = value
}
}
}

value := reflect.ValueOf(source)
if !value.IsValid() || value.Kind() != reflect.Ptr || value.IsNil() {
return nil, fmt.Errorf("can not fetch cloud resource. %v is not a valid pointer.", value)
Expand Down
34 changes: 34 additions & 0 deletions aws/conv/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,40 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
)

func TestNewResourceWithExtraProperties(t *testing.T) {
t.Run("valid pairs", func(t *testing.T) {
r, err := NewResource(&ec2.Instance{}, "count", 3, "name", "john")
if err != nil {
t.Fatal(err)
}

p, _ := r.Property("count")
if got, want := p, 3; got != want {
t.Fatalf("got %v, want %v", got, want)
}
p, _ = r.Property("name")
if got, want := p, "john"; got != want {
t.Fatalf("got %v, want %v", got, want)
}
})
t.Run("invalid pairs", func(t *testing.T) {
r, err := NewResource(&ec2.Instance{}, "count", 3, "name", "john", "keyonly")
if err != nil {
t.Fatal(err)
}

if _, exists := r.Property("count"); exists {
t.Fatalf("unexpected key")
}
if _, exists := r.Property("name"); exists {
t.Fatalf("unexpected key")
}
if _, exists := r.Property("keyonly"); exists {
t.Fatalf("unexpected key")
}
})
}

func TestTransformFunctions(t *testing.T) {
t.Parallel()
t.Run("extractTag", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions aws/conv/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ var awsResourcesDef = map[string]map[string]*propertyTransform{
},
cloud.Record: {
properties.Name: {name: "Name", transform: extractValueFn},
properties.Zone: {name: "Zone"},
properties.Failover: {name: "Failover", transform: extractValueFn},
properties.Continent: {name: "GeoLocation", transform: extractFieldFn("ContinentCode")},
properties.Country: {name: "GeoLocation", transform: extractFieldFn("CountryCode")},
Expand Down
8 changes: 4 additions & 4 deletions aws/fetch/manual_fetchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func addManualDnsFetchFuncs(conf *Config, funcs map[string]fetch.Func) {
return resources, objects, nil
}

filterOnZoneName, hasNameFilter := getUserFiltersFromContext(ctx)["name"]
zoneName, hasZoneFilter := getUserFiltersFromContext(ctx)["zone"]

errC := make(chan error)
zoneC := make(chan *route53.HostedZone)
Expand All @@ -839,8 +839,8 @@ func addManualDnsFetchFuncs(conf *Config, funcs map[string]fetch.Func) {
err := conf.APIs.Route53.ListHostedZonesPages(&route53.ListHostedZonesInput{},
func(out *route53.ListHostedZonesOutput, lastPage bool) (shouldContinue bool) {
for _, output := range out.HostedZones {
if hasNameFilter {
if strings.Contains(strings.ToLower(*output.Name), strings.ToLower(filterOnZoneName)) {
if hasZoneFilter {
if strings.Contains(strings.ToLower(*output.Name), strings.ToLower(zoneName)) {
zoneC <- output
}
} else {
Expand All @@ -866,7 +866,7 @@ func addManualDnsFetchFuncs(conf *Config, funcs map[string]fetch.Func) {
func(out *route53.ListResourceRecordSetsOutput, lastPage bool) (shouldContinue bool) {
for _, output := range out.ResourceRecordSets {
objectsC <- output
res, err := awsconv.NewResource(output)
res, err := awsconv.NewResource(output, properties.Zone, *z.Name)
if err != nil {
errC <- err
}
Expand Down
10 changes: 5 additions & 5 deletions aws/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,11 @@ func TestBuildDnsRdfGraph(t *testing.T) {
"/hostedzone/12345": resourcetest.Zone("/hostedzone/12345").Prop(p.Name, "my.first.domain").Build(),
"/hostedzone/23456": resourcetest.Zone("/hostedzone/23456").Prop(p.Name, "my.second.domain").Build(),
"/hostedzone/34567": resourcetest.Zone("/hostedzone/34567").Prop(p.Name, "my.third.domain").Build(),
"awls-91fa0a45": resourcetest.Record("awls-91fa0a45").Prop(p.Name, "subdomain1.my.first.domain").Prop(p.Type, "A").Prop(p.TTL, 10).Prop(p.Records, []string{"1.2.3.4", "2.3.4.5"}).Build(),
"awls-920c0a46": resourcetest.Record("awls-920c0a46").Prop(p.Name, "subdomain2.my.first.domain").Prop(p.Type, "A").Prop(p.TTL, 10).Prop(p.Records, []string{"3.4.5.6"}).Build(),
"awls-be1e0b6a": resourcetest.Record("awls-be1e0b6a").Prop(p.Name, "subdomain3.my.first.domain").Prop(p.Type, "CNAME").Prop(p.TTL, 60).Prop(p.Records, []string{"4.5.6.7"}).Build(),
"awls-9c420a99": resourcetest.Record("awls-9c420a99").Prop(p.Name, "subdomain1.my.second.domain").Prop(p.Type, "A").Prop(p.TTL, 30).Prop(p.Records, []string{"5.6.7.8"}).Build(),
"awls-c9b80bbe": resourcetest.Record("awls-c9b80bbe").Prop(p.Name, "subdomain3.my.second.domain").Prop(p.Type, "CNAME").Prop(p.TTL, 10).Prop(p.Records, []string{"6.7.8.9"}).Build(),
"awls-91fa0a45": resourcetest.Record("awls-91fa0a45").Prop(p.Name, "subdomain1.my.first.domain").Prop(p.Zone, "my.first.domain").Prop(p.Type, "A").Prop(p.TTL, 10).Prop(p.Records, []string{"1.2.3.4", "2.3.4.5"}).Build(),
"awls-920c0a46": resourcetest.Record("awls-920c0a46").Prop(p.Name, "subdomain2.my.first.domain").Prop(p.Zone, "my.first.domain").Prop(p.Type, "A").Prop(p.TTL, 10).Prop(p.Records, []string{"3.4.5.6"}).Build(),
"awls-be1e0b6a": resourcetest.Record("awls-be1e0b6a").Prop(p.Name, "subdomain3.my.first.domain").Prop(p.Zone, "my.first.domain").Prop(p.Type, "CNAME").Prop(p.TTL, 60).Prop(p.Records, []string{"4.5.6.7"}).Build(),
"awls-9c420a99": resourcetest.Record("awls-9c420a99").Prop(p.Name, "subdomain1.my.second.domain").Prop(p.Zone, "my.second.domain").Prop(p.Type, "A").Prop(p.TTL, 30).Prop(p.Records, []string{"5.6.7.8"}).Build(),
"awls-c9b80bbe": resourcetest.Record("awls-c9b80bbe").Prop(p.Name, "subdomain3.my.second.domain").Prop(p.Zone, "my.second.domain").Prop(p.Type, "CNAME").Prop(p.TTL, 10).Prop(p.Records, []string{"6.7.8.9"}).Build(),
}
expectedChildren := map[string][]string{
"/hostedzone/12345": {"awls-91fa0a45", "awls-920c0a46", "awls-be1e0b6a"},
Expand Down
3 changes: 2 additions & 1 deletion console/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var ColumnsInListing = map[string][]string{
cloud.Topic: {properties.ID},
cloud.Queue: {properties.ID, properties.ApproximateMessageCount, properties.Created, properties.Modified, properties.Delay},
cloud.Zone: {properties.ID, properties.Name, properties.Comment, properties.Private, properties.RecordCount, properties.CallerReference},
cloud.Record: {properties.ID, properties.Type, properties.Name, properties.Records, properties.Alias, properties.TTL},
cloud.Record: {properties.ID, properties.Type, properties.Name, properties.Records, properties.Zone, properties.Alias, properties.TTL},
cloud.Function: {properties.Name, properties.Size, properties.Memory, properties.Runtime, properties.Version, properties.Modified, properties.Description},
cloud.Metric: {properties.ID, properties.Name, properties.Namespace, properties.Dimensions},
cloud.Alarm: {properties.Name, properties.Namespace, properties.MetricName, properties.Description, properties.State, properties.Updated, properties.Dimensions},
Expand Down Expand Up @@ -432,6 +432,7 @@ var DefaultsColumnDefinitions = map[string][]ColumnDefinition{
StringColumnDefinition{Prop: properties.Type},
StringColumnDefinition{Prop: properties.Name},
SliceColumnDefinition{StringColumnDefinition{Prop: properties.Records}},
StringColumnDefinition{Prop: properties.Zone},
StringColumnDefinition{Prop: properties.Alias},
StringColumnDefinition{Prop: properties.TTL},
},
Expand Down

0 comments on commit 4c3751d

Please sign in to comment.