Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow haproxy template to list all app env values #218

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions services/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"bytes"
"github.com/QubitProducts/bamboo/services/marathon"
"github.com/QubitProducts/bamboo/services/service"
"strings"
"text/template"
Expand All @@ -17,19 +18,42 @@ func getService(data map[string]service.Service, appId string) service.Service {
return serviceModel
}

func getAppEnvValues(envVar string, appList marathon.AppList) (envValues []string) {
for _, app := range appList {
value, ok := app.Env[envVar]
if ok {
envValues = append(envValues, value)
}
}
return unique(envValues)
}

func unique(strings []string) (uniqueStrings []string) {
seen := make(map[string]bool)
for _, s := range strings {
if !seen[s] {
uniqueStrings = append(uniqueStrings, s)
seen[s] = true
}
}
return uniqueStrings
}

/*
Returns string content of a rendered template
*/
func RenderTemplate(templateName string, templateContent string, data interface{}) (string, error) {
funcMap := template.FuncMap{
"hasKey": hasKey,
"getService": getService,
"Split": strings.Split,
"Contains": strings.Contains,
"Join": strings.Join,
"Replace": strings.Replace,
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower}
"hasKey": hasKey,
"getService": getService,
"getAppEnvValues": getAppEnvValues,
"Split": strings.Split,
"Contains": strings.Contains,
"Join": strings.Join,
"Replace": strings.Replace,
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"unique": unique}

tpl := template.Must(template.New(templateName).Funcs(funcMap).Parse(templateContent))

Expand Down
69 changes: 69 additions & 0 deletions services/template/template_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package template

import (
"github.com/QubitProducts/bamboo/services/marathon"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
Expand All @@ -18,6 +19,60 @@ func TestTemplateWriter(t *testing.T) {
})
}

type TemplateData struct {
Apps marathon.AppList
}

func TestTemplateGetAppEnvValuesFunction(t *testing.T) {
Convey("#RenderTemplate", t, func() {
templateName := "templateName"
apps := marathon.AppList{}
apps = append(apps, marathon.App{
Id: "app1",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10001",
},
})
apps = append(apps, marathon.App{
Id: "app2",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10001",
},
})
apps = append(apps, marathon.App{
Id: "app3",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10002",
},
})
apps = append(apps, marathon.App{
Id: "app4",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10001",
},
})
apps = append(apps, marathon.App{
Id: "app5",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10003",
},
})
apps = append(apps, marathon.App{
Id: "app6",
Env: map[string]string{
"BAMBOO_TCP_PORT": "10002",
},
})

templateData := TemplateData{Apps: apps}
templateContent := "{{ range $port := .Apps | getAppEnvValues \"BAMBOO_TCP_PORT\" }}{{ $port }} {{ end }}"
Convey("should list the three unique ports", func() {
content, _ := RenderTemplate(templateName, templateContent, templateData)
So(content, ShouldEqual, "10001 10002 10003 ")
})
})
}

func TestTemplateSplitFunction(t *testing.T) {
Convey("#RenderTemplate", t, func() {
templateName := "templateName"
Expand All @@ -31,6 +86,20 @@ func TestTemplateSplitFunction(t *testing.T) {
})
}

func TestTemplateUniqueFunction(t *testing.T) {
Convey("#RenderTemplate", t, func() {
templateName := "templateName"
domains := []string{"example.com", "example.com", "example.org", "example.com", "example.org", "example.net"}
params := map[string][]string{"domains": domains}

templateContent := "{{ range unique .domains }}{{ . }} {{ end }}"
Convey("should create a list of three distinct domains", func() {
content, _ := RenderTemplate(templateName, templateContent, params)
So(content, ShouldEqual, "example.com example.org example.net ")
})
})
}

func TestTemplateContainsFunction(t *testing.T) {
Convey("#RenderTemplate", t, func() {
templateName := "templateName"
Expand Down