Skip to content

Commit

Permalink
Merge pull request #8 from GoogleCloudPlatform/develop
Browse files Browse the repository at this point in the history
Update to v0.1.1-alpha
  • Loading branch information
heyealex authored Oct 18, 2021
2 parents d4eddb3 + 827770c commit fdaeecf
Show file tree
Hide file tree
Showing 37 changed files with 246 additions and 105 deletions.
51 changes: 51 additions & 0 deletions .tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
plugin "google" {
enabled = true
version = "0.12.1"
source = "github.com/terraform-linters/tflint-ruleset-google"
}
rule "terraform_deprecated_index" {
enabled = true
}
rule "terraform_unused_declarations" {
enabled = true
}
rule "terraform_documented_variables" {
enabled = true
}
rule "terraform_comment_syntax" {
enabled = true
}
rule "terraform_documented_outputs" {
enabled = true
}
rule "terraform_documented_variables" {
enabled = true
}
rule "terraform_typed_variables" {
enabled = true
}
rule "terraform_naming_convention" {
enabled = true
}
rule "terraform_required_version" {
enabled = true
}
rule "terraform_required_providers" {
enabled = true
}
rule "terraform_unused_required_providers" {
enabled = true
}
1 change: 1 addition & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package cmd defines command line utilities for ghpc
package cmd

