diff --git a/cmd/create.go b/cmd/create.go index 452ff1e546..54a5ecd432 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -77,18 +77,18 @@ func runCreateCmd(cmd *cobra.Command, args []string) { bpFilename = args[0] } - blueprintConfig := config.NewBlueprintConfig(bpFilename) - if err := blueprintConfig.SetCLIVariables(cliVariables); err != nil { + deploymentConfig := config.NewDeploymentConfig(bpFilename) + if err := deploymentConfig.SetCLIVariables(cliVariables); err != nil { log.Fatalf("Failed to set the variables at CLI: %v", err) } - if err := blueprintConfig.SetBackendConfig(cliBEConfigVars); err != nil { + if err := deploymentConfig.SetBackendConfig(cliBEConfigVars); err != nil { log.Fatalf("Failed to set the backend config at CLI: %v", err) } - if err := blueprintConfig.SetValidationLevel(validationLevel); err != nil { + if err := deploymentConfig.SetValidationLevel(validationLevel); err != nil { log.Fatal(err) } - blueprintConfig.ExpandConfig() - if err := reswriter.WriteBlueprint(&blueprintConfig.Config, outputDir, overwriteDeployment); err != nil { + deploymentConfig.ExpandConfig() + if err := reswriter.WriteBlueprint(&deploymentConfig.Config, outputDir, overwriteDeployment); err != nil { var target *reswriter.OverwriteDeniedError if errors.As(err, &target) { fmt.Printf("\n%s\n", err.Error()) diff --git a/cmd/expand.go b/cmd/expand.go index 1d46bef39d..55d6308143 100644 --- a/cmd/expand.go +++ b/cmd/expand.go @@ -58,18 +58,18 @@ func runExpandCmd(cmd *cobra.Command, args []string) { bpFilename = args[0] } - blueprintConfig := config.NewBlueprintConfig(bpFilename) - if err := blueprintConfig.SetCLIVariables(cliVariables); err != nil { + deploymentConfig := config.NewDeploymentConfig(bpFilename) + if err := deploymentConfig.SetCLIVariables(cliVariables); err != nil { log.Fatalf("Failed to set the variables at CLI: %v", err) } - if err := blueprintConfig.SetBackendConfig(cliBEConfigVars); err != nil { + if err := deploymentConfig.SetBackendConfig(cliBEConfigVars); err != nil { log.Fatalf("Failed to set the backend config at CLI: %v", err) } - if err := blueprintConfig.SetValidationLevel(validationLevel); err != nil { + if err := deploymentConfig.SetValidationLevel(validationLevel); err != nil { log.Fatal(err) } - blueprintConfig.ExpandConfig() - blueprintConfig.ExportYamlConfig(outputFilename) + deploymentConfig.ExpandConfig() + deploymentConfig.ExportYamlConfig(outputFilename) fmt.Printf( "Expanded Environment Definition created successfully, saved as %s.\n", outputFilename) } diff --git a/pkg/config/config.go b/pkg/config/config.go index d4dd963a95..569208e2ca 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -111,14 +111,14 @@ func isValidValidationLevel(level int) bool { } // SetValidationLevel allows command-line tools to set the validation level -func (bc *BlueprintConfig) SetValidationLevel(level string) error { +func (dc *DeploymentConfig) SetValidationLevel(level string) error { switch level { case "ERROR": - bc.Config.ValidationLevel = validationError + dc.Config.ValidationLevel = validationError case "WARNING": - bc.Config.ValidationLevel = validationWarning + dc.Config.ValidationLevel = validationWarning case "IGNORE": - bc.Config.ValidationLevel = validationIgnore + dc.Config.ValidationLevel = validationIgnore default: return fmt.Errorf("invalid validation level (\"ERROR\", \"WARNING\", \"IGNORE\")") } @@ -172,9 +172,9 @@ type Module struct { // createWrapSettingsWith ensures WrapSettingsWith field is not nil, if it is // a new map is created. -func (r *Module) createWrapSettingsWith() { - if r.WrapSettingsWith == nil { - r.WrapSettingsWith = make(map[string][]string) +func (m *Module) createWrapSettingsWith() { + if m.WrapSettingsWith == nil { + m.WrapSettingsWith = make(map[string][]string) } } @@ -191,9 +191,9 @@ type YamlConfig struct { TerraformBackendDefaults TerraformBackend `yaml:"terraform_backend_defaults"` } -// BlueprintConfig is a container for the imported YAML data and supporting data for +// DeploymentConfig is a container for the imported YAML data and supporting data for // creating the blueprint from it -type BlueprintConfig struct { +type DeploymentConfig struct { Config YamlConfig // Indexed by Resource Group name and Module Source ModulesInfo map[string]map[string]resreader.ModuleInfo @@ -203,20 +203,20 @@ type BlueprintConfig struct { } // ExpandConfig expands the yaml config in place -func (bc *BlueprintConfig) ExpandConfig() { - bc.setModulesInfo() - bc.validateConfig() - bc.expand() - bc.validate() - bc.expanded = true +func (dc *DeploymentConfig) ExpandConfig() { + dc.setModulesInfo() + dc.validateConfig() + dc.expand() + dc.validate() + dc.expanded = true } -// NewBlueprintConfig is a constructor for BlueprintConfig -func NewBlueprintConfig(configFilename string) BlueprintConfig { - newBlueprintConfig := BlueprintConfig{ +// NewDeploymentConfig is a constructor for DeploymentConfig +func NewDeploymentConfig(configFilename string) DeploymentConfig { + newDeploymentConfig := DeploymentConfig{ Config: importYamlConfig(configFilename), } - return newBlueprintConfig + return newDeploymentConfig } // ImportYamlConfig imports the blueprint configuration provided. @@ -254,8 +254,8 @@ func importYamlConfig(yamlConfigFilename string) YamlConfig { } // ExportYamlConfig exports the internal representation of a blueprint config -func (bc BlueprintConfig) ExportYamlConfig(outputFilename string) ([]byte, error) { - d, err := yaml.Marshal(&bc.Config) +func (dc DeploymentConfig) ExportYamlConfig(outputFilename string) ([]byte, error) { + d, err := yaml.Marshal(&dc.Config) if err != nil { return d, fmt.Errorf("%s: %w", errorMessages["yamlMarshalError"], err) } @@ -280,7 +280,7 @@ func createModuleInfo( ri, err := reader.GetModuleInfo(mod.Source, mod.Kind) if err != nil { log.Fatalf( - "failed to get info for module at %s while setting bc.ModulesInfo: %e", + "failed to get info for module at %s while setting dc.ModulesInfo: %e", mod.Source, err) } modInfo[mod.Source] = ri @@ -290,10 +290,10 @@ func createModuleInfo( } // setModulesInfo populates needed information from modules -func (bc *BlueprintConfig) setModulesInfo() { - bc.ModulesInfo = make(map[string]map[string]resreader.ModuleInfo) - for _, grp := range bc.Config.DeploymentGroups { - bc.ModulesInfo[grp.Name] = createModuleInfo(grp) +func (dc *DeploymentConfig) setModulesInfo() { + dc.ModulesInfo = make(map[string]map[string]resreader.ModuleInfo) + for _, grp := range dc.Config.DeploymentGroups { + dc.ModulesInfo[grp.Name] = createModuleInfo(grp) } } @@ -365,20 +365,20 @@ func checkUsedModuleNames( } // validateConfig runs a set of simple early checks on the imported input YAML -func (bc *BlueprintConfig) validateConfig() { - moduleToGroup, err := checkModuleAndGroupNames(bc.Config.DeploymentGroups) +func (dc *DeploymentConfig) validateConfig() { + moduleToGroup, err := checkModuleAndGroupNames(dc.Config.DeploymentGroups) if err != nil { log.Fatal(err) } - bc.ModuleToGroup = moduleToGroup + dc.ModuleToGroup = moduleToGroup if err = checkUsedModuleNames( - bc.Config.DeploymentGroups, bc.ModuleToGroup); err != nil { + dc.Config.DeploymentGroups, dc.ModuleToGroup); err != nil { log.Fatal(err) } } // SetCLIVariables sets the variables at CLI -func (bc *BlueprintConfig) SetCLIVariables(cliVariables []string) error { +func (dc *DeploymentConfig) SetCLIVariables(cliVariables []string) error { for _, cliVar := range cliVariables { arr := strings.SplitN(cliVar, "=", 2) @@ -387,18 +387,18 @@ func (bc *BlueprintConfig) SetCLIVariables(cliVariables []string) error { } key, value := arr[0], arr[1] - bc.Config.Vars[key] = value + dc.Config.Vars[key] = value } return nil } // SetBackendConfig sets the backend config variables at CLI -func (bc *BlueprintConfig) SetBackendConfig(cliBEConfigVars []string) error { +func (dc *DeploymentConfig) SetBackendConfig(cliBEConfigVars []string) error { // Set "gcs" as default value when --backend-config is specified at CLI if len(cliBEConfigVars) > 0 { - bc.Config.TerraformBackendDefaults.Type = "gcs" - bc.Config.TerraformBackendDefaults.Configuration = make(map[string]interface{}) + dc.Config.TerraformBackendDefaults.Type = "gcs" + dc.Config.TerraformBackendDefaults.Configuration = make(map[string]interface{}) } for _, config := range cliBEConfigVars { @@ -411,9 +411,9 @@ func (bc *BlueprintConfig) SetBackendConfig(cliBEConfigVars []string) error { key, value := arr[0], arr[1] switch key { case "type": - bc.Config.TerraformBackendDefaults.Type = value + dc.Config.TerraformBackendDefaults.Type = value default: - bc.Config.TerraformBackendDefaults.Configuration[key] = value + dc.Config.TerraformBackendDefaults.Configuration[key] = value } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 036c6f65d2..c0983392de 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -160,7 +160,7 @@ func cleanErrorRegexp(errRegexp string) string { return errRegexp } -func getBlueprintConfigForTest() BlueprintConfig { +func getDeploymentConfigForTest() DeploymentConfig { testModuleSource := "testSource" testModule := Module{ Source: testModuleSource, @@ -205,7 +205,7 @@ func getBlueprintConfigForTest() BlueprintConfig { }, } - return BlueprintConfig{ + return DeploymentConfig{ Config: testYamlConfig, ModulesInfo: map[string]map[string]resreader.ModuleInfo{ "group1": { @@ -216,7 +216,7 @@ func getBlueprintConfigForTest() BlueprintConfig { } } -func getBasicBlueprintConfigWithTestModule() BlueprintConfig { +func getBasicDeploymentConfigWithTestModule() DeploymentConfig { testModuleSource := filepath.Join(tmpTestDir, "module") testDeploymentGroup := DeploymentGroup{ Name: "primary", @@ -229,7 +229,7 @@ func getBasicBlueprintConfigWithTestModule() BlueprintConfig { }, }, } - return BlueprintConfig{ + return DeploymentConfig{ Config: YamlConfig{ Vars: make(map[string]interface{}), DeploymentGroups: []DeploymentGroup{testDeploymentGroup}, @@ -240,18 +240,18 @@ func getBasicBlueprintConfigWithTestModule() BlueprintConfig { /* Tests */ // config.go func (s *MySuite) TestExpandConfig(c *C) { - bc := getBasicBlueprintConfigWithTestModule() - bc.ExpandConfig() + dc := getBasicDeploymentConfigWithTestModule() + dc.ExpandConfig() } func (s *MySuite) TestSetModulesInfo(c *C) { - bc := getBasicBlueprintConfigWithTestModule() - bc.setModulesInfo() + dc := getBasicDeploymentConfigWithTestModule() + dc.setModulesInfo() } func (s *MySuite) TestCreateModuleInfo(c *C) { - bc := getBasicBlueprintConfigWithTestModule() - createModuleInfo(bc.Config.DeploymentGroups[0]) + dc := getBasicDeploymentConfigWithTestModule() + createModuleInfo(dc.Config.DeploymentGroups[0]) } func (s *MySuite) TestGetResouceByID(c *C) { @@ -309,18 +309,18 @@ func (s *MySuite) TestHasKind(c *C) { } func (s *MySuite) TestCheckModuleAndGroupNames(c *C) { - bc := getBlueprintConfigForTest() - checkModuleAndGroupNames(bc.Config.DeploymentGroups) - testModID := bc.Config.DeploymentGroups[0].Modules[0].ID - c.Assert(bc.ModuleToGroup[testModID], Equals, 0) + dc := getDeploymentConfigForTest() + checkModuleAndGroupNames(dc.Config.DeploymentGroups) + testModID := dc.Config.DeploymentGroups[0].Modules[0].ID + c.Assert(dc.ModuleToGroup[testModID], Equals, 0) } func (s *MySuite) TestNewBlueprint(c *C) { - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() outFile := filepath.Join(tmpTestDir, "out_TestNewBlueprint.yaml") - bc.ExportYamlConfig(outFile) - newBC := NewBlueprintConfig(outFile) - c.Assert(bc.Config, DeepEquals, newBC.Config) + dc.ExportYamlConfig(outFile) + newDC := NewDeploymentConfig(outFile) + c.Assert(dc.Config, DeepEquals, newDC.Config) } func (s *MySuite) TestImportYamlConfig(c *C) { @@ -338,16 +338,16 @@ func (s *MySuite) TestImportYamlConfig(c *C) { func (s *MySuite) TestExportYamlConfig(c *C) { // Return bytes - bc := BlueprintConfig{} - bc.Config = expectedSimpleYamlConfig - obtainedYaml, err := bc.ExportYamlConfig("") + dc := DeploymentConfig{} + dc.Config = expectedSimpleYamlConfig + obtainedYaml, err := dc.ExportYamlConfig("") c.Assert(err, IsNil) c.Assert(obtainedYaml, Not(IsNil)) // Write file outFilename := "out_TestExportYamlConfig.yaml" outFile := filepath.Join(tmpTestDir, outFilename) - bc.ExportYamlConfig(outFile) + dc.ExportYamlConfig(outFile) fileInfo, err := os.Stat(outFile) c.Assert(err, IsNil) c.Assert(fileInfo.Name(), Equals, outFilename) @@ -357,11 +357,11 @@ func (s *MySuite) TestExportYamlConfig(c *C) { func (s *MySuite) TestSetCLIVariables(c *C) { // Success - bc := getBasicBlueprintConfigWithTestModule() - c.Assert(bc.Config.Vars["project_id"], IsNil) - c.Assert(bc.Config.Vars["deployment_name"], IsNil) - c.Assert(bc.Config.Vars["region"], IsNil) - c.Assert(bc.Config.Vars["zone"], IsNil) + dc := getBasicDeploymentConfigWithTestModule() + c.Assert(dc.Config.Vars["project_id"], IsNil) + c.Assert(dc.Config.Vars["deployment_name"], IsNil) + c.Assert(dc.Config.Vars["region"], IsNil) + c.Assert(dc.Config.Vars["zone"], IsNil) cliProjectID := "cli_test_project_id" cliDeploymentName := "cli_deployment_name" @@ -375,36 +375,36 @@ func (s *MySuite) TestSetCLIVariables(c *C) { fmt.Sprintf("zone=%s", cliZone), fmt.Sprintf("kv=%s", cliKeyVal), } - err := bc.SetCLIVariables(cliVars) + err := dc.SetCLIVariables(cliVars) c.Assert(err, IsNil) - c.Assert(bc.Config.Vars["project_id"], Equals, cliProjectID) - c.Assert(bc.Config.Vars["deployment_name"], Equals, cliDeploymentName) - c.Assert(bc.Config.Vars["region"], Equals, cliRegion) - c.Assert(bc.Config.Vars["zone"], Equals, cliZone) - c.Assert(bc.Config.Vars["kv"], Equals, cliKeyVal) + c.Assert(dc.Config.Vars["project_id"], Equals, cliProjectID) + c.Assert(dc.Config.Vars["deployment_name"], Equals, cliDeploymentName) + c.Assert(dc.Config.Vars["region"], Equals, cliRegion) + c.Assert(dc.Config.Vars["zone"], Equals, cliZone) + c.Assert(dc.Config.Vars["kv"], Equals, cliKeyVal) // Failure: Variable without '=' - bc = getBasicBlueprintConfigWithTestModule() - c.Assert(bc.Config.Vars["project_id"], IsNil) + dc = getBasicDeploymentConfigWithTestModule() + c.Assert(dc.Config.Vars["project_id"], IsNil) invalidNonEQVars := []string{ fmt.Sprintf("project_id%s", cliProjectID), } - err = bc.SetCLIVariables(invalidNonEQVars) + err = dc.SetCLIVariables(invalidNonEQVars) expErr := "invalid format: .*" c.Assert(err, ErrorMatches, expErr) - c.Assert(bc.Config.Vars["project_id"], IsNil) + c.Assert(dc.Config.Vars["project_id"], IsNil) } func (s *MySuite) TestSetBackendConfig(c *C) { // Success - bc := getBlueprintConfigForTest() - c.Assert(bc.Config.TerraformBackendDefaults.Type, Equals, "") - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["bucket"], IsNil) - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["impersonate_service_account"], IsNil) - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["prefix"], IsNil) + dc := getDeploymentConfigForTest() + c.Assert(dc.Config.TerraformBackendDefaults.Type, Equals, "") + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["bucket"], IsNil) + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["impersonate_service_account"], IsNil) + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["prefix"], IsNil) cliBEType := "gcs" cliBEBucket := "a_bucket" @@ -416,23 +416,23 @@ func (s *MySuite) TestSetBackendConfig(c *C) { fmt.Sprintf("impersonate_service_account=%s", cliBESA), fmt.Sprintf("prefix=%s", cliBEPrefix), } - err := bc.SetBackendConfig(cliBEConfigVars) + err := dc.SetBackendConfig(cliBEConfigVars) c.Assert(err, IsNil) - c.Assert(bc.Config.TerraformBackendDefaults.Type, Equals, cliBEType) - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["bucket"], Equals, cliBEBucket) - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["impersonate_service_account"], Equals, cliBESA) - c.Assert(bc.Config.TerraformBackendDefaults.Configuration["prefix"], Equals, cliBEPrefix) + c.Assert(dc.Config.TerraformBackendDefaults.Type, Equals, cliBEType) + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["bucket"], Equals, cliBEBucket) + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["impersonate_service_account"], Equals, cliBESA) + c.Assert(dc.Config.TerraformBackendDefaults.Configuration["prefix"], Equals, cliBEPrefix) // Failure: Variable without '=' - bc = getBlueprintConfigForTest() - c.Assert(bc.Config.TerraformBackendDefaults.Type, Equals, "") + dc = getDeploymentConfigForTest() + c.Assert(dc.Config.TerraformBackendDefaults.Type, Equals, "") invalidNonEQVars := []string{ fmt.Sprintf("type%s", cliBEType), fmt.Sprintf("bucket%s", cliBEBucket), } - err = bc.SetBackendConfig(invalidNonEQVars) + err = dc.SetBackendConfig(invalidNonEQVars) expErr := "invalid format: .*" c.Assert(err, ErrorMatches, expErr) @@ -448,16 +448,16 @@ func TestMain(m *testing.M) { func (s *MySuite) TestValidationLevels(c *C) { var err error var ok bool - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() validLevels := []string{"ERROR", "WARNING", "IGNORE"} for idx, level := range validLevels { - err = bc.SetValidationLevel(level) + err = dc.SetValidationLevel(level) c.Assert(err, IsNil) ok = isValidValidationLevel(idx) c.Assert(ok, Equals, true) } - err = bc.SetValidationLevel("INVALID") + err = dc.SetValidationLevel("INVALID") c.Assert(err, NotNil) // check that our test for iota enum is working @@ -549,29 +549,29 @@ func (s *MySuite) TestResolveGlobalVariables(c *C) { var testkey1 = "testkey1" var testkey2 = "testkey2" var testkey3 = "testkey3" - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() ctyMap := make(map[string]cty.Value) - err = bc.Config.ResolveGlobalVariables(ctyMap) + err = dc.Config.ResolveGlobalVariables(ctyMap) c.Assert(err, IsNil) // confirm plain string is unchanged and does not error testCtyString := cty.StringVal("testval") ctyMap[testkey1] = testCtyString - err = bc.Config.ResolveGlobalVariables(ctyMap) + err = dc.Config.ResolveGlobalVariables(ctyMap) c.Assert(err, IsNil) c.Assert(ctyMap[testkey1], Equals, testCtyString) // confirm literal, non-global, variable is unchanged and does not error testCtyString = cty.StringVal("((module.testval))") ctyMap[testkey1] = testCtyString - err = bc.Config.ResolveGlobalVariables(ctyMap) + err = dc.Config.ResolveGlobalVariables(ctyMap) c.Assert(err, IsNil) c.Assert(ctyMap[testkey1], Equals, testCtyString) // confirm failed resolution of a literal global testCtyString = cty.StringVal("((var.test_global_var))") ctyMap[testkey1] = testCtyString - err = bc.Config.ResolveGlobalVariables(ctyMap) + err = dc.Config.ResolveGlobalVariables(ctyMap) c.Assert(err, NotNil) c.Assert(err.Error(), Matches, ".*Unsupported attribute;.*") @@ -581,14 +581,14 @@ func (s *MySuite) TestResolveGlobalVariables(c *C) { testGlobalVarBool := "test_global_bool" testGlobalValBool := "testval" testPlainString := "plain-string" - bc.Config.Vars[testGlobalVarString] = testGlobalValString - bc.Config.Vars[testGlobalVarBool] = testGlobalValBool + dc.Config.Vars[testGlobalVarString] = testGlobalValString + dc.Config.Vars[testGlobalVarBool] = testGlobalValBool testCtyString = cty.StringVal(fmt.Sprintf("((var.%s))", testGlobalVarString)) testCtyBool := cty.StringVal(fmt.Sprintf("((var.%s))", testGlobalVarBool)) ctyMap[testkey1] = testCtyString ctyMap[testkey2] = testCtyBool ctyMap[testkey3] = cty.StringVal(testPlainString) - err = bc.Config.ResolveGlobalVariables(ctyMap) + err = dc.Config.ResolveGlobalVariables(ctyMap) c.Assert(err, IsNil) c.Assert(ctyMap[testkey1], Equals, cty.StringVal(testGlobalValString)) c.Assert(ctyMap[testkey2], Equals, cty.StringVal(testGlobalValBool)) diff --git a/pkg/config/expand.go b/pkg/config/expand.go index f9f834bc9d..b358c7c581 100644 --- a/pkg/config/expand.go +++ b/pkg/config/expand.go @@ -42,54 +42,54 @@ const ( // expand expands variables and strings in the yaml config. Used directly by // ExpandConfig for the create and expand commands. -func (bc *BlueprintConfig) expand() { - bc.addSettingsToModules() - if err := bc.expandBackends(); err != nil { +func (dc *DeploymentConfig) expand() { + dc.addSettingsToModules() + if err := dc.expandBackends(); err != nil { log.Fatalf("failed to apply default backend to deployment groups: %v", err) } - if err := bc.addDefaultValidators(); err != nil { + if err := dc.addDefaultValidators(); err != nil { log.Fatalf( "failed to update validators when expanding the config: %v", err) } - if err := bc.combineLabels(); err != nil { + if err := dc.combineLabels(); err != nil { log.Fatalf( "failed to update module labels when expanding the config: %v", err) } - if err := bc.applyUseModules(); err != nil { + if err := dc.applyUseModules(); err != nil { log.Fatalf( "failed to apply \"use\" modules when expanding the config: %v", err) } - if err := bc.applyGlobalVariables(); err != nil { + if err := dc.applyGlobalVariables(); err != nil { log.Fatalf( "failed to apply global variables in modules when expanding the config: %v", err) } - bc.expandVariables() + dc.expandVariables() } -func (bc *BlueprintConfig) addSettingsToModules() { - for iGrp, grp := range bc.Config.DeploymentGroups { +func (dc *DeploymentConfig) addSettingsToModules() { + for iGrp, grp := range dc.Config.DeploymentGroups { for iMod, mod := range grp.Modules { if mod.Settings == nil { - bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings = + dc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings = make(map[string]interface{}) } } } } -func (bc *BlueprintConfig) expandBackends() error { +func (dc *DeploymentConfig) expandBackends() error { // 1. DEFAULT: use TerraformBackend configuration (if supplied) in each // resource group // 2. If top-level TerraformBackendDefaults is defined, insert that // backend into resource groups which have no explicit // TerraformBackend // 3. In all cases, add a prefix for GCS backends if one is not defined - yamlConfig := &bc.Config + yamlConfig := &dc.Config if yamlConfig.TerraformBackendDefaults.Type != "" { for i := range yamlConfig.DeploymentGroups { grp := &yamlConfig.DeploymentGroups[i] @@ -169,17 +169,17 @@ func useModule( // applyUseModules applies variables from modules listed in the "use" field // when/if applicable -func (bc *BlueprintConfig) applyUseModules() error { - for iGrp := range bc.Config.DeploymentGroups { - group := &bc.Config.DeploymentGroups[iGrp] +func (dc *DeploymentConfig) applyUseModules() error { + for iGrp := range dc.Config.DeploymentGroups { + group := &dc.Config.DeploymentGroups[iGrp] for iMod := range group.Modules { mod := &group.Modules[iMod] - modInfo := bc.ModulesInfo[group.Name][mod.Source] + modInfo := dc.ModulesInfo[group.Name][mod.Source] modInputs := getModuleInputMap(modInfo.Inputs) changedSettings := make(map[string]bool) for _, useModID := range mod.Use { useMod := group.getModuleByID(useModID) - useInfo := bc.ModulesInfo[group.Name][useMod.Source] + useInfo := dc.ModulesInfo[group.Name][useMod.Source] if useMod.ID == "" { return fmt.Errorf("could not find module %s used by %s in group %s", useModID, mod.ID, group.Name) @@ -191,9 +191,9 @@ func (bc *BlueprintConfig) applyUseModules() error { return nil } -func (bc BlueprintConfig) moduleHasInput( +func (dc DeploymentConfig) moduleHasInput( depGroup string, source string, inputName string) bool { - for _, input := range bc.ModulesInfo[depGroup][source].Inputs { + for _, input := range dc.ModulesInfo[depGroup][source].Inputs { if input.Name == inputName { return true } @@ -244,26 +244,26 @@ func toStringInterfaceMap(i interface{}) (map[string]interface{}, error) { // combineLabels sets defaults for labels based on other variables and merges // the global labels defined in Vars with module setting labels. It also // determines the role and sets it for each module independently. -func (bc *BlueprintConfig) combineLabels() error { +func (dc *DeploymentConfig) combineLabels() error { defaultLabels := map[string]interface{}{ - blueprintLabel: bc.Config.BlueprintName, - deploymentLabel: getDeploymentName(bc.Config.Vars), + blueprintLabel: dc.Config.BlueprintName, + deploymentLabel: getDeploymentName(dc.Config.Vars), } labels := "labels" var globalLabels map[string]interface{} // Add defaults to global labels if they don't already exist - if _, exists := bc.Config.Vars[labels]; !exists { - bc.Config.Vars[labels] = defaultLabels + if _, exists := dc.Config.Vars[labels]; !exists { + dc.Config.Vars[labels] = defaultLabels } // Cast global labels so we can index into them - globalLabels, err := toStringInterfaceMap(bc.Config.Vars[labels]) + globalLabels, err := toStringInterfaceMap(dc.Config.Vars[labels]) if err != nil { return fmt.Errorf( "%s: found %T", errorMessages["globalLabelType"], - bc.Config.Vars[labels]) + dc.Config.Vars[labels]) } // Add both default labels if they don't already exist @@ -274,10 +274,10 @@ func (bc *BlueprintConfig) combineLabels() error { globalLabels[deploymentLabel] = defaultLabels[deploymentLabel] } - for iGrp, grp := range bc.Config.DeploymentGroups { + for iGrp, grp := range dc.Config.DeploymentGroups { for iMod, mod := range grp.Modules { // Check if labels are set for this module - if !bc.moduleHasInput(grp.Name, mod.Source, labels) { + if !dc.moduleHasInput(grp.Name, mod.Source, labels) { continue } @@ -300,11 +300,11 @@ func (bc *BlueprintConfig) combineLabels() error { if _, exists := modLabels[roleLabel]; !exists { modLabels[roleLabel] = getRole(mod.Source) } - bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings[labels] = + dc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings[labels] = modLabels } } - bc.Config.Vars[labels] = globalLabels + dc.Config.Vars[labels] = globalLabels return nil } @@ -351,15 +351,15 @@ func updateGlobalVarTypes(vars map[string]interface{}) error { // applyGlobalVariables takes any variables defined at the global level and // applies them to module settings if not already set. -func (bc *BlueprintConfig) applyGlobalVariables() error { +func (dc *DeploymentConfig) applyGlobalVariables() error { // Update global variable types to match - if err := updateGlobalVarTypes(bc.Config.Vars); err != nil { + if err := updateGlobalVarTypes(dc.Config.Vars); err != nil { return err } - for _, grp := range bc.Config.DeploymentGroups { + for _, grp := range dc.Config.DeploymentGroups { err := applyGlobalVarsInGroup( - grp, bc.ModulesInfo[grp.Name], bc.Config.Vars) + grp, dc.ModulesInfo[grp.Name], dc.Config.Vars) if err != nil { return err } @@ -555,25 +555,25 @@ func updateVariables( // expandVariables recurses through the data structures in the yaml config and // expands all variables -func (bc *BlueprintConfig) expandVariables() { - for _, validator := range bc.Config.Validators { - err := updateVariables(varContext{yamlConfig: bc.Config}, validator.Inputs, make(map[string]int)) +func (dc *DeploymentConfig) expandVariables() { + for _, validator := range dc.Config.Validators { + err := updateVariables(varContext{yamlConfig: dc.Config}, validator.Inputs, make(map[string]int)) if err != nil { log.Fatalf("expandVariables: %v", err) } } - for iGrp, grp := range bc.Config.DeploymentGroups { + for iGrp, grp := range dc.Config.DeploymentGroups { for iMod := range grp.Modules { context := varContext{ groupIndex: iGrp, modIndex: iMod, - yamlConfig: bc.Config, + yamlConfig: dc.Config, } err := updateVariables( context, - bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings, - bc.ModuleToGroup) + dc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings, + dc.ModuleToGroup) if err != nil { log.Fatalf("expandVariables: %v", err) } @@ -583,15 +583,15 @@ func (bc *BlueprintConfig) expandVariables() { // this function adds default validators to the blueprint if none have been // defined. default validators are only added for global variables that exist -func (bc *BlueprintConfig) addDefaultValidators() error { - if bc.Config.Validators != nil { +func (dc *DeploymentConfig) addDefaultValidators() error { + if dc.Config.Validators != nil { return nil } - bc.Config.Validators = []validatorConfig{} + dc.Config.Validators = []validatorConfig{} - _, projectIDExists := bc.Config.Vars["project_id"] - _, regionExists := bc.Config.Vars["region"] - _, zoneExists := bc.Config.Vars["zone"] + _, projectIDExists := dc.Config.Vars["project_id"] + _, regionExists := dc.Config.Vars["region"] + _, zoneExists := dc.Config.Vars["zone"] if projectIDExists { v := validatorConfig{ @@ -600,7 +600,7 @@ func (bc *BlueprintConfig) addDefaultValidators() error { "project_id": "$(vars.project_id)", }, } - bc.Config.Validators = append(bc.Config.Validators, v) + dc.Config.Validators = append(dc.Config.Validators, v) } if projectIDExists && regionExists { @@ -611,7 +611,7 @@ func (bc *BlueprintConfig) addDefaultValidators() error { "region": "$(vars.region)", }, } - bc.Config.Validators = append(bc.Config.Validators, v) + dc.Config.Validators = append(dc.Config.Validators, v) } @@ -623,7 +623,7 @@ func (bc *BlueprintConfig) addDefaultValidators() error { "zone": "$(vars.zone)", }, } - bc.Config.Validators = append(bc.Config.Validators, v) + dc.Config.Validators = append(dc.Config.Validators, v) } if projectIDExists && regionExists && zoneExists { @@ -635,7 +635,7 @@ func (bc *BlueprintConfig) addDefaultValidators() error { "zone": "$(vars.zone)", }, } - bc.Config.Validators = append(bc.Config.Validators, v) + dc.Config.Validators = append(dc.Config.Validators, v) } return nil } diff --git a/pkg/config/expand_test.go b/pkg/config/expand_test.go index 2040185b94..2ef29e25e8 100644 --- a/pkg/config/expand_test.go +++ b/pkg/config/expand_test.go @@ -22,43 +22,43 @@ import ( ) func (s *MySuite) TestExpand(c *C) { - bc := getBlueprintConfigForTest() - bc.expand() + dc := getDeploymentConfigForTest() + dc.expand() } func (s *MySuite) TestExpandBackends(c *C) { - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() // Simple test: Does Nothing - err := bc.expandBackends() + err := dc.expandBackends() c.Assert(err, IsNil) tfBackend := &TerraformBackend{ Type: "gcs", Configuration: make(map[string]interface{}), } - bc.Config.TerraformBackendDefaults = *tfBackend - err = bc.expandBackends() + dc.Config.TerraformBackendDefaults = *tfBackend + err = dc.expandBackends() c.Assert(err, IsNil) - grp := bc.Config.DeploymentGroups[0] + grp := dc.Config.DeploymentGroups[0] c.Assert(grp.TerraformBackend.Type, Not(Equals), "") gotPrefix := grp.TerraformBackend.Configuration["prefix"] - expPrefix := fmt.Sprintf("%s/%s", bc.Config.BlueprintName, grp.Name) + expPrefix := fmt.Sprintf("%s/%s", dc.Config.BlueprintName, grp.Name) c.Assert(gotPrefix, Equals, expPrefix) // Add a new resource group, ensure each group name is included newGroup := DeploymentGroup{ Name: "group2", } - bc.Config.DeploymentGroups = append(bc.Config.DeploymentGroups, newGroup) - bc.Config.Vars["deployment_name"] = "testDeployment" - err = bc.expandBackends() + dc.Config.DeploymentGroups = append(dc.Config.DeploymentGroups, newGroup) + dc.Config.Vars["deployment_name"] = "testDeployment" + err = dc.expandBackends() c.Assert(err, IsNil) - newGrp := bc.Config.DeploymentGroups[1] + newGrp := dc.Config.DeploymentGroups[1] c.Assert(newGrp.TerraformBackend.Type, Not(Equals), "") gotPrefix = newGrp.TerraformBackend.Configuration["prefix"] - expPrefix = fmt.Sprintf("%s/%s/%s", bc.Config.BlueprintName, - bc.Config.Vars["deployment_name"], newGrp.Name) + expPrefix = fmt.Sprintf("%s/%s/%s", dc.Config.BlueprintName, + dc.Config.Vars["deployment_name"], newGrp.Name) c.Assert(gotPrefix, Equals, expPrefix) } @@ -166,28 +166,28 @@ func (s *MySuite) TestApplyUseModules(c *C) { } // Simple Case - bc := getBlueprintConfigForTest() - err := bc.applyUseModules() + dc := getDeploymentConfigForTest() + err := dc.applyUseModules() c.Assert(err, IsNil) // Has Use Modules - bc.Config.DeploymentGroups[0].Modules = append( - bc.Config.DeploymentGroups[0].Modules, usingModule) - bc.Config.DeploymentGroups[0].Modules = append( - bc.Config.DeploymentGroups[0].Modules, usedModule) - - grpName := bc.Config.DeploymentGroups[0].Name - usingInfo := bc.ModulesInfo[grpName][usingModuleSource] - usedInfo := bc.ModulesInfo[grpName][usedModuleSource] + dc.Config.DeploymentGroups[0].Modules = append( + dc.Config.DeploymentGroups[0].Modules, usingModule) + dc.Config.DeploymentGroups[0].Modules = append( + dc.Config.DeploymentGroups[0].Modules, usedModule) + + grpName := dc.Config.DeploymentGroups[0].Name + usingInfo := dc.ModulesInfo[grpName][usingModuleSource] + usedInfo := dc.ModulesInfo[grpName][usedModuleSource] usingInfo.Inputs = []resreader.VarInfo{sharedVar} usedInfo.Outputs = []resreader.VarInfo{sharedVar} - err = bc.applyUseModules() + err = dc.applyUseModules() c.Assert(err, IsNil) // Use ID doesn't exists (fail) - modLen := len(bc.Config.DeploymentGroups[0].Modules) - bc.Config.DeploymentGroups[0].Modules[modLen-1].ID = "wrongID" - err = bc.applyUseModules() + modLen := len(dc.Config.DeploymentGroups[0].Modules) + dc.Config.DeploymentGroups[0].Modules[modLen-1].ID = "wrongID" + err = dc.applyUseModules() c.Assert(err, ErrorMatches, "could not find module .* used by .* in group .*") } @@ -246,20 +246,20 @@ func (s *MySuite) TestUpdateVariableType(c *C) { } func (s *MySuite) TestCombineLabels(c *C) { - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() - err := bc.combineLabels() + err := dc.combineLabels() c.Assert(err, IsNil) // Were global labels created? - _, exists := bc.Config.Vars["labels"] + _, exists := dc.Config.Vars["labels"] c.Assert(exists, Equals, true) // Was the ghpc_blueprint label set correctly? - globalLabels := bc.Config.Vars["labels"].(map[string]interface{}) + globalLabels := dc.Config.Vars["labels"].(map[string]interface{}) ghpcBlueprint, exists := globalLabels[blueprintLabel] c.Assert(exists, Equals, true) - c.Assert(ghpcBlueprint, Equals, bc.Config.BlueprintName) + c.Assert(ghpcBlueprint, Equals, dc.Config.BlueprintName) // Was the ghpc_deployment label set correctly? ghpcDeployment, exists := globalLabels[deploymentLabel] @@ -267,10 +267,10 @@ func (s *MySuite) TestCombineLabels(c *C) { c.Assert(ghpcDeployment, Equals, "undefined") // Was "labels" created for the module with no settings? - _, exists = bc.Config.DeploymentGroups[0].Modules[0].Settings["labels"] + _, exists = dc.Config.DeploymentGroups[0].Modules[0].Settings["labels"] c.Assert(exists, Equals, true) - moduleLabels := bc.Config.DeploymentGroups[0].Modules[0]. + moduleLabels := dc.Config.DeploymentGroups[0].Modules[0]. Settings["labels"].(map[interface{}]interface{}) // Was the role created correctly? @@ -279,47 +279,47 @@ func (s *MySuite) TestCombineLabels(c *C) { c.Assert(ghpcRole, Equals, "other") // Test invalid labels - bc.Config.Vars["labels"] = "notAMap" - err = bc.combineLabels() + dc.Config.Vars["labels"] = "notAMap" + err = dc.combineLabels() expectedErrorStr := fmt.Sprintf("%s: found %T", - errorMessages["globalLabelType"], bc.Config.Vars["labels"]) + errorMessages["globalLabelType"], dc.Config.Vars["labels"]) c.Assert(err, ErrorMatches, expectedErrorStr) } func (s *MySuite) TestApplyGlobalVariables(c *C) { - bc := getBlueprintConfigForTest() - testModule := bc.Config.DeploymentGroups[0].Modules[0] + dc := getDeploymentConfigForTest() + testModule := dc.Config.DeploymentGroups[0].Modules[0] // Test no inputs, none required - err := bc.applyGlobalVariables() + err := dc.applyGlobalVariables() c.Assert(err, IsNil) // Test no inputs, one required, doesn't exist in globals - bc.ModulesInfo["group1"][testModule.Source] = resreader.ModuleInfo{ + dc.ModulesInfo["group1"][testModule.Source] = resreader.ModuleInfo{ Inputs: []resreader.VarInfo{requiredVar}, } - err = bc.applyGlobalVariables() + err = dc.applyGlobalVariables() expectedErrorStr := fmt.Sprintf("%s: Module ID: %s Setting: %s", errorMessages["missingSetting"], testModule.ID, requiredVar.Name) c.Assert(err, ErrorMatches, expectedErrorStr) // Test no input, one required, exists in globals - bc.Config.Vars[requiredVar.Name] = "val" - err = bc.applyGlobalVariables() + dc.Config.Vars[requiredVar.Name] = "val" + err = dc.applyGlobalVariables() c.Assert(err, IsNil) c.Assert( - bc.Config.DeploymentGroups[0].Modules[0].Settings[requiredVar.Name], + dc.Config.DeploymentGroups[0].Modules[0].Settings[requiredVar.Name], Equals, fmt.Sprintf("((var.%s))", requiredVar.Name)) // Test one input, one required - bc.Config.DeploymentGroups[0].Modules[0].Settings[requiredVar.Name] = "val" - err = bc.applyGlobalVariables() + dc.Config.DeploymentGroups[0].Modules[0].Settings[requiredVar.Name] = "val" + err = dc.applyGlobalVariables() c.Assert(err, IsNil) // Test one input, none required, exists in globals - bc.ModulesInfo["group1"][testModule.Source].Inputs[0].Required = false - err = bc.applyGlobalVariables() + dc.ModulesInfo["group1"][testModule.Source].Inputs[0].Required = false + err = dc.applyGlobalVariables() c.Assert(err, IsNil) } diff --git a/pkg/config/validate.go b/pkg/config/validate.go index f04abf9b59..6bc36969ae 100644 --- a/pkg/config/validate.go +++ b/pkg/config/validate.go @@ -35,39 +35,39 @@ const ( ) // validate is the top-level function for running the validation suite. -func (bc BlueprintConfig) validate() { - if err := bc.validateVars(); err != nil { +func (dc DeploymentConfig) validate() { + if err := dc.validateVars(); err != nil { log.Fatal(err) } // variables should be validated before running validators - if err := bc.executeValidators(); err != nil { + if err := dc.executeValidators(); err != nil { log.Fatal(err) } - if err := bc.validateModules(); err != nil { + if err := dc.validateModules(); err != nil { log.Fatal(err) } - if err := bc.validateModuleSettings(); err != nil { + if err := dc.validateModuleSettings(); err != nil { log.Fatal(err) } } // performs validation of global variables -func (bc BlueprintConfig) executeValidators() error { +func (dc DeploymentConfig) executeValidators() error { var errored, warned bool - implementedValidators := bc.getValidators() + implementedValidators := dc.getValidators() - if bc.Config.ValidationLevel == validationIgnore { + if dc.Config.ValidationLevel == validationIgnore { return nil } - for _, validator := range bc.Config.Validators { + for _, validator := range dc.Config.Validators { if f, ok := implementedValidators[validator.Validator]; ok { err := f(validator) if err != nil { var prefix string - switch bc.Config.ValidationLevel { + switch dc.Config.ValidationLevel { case validationWarning: warned = true prefix = "warning: " @@ -108,8 +108,8 @@ func (bc BlueprintConfig) executeValidators() error { } // validateVars checks the global variables for viable types -func (bc BlueprintConfig) validateVars() error { - vars := bc.Config.Vars +func (dc DeploymentConfig) validateVars() error { + vars := dc.Config.Vars nilErr := "global variable %s was not set" // Check for project_id @@ -175,13 +175,13 @@ func validateOutputs(mod Module, modInfo resreader.ModuleInfo) error { } // validateModules ensures parameters set in modules are set correctly. -func (bc BlueprintConfig) validateModules() error { - for _, grp := range bc.Config.DeploymentGroups { +func (dc DeploymentConfig) validateModules() error { + for _, grp := range dc.Config.DeploymentGroups { for _, mod := range grp.Modules { if err := validateModule(mod); err != nil { return err } - modInfo := bc.ModulesInfo[grp.Name][mod.Source] + modInfo := dc.ModulesInfo[grp.Name][mod.Source] if err := validateOutputs(mod, modInfo); err != nil { return err } @@ -219,8 +219,8 @@ func validateSettings( // validateModuleSettings verifies that no additional settings are provided // that don't have a counterpart variable in the module -func (bc BlueprintConfig) validateModuleSettings() error { - for _, grp := range bc.Config.DeploymentGroups { +func (dc DeploymentConfig) validateModuleSettings() error { + for _, grp := range dc.Config.DeploymentGroups { for _, mod := range grp.Modules { reader := sourcereader.Factory(mod.Source) info, err := reader.GetModuleInfo(mod.Source, mod.Kind) @@ -237,12 +237,12 @@ func (bc BlueprintConfig) validateModuleSettings() error { return nil } -func (bc *BlueprintConfig) getValidators() map[string]func(validatorConfig) error { +func (dc *DeploymentConfig) getValidators() map[string]func(validatorConfig) error { allValidators := map[string]func(validatorConfig) error{ - testProjectExistsName.String(): bc.testProjectExists, - testRegionExistsName.String(): bc.testRegionExists, - testZoneExistsName.String(): bc.testZoneExists, - testZoneInRegionName.String(): bc.testZoneInRegion, + testProjectExistsName.String(): dc.testProjectExists, + testRegionExistsName.String(): dc.testRegionExists, + testZoneExistsName.String(): dc.testZoneExists, + testZoneInRegionName.String(): dc.testZoneInRegion, } return allValidators } @@ -270,7 +270,7 @@ func testInputList(function string, inputs map[string]interface{}, requiredInput return nil } -func (bc *BlueprintConfig) testProjectExists(validator validatorConfig) error { +func (dc *DeploymentConfig) testProjectExists(validator validatorConfig) error { requiredInputs := []string{"project_id"} funcName := testProjectExistsName.String() funcErrorMsg := fmt.Sprintf("validator %s failed", funcName) @@ -285,7 +285,7 @@ func (bc *BlueprintConfig) testProjectExists(validator validatorConfig) error { return err } - projectID, err := bc.getStringValue(validator.Inputs["project_id"]) + projectID, err := dc.getStringValue(validator.Inputs["project_id"]) if err != nil { log.Print(funcErrorMsg) return err @@ -299,7 +299,7 @@ func (bc *BlueprintConfig) testProjectExists(validator validatorConfig) error { return err } -func (bc *BlueprintConfig) testRegionExists(validator validatorConfig) error { +func (dc *DeploymentConfig) testRegionExists(validator validatorConfig) error { requiredInputs := []string{"project_id", "region"} funcName := testRegionExistsName.String() funcErrorMsg := fmt.Sprintf("validator %s failed", funcName) @@ -313,12 +313,12 @@ func (bc *BlueprintConfig) testRegionExists(validator validatorConfig) error { return err } - projectID, err := bc.getStringValue(validator.Inputs["project_id"]) + projectID, err := dc.getStringValue(validator.Inputs["project_id"]) if err != nil { log.Print(funcErrorMsg) return err } - region, err := bc.getStringValue(validator.Inputs["region"]) + region, err := dc.getStringValue(validator.Inputs["region"]) if err != nil { log.Print(funcErrorMsg) return err @@ -332,7 +332,7 @@ func (bc *BlueprintConfig) testRegionExists(validator validatorConfig) error { return err } -func (bc *BlueprintConfig) testZoneExists(validator validatorConfig) error { +func (dc *DeploymentConfig) testZoneExists(validator validatorConfig) error { requiredInputs := []string{"project_id", "zone"} funcName := testZoneExistsName.String() funcErrorMsg := fmt.Sprintf("validator %s failed", funcName) @@ -346,12 +346,12 @@ func (bc *BlueprintConfig) testZoneExists(validator validatorConfig) error { return err } - projectID, err := bc.getStringValue(validator.Inputs["project_id"]) + projectID, err := dc.getStringValue(validator.Inputs["project_id"]) if err != nil { log.Print(funcErrorMsg) return err } - zone, err := bc.getStringValue(validator.Inputs["zone"]) + zone, err := dc.getStringValue(validator.Inputs["zone"]) if err != nil { log.Print(funcErrorMsg) return err @@ -365,7 +365,7 @@ func (bc *BlueprintConfig) testZoneExists(validator validatorConfig) error { return err } -func (bc *BlueprintConfig) testZoneInRegion(validator validatorConfig) error { +func (dc *DeploymentConfig) testZoneInRegion(validator validatorConfig) error { requiredInputs := []string{"project_id", "region", "zone"} funcName := testZoneInRegionName.String() funcErrorMsg := fmt.Sprintf("validator %s failed", funcName) @@ -379,17 +379,17 @@ func (bc *BlueprintConfig) testZoneInRegion(validator validatorConfig) error { return err } - projectID, err := bc.getStringValue(validator.Inputs["project_id"]) + projectID, err := dc.getStringValue(validator.Inputs["project_id"]) if err != nil { log.Print(funcErrorMsg) return err } - zone, err := bc.getStringValue(validator.Inputs["zone"]) + zone, err := dc.getStringValue(validator.Inputs["zone"]) if err != nil { log.Print(funcErrorMsg) return err } - region, err := bc.getStringValue(validator.Inputs["region"]) + region, err := dc.getStringValue(validator.Inputs["region"]) if err != nil { log.Print(funcErrorMsg) return err @@ -407,7 +407,7 @@ func (bc *BlueprintConfig) testZoneInRegion(validator validatorConfig) error { // variable inputReference in form ((var.project_id)) // if it is a literal global variable defined as a string, return value as string // in all other cases, return empty string and error -func (bc *BlueprintConfig) getStringValue(inputReference interface{}) (string, error) { +func (dc *DeploymentConfig) getStringValue(inputReference interface{}) (string, error) { varRef, ok := inputReference.(string) if !ok { return "", fmt.Errorf("the value %s cannot be cast to a string", inputReference) @@ -422,7 +422,7 @@ func (bc *BlueprintConfig) getStringValue(inputReference interface{}) (string, e // checked for existence. handle if user has explicitly passed // ((var.does_not_exit)) or ((not_a_varsrc.not_a_var)) if varSrc == "var" { - if val, ok := bc.Config.Vars[varName]; ok { + if val, ok := dc.Config.Vars[varName]; ok { valString, ok := val.(string) if ok { return valString, nil diff --git a/pkg/config/validator_test.go b/pkg/config/validator_test.go index 307f173548..cf2d1e836e 100644 --- a/pkg/config/validator_test.go +++ b/pkg/config/validator_test.go @@ -34,34 +34,34 @@ const ( ) func (s *MySuite) TestValidateModules(c *C) { - bc := getBlueprintConfigForTest() - bc.validateModules() + dc := getDeploymentConfigForTest() + dc.validateModules() } func (s *MySuite) TestValidateVars(c *C) { // Success - bc := getBlueprintConfigForTest() - err := bc.validateVars() + dc := getDeploymentConfigForTest() + err := dc.validateVars() c.Assert(err, IsNil) // Fail: Nil project_id - bc.Config.Vars["project_id"] = nil - err = bc.validateVars() + dc.Config.Vars["project_id"] = nil + err = dc.validateVars() c.Assert(err, ErrorMatches, "global variable project_id was not set") // Success: project_id not set - delete(bc.Config.Vars, "project_id") + delete(dc.Config.Vars, "project_id") var buf bytes.Buffer log.SetOutput(&buf) - err = bc.validateVars() + err = dc.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() + dc.Config.Vars["labels"] = "a_string" + err = dc.validateVars() c.Assert(err, ErrorMatches, "vars.labels must be a map") } @@ -75,13 +75,13 @@ func (s *MySuite) TestValidateModuleSettings(c *C) { TerraformBackend: TerraformBackend{}, Modules: []Module{{Kind: "terraform", Source: testSource, Settings: testSettings}}, } - bc := BlueprintConfig{ + dc := DeploymentConfig{ Config: YamlConfig{DeploymentGroups: []DeploymentGroup{testDeploymentGroup}}, ModulesInfo: map[string]map[string]resreader.ModuleInfo{}, ModuleToGroup: map[string]int{}, expanded: false, } - bc.validateModuleSettings() + dc.validateModuleSettings() } func (s *MySuite) TestValidateSettings(c *C) { @@ -168,24 +168,24 @@ func (s *MySuite) TestValidateOutputs(c *C) { } func (s *MySuite) TestAddDefaultValidators(c *C) { - bc := getBlueprintConfigForTest() - bc.addDefaultValidators() - c.Assert(bc.Config.Validators, HasLen, 0) - - bc.Config.Validators = nil - bc.Config.Vars["project_id"] = "not-a-project" - bc.addDefaultValidators() - c.Assert(bc.Config.Validators, HasLen, 1) - - bc.Config.Validators = nil - bc.Config.Vars["region"] = "us-central1" - bc.addDefaultValidators() - c.Assert(bc.Config.Validators, HasLen, 2) - - bc.Config.Validators = nil - bc.Config.Vars["zone"] = "us-central1-c" - bc.addDefaultValidators() - c.Assert(bc.Config.Validators, HasLen, 4) + dc := getDeploymentConfigForTest() + dc.addDefaultValidators() + c.Assert(dc.Config.Validators, HasLen, 0) + + dc.Config.Validators = nil + dc.Config.Vars["project_id"] = "not-a-project" + dc.addDefaultValidators() + c.Assert(dc.Config.Validators, HasLen, 1) + + dc.Config.Validators = nil + dc.Config.Vars["region"] = "us-central1" + dc.addDefaultValidators() + c.Assert(dc.Config.Validators, HasLen, 2) + + dc.Config.Validators = nil + dc.Config.Vars["zone"] = "us-central1-c" + dc.addDefaultValidators() + c.Assert(dc.Config.Validators, HasLen, 4) } func (s *MySuite) TestTestInputList(c *C) { @@ -234,49 +234,49 @@ func (s *MySuite) TestTestInputList(c *C) { // if it is a literal global variable defined as a string, return value as string // in all other cases, return empty string and error func (s *MySuite) TestGetStringValue(c *C) { - bc := getBlueprintConfigForTest() - bc.Config.Vars["goodvar"] = "testval" - bc.Config.Vars["badvar"] = 2 + dc := getDeploymentConfigForTest() + dc.Config.Vars["goodvar"] = "testval" + dc.Config.Vars["badvar"] = 2 // test non-string values return error - _, err := bc.getStringValue(2) + _, err := dc.getStringValue(2) c.Assert(err, Not(IsNil)) // test strings that are not literal variables return error and empty string - strVal, err := bc.getStringValue("hello") + strVal, err := dc.getStringValue("hello") c.Assert(err, Not(IsNil)) c.Assert(strVal, Equals, "") // test literal variables that refer to strings return their value - strVal, err = bc.getStringValue("(( var.goodvar ))") + strVal, err = dc.getStringValue("(( var.goodvar ))") c.Assert(err, IsNil) - c.Assert(strVal, Equals, bc.Config.Vars["goodvar"]) + c.Assert(strVal, Equals, dc.Config.Vars["goodvar"]) // test literal variables that refer to non-strings return error - _, err = bc.getStringValue("(( var.badvar ))") + _, err = dc.getStringValue("(( var.badvar ))") c.Assert(err, Not(IsNil)) } func (s *MySuite) TestExecuteValidators(c *C) { - bc := getBlueprintConfigForTest() - bc.Config.Validators = []validatorConfig{ + dc := getDeploymentConfigForTest() + dc.Config.Validators = []validatorConfig{ { Validator: "unimplemented-validator", Inputs: map[string]interface{}{}, }, } - err := bc.executeValidators() + err := dc.executeValidators() c.Assert(err, ErrorMatches, validationErrorMsg) - bc.Config.Validators = []validatorConfig{ + dc.Config.Validators = []validatorConfig{ { Validator: testProjectExistsName.String(), Inputs: map[string]interface{}{}, }, } - err = bc.executeValidators() + err = dc.executeValidators() c.Assert(err, ErrorMatches, validationErrorMsg) } @@ -286,11 +286,11 @@ func (s *MySuite) TestExecuteValidators(c *C) { // development of mock functions for Cloud API calls func (s *MySuite) TestProjectExistsValidator(c *C) { var err error - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() emptyValidator := validatorConfig{} // test validator fails for config without validator id - err = bc.testProjectExists(emptyValidator) + err = dc.testProjectExists(emptyValidator) c.Assert(err, ErrorMatches, passedWrongValidatorRegex) // test validator fails for config without any inputs @@ -298,12 +298,12 @@ func (s *MySuite) TestProjectExistsValidator(c *C) { Validator: testProjectExistsName.String(), Inputs: map[string]interface{}{}, } - err = bc.testProjectExists(projectValidator) + err = dc.testProjectExists(projectValidator) c.Assert(err, ErrorMatches, missingRequiredInputRegex) // test validators fail when input global variables are undefined projectValidator.Inputs["project_id"] = "((var.project_id))" - err = bc.testProjectExists(projectValidator) + err = dc.testProjectExists(projectValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) // TODO: implement a mock client to test success of test_project_exists @@ -311,11 +311,11 @@ func (s *MySuite) TestProjectExistsValidator(c *C) { func (s *MySuite) TestRegionExistsValidator(c *C) { var err error - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() emptyValidator := validatorConfig{} // test validator fails for config without validator id - err = bc.testRegionExists(emptyValidator) + err = dc.testRegionExists(emptyValidator) c.Assert(err, ErrorMatches, passedWrongValidatorRegex) // test validator fails for config without any inputs @@ -323,16 +323,16 @@ func (s *MySuite) TestRegionExistsValidator(c *C) { Validator: testRegionExistsName.String(), Inputs: map[string]interface{}{}, } - err = bc.testRegionExists(regionValidator) + err = dc.testRegionExists(regionValidator) c.Assert(err, ErrorMatches, missingRequiredInputRegex) // test validators fail when input global variables are undefined regionValidator.Inputs["project_id"] = "((var.project_id))" regionValidator.Inputs["region"] = "((var.region))" - err = bc.testRegionExists(regionValidator) + err = dc.testRegionExists(regionValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) - bc.Config.Vars["project_id"] = "invalid-project" - err = bc.testRegionExists(regionValidator) + dc.Config.Vars["project_id"] = "invalid-project" + err = dc.testRegionExists(regionValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) // TODO: implement a mock client to test success of test_region_exists @@ -340,11 +340,11 @@ func (s *MySuite) TestRegionExistsValidator(c *C) { func (s *MySuite) TestZoneExistsValidator(c *C) { var err error - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() emptyValidator := validatorConfig{} // test validator fails for config without validator id - err = bc.testZoneExists(emptyValidator) + err = dc.testZoneExists(emptyValidator) c.Assert(err, ErrorMatches, passedWrongValidatorRegex) // test validator fails for config without any inputs @@ -352,16 +352,16 @@ func (s *MySuite) TestZoneExistsValidator(c *C) { Validator: testZoneExistsName.String(), Inputs: map[string]interface{}{}, } - err = bc.testZoneExists(zoneValidator) + err = dc.testZoneExists(zoneValidator) c.Assert(err, ErrorMatches, missingRequiredInputRegex) // test validators fail when input global variables are undefined zoneValidator.Inputs["project_id"] = "((var.project_id))" zoneValidator.Inputs["zone"] = "((var.zone))" - err = bc.testZoneExists(zoneValidator) + err = dc.testZoneExists(zoneValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) - bc.Config.Vars["project_id"] = "invalid-project" - err = bc.testZoneExists(zoneValidator) + dc.Config.Vars["project_id"] = "invalid-project" + err = dc.testZoneExists(zoneValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) // TODO: implement a mock client to test success of test_zone_exists @@ -369,11 +369,11 @@ func (s *MySuite) TestZoneExistsValidator(c *C) { func (s *MySuite) TestZoneInRegionValidator(c *C) { var err error - bc := getBlueprintConfigForTest() + dc := getDeploymentConfigForTest() emptyValidator := validatorConfig{} // test validator fails for config without validator id - err = bc.testZoneInRegion(emptyValidator) + err = dc.testZoneInRegion(emptyValidator) c.Assert(err, ErrorMatches, passedWrongValidatorRegex) // test validator fails for config without any inputs @@ -381,20 +381,20 @@ func (s *MySuite) TestZoneInRegionValidator(c *C) { Validator: testZoneInRegionName.String(), Inputs: map[string]interface{}{}, } - err = bc.testZoneInRegion(zoneInRegionValidator) + err = dc.testZoneInRegion(zoneInRegionValidator) c.Assert(err, ErrorMatches, missingRequiredInputRegex) // test validators fail when input global variables are undefined zoneInRegionValidator.Inputs["project_id"] = "((var.project_id))" zoneInRegionValidator.Inputs["region"] = "((var.region))" zoneInRegionValidator.Inputs["zone"] = "((var.zone))" - err = bc.testZoneInRegion(zoneInRegionValidator) + err = dc.testZoneInRegion(zoneInRegionValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) - bc.Config.Vars["project_id"] = "invalid-project" - err = bc.testZoneInRegion(zoneInRegionValidator) + dc.Config.Vars["project_id"] = "invalid-project" + err = dc.testZoneInRegion(zoneInRegionValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) - bc.Config.Vars["zone"] = "invalid-zone" - err = bc.testZoneInRegion(zoneInRegionValidator) + dc.Config.Vars["zone"] = "invalid-zone" + err = dc.testZoneInRegion(zoneInRegionValidator) c.Assert(err, ErrorMatches, undefinedGlobalVariableRegex) // TODO: implement a mock client to test success of test_zone_in_region diff --git a/pkg/resreader/resreader.go b/pkg/resreader/resreader.go index 0c227b5466..27dc32c889 100644 --- a/pkg/resreader/resreader.go +++ b/pkg/resreader/resreader.go @@ -37,9 +37,9 @@ type ModuleInfo struct { } // GetOutputsAsMap returns the outputs list as a map for quicker access -func (ri ModuleInfo) GetOutputsAsMap() map[string]VarInfo { +func (i ModuleInfo) GetOutputsAsMap() map[string]VarInfo { outputsMap := make(map[string]VarInfo) - for _, output := range ri.Outputs { + for _, output := range i.Outputs { outputsMap[output.Name] = output } return outputsMap