Skip to content

Commit

Permalink
Add -strict to error if there is a key missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mongey committed Oct 11, 2019
1 parent dee2715 commit 897db13
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 19 deletions.
4 changes: 3 additions & 1 deletion command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (c *DeployCommand) Run(args []string) int {

var err error
var level, format string
var strictMode bool

config := &levant.DeployConfig{
Client: &structs.ClientConfig{},
Expand All @@ -125,6 +126,7 @@ func (c *DeployCommand) Run(args []string) int {
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.StringVar(&config.Deploy.VaultToken, "vault-token", "", "")
flags.BoolVar(&config.Deploy.EnvVault, "vault", false, "")
flags.BoolVar(&strictMode, "strict", false, "")

flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")

Expand Down Expand Up @@ -159,7 +161,7 @@ func (c *DeployCommand) Run(args []string) int {
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars)
config.Template.VariableFiles, config.Client.ConsulAddr, strictMode, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
4 changes: 2 additions & 2 deletions command/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDeploy_checkCanaryAutoPromote(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars)
job, err := template.RenderJob(c.File, []string{}, "", false, &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestDeploy_checkForceBatch(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars)
job, err := template.RenderJob(c.File, []string{}, "", false, &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down
4 changes: 3 additions & 1 deletion command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (c *PlanCommand) Run(args []string) int {
Template: &structs.TemplateConfig{},
}

var strictMode bool
flags := c.Meta.FlagSet("plan", FlagSetVars)
flags.Usage = func() { c.UI.Output(c.Help()) }

Expand All @@ -98,6 +99,7 @@ func (c *PlanCommand) Run(args []string) int {
flags.StringVar(&level, "log-level", "INFO", "")
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")
flags.BoolVar(&strictMode, "strict", false, "")

if err = flags.Parse(args); err != nil {
return 1
Expand All @@ -124,7 +126,7 @@ func (c *PlanCommand) Run(args []string) int {
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars)
config.Template.VariableFiles, config.Client.ConsulAddr, strictMode, &c.Meta.flagVars)

if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
Expand Down
4 changes: 3 additions & 1 deletion command/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (c *RenderCommand) Run(args []string) int {

var addr, outPath, templateFile string
var variables []string
var strictMode bool
var err error
var tpl *bytes.Buffer

Expand All @@ -69,6 +70,7 @@ func (c *RenderCommand) Run(args []string) int {
flags.StringVar(&addr, "consul-address", "", "")
flags.Var((*helper.FlagStringSlice)(&variables), "var-file", "")
flags.StringVar(&outPath, "out", "", "")
flags.BoolVar(&strictMode, "strict", false, "")

if err = flags.Parse(args); err != nil {
return 1
Expand All @@ -89,7 +91,7 @@ func (c *RenderCommand) Run(args []string) int {
return 1
}

tpl, err = template.RenderTemplate(templateFile, variables, addr, &c.Meta.flagVars)
tpl, err = template.RenderTemplate(templateFile, variables, addr, strictMode, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
7 changes: 4 additions & 3 deletions template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

// RenderJob takes in a template and variables performing a render of the
// template followed by Nomad jobspec parse.
func RenderJob(templateFile string, variableFiles []string, addr string, flagVars *map[string]string) (job *nomad.Job, err error) {
func RenderJob(templateFile string, variableFiles []string, addr string, strictMode bool, flagVars *map[string]string) (job *nomad.Job, err error) {
var tpl *bytes.Buffer
tpl, err = RenderTemplate(templateFile, variableFiles, addr, flagVars)
tpl, err = RenderTemplate(templateFile, variableFiles, addr, strictMode, flagVars)
if err != nil {
return
}
Expand All @@ -32,12 +32,13 @@ func RenderJob(templateFile string, variableFiles []string, addr string, flagVar

// RenderTemplate is the main entry point to render the template based on the
// passed variables file.
func RenderTemplate(templateFile string, variableFiles []string, addr string, flagVars *map[string]string) (tpl *bytes.Buffer, err error) {
func RenderTemplate(templateFile string, variableFiles []string, addr string, strictMode bool, flagVars *map[string]string) (tpl *bytes.Buffer, err error) {

t := &tmpl{}
t.flagVariables = flagVars
t.jobTemplateFile = templateFile
t.variableFiles = variableFiles
t.errMissingKey = strictMode

c, err := client.NewConsulClient(addr)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions template/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
fVars := make(map[string]string)

// Test basic TF template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -34,7 +34,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test basic YAML template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -43,7 +43,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -52,7 +52,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files of different types
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -62,7 +62,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {

// Test multiple var-files with var-args
fVars["job_name"] = testJobNameOverwrite2
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,7 +71,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test empty var-args and empty variable file render.
job, err = RenderJob("test-fixtures/none_templated.nomad", []string{}, "", &fVars)
job, err = RenderJob("test-fixtures/none_templated.nomad", []string{}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -82,7 +82,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
// Test var-args only render.
delete(fVars, "job_name")
fVars["job_name"] = testJobName
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -94,7 +94,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
delete(fVars, "job_name")
fVars["datacentre"] = testDCName
os.Setenv(testEnvName, testEnvValue)
job, err = RenderJob("test-fixtures/multi_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/multi_templated.nomad", []string{"test-fixtures/test.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand Down
9 changes: 8 additions & 1 deletion template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type tmpl struct {
flagVariables *map[string]string
jobTemplateFile string
variableFiles []string
errMissingKey bool
}

const (
Expand All @@ -28,7 +29,13 @@ const (
func (t *tmpl) newTemplate() *template.Template {
tmpl := template.New("jobTemplate")
tmpl.Delims(leftDelim, rightDelim)
tmpl.Option("missingkey=zero")

if t.errMissingKey {
tmpl.Option("missingkey=error")
} else {
tmpl.Option("missingkey=zero")
}

tmpl.Funcs(funcMap(t.consulClient))
return tmpl
}
1 change: 0 additions & 1 deletion test/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func Test(t *testing.T, c TestCase) {
if err != nil {
t.Fatalf("failed to create nomad client: %s", err)
}

state := &TestState{
JobName: fmt.Sprintf("levant-%s", t.Name()),
Nomad: nomad,
Expand Down
2 changes: 1 addition & 1 deletion test/acctest/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c DeployTestStepRunner) Run(s *TestState) error {
}
c.Vars["job_name"] = s.JobName

job, err := template.RenderJob("fixtures/"+c.FixtureName, []string{}, "", &c.Vars)
job, err := template.RenderJob("fixtures/"+c.FixtureName, []string{}, "", false, &c.Vars)
if err != nil {
return fmt.Errorf("error rendering template: %s", err)
}
Expand Down

0 comments on commit 897db13

Please sign in to comment.