I have a yaml configuration file that contains sets of configuration commands to send to network devices. Inside of each set, there are vendor-specific keys and the values for each vendor key can be either a configuration command string, a list of configuration command strings, or a list of key-value pairs mapping a vendor-specific model string to a configuration command string. Below is an example:
# example.yml
---
cmds:
setup:
cisco: "terminal length 0"
config:
cisco:
- basic : "show version"
- basic : "show boot"
"3560" : "3560 boot command"
"2960x": "2960x boot command"
- basic : "dir flash:"
"3560" : "3560 dir command"
cleanup:
cisco: ["terminal no length", "quit"]
I want to combine these commands into a map like so:
var cmdMap = map[string][]string{
"cisco": []string{
"terminal length 0",
"show version",
"show boot",
"dir flash:",
"terminal no length",
"quit",
},
"cisco.3560": []string{
"terminal length 0",
"show version",
"3560 boot command",
"3560 dir command",
"terminal no length",
"quit",
},
"cisco.2960x": []string{
"terminal length 0",
"show version",
"2960x boot command",
"dir flash:",
"terminal no length",
"quit",
}
}
I am using spf13/viper to handle parsing the yaml file and have been able to add the specific commands to each vendor and model, but adding the commands that apply to both vendor and specific model is where I am stuck. This is the actual output of my program followed by my code:
$ go run main.go example.yml
cmdMap["cisco"]
terminal length 0
show version
show boot
dir flash:
terminal no length
quit
# missing terminal length 0, show version, etc.
cmdMap["cisco.3560"]
3560 boot command
3560 dir command
# missing terminal length 0, show version, etc.
cmdMap["cisco.2960x"]
2960x boot command
My code:
package main
import (
"github.com/spf13/viper"
"fmt"
"flag"
"log"
)
func main() {
flag.Parse()
cfgFile := flag.Arg(0)
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(cfgFile)
if err := v.ReadInConfig(); err != nil {
log.Fatal(err)
}
for k, v := range MapCfgCmds(v.GetStringMap("cmds")) {
fmt.Printf("cmdMap[\"%s\"]
", k)
for _, cmd := range v {
fmt.Println(cmd)
}
fmt.Println()
}
}
func MapCfgCmds(cfgCmds map[string]interface{}) map[string][]string {
cmdMap := make(map[string][]string)
for _, cmdSet := range cfgCmds {
cmdSet, _ := cmdSet.(map[string]interface{})
for vendor, cmdList := range cmdSet {
switch cmds := cmdList.(type) {
case string:
// single string command (i.e., vendor: cmd)
cmdMap[vendor] = append(cmdMap[vendor], cmds)
case []interface{}:
for _, cmd := range cmds {
switch c := cmd.(type) {
case string:
// list of strings (i.e., vendor: [cmd1,cmd2,...,cmdN])
cmdMap[vendor] = append(cmdMap[vendor], c)
case map[interface{}]interface{}:
// This is where I am stuck
//
// list of key-value pairs (i.e., vendor: {model: modelCmd})
for model, modelCmd := range c {
modelCmd, _ := modelCmd.(string)
if model == "basic" {
cmdMap[vendor] = append(cmdMap[vendor], modelCmd)
continue
}
modelKey := fmt.Sprintf("%s.%s", vendor, model)
cmdMap[modelKey] = append(cmdMap[modelKey], modelCmd)
}
}
}
}
}
}
return cmdMap
}
How can I combine the "universal" and model-specific commands to get the expected cmdMap
value from above?
I think viper is not helping you here, in the sense that viper does a lot of things you don't need, but it doesn't do one thing you could use here - clear mapping of the data. If you use the yaml library directly, you could declare a structure that corresponds to your data and makes it easier to understand it.
There are several possible approaches to your problem, here is my attempt at solving it (you might need to tweak few things as I wrote it in the editor, without compiling it):
type Data struct {
Cmds struct {
Setup map[string]interface{} `yaml:"setup"`
Config map[string][]map[string]string `yaml:"config"`
Cleanup map[string][]string `yaml:"cleanup"`
} `yaml:"cmds"`
}
data := Data{}
err := yaml.Unmarshal([]byte(input), &data)
if err != nil {
log.Fatalf("error: %v", err)
}
setupCmds := make(map[string][]string)
cleanupCmds := make(map[string][]string)
result := make(map[string][]string)
// Prepare setup commands, grouped by vendor
for vendor, setupCmd := range data.Cmds.Setup {
setupCmds[vendor] = append(setupCmds[vendor], setupCmd)
}
// Prepare cleanup commands, grouped by vendor
for vendor, commands := range data.Cmds.Cleanup {
cleanupCmds[vendor] = append(cleanupCmds[vendor], commands...)
}
// iterate over vendors and models, combine with setup & cleanup commands and store in result
for vendor, configCmds := range data.Cmds.Config { // vendor = string (e.g. "cisco"), configCmds = []map[string][string] (e.g. - basic: "show version")
// we now how many config commands there will be
result[vendor] = make([]string, len(configCmds))
// variantsCache will store all variants we've seen so far
variantsCache := make(map[string]struct{})
for i, model := range models { // i = int (number of command) model = map[string]string
// we assume "basic" is available for each command
result[vendor][i] = model["basic"]
for variant, command := range model { // variant = string (e.g. "basic"), command = string (e.g. "show version")
if variant == "basic" {
// we already covered that
continue
}
variantKey := vendor + "." + variant
variantsCache[variantKey]
if _, ok := result[variantKey]; !ok {
// first command for this model, create a slice
result[variantKey] = make([]string, len(configCmds))
}
result[variantKey][i] = command
}
}
// We need to iterate over all commands for all variants and copy "basic" command if there is none
for variant, _ := range variantsCache {
for i, command := range result[variant] {
if command == "" {
// copy the "basic" command, since there was no variant specific command
result[variant][i] = result[vendor][i]
}
}
}
}
// combine setup and cleanup with config
for variant, _ := result {
// will return "cisco" for both "cisco" and "cisco.x3650"
vendor := strings.Split(variant, ".")[0]
result[variant] = append(setupCmds[vendor], result[variant]...)
result[variant] = append(result[variant], cleanupCmds[vendor]...)
}
return result
You can combine them after the loop of constructing cmdMap
.
for vendor := range cmdMap {
// get the base name of the vendor
s := strings.SplitN(vender, ".", 2)
// if vendor is a basic one, skip it.
if len(s) == 1 {
continue
}
// add basic cmd into the specified ones.
base = s[0]
cmdMap[vendor] = append(cmdMap[vendor], cmdMap[base]...)
}
Note that cmdMap[base]...
is a use of variadic parmaters of append. You can see more here: https://golang.org/ref/spec#Passing_arguments_to_..._parameters