From 8007162593cc966693b64cd3d793810b7dfe9e41 Mon Sep 17 00:00:00 2001
From: Brian Brazil <brian.brazil@robustperception.io>
Date: Wed, 26 Sep 2018 16:32:50 +0100
Subject: [PATCH] Make lookup config field names more understandable.

old_indexes -> source_indexes
new_index -> lookup
keep_old -> keep_source_indexes

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
---
 generator/README.md     |  9 +++---
 generator/config.go     |  6 ++--
 generator/generator.yml | 68 ++++++++++++++++++++---------------------
 generator/tree.go       | 18 +++++------
 generator/tree_test.go  | 30 +++++++++---------
 5 files changed, 66 insertions(+), 65 deletions(-)

diff --git a/generator/README.md b/generator/README.md
index 25eb4b68..7a0f4508 100644
--- a/generator/README.md
+++ b/generator/README.md
@@ -86,15 +86,16 @@ modules:
                             # Required if context is configured on the device.
 
     lookups:  # Optional list of lookups to perform.
-              # If keep_old is the default of false, this must only be used when the new index is unique.
+              # The default for `keep_source_indexes` is false. Indexes must be unique for this option to be used.
 
       # If the index of a table is bsnDot11EssIndex, usually that'd be the label
       # on the resulting metrics from that table. Instead, use the index to
       # lookup the bsnDot11EssSsid table entry and create a bsnDot11EssSsid label
       # with that value.
-      - old_indexes: [bsnDot11EssIndex]
-        new_index: bsnDot11EssSsid
-        keep_old: false  # If true, don't delete old index label for this lookup.
+      - source_indexes: [bsnDot11EssIndex]
+        lookup: bsnDot11EssSsid
+        keep_source_indexes: false  # If true, don't delete source index labels for this lookup.
+                                    # This is useful when the new index has duplicates.
 
      overrides: # Allows for per-module overrides of bits of MIBs
        metricName:
diff --git a/generator/config.go b/generator/config.go
index 815b55ae..0e285f27 100644
--- a/generator/config.go
+++ b/generator/config.go
@@ -53,7 +53,7 @@ type ModuleConfig struct {
 }
 
 type Lookup struct {
-	OldIndexes []string `yaml:"old_indexes"`
-	NewIndex   string   `yaml:"new_index"`
-	KeepOld    bool     `yaml:"keep_old,omitempty"`
+	SourceIndexes     []string `yaml:"source_indexes"`
+	Lookup            string   `yaml:"lookup"`
+	KeepSourceIndexes bool     `yaml:"keep_source_indexes,omitempty"`
 }
diff --git a/generator/generator.yml b/generator/generator.yml
index 1269a4ad..0b80d04f 100644
--- a/generator/generator.yml
+++ b/generator/generator.yml
@@ -6,21 +6,21 @@ modules:
   if_mib_ifalias:
     walk: [sysUpTime, interfaces, ifXTable]
     lookups:
-      - old_indexes: [ifIndex]
-        new_index: ifAlias
+      - source_indexes: [ifIndex]
+        lookup: ifAlias
   # Interfaces if ifDescr is unique.
   if_mib_ifdescr:
     walk: [sysUpTime, interfaces, ifXTable]
     lookups:
-      - old_indexes: [ifIndex]
-        new_index: ifDescr
+      - source_indexes: [ifIndex]
+        lookup: ifDescr
   # Interfaces if ifName is unique.
   if_mib_ifname:
     walk: [sysUpTime, interfaces, ifXTable]
     lookups:
-      - old_indexes: [ifIndex]
+      - source_indexes: [ifIndex]
         # Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
-        new_index: 1.3.6.1.2.1.31.1.1.1.1 # ifName
+        lookup: 1.3.6.1.2.1.31.1.1.1.1 # ifName
 
 # Cisco Wireless LAN Controller
   cisco_wlc:
@@ -35,10 +35,10 @@ modules:
       - 1.3.6.1.4.1.14179.2.2.13.1.3   # bsnAPIfLoadChannelUtilization
       - 1.3.6.1.4.1.14179.2.2.15.1.21  # bsnAPIfDBNoisePower
     lookups:
