Skip to content

Commit

Permalink
Convert Resource Group to Deployment Group
Browse files Browse the repository at this point in the history
Changes the internal representation from the outdated resource group to
deployment group.
  • Loading branch information
heyealex committed May 2, 2022
1 parent a734a45 commit a49092b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 116 deletions.
32 changes: 16 additions & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ var errorMessages = map[string]string{
"invalidOutput": "requested output was not found in the module",
}

// ResourceGroup defines a group of Modules that are all executed together
type ResourceGroup struct {
// DeploymentGroup defines a group of Modules that are all executed together
type DeploymentGroup struct {
Name string `yaml:"group"`
TerraformBackend TerraformBackend `yaml:"terraform_backend"`
Modules []Module `yaml:"modules"`
}

func (g ResourceGroup) getModuleByID(modID string) Module {
func (g DeploymentGroup) getModuleByID(modID string) Module {
for i := range g.Modules {
mod := g.Modules[i]
if g.Modules[i].ID == modID {
Expand Down Expand Up @@ -147,9 +147,9 @@ type validatorConfig struct {
}

// HasKind checks to see if a resource group contains any modules of the given
// kind. Note that a resourceGroup should never have more than one kind, this
// kind. Note that a DeploymentGroup should never have more than one kind, this
// function is used in the validation step to ensure that is true.
func (g ResourceGroup) HasKind(kind string) bool {
func (g DeploymentGroup) HasKind(kind string) bool {
for _, mod := range g.Modules {
if mod.Kind == kind {
return true
Expand Down Expand Up @@ -187,8 +187,8 @@ type YamlConfig struct {
Validators []validatorConfig
ValidationLevel int `yaml:"validation_level,omitempty"`
Vars map[string]interface{}
ResourceGroups []ResourceGroup `yaml:"deployment_groups"`
TerraformBackendDefaults TerraformBackend `yaml:"terraform_backend_defaults"`
DeploymentGroups []DeploymentGroup `yaml:"deployment_groups"`
TerraformBackendDefaults TerraformBackend `yaml:"terraform_backend_defaults"`
}

// BlueprintConfig is a container for the imported YAML data and supporting data for
Expand Down Expand Up @@ -272,9 +272,9 @@ func (bc BlueprintConfig) ExportYamlConfig(outputFilename string) ([]byte, error
}

func createModuleInfo(
resourceGroup ResourceGroup) map[string]resreader.ModuleInfo {
deploymentGroup DeploymentGroup) map[string]resreader.ModuleInfo {
modInfo := make(map[string]resreader.ModuleInfo)
for _, mod := range resourceGroup.Modules {
for _, mod := range deploymentGroup.Modules {
if _, exists := modInfo[mod.Source]; !exists {
reader := sourcereader.Factory(mod.Source)
ri, err := reader.GetModuleInfo(mod.Source, mod.Kind)
Expand All @@ -292,7 +292,7 @@ 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.ResourceGroups {
for _, grp := range bc.Config.DeploymentGroups {
bc.ModulesInfo[grp.Name] = createModuleInfo(grp)
}
}
Expand All @@ -314,10 +314,10 @@ func validateGroupName(name string, usedNames map[string]bool) {
// checkModuleAndGroupNames checks and imports module and resource group IDs
// and names respectively.
func checkModuleAndGroupNames(
resGroups []ResourceGroup) (map[string]int, error) {
depGroups []DeploymentGroup) (map[string]int, error) {
moduleToGroup := make(map[string]int)
groupNames := make(map[string]bool)
for iGrp, grp := range resGroups {
for iGrp, grp := range depGroups {
validateGroupName(grp.Name, groupNames)
var groupKind string
for _, mod := range grp.Modules {
Expand Down Expand Up @@ -345,8 +345,8 @@ func checkModuleAndGroupNames(
// checkUsedModuleNames verifies that any used modules have valid names and
// are in the correct group
func checkUsedModuleNames(
resGroups []ResourceGroup, idToGroup map[string]int) error {
for iGrp, grp := range resGroups {
depGroups []DeploymentGroup, idToGroup map[string]int) error {
for iGrp, grp := range depGroups {
for _, mod := range grp.Modules {
for _, usedMod := range mod.Use {
// Check if module even exists
Expand All @@ -366,13 +366,13 @@ func checkUsedModuleNames(

// validateConfig runs a set of simple early checks on the imported input YAML
func (bc *BlueprintConfig) validateConfig() {
moduleToGroup, err := checkModuleAndGroupNames(bc.Config.ResourceGroups)
moduleToGroup, err := checkModuleAndGroupNames(bc.Config.DeploymentGroups)
if err != nil {
log.Fatal(err)
}
bc.ModuleToGroup = moduleToGroup
if err = checkUsedModuleNames(
bc.Config.ResourceGroups, bc.ModuleToGroup); err != nil {
bc.Config.DeploymentGroups, bc.ModuleToGroup); err != nil {
log.Fatal(err)
}
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ deployment_groups:
"deployment_name": "deployment_name",
}
expectedSimpleYamlConfig YamlConfig = YamlConfig{
BlueprintName: "simple",
Vars: map[string]interface{}{"labels": defaultLabels},
ResourceGroups: []ResourceGroup{{Name: "ResourceGroup1", TerraformBackend: TerraformBackend{}, Modules: testModules}},
BlueprintName: "simple",
Vars: map[string]interface{}{"labels": defaultLabels},
DeploymentGroups: []DeploymentGroup{{Name: "DeploymentGroup1", TerraformBackend: TerraformBackend{}, Modules: testModules}},
TerraformBackendDefaults: TerraformBackend{
Type: "",
Configuration: map[string]interface{}{},
Expand Down Expand Up @@ -193,7 +193,7 @@ func getBlueprintConfigForTest() BlueprintConfig {
Type: "",
Configuration: map[string]interface{}{},
},
ResourceGroups: []ResourceGroup{
DeploymentGroups: []DeploymentGroup{
{
Name: "group1",
TerraformBackend: TerraformBackend{
Expand All @@ -218,7 +218,7 @@ func getBlueprintConfigForTest() BlueprintConfig {

func getBasicBlueprintConfigWithTestModule() BlueprintConfig {
testModuleSource := filepath.Join(tmpTestDir, "module")
testResourceGroup := ResourceGroup{
testDeploymentGroup := DeploymentGroup{
Name: "primary",
Modules: []Module{
{
Expand All @@ -231,8 +231,8 @@ func getBasicBlueprintConfigWithTestModule() BlueprintConfig {
}
return BlueprintConfig{
Config: YamlConfig{
Vars: make(map[string]interface{}),
ResourceGroups: []ResourceGroup{testResourceGroup},
Vars: make(map[string]interface{}),
DeploymentGroups: []DeploymentGroup{testDeploymentGroup},
},
}
}
Expand All @@ -251,14 +251,14 @@ func (s *MySuite) TestSetModulesInfo(c *C) {

func (s *MySuite) TestCreateModuleInfo(c *C) {
bc := getBasicBlueprintConfigWithTestModule()
createModuleInfo(bc.Config.ResourceGroups[0])
createModuleInfo(bc.Config.DeploymentGroups[0])
}

func (s *MySuite) TestGetResouceByID(c *C) {
testID := "testID"

// No Modules
rg := ResourceGroup{}
rg := DeploymentGroup{}
got := rg.getModuleByID(testID)
c.Assert(got, DeepEquals, Module{})

Expand All @@ -276,7 +276,7 @@ func (s *MySuite) TestGetResouceByID(c *C) {

func (s *MySuite) TestHasKind(c *C) {
// No Modules
rg := ResourceGroup{}
rg := DeploymentGroup{}
c.Assert(rg.HasKind("terraform"), Equals, false)
c.Assert(rg.HasKind("packer"), Equals, false)
c.Assert(rg.HasKind("notAKind"), Equals, false)
Expand Down Expand Up @@ -310,8 +310,8 @@ func (s *MySuite) TestHasKind(c *C) {

func (s *MySuite) TestCheckModuleAndGroupNames(c *C) {
bc := getBlueprintConfigForTest()
checkModuleAndGroupNames(bc.Config.ResourceGroups)
testModID := bc.Config.ResourceGroups[0].Modules[0].ID
checkModuleAndGroupNames(bc.Config.DeploymentGroups)
testModID := bc.Config.DeploymentGroups[0].Modules[0].ID
c.Assert(bc.ModuleToGroup[testModID], Equals, 0)
}

Expand All @@ -332,8 +332,8 @@ func (s *MySuite) TestImportYamlConfig(c *C) {
Equals,
len(expectedSimpleYamlConfig.Vars["labels"].(map[string]interface{})),
)
c.Assert(obtainedYamlConfig.ResourceGroups[0].Modules[0].ID,
Equals, expectedSimpleYamlConfig.ResourceGroups[0].Modules[0].ID)
c.Assert(obtainedYamlConfig.DeploymentGroups[0].Modules[0].ID,
Equals, expectedSimpleYamlConfig.DeploymentGroups[0].Modules[0].ID)
}

func (s *MySuite) TestExportYamlConfig(c *C) {
Expand Down
34 changes: 17 additions & 17 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func (bc *BlueprintConfig) expand() {
}

func (bc *BlueprintConfig) addSettingsToModules() {
for iGrp, grp := range bc.Config.ResourceGroups {
for iGrp, grp := range bc.Config.DeploymentGroups {
for iMod, mod := range grp.Modules {
if mod.Settings == nil {
bc.Config.ResourceGroups[iGrp].Modules[iMod].Settings =
bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings =
make(map[string]interface{})
}
}
Expand All @@ -91,8 +91,8 @@ func (bc *BlueprintConfig) expandBackends() error {
// 3. In all cases, add a prefix for GCS backends if one is not defined
yamlConfig := &bc.Config
if yamlConfig.TerraformBackendDefaults.Type != "" {
for i := range yamlConfig.ResourceGroups {
grp := &yamlConfig.ResourceGroups[i]
for i := range yamlConfig.DeploymentGroups {
grp := &yamlConfig.DeploymentGroups[i]
if grp.TerraformBackend.Type == "" {
grp.TerraformBackend.Type = yamlConfig.TerraformBackendDefaults.Type
grp.TerraformBackend.Configuration = make(map[string]interface{})
Expand Down Expand Up @@ -170,8 +170,8 @@ 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.ResourceGroups {
group := &bc.Config.ResourceGroups[iGrp]
for iGrp := range bc.Config.DeploymentGroups {
group := &bc.Config.DeploymentGroups[iGrp]
for iMod := range group.Modules {
mod := &group.Modules[iMod]
modInfo := bc.ModulesInfo[group.Name][mod.Source]
Expand All @@ -192,8 +192,8 @@ func (bc *BlueprintConfig) applyUseModules() error {
}

func (bc BlueprintConfig) moduleHasInput(
resGroup string, source string, inputName string) bool {
for _, input := range bc.ModulesInfo[resGroup][source].Inputs {
depGroup string, source string, inputName string) bool {
for _, input := range bc.ModulesInfo[depGroup][source].Inputs {
if input.Name == inputName {
return true
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func (bc *BlueprintConfig) combineLabels() error {
globalLabels[deploymentLabel] = defaultLabels[deploymentLabel]
}

for iGrp, grp := range bc.Config.ResourceGroups {
for iGrp, grp := range bc.Config.DeploymentGroups {
for iMod, mod := range grp.Modules {
// Check if labels are set for this module
if !bc.moduleHasInput(grp.Name, mod.Source, labels) {
Expand All @@ -300,7 +300,7 @@ func (bc *BlueprintConfig) combineLabels() error {
if _, exists := modLabels[roleLabel]; !exists {
modLabels[roleLabel] = getRole(mod.Source)
}
bc.Config.ResourceGroups[iGrp].Modules[iMod].Settings[labels] =
bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings[labels] =
modLabels
}
}
Expand All @@ -309,10 +309,10 @@ func (bc *BlueprintConfig) combineLabels() error {
}

func applyGlobalVarsInGroup(
resourceGroup ResourceGroup,
deploymentGroup DeploymentGroup,
modInfo map[string]resreader.ModuleInfo,
globalVars map[string]interface{}) error {
for _, mod := range resourceGroup.Modules {
for _, mod := range deploymentGroup.Modules {
for _, input := range modInfo[mod.Source].Inputs {

// Module setting exists? Nothing more needs to be done.
Expand Down Expand Up @@ -357,7 +357,7 @@ func (bc *BlueprintConfig) applyGlobalVariables() error {
return err
}

for _, grp := range bc.Config.ResourceGroups {
for _, grp := range bc.Config.DeploymentGroups {
err := applyGlobalVarsInGroup(
grp, bc.ModulesInfo[grp.Name], bc.Config.Vars)
if err != nil {
Expand All @@ -374,7 +374,7 @@ type varContext struct {
yamlConfig YamlConfig
}

// Needs ResourceGroups, variable string, current group,
// Needs DeploymentGroups, variable string, current group,
func expandSimpleVariable(
context varContext,
modToGrp map[string]int) (string, error) {
Expand Down Expand Up @@ -419,7 +419,7 @@ func expandSimpleVariable(
}

// Get the module info
refGrp := context.yamlConfig.ResourceGroups[refGrpIndex]
refGrp := context.yamlConfig.DeploymentGroups[refGrpIndex]
refModIndex := -1
for i := range refGrp.Modules {
if refGrp.Modules[i].ID == varSource {
Expand Down Expand Up @@ -563,7 +563,7 @@ func (bc *BlueprintConfig) expandVariables() {
}
}

for iGrp, grp := range bc.Config.ResourceGroups {
for iGrp, grp := range bc.Config.DeploymentGroups {
for iMod := range grp.Modules {
context := varContext{
groupIndex: iGrp,
Expand All @@ -572,7 +572,7 @@ func (bc *BlueprintConfig) expandVariables() {
}
err := updateVariables(
context,
bc.Config.ResourceGroups[iGrp].Modules[iMod].Settings,
bc.Config.DeploymentGroups[iGrp].Modules[iMod].Settings,
bc.ModuleToGroup)
if err != nil {
log.Fatalf("expandVariables: %v", err)
Expand Down
Loading

0 comments on commit a49092b

Please sign in to comment.