~egtann/inv2config

e6e17fe2f8917f1d71b369e9de167f76ab5818c9 — Evan Tann 2 months ago f27c93b master
support new inventory.json format
2 files changed, 33 insertions(+), 6 deletions(-)

M README.md
M main.go
M README.md => README.md +10 -2
@@ 6,7 6,12 @@ outputs an SRP config file pointing at specific IPs.
Example inventory.json:

```json
{"10.128.0.1":["my-app"]}
{
	"gcp:my-project:us-central1:us-central1-b": {
		"internal": {"10.128.0.1": ["my-app"]},
		"external": {"100.10.1.1": ["my-app"]}
	}
}
```

Example service-based SRP config file:


@@ 31,7 36,10 @@ Example service-based SRP config file:
To use:

```bash
inventory2config -i inventory.json -c config_service.json > config.json
inv2config \
	-i inventory.json \
	-c config_service.json \
	-p "gcp:my-project:us-central1:us-central1-b" > config.json
```

This outputs a config.json with backends populated with the correct ip:port

M main.go => main.go +23 -4
@@ 3,6 3,7 @@ package main
import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"os"


@@ 20,11 21,16 @@ func main() {
func run() error {
	// Read an inventory file and an SRP config file
	var (
		invFile = flag.String("i", "inventory.json", "inventory file")
		srpFile = flag.String("c", "config_service.json", "srp config file")
		invFile  = flag.String("i", "inventory.json", "inventory file")
		srpFile  = flag.String("c", "config_service.json", "srp config file")
		provider = flag.String("p", "", "provider")
		external = flag.Bool("x", false, "external ips")
	)
	flag.Parse()

	if *provider == "" {
		return errors.New("-p is required")
	}
	conf, err := parseSRPConfig(*srpFile)
	if err != nil {
		return fmt.Errorf("parse srp config: %w", err)


@@ 33,10 39,20 @@ func run() error {
	if err != nil {
		return fmt.Errorf("parse inventory: %w", err)
	}
	providerIPs, ok := inventory[*provider]
	if !ok {
		return errors.New("provider is not in inventory")
	}
	var ips map[string][]string
	if *external {
		ips = providerIPs.External
	} else {
		ips = providerIPs.Internal
	}

	// Convert our inventory into a data structure for fast lookups
	serviceToIPs := map[string][]string{}
	for ip, serviceNames := range inventory {
	for ip, serviceNames := range ips {
		for _, name := range serviceNames {
			serviceToIPs[name] = append(serviceToIPs[name], ip)
		}


@@ 93,7 109,10 @@ func parseSRPConfig(filename string) (*srpConfig, error) {
	return conf, nil
}

type inventory map[string][]string
type inventory map[string]struct {
	Internal map[string][]string `json:"internal"`
	External map[string][]string `json:"external"`
}

func parseInventory(filename string) (inventory, error) {
	fi, err := os.Open(filename)