-      - old_indexes: [bsnDot11EssIndex]
-        new_index: bsnDot11EssSsid
-      - old_indexes: [bsnAPDot3MacAddress]
-        new_index: bsnAPName
+      - source_indexes: [bsnDot11EssIndex]
+        lookup: bsnDot11EssSsid
+      - source_indexes: [bsnAPDot3MacAddress]
+        lookup: bsnAPName
 
 # APC/Schneider UPS Network Management Cards
 #
@@ -68,10 +68,10 @@ modules:
       - 1.3.6.1.4.1.318.1.1.1.12      # upsOutletGroups
       - 1.3.6.1.4.1.318.1.1.10.2.3.2  # iemStatusProbesTable
     lookups:
-      - old_indexes: [upsOutletGroupStatusIndex]
-        new_index: upsOutletGroupStatusName
-      - old_indexes: [iemStatusProbeIndex]
-        new_index: iemStatusProbeName
+      - source_indexes: [upsOutletGroupStatusIndex]
+        lookup: upsOutletGroupStatusName
+      - source_indexes: [iemStatusProbeIndex]
+        lookup: iemStatusProbeName
 
 # ServerTech Sentry 3 MIB
 #
@@ -159,23 +159,23 @@ modules:
       - 1.3.6.1.4.1.6574.102     # spaceIO
       - 1.3.6.1.4.1.6574.104     # synologyiSCSILUN
     lookups:
-      - old_indexes: [spaceIOIndex]
-        new_index: spaceIODevice
-      - old_indexes: [storageIOIndex]
-        new_index: storageIODevice
-      - old_indexes: [serviceInfoIndex]
-        new_index: serviceName
-      - old_indexes: [ifIndex]
+      - source_indexes: [spaceIOIndex]
+        lookup: spaceIODevice
+      - source_indexes: [storageIOIndex]
+        lookup: storageIODevice
+      - source_indexes: [serviceInfoIndex]
+        lookup: serviceName
+      - source_indexes: [ifIndex]
         # Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
-        new_index: 1.3.6.1.2.1.31.1.1.1.1 # ifName
-      - old_indexes: [diskIndex]
-        new_index: diskID
-      - old_indexes: [raidIndex]
-        new_index: raidName
-      - old_indexes: [laIndex]
-        new_index: laNames
-      - old_indexes: [hrStorageIndex]
-        new_index: hrStorageDescr
+        lookup: 1.3.6.1.2.1.31.1.1.1.1 # ifName
+      - source_indexes: [diskIndex]
+        lookup: diskID
+      - source_indexes: [raidIndex]
+        lookup: raidName
+      - source_indexes: [laIndex]
+        lookup: laNames
+      - source_indexes: [hrStorageIndex]
+        lookup: hrStorageDescr
 
 # DD-WRT
 #
@@ -195,10 +195,10 @@ modules:
       - 1.3.6.1.4.1.2021.10.1.5 # laLoadInt
       - 1.3.6.1.4.1.2021.11 # systemStats
     lookups:
-    - old_indexes: [ifIndex]
-      new_index: ifDescr
-    - old_indexes: [laIndex]
-      new_index: laNames
+    - source_indexes: [ifIndex]
+      lookup: ifDescr
+    - source_indexes: [laIndex]
+      lookup: laNames
 
 # Ubiquiti / AirMAX
 #
