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

Add validation for global vars #32

Merged
merged 3 commits into from
Nov 12, 2021
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/hashicorp/terraform-config-inspect v0.0.0-20210625153042-09f34846faab
github.com/imdario/mergo v0.3.12 // indirect
github.com/otiai10/copy v1.6.0
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.2.1
github.com/zclconf/go-cty v1.9.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
11 changes: 9 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ func (bc BlueprintConfig) expand() {
}

func (bc BlueprintConfig) validate() {
bc.validateResources()
bc.validateResourceSettings()
if err := bc.validateVars(); err != nil {
log.Fatal(err)
}
if err := bc.validateResources(); err != nil {
log.Fatal(err)
}
if err := bc.validateResourceSettings(); err != nil {
log.Fatal(err)
}
}
29 changes: 29 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package config

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"testing"

"hpc-toolkit/pkg/resreader"
Expand Down Expand Up @@ -593,6 +595,33 @@ func (s *MySuite) TestValidateResources(c *C) {
bc.validateResources()
}

func (s *MySuite) TestValidateVars(c *C) {
// Success
bc := getBlueprintConfigForTest()
err := bc.validateVars()
c.Assert(err, IsNil)

// Fail: Nil project_id
bc.Config.Vars["project_id"] = nil
err = bc.validateVars()
c.Assert(err, ErrorMatches, "global variable project_id was not set")

// Success: project_id not set
delete(bc.Config.Vars, "project_id")
var buf bytes.Buffer
log.SetOutput(&buf)
err = bc.validateVars()
log.SetOutput(os.Stderr)
c.Assert(err, IsNil)
hasWarning := strings.Contains(buf.String(), "WARNING: No project_id")
c.Assert(hasWarning, Equals, true)

// Fail: labels not a map
bc.Config.Vars["labels"] = "a_string"
err = bc.validateVars()
c.Assert(err, ErrorMatches, "vars.labels must be a map")
}

func (s *MySuite) TestValidateResouceSettings(c *C) {
testSource := path.Join(tmpTestDir, "resource")
testSettings := map[string]interface{}{
Expand Down
49 changes: 38 additions & 11 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,37 @@ import (

"hpc-toolkit/pkg/resreader"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

// validateVars checks the global variables for viable types
func (bc BlueprintConfig) validateVars() error {
vars := bc.Config.Vars
nilErr := "global variable %s was not set"

// Check for project_id
if _, ok := vars["project_id"]; !ok {
log.Println("WARNING: No project_id in global variables")
}

// Check type of labels (if they are defined)
if labels, ok := vars["labels"]; ok {
if _, ok := labels.(map[string]interface{}); !ok {
return errors.New("vars.labels must be a map")
}
}

// Check for any nil values
for key, val := range vars {
if val == nil {
return fmt.Errorf(nilErr, key)
}
}

return nil
}

func resource2String(c Resource) string {
cBytes, _ := yaml.Marshal(&c)
return string(cBytes)
Expand All @@ -49,15 +77,15 @@ func hasIllegalChars(name string) bool {
}

// validateResources ensures parameters set in resources are set correctly.
func (bc BlueprintConfig) validateResources() {
func (bc BlueprintConfig) validateResources() error {
for _, grp := range bc.Config.ResourceGroups {
for _, res := range grp.Resources {
err := validateResource(res)
if err != nil {
log.Fatal(err)
if err := validateResource(res); err != nil {
return err
}
}
}
return nil
}

type resourceVariables struct {
Expand Down Expand Up @@ -89,21 +117,20 @@ func validateSettings(

// validateResourceSettings verifies that no additional settings are provided
// that don't have a counterpart variable in the resource.
func (bc BlueprintConfig) validateResourceSettings() {
func (bc BlueprintConfig) validateResourceSettings() error {
for _, grp := range bc.Config.ResourceGroups {
for _, res := range grp.Resources {
reader := resreader.Factory(res.Kind)
info, err := reader.GetInfo(res.Source)
if err != nil {
log.Fatalf(
"failed to get info for resource at %s while validating resource settings: %e",
res.Source, err)
errStr := "failed to get info for resource at %s while validating resource settings"
return errors.Wrapf(err, errStr, res.Source)
}
if err = validateSettings(res, info); err != nil {
log.Fatalf(
"found an issue while validating settings for resource at %s: %e",
res.Source, err)
errStr := "found an issue while validating settings for resource at %s"
return errors.Wrapf(err, errStr, res.Source)
}
}
}
return nil
}