Skip to content

Commit

Permalink
Internal update, BlueprintConfig to DeploymentConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
heyealex committed May 2, 2022
1 parent a49092b commit 4435ffd
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 317 deletions.
12 changes: 6 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
74 changes: 37 additions & 37 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\")")
}
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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
}

}
Expand Down
Loading

0 comments on commit 4435ffd

Please sign in to comment.