import (
Expand Down
1 change: 1 addition & 0 deletions cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package cmd defines command line utilities for ghpc
package cmd

import (
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package cmd defines command line utilities for ghpc
package cmd

import (
Expand All @@ -33,10 +34,11 @@ HPC deployments on the Google Cloud Platform.`,
log.Fatalf("cmd.Help function failed: %s", err)
}
},
Version: "v0.1.0-alpha (private preview)",
Version: "v0.1.1-alpha (private preview)",
}
)

// Execute the root command
func Execute() error {
return rootCmd.Execute()
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package config manages and updates the ghpc input config
package config

import (
Expand Down Expand Up @@ -220,8 +221,12 @@ func (bc *BlueprintConfig) checkResourceAndGroupNames() {
// expand expands variables and strings in the yaml config
func (bc BlueprintConfig) expand() {
bc.addSettingsToResources()
bc.combineLabels()
bc.applyGlobalVariables()
if err := bc.combineLabels(); err != nil {
log.Fatal(err)
}
if err := bc.applyGlobalVariables(); err != nil {
log.Fatal(err)
}
bc.expandVariables()
}

Expand Down
19 changes: 18 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {
err := bc.applyGlobalVariables()
c.Assert(err, IsNil)

// Test no inputs, one required
// Test no inputs, one required, doesn't exist in globals
bc.ResourcesInfo["group1"][testResource.Source] = resreader.ResourceInfo{
Inputs: []resreader.VarInfo{requiredVar},
}
Expand All @@ -243,6 +243,23 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {
errorMessages["missingSetting"], testResource.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()
c.Assert(err, IsNil)
c.Assert(
bc.Config.ResourceGroups[0].Resources[0].Settings[requiredVar.Name],
Equals, fmt.Sprintf("((var.%s))", requiredVar.Name))

// Test one input, one required
bc.Config.ResourceGroups[0].Resources[0].Settings[requiredVar.Name] = "val"
err = bc.applyGlobalVariables()
c.Assert(err, IsNil)

// Test one input, none required, exists in globals
bc.ResourcesInfo["group1"][testResource.Source].Inputs[0].Required = false
err = bc.applyGlobalVariables()
c.Assert(err, IsNil)
}

func (s *MySuite) TestIsSimpleVariable(c *C) {
Expand Down
122 changes: 63 additions & 59 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,25 @@ func applyGlobalVarsInGroup(
globalVars map[string]interface{}) error {
for _, res := range resourceGroup.Resources {
for _, input := range resInfo[res.Source].Inputs {
if input.Required == true {
// Exists? Continue
if _, ok := res.Settings[input.Name]; ok {
continue
}

// Exists at top level? Update and Continue
if _, ok := globalVars[input.Name]; ok {
res.Settings[input.Name] = fmt.Sprintf("((var.%s))", input.Name)
} else {
return fmt.Errorf("%s: Resource.ID: %s Setting: %s",
errorMessages["missingSetting"], res.ID, input.Name)
}
// Resource setting exists? Nothing more needs to be done.
if _, ok := res.Settings[input.Name]; ok {
continue
}

// If it's not set, is there a global we can use?
if _, ok := globalVars[input.Name]; ok {
res.Settings[input.Name] = fmt.Sprintf("((var.%s))", input.Name)
continue
}

if input.Required == true {
// It's not explicitly set, and not global is set
// Fail if no default has been set
return fmt.Errorf("%s: Resource.ID: %s Setting: %s",
errorMessages["missingSetting"], res.ID, input.Name)
}
// Default exists, the resource will handle it
}
}
return nil
Expand All @@ -169,15 +174,14 @@ func applyGlobalVarsInGroup(
// applyGlobalVariables takes any variables defined at the global level and
// applies them to resources settings if not already set.
func (bc *BlueprintConfig) applyGlobalVariables() error {
var err error
for _, grp := range bc.Config.ResourceGroups {
err = applyGlobalVarsInGroup(
err := applyGlobalVarsInGroup(
grp, bc.ResourcesInfo[grp.Name], bc.Config.Vars)
if err != nil {
break
return err
}
}
return err
return nil
}

type varContext struct {
Expand Down Expand Up @@ -217,48 +221,48 @@ func expandSimpleVariable(
errorMessages["varNotFound"], context.varString)
}
return fmt.Sprintf("((var.%s))", varValue), nil
} else { // Resource variable
}

// Verify resource exists
refGrpIndex, ok := resToGrp[varSource]
if !ok {
return "", fmt.Errorf("%s: resource %s was not found",
errorMessages["varNotFound"], varSource)
}
if refGrpIndex != context.groupIndex {
log.Fatalf("Unimplemented: references to other groups are not yet supported")
}
// Resource variable
// Verify resource exists
refGrpIndex, ok := resToGrp[varSource]
if !ok {
return "", fmt.Errorf("%s: resource %s was not found",
errorMessages["varNotFound"], varSource)
}
if refGrpIndex != context.groupIndex {
log.Fatalf("Unimplemented: references to other groups are not yet supported")
}

// Get the resource info
refGrp := context.yamlConfig.ResourceGroups[refGrpIndex]
refResIndex := -1
for i := range refGrp.Resources {
if refGrp.Resources[i].ID == varSource {
refResIndex = i
break
}
}
if refResIndex == -1 {
log.Fatalf("Could not find resource referenced by variable %s",
context.varString)
}
refRes := refGrp.Resources[refResIndex]
resInfo := resreader.Factory(refRes.Kind).GetInfo(refRes.Source)

// Verify output exists in resource
found := false
for _, output := range resInfo.Outputs {
if output.Name == varValue {
found = true
break
}
// Get the resource info
refGrp := context.yamlConfig.ResourceGroups[refGrpIndex]
refResIndex := -1
for i := range refGrp.Resources {
if refGrp.Resources[i].ID == varSource {
refResIndex = i
break
}
if !found {
return "", fmt.Errorf("%s: resource %s did not have output %s",
errorMessages["noOutput"], refRes.ID, varValue)
}
if refResIndex == -1 {
log.Fatalf("Could not find resource referenced by variable %s",
context.varString)
}
refRes := refGrp.Resources[refResIndex]
resInfo := resreader.Factory(refRes.Kind).GetInfo(refRes.Source)

// Verify output exists in resource
found := false
for _, output := range resInfo.Outputs {
if output.Name == varValue {
found = true
break
}
return fmt.Sprintf("((module.%s.%s))", varSource, varValue), nil
}
if !found {
return "", fmt.Errorf("%s: resource %s did not have output %s",
errorMessages["noOutput"], refRes.ID, varValue)
}
return fmt.Sprintf("((module.%s.%s))", varSource, varValue), nil
}

func expandVariable(
Expand Down Expand Up @@ -318,7 +322,7 @@ func updateVariableType(
interfaceSlice[i], err = updateVariableType(
interfaceSlice[i], context, resToGrp)
if err != nil {
break
return interfaceSlice, err
}
}
}
Expand All @@ -329,7 +333,7 @@ func updateVariableType(
for k, v := range interfaceMap {
retMap[k], err = updateVariableType(v, context, resToGrp)
if err != nil {
break
return interfaceMap, err
}
}
return retMap, err
Expand All @@ -342,14 +346,14 @@ func updateVariables(
context varContext,
interfaceMap map[string]interface{},
resToGrp map[string]int) error {
var err error
for key, value := range interfaceMap {
interfaceMap[key], err = updateVariableType(value, context, resToGrp)
updatedVal, err := updateVariableType(value, context, resToGrp)
if err != nil {
break
return err
}
interfaceMap[key] = updatedVal
}
return err
return nil
}

// handlePrimitives recurses through the data structures in the yaml config and
Expand Down
1 change: 0 additions & 1 deletion pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

// Package config wraps functionality around the user input config
package config

import (
Expand Down
10 changes: 5 additions & 5 deletions pkg/resreader/packerreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ func addTfExtension(filename string) {
}

func getHCLFiles(dir string) []string {
all_files, err := ioutil.ReadDir(dir)
allFiles, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatalf("Failed to read packer source directory %s", dir)
}
var hcl_files []string
for _, f := range all_files {
var hclFiles []string
for _, f := range allFiles {
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) == ".hcl" {
hcl_files = append(hcl_files, path.Join(dir, f.Name()))
hclFiles = append(hclFiles, path.Join(dir, f.Name()))
}
}
return hcl_files
return hclFiles
}

func copyHCLFilesToTmp(dir string) (string, []string) {
Expand Down
1 change: 1 addition & 0 deletions pkg/resreader/resreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

// Package resreader extracts necessary information from resources
package resreader

import "log"
Expand Down
9 changes: 9 additions & 0 deletions pkg/resreader/resreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,28 @@ func createTmpResource() {
"Failed to create temp dir for resource in resreader_test, %v", err)
}
mainFile, err := os.Create(path.Join(tmpResourceDir, "main.tf"))
if err != nil {
log.Fatalf("Failed to create main.tf: %v", err)
}
_, err = mainFile.WriteString(testMainTf)
if err != nil {
log.Fatalf("resreader_test: Failed to write main.tf test file. %v", err)
}

varFile, err := os.Create(path.Join(tmpResourceDir, "variables.tf"))
if err != nil {
log.Fatalf("Failed to create variables.tf: %v", err)
}
_, err = varFile.WriteString(testVariablesTf)
if err != nil {
log.Fatalf(
"resreader_test: Failed to write variables.tf test file. %v", err)
}

outFile, err := os.Create(path.Join(tmpResourceDir, "outputs.tf"))
if err != nil {
log.Fatalf("Failed to create outputs.tf: %v", err)
}
_, err = outFile.WriteString(testOutputsTf)
if err != nil {
log.Fatalf("resreader_test: Failed to write outputs.tf test file. %v", err)
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fdaeecf

Please sign in to comment.