diff --git a/generator/tree.go b/generator/tree.go
index 7717ce4b..0acccf57 100644
--- a/generator/tree.go
+++ b/generator/tree.go
@@ -375,27 +375,27 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
 			foundIndexes := 0
 			// See if all lookup indexes are present.
 			for _, index := range metric.Indexes {
-				for _, lookupIndex := range lookup.OldIndexes {
+				for _, lookupIndex := range lookup.SourceIndexes {
 					if index.Labelname == lookupIndex {
 						foundIndexes++
 					}
 				}
 			}
-			if foundIndexes == len(lookup.OldIndexes) {
-				if _, ok := nameToNode[lookup.NewIndex]; !ok {
-					log.Fatalf("Unknown index '%s'", lookup.NewIndex)
+			if foundIndexes == len(lookup.SourceIndexes) {
+				if _, ok := nameToNode[lookup.Lookup]; !ok {
+					log.Fatalf("Unknown index '%s'", lookup.Lookup)
 				}
-				indexNode := nameToNode[lookup.NewIndex]
+				indexNode := nameToNode[lookup.Lookup]
 				typ, ok := metricType(indexNode.Type)
 				if !ok {
-					log.Fatalf("Unknown index type %s for %s", indexNode.Type, lookup.NewIndex)
+					log.Fatalf("Unknown index type %s for %s", indexNode.Type, lookup.Lookup)
 				}
 				l := &config.Lookup{
 					Labelname: sanitizeLabelName(indexNode.Label),
 					Type:      typ,
 					Oid:       indexNode.Oid,
 				}
-				for _, oldIndex := range lookup.OldIndexes {
+				for _, oldIndex := range lookup.SourceIndexes {
 					l.Labels = append(l.Labels, sanitizeLabelName(oldIndex))
 				}
 				metric.Lookups = append(metric.Lookups, l)
@@ -407,9 +407,9 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
 				} else {
 					needToWalk[indexNode.Oid] = struct{}{}
 				}
-				if !lookup.KeepOld {
+				if !lookup.KeepSourceIndexes {
 					// Avoid leaving the old labelname around.
-					toDelete = append(toDelete, lookup.OldIndexes...)
+					toDelete = append(toDelete, lookup.SourceIndexes...)
 				}
 			}
 		}
diff --git a/generator/tree_test.go b/generator/tree_test.go
index 51bcbef4..50f89475 100644
--- a/generator/tree_test.go
+++ b/generator/tree_test.go
@@ -955,9 +955,9 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"octetFoo"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"octetIndex"},
-						NewIndex:   "octetDesc",
-						KeepOld:    true,
+						SourceIndexes:     []string{"octetIndex"},
+						Lookup:            "octetDesc",
+						KeepSourceIndexes: true,
 					},
 				},
 			},
@@ -1003,8 +1003,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"octetFoo"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"octetIndex"},
-						NewIndex:   "octetDesc",
+						SourceIndexes: []string{"octetIndex"},
+						Lookup:        "octetDesc",
 					},
 				},
 			},
@@ -1053,8 +1053,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"octetFoo"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"octetIndex"},
-						NewIndex:   "1.1.1.2",
+						SourceIndexes: []string{"octetIndex"},
+						Lookup:        "1.1.1.2",
 					},
 				},
 			},
@@ -1104,8 +1104,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"octetFoo"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"octetIndex", "octetIndex2"},
-						NewIndex:   "octetDesc",
+						SourceIndexes: []string{"octetIndex", "octetIndex2"},
+						Lookup:        "octetDesc",
 					},
 				},
 			},
@@ -1184,8 +1184,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"octet^Foo"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"octet&Index"},
-						NewIndex:   "1.1.1.2",
+						SourceIndexes: []string{"octet&Index"},
+						Lookup:        "1.1.1.2",
 					},
 				},
 			},
@@ -1346,8 +1346,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"1.1.1.2.100", "1.1.1.4.100", "1.1.1.2.200"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"tableIndex"},
-						NewIndex:   "tableDesc",
+						SourceIndexes: []string{"tableIndex"},
+						Lookup:        "tableDesc",
 					},
 				},
 			},
@@ -1419,8 +1419,8 @@ func TestGenerateConfigModule(t *testing.T) {
 				Walk: []string{"1.1.1.2.100", "1.1.1.3"},
 				Lookups: []*Lookup{
 					{
-						OldIndexes: []string{"tableIndex"},
-						NewIndex:   "tableDesc",
+						SourceIndexes: []string{"tableIndex"},
+						Lookup:        "tableDesc",
 					},
 				},
 			},