Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #15 from vincent99/master
Browse files Browse the repository at this point in the history
Show version, Support slash in keys
  • Loading branch information
ibuildthecloud committed Mar 22, 2016
2 parents 97c91eb + 5bb68ca commit 5341fd0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
8 changes: 3 additions & 5 deletions answers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
log "github.com/Sirupsen/logrus"
"reflect"
"strconv"
"strings"
)

func (answers *Versions) Versions() []string {
Expand All @@ -16,7 +15,7 @@ func (answers *Versions) Versions() []string {
return out
}

func (answers *Versions) Matching(version string, ip string, path string) (interface{}, bool) {
func (answers *Versions) Matching(version string, ip string, path []string) (interface{}, bool) {
var out interface{}

all, ok := (*answers)[version]
Expand Down Expand Up @@ -48,11 +47,10 @@ func (answers *Versions) Matching(version string, ip string, path string) (inter
return nil, false
}

func valueForPath(in *interface{}, path string) (interface{}, bool) {
func valueForPath(in *interface{}, path []string) (interface{}, bool) {
out := *in
parts := strings.Split(path, "/")

for _, key := range parts {
for _, key := range path {
valid := false

switch v := out.(type) {
Expand Down
8 changes: 7 additions & 1 deletion example/answers.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"2015-07-25": {
"default": {
"host": null,
"host": null,
"sla/sh": {
"sub": "key"
},
"version": "20-aff454dabf2760385d247a5955488ff375b8230edefe079d3748ab44426edd9f",
"hosts": [
{
Expand Down Expand Up @@ -241,6 +244,9 @@
},
"2015-12-19": {
"default": {
"sla/sh": {
"sub": "key"
},
"host": null,
"version": "20-aff454dabf2760385d247a5955488ff375b8230edefe079d3748ab44426edd9f",
"hosts": [
Expand Down
42 changes: 31 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"sort"
Expand Down Expand Up @@ -36,6 +37,7 @@ const (
)

var (
showVersion = flag.Bool("version", false, "Show version")
debug = flag.Bool("debug", false, "Debug")
enableXff = flag.Bool("xff", false, "X-Forwarded-For header support")
listen = flag.String("listen", ":80", "Address to listen to (TCP)")
Expand All @@ -47,15 +49,22 @@ var (
router = mux.NewRouter()
answers Versions

VERSION string
wantRevision = 1
loadedRevision = 0
loading = false
ticker = time.NewTicker(1 * time.Second)
)

func main() {
log.Info("Starting rancher-metadata")
parseFlags()

if *showVersion {
fmt.Printf("%s\n", VERSION)
os.Exit(0)
}

log.Infof("Starting rancher-metadata %s", VERSION)
err := loadAnswers()
if err != nil {
log.Fatal("Cannot startup without a valid Answers file")
Expand Down Expand Up @@ -270,11 +279,22 @@ func metadata(w http.ResponseWriter, req *http.Request) {
}
}

key := strings.TrimRight(vars["key"], "/")
displayKey := "/" + key
path := strings.TrimRight(req.URL.EscapedPath()[1:], "/")
pathSegments := strings.Split(path, "/")[1:]
displayKey := ""
var err error
for i := 0; err == nil && i < len(pathSegments); i++ {
displayKey += "/" + pathSegments[i]
pathSegments[i], err = url.QueryUnescape(pathSegments[i])
}

if err != nil {
respondError(w, req, err.Error(), http.StatusBadRequest)
return
}

log.WithFields(log.Fields{"version": version, "client": clientIp}).Debugf("Searching for: %s", key)
val, ok := answers.Matching(version, clientIp, key)
log.WithFields(log.Fields{"version": version, "client": clientIp}).Debugf("Searching for: %s", displayKey)
val, ok := answers.Matching(version, clientIp, pathSegments)

if ok {
log.WithFields(log.Fields{"version": version, "client": clientIp}).Infof("OK: %s", displayKey)
Expand Down Expand Up @@ -351,9 +371,9 @@ func respondText(w http.ResponseWriter, req *http.Request, val interface{}) {
_, isMap := vv.(map[string]interface{})
_, isArray := vv.([]interface{})
if isMap || isArray {
out[i] = fmt.Sprintf("%s/\n", k)
out[i] = fmt.Sprintf("%s/\n", url.QueryEscape(k))
} else {
out[i] = fmt.Sprintf("%s\n", k)
out[i] = fmt.Sprintf("%s\n", url.QueryEscape(k))
}
i++
}
Expand All @@ -368,19 +388,19 @@ func respondText(w http.ResponseWriter, req *http.Request, val interface{}) {
_, isArray := vv.([]interface{})

if isMap {
// If the child is a map and has a "name" property, show "key=name"
// If the child is a map and has a "name" property, show index=name ("0=foo")
name, ok := vvMap[MAGIC_ARRAY_KEY]
if ok {
fmt.Fprintf(w, "%d=%s\n", k, name)
fmt.Fprintf(w, "%d=%s\n", k, url.QueryEscape(name.(string)))
continue
}
}

if isMap || isArray {
// If the child is a map or array, show "key/"
// If the child is a map or array, show index ("0/")
fmt.Fprintf(w, "%d/\n", k)
} else {
// Otherwise, show "key"
// Otherwise, show index ("0" )
fmt.Fprintf(w, "%d\n", k)
}
}
Expand Down

0 comments on commit 5341fd0

Please sign in to comment.