Skip to content

Commit

Permalink
Internal YamlConfig to Blueprint conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
heyealex committed May 2, 2022
1 parent 4435ffd commit 8ece1de
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 117 deletions.
2 changes: 1 addition & 1 deletion cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runExpandCmd(cmd *cobra.Command, args []string) {
log.Fatal(err)
}
deploymentConfig.ExpandConfig()
deploymentConfig.ExportYamlConfig(outputFilename)
deploymentConfig.ExportBlueprint(outputFilename)
fmt.Printf(
"Expanded Environment Definition created successfully, saved as %s.\n", outputFilename)
}
54 changes: 27 additions & 27 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ func (m *Module) createWrapSettingsWith() {
}
}

// YamlConfig stores the contents on the User YAML
// Blueprint stores the contents on the User YAML
// omitempty on validation_level ensures that expand will not expose the setting
// unless it has been set to a non-default value; the implementation as an
// integer is primarily for internal purposes even if it can be set in blueprint
type YamlConfig struct {
type Blueprint struct {
BlueprintName string `yaml:"blueprint_name"`
Validators []validatorConfig
ValidationLevel int `yaml:"validation_level,omitempty"`
Expand All @@ -194,7 +194,7 @@ type YamlConfig struct {
// DeploymentConfig is a container for the imported YAML data and supporting data for
// creating the blueprint from it
type DeploymentConfig struct {
Config YamlConfig
Config Blueprint
// Indexed by Resource Group name and Module Source
ModulesInfo map[string]map[string]resreader.ModuleInfo
// Maps module ID to group index
Expand All @@ -214,47 +214,47 @@ func (dc *DeploymentConfig) ExpandConfig() {
// NewDeploymentConfig is a constructor for DeploymentConfig
func NewDeploymentConfig(configFilename string) DeploymentConfig {
newDeploymentConfig := DeploymentConfig{
Config: importYamlConfig(configFilename),
Config: importBlueprint(configFilename),
}
return newDeploymentConfig
}

// ImportYamlConfig imports the blueprint configuration provided.
func importYamlConfig(yamlConfigFilename string) YamlConfig {
yamlConfigText, err := ioutil.ReadFile(yamlConfigFilename)
// ImportBlueprint imports the blueprint configuration provided.
func importBlueprint(blueprintFilename string) Blueprint {
blueprintText, err := ioutil.ReadFile(blueprintFilename)
if err != nil {
log.Fatalf("%s, filename=%s: %v",
errorMessages["fileLoadError"], yamlConfigFilename, err)
errorMessages["fileLoadError"], blueprintFilename, err)
}

var yamlConfig YamlConfig
err = yaml.UnmarshalStrict(yamlConfigText, &yamlConfig)
var blueprint Blueprint
err = yaml.UnmarshalStrict(blueprintText, &blueprint)

if err != nil {
log.Fatalf("%s filename=%s: %v",
errorMessages["yamlUnmarshalError"], yamlConfigFilename, err)
errorMessages["yamlUnmarshalError"], blueprintFilename, err)
}

// Ensure Vars is not a nil map if not set by the user
if len(yamlConfig.Vars) == 0 {
yamlConfig.Vars = make(map[string]interface{})
if len(blueprint.Vars) == 0 {
blueprint.Vars = make(map[string]interface{})
}

if len(yamlConfig.Vars) == 0 {
yamlConfig.Vars = make(map[string]interface{})
if len(blueprint.Vars) == 0 {
blueprint.Vars = make(map[string]interface{})
}

// if the validation level has been explicitly set to an invalid value
// in YAML blueprint then silently default to validationError
if !isValidValidationLevel(yamlConfig.ValidationLevel) {
yamlConfig.ValidationLevel = validationError
if !isValidValidationLevel(blueprint.ValidationLevel) {
blueprint.ValidationLevel = validationError
}

return yamlConfig
return blueprint
}

// ExportYamlConfig exports the internal representation of a blueprint config
func (dc DeploymentConfig) ExportYamlConfig(outputFilename string) ([]byte, error) {
// ExportBlueprint exports the internal representation of a blueprint config
func (dc DeploymentConfig) ExportBlueprint(outputFilename string) ([]byte, error) {
d, err := yaml.Marshal(&dc.Config)
if err != nil {
return d, fmt.Errorf("%s: %w", errorMessages["yamlMarshalError"], err)
Expand Down Expand Up @@ -485,13 +485,13 @@ func ConvertMapToCty(iMap map[string]interface{}) (map[string]cty.Value, error)
// ResolveGlobalVariables given a map of strings to cty.Value types, will examine
// all cty.Values that are of type cty.String. If they are literal global variables,
// then they are replaced by the cty.Value of the corresponding entry in
// yc.Vars. All other cty.Values are unmodified.
// ERROR: if conversion from yc.Vars to map[string]cty.Value fails
// b.Vars. All other cty.Values are unmodified.
// ERROR: if conversion from b.Vars to map[string]cty.Value fails
// ERROR: if (somehow) the cty.String cannot be converted to a Go string
// ERROR: rely on HCL TraverseAbs to bubble up "diagnostics" when the global variable
// being resolved does not exist in yc.Vars
func (yc *YamlConfig) ResolveGlobalVariables(ctyMap map[string]cty.Value) error {
ctyVars, err := ConvertMapToCty(yc.Vars)
// being resolved does not exist in b.Vars
func (b *Blueprint) ResolveGlobalVariables(ctyMap map[string]cty.Value) error {
ctyVars, err := ConvertMapToCty(b.Vars)
if err != nil {
return fmt.Errorf("could not convert global variables to cty map")
}
Expand Down Expand Up @@ -533,8 +533,8 @@ func (err *DeploymentNameError) Error() string {
}

// DeploymentName returns the deployment_name from the config and does approperate checks.
func (yc *YamlConfig) DeploymentName() (string, error) {
nameInterface, found := yc.Vars["deployment_name"]
func (b *Blueprint) DeploymentName() (string, error) {
nameInterface, found := b.Vars["deployment_name"]
if !found {
return "", &DeploymentNameError{"deployment_name variable not defined."}
}
Expand Down
36 changes: 18 additions & 18 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ deployment_groups:
"ghpc_blueprint": "simple",
"deployment_name": "deployment_name",
}
expectedSimpleYamlConfig YamlConfig = YamlConfig{
expectedSimpleBlueprint Blueprint = Blueprint{
BlueprintName: "simple",
Vars: map[string]interface{}{"labels": defaultLabels},
DeploymentGroups: []DeploymentGroup{{Name: "DeploymentGroup1", TerraformBackend: TerraformBackend{}, Modules: testModules}},
Expand Down Expand Up @@ -185,7 +185,7 @@ func getDeploymentConfigForTest() DeploymentConfig {
testModuleInfo := resreader.ModuleInfo{
Inputs: []resreader.VarInfo{testLabelVarInfo},
}
testYamlConfig := YamlConfig{
testBlueprint := Blueprint{
BlueprintName: "simple",
Validators: []validatorConfig{},
Vars: map[string]interface{}{},
Expand All @@ -206,7 +206,7 @@ func getDeploymentConfigForTest() DeploymentConfig {
}

return DeploymentConfig{
Config: testYamlConfig,
Config: testBlueprint,
ModulesInfo: map[string]map[string]resreader.ModuleInfo{
"group1": {
testModuleSource: testModuleInfo,
Expand All @@ -230,7 +230,7 @@ func getBasicDeploymentConfigWithTestModule() DeploymentConfig {
},
}
return DeploymentConfig{
Config: YamlConfig{
Config: Blueprint{
Vars: make(map[string]interface{}),
DeploymentGroups: []DeploymentGroup{testDeploymentGroup},
},
Expand Down Expand Up @@ -318,36 +318,36 @@ func (s *MySuite) TestCheckModuleAndGroupNames(c *C) {
func (s *MySuite) TestNewBlueprint(c *C) {
dc := getDeploymentConfigForTest()
outFile := filepath.Join(tmpTestDir, "out_TestNewBlueprint.yaml")
dc.ExportYamlConfig(outFile)
dc.ExportBlueprint(outFile)
newDC := NewDeploymentConfig(outFile)
c.Assert(dc.Config, DeepEquals, newDC.Config)
}

func (s *MySuite) TestImportYamlConfig(c *C) {
obtainedYamlConfig := importYamlConfig(simpleYamlFilename)
c.Assert(obtainedYamlConfig.BlueprintName,
Equals, expectedSimpleYamlConfig.BlueprintName)
func (s *MySuite) TestImportBlueprint(c *C) {
obtainedBlueprint := importBlueprint(simpleYamlFilename)
c.Assert(obtainedBlueprint.BlueprintName,
Equals, expectedSimpleBlueprint.BlueprintName)
c.Assert(
len(obtainedYamlConfig.Vars["labels"].(map[interface{}]interface{})),
len(obtainedBlueprint.Vars["labels"].(map[interface{}]interface{})),
Equals,
len(expectedSimpleYamlConfig.Vars["labels"].(map[string]interface{})),
len(expectedSimpleBlueprint.Vars["labels"].(map[string]interface{})),
)
c.Assert(obtainedYamlConfig.DeploymentGroups[0].Modules[0].ID,
Equals, expectedSimpleYamlConfig.DeploymentGroups[0].Modules[0].ID)
c.Assert(obtainedBlueprint.DeploymentGroups[0].Modules[0].ID,
Equals, expectedSimpleBlueprint.DeploymentGroups[0].Modules[0].ID)
}

func (s *MySuite) TestExportYamlConfig(c *C) {
func (s *MySuite) TestExportBlueprint(c *C) {
// Return bytes
dc := DeploymentConfig{}
dc.Config = expectedSimpleYamlConfig
obtainedYaml, err := dc.ExportYamlConfig("")
dc.Config = expectedSimpleBlueprint
obtainedYaml, err := dc.ExportBlueprint("")
c.Assert(err, IsNil)
c.Assert(obtainedYaml, Not(IsNil))

// Write file
outFilename := "out_TestExportYamlConfig.yaml"
outFilename := "out_TestExportBlueprint.yaml"
outFile := filepath.Join(tmpTestDir, outFilename)
dc.ExportYamlConfig(outFile)
dc.ExportBlueprint(outFile)
fileInfo, err := os.Stat(outFile)
c.Assert(err, IsNil)
c.Assert(fileInfo.Name(), Equals, outFilename)
Expand Down
26 changes: 13 additions & 13 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ func (dc *DeploymentConfig) expandBackends() error {
// 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 := &dc.Config
if yamlConfig.TerraformBackendDefaults.Type != "" {
for i := range yamlConfig.DeploymentGroups {
grp := &yamlConfig.DeploymentGroups[i]
blueprint := &dc.Config
if blueprint.TerraformBackendDefaults.Type != "" {
for i := range blueprint.DeploymentGroups {
grp := &blueprint.DeploymentGroups[i]
if grp.TerraformBackend.Type == "" {
grp.TerraformBackend.Type = yamlConfig.TerraformBackendDefaults.Type
grp.TerraformBackend.Type = blueprint.TerraformBackendDefaults.Type
grp.TerraformBackend.Configuration = make(map[string]interface{})
for k, v := range yamlConfig.TerraformBackendDefaults.Configuration {
for k, v := range blueprint.TerraformBackendDefaults.Configuration {
grp.TerraformBackend.Configuration[k] = v
}
}
if grp.TerraformBackend.Type == "gcs" && grp.TerraformBackend.Configuration["prefix"] == nil {
DeploymentName := yamlConfig.Vars["deployment_name"]
prefix := yamlConfig.BlueprintName
DeploymentName := blueprint.Vars["deployment_name"]
prefix := blueprint.BlueprintName
if DeploymentName != nil {
prefix += "/" + DeploymentName.(string)
}
Expand Down Expand Up @@ -371,7 +371,7 @@ type varContext struct {
varString string
groupIndex int
modIndex int
yamlConfig YamlConfig
blueprint Blueprint
}

// Needs DeploymentGroups, variable string, current group,
Expand Down Expand Up @@ -399,7 +399,7 @@ func expandSimpleVariable(

if varSource == "vars" { // Global variable
// Verify global variable exists
if _, ok := context.yamlConfig.Vars[varValue]; !ok {
if _, ok := context.blueprint.Vars[varValue]; !ok {
return "", fmt.Errorf("%s: %s is not a global variable",
errorMessages["varNotFound"], context.varString)
}
Expand All @@ -419,7 +419,7 @@ func expandSimpleVariable(
}

// Get the module info
refGrp := context.yamlConfig.DeploymentGroups[refGrpIndex]
refGrp := context.blueprint.DeploymentGroups[refGrpIndex]
refModIndex := -1
for i := range refGrp.Modules {
if refGrp.Modules[i].ID == varSource {
Expand Down Expand Up @@ -557,7 +557,7 @@ func updateVariables(
// expands all variables
func (dc *DeploymentConfig) expandVariables() {
for _, validator := range dc.Config.Validators {
err := updateVariables(varContext{yamlConfig: dc.Config}, validator.Inputs, make(map[string]int))
err := updateVariables(varContext{blueprint: dc.Config}, validator.Inputs, make(map[string]int))
if err != nil {
log.Fatalf("expandVariables: %v", err)
}
Expand All @@ -568,7 +568,7 @@ func (dc *DeploymentConfig) expandVariables() {
context := varContext{
groupIndex: iGrp,
modIndex: iMod,
yamlConfig: dc.Config,
blueprint: dc.Config,
}
err := updateVariables(
context,
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (s *MySuite) TestExpandSimpleVariable(c *C) {
Kind: "terraform",
Source: "./module/testpath",
}
testYamlConfig := YamlConfig{
testBlueprint := Blueprint{
BlueprintName: "",
Vars: make(map[string]interface{}),
DeploymentGroups: []DeploymentGroup{{
Expand All @@ -396,7 +396,7 @@ func (s *MySuite) TestExpandSimpleVariable(c *C) {
TerraformBackendDefaults: TerraformBackend{},
}
testVarContext := varContext{
yamlConfig: testYamlConfig,
blueprint: testBlueprint,
modIndex: 0,
groupIndex: 0,
}
Expand All @@ -415,7 +415,7 @@ func (s *MySuite) TestExpandSimpleVariable(c *C) {
c.Assert(err, ErrorMatches, expectedErr)

// Global variable: Success
testVarContext.yamlConfig.Vars["globalExists"] = "existsValue"
testVarContext.blueprint.Vars["globalExists"] = "existsValue"
testVarContext.varString = "$(vars.globalExists)"
got, err := expandSimpleVariable(testVarContext, testModToGrp)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *MySuite) TestValidateModuleSettings(c *C) {
Modules: []Module{{Kind: "terraform", Source: testSource, Settings: testSettings}},
}
dc := DeploymentConfig{
Config: YamlConfig{DeploymentGroups: []DeploymentGroup{testDeploymentGroup}},
Config: Blueprint{DeploymentGroups: []DeploymentGroup{testDeploymentGroup}},
ModulesInfo: map[string]map[string]resreader.ModuleInfo{},
ModuleToGroup: map[string]int{},
expanded: false,
Expand Down
12 changes: 6 additions & 6 deletions pkg/reswriter/packerwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func printPackerInstructions(grpPath string) {
}

// writeModuleLevel writes any needed files to the module layer
func (w PackerWriter) writeModuleLevel(yamlConfig *config.YamlConfig, outputDir string) error {
for _, grp := range yamlConfig.DeploymentGroups {
deploymentName, err := yamlConfig.DeploymentName()
func (w PackerWriter) writeModuleLevel(blueprint *config.Blueprint, outputDir string) error {
for _, grp := range blueprint.DeploymentGroups {
deploymentName, err := blueprint.DeploymentName()
if err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ func (w PackerWriter) writeModuleLevel(yamlConfig *config.YamlConfig, outputDir
return fmt.Errorf(
"error converting global vars to cty for writing: %v", err)
}
err = yamlConfig.ResolveGlobalVariables(ctySettings)
err = blueprint.ResolveGlobalVariables(ctySettings)
if err != nil {
return err
}
Expand All @@ -90,8 +90,8 @@ func writePackerAutovars(vars map[string]cty.Value, dst string) error {

// writeDeploymentGroups writes any needed files to the top and module levels
// of the blueprint
func (w PackerWriter) writeDeploymentGroups(yamlConfig *config.YamlConfig, outputDir string) error {
return w.writeModuleLevel(yamlConfig, outputDir)
func (w PackerWriter) writeDeploymentGroups(blueprint *config.Blueprint, outputDir string) error {
return w.writeModuleLevel(blueprint, outputDir)
}

func (w PackerWriter) restoreState(deploymentDir string) error {
Expand Down
Loading

0 comments on commit 8ece1de

Please sign in to comment.