Skip to content

Commit efa3418

Browse files
committed
Terraform env version to support HCL2
1 parent 9d3c737 commit efa3418

6 files changed

Lines changed: 105 additions & 13 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
src/build/
33
config.tf
44
environment.tf
5-
.env
5+
.env
6+
bin

src/cli.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type App struct {
1414
log *Log
1515
isCi bool
1616
projectPath string
17+
envVersion string
1718
}
1819

1920
func Init() (a *App) {
@@ -39,6 +40,12 @@ func Init() (a *App) {
3940
Envar(CiEnvVar).
4041
BoolVar(&a.isCi)
4142

43+
a.cli.Flag("ev", "Terraform environment version (evolution version)").
44+
Default(defaultEnvironmentVersion).
45+
Short('E').
46+
Envar(EnvVersionVar).
47+
StringVar(&a.envVersion)
48+
4249
a.cli.Flag("path", "Terraform project path").
4350
Default(a.pwd).
4451
Short('p').

src/command_backend.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type BackendCommand struct {
4747
app *App
4848
log *Log
4949
environment string
50+
modulesDir string
5051
invokerEnabled bool
5152
modulesPath string
5253
environmentConfigPath string
@@ -109,16 +110,22 @@ func (c *BackendCommand) validate(context *kingpin.ParseContext) error {
109110

110111
c.template = c.app.ParseTemplate(backendTemplate)
111112

113+
if c.app.isNewEnvVersion() {
114+
c.modulesDir = ModulesDirV2
115+
} else {
116+
c.modulesDir = ModulesDir
117+
}
118+
112119
c.app.ValidatePath()
113120

114121
c.log.ShowOpts("Environment", c.environment)
115122
if err, isValid := ValidateEnvironment(c.environment); !isValid {
116123
c.log.ErrorFWithUsage(err)
117124
}
118125

119-
modulesAbsPath, isFoundModules := c.app.findModules(c.app.projectPath)
126+
modulesAbsPath, isFoundModules := c.app.findModules(c.app.projectPath, c.modulesDir)
120127
if !isFoundModules {
121-
c.log.ErrorF("Cant find '%s' dir", ModulesDir)
128+
c.log.ErrorF("Cant find '%s' dir", c.modulesDir)
122129
}
123130
c.modulesPath = modulesAbsPath
124131

src/command_env.go

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
const (
1111
environmentTemplate = `######################################
1212
## DO NOT EDIT THIS FILE ##
13+
## Environment version: 1 ##
1314
## Generated by tfconfig ##
1415
## tfconfig {{.TfconfigVersion}} ##
1516
######################################
@@ -57,6 +58,62 @@ provider "aws" {
5758
`
5859
)
5960

61+
const (
62+
environmentTemplateV2 = `######################################
63+
## DO NOT EDIT THIS FILE ##
64+
## Environment version: 2 ##
65+
## Generated by tfconfig ##
66+
## tfconfig {{.TfconfigVersion}} ##
67+
######################################
68+
69+
module "config" {
70+
source = "{{.ConfigModulePath}}"
71+
}
72+
73+
locals {
74+
name = "{{.ProjectName}}"
75+
domain = "{{.ProjectDomain}}"
76+
git_repo = "{{.GitRepo}}"
77+
aws_profile = "{{ if .Local }}{{.AwsProfile}}{{ else }}default{{ end }}"
78+
}
79+
80+
terraform {
81+
backend "s3" {
82+
encrypt = true{{ if .Local }}
83+
profile = "{{.AwsProfile}}"{{ end }}
84+
}
85+
required_providers {
86+
aws = {
87+
source = "hashicorp/aws"
88+
version = "{{.AwsProviderVersion}}"
89+
}
90+
null = {
91+
source = "hashicorp/null"
92+
version = "{{.NullProviderVersion}}"
93+
}
94+
random = {
95+
source = "hashicorp/random"
96+
version = "{{.RandomProviderVersion}}"
97+
}
98+
cloudinit = {
99+
source = "hashicorp/cloudinit"
100+
version = "{{.TemplateProviderVersion}}"
101+
}
102+
dns = {
103+
source = "hashicorp/dns"
104+
version = "{{.DnsProviderVersion}}"
105+
}
106+
}
107+
}
108+
109+
provider "aws" {
110+
region = module.config.region{{ if .Local }}
111+
profile = local.aws_profile{{ end }}
112+
}
113+
114+
`
115+
)
116+
60117
type ProjectConfig struct {
61118
ConfigModulePath string
62119
TfconfigVersion string
@@ -82,6 +139,7 @@ type EnvCommand struct {
82139
app *App
83140
log *Log
84141
environment string
142+
modulesDir string
85143
modulesPath string
86144
modulesSource string
87145
modulesPathAbs string
@@ -139,7 +197,14 @@ func (c *EnvCommand) run(context *kingpin.ParseContext) error {
139197
}
140198

141199
func (c *EnvCommand) validate(context *kingpin.ParseContext) error {
142-
c.template = c.app.ParseTemplate(environmentTemplate)
200+
//c.template = c.app.ParseTemplate(environmentTemplate)
201+
if c.app.isNewEnvVersion() {
202+
c.template = c.app.ParseTemplate(environmentTemplateV2)
203+
c.modulesDir = ModulesDirV2
204+
} else {
205+
c.template = c.app.ParseTemplate(environmentTemplate)
206+
c.modulesDir = ModulesDir
207+
}
143208

144209
c.app.ValidatePath()
145210

@@ -159,9 +224,9 @@ func (c *EnvCommand) validate(context *kingpin.ParseContext) error {
159224

160225
// TODO move under normalized path resolving
161226
// TODO refactor modules shouldnt looking twice
162-
modulesAbsPath, isFoundModules := c.app.findModules(c.app.projectPath)
227+
modulesAbsPath, isFoundModules := c.app.findModules(c.app.projectPath, c.modulesDir)
163228
if !isFoundModules {
164-
c.log.ErrorF("Cant find '%s' dir", ModulesDir)
229+
c.log.ErrorF("Cant find '%s' dir", c.modulesDir)
165230
}
166231
c.modulesPathAbs = modulesAbsPath
167232

@@ -189,7 +254,7 @@ func (c *EnvCommand) validate(context *kingpin.ParseContext) error {
189254
// TODO modules shouldnt searching twice, see todo above
190255
modulesPath, isFoundModules := c.findModules(c.app.projectPath)
191256
if !isFoundModules {
192-
c.log.ErrorF("Cant find '%s' dir", ModulesDir)
257+
c.log.ErrorF("Cant find '%s' dir", c.modulesDir)
193258
}
194259

195260
c.modulesPath = modulesPath
@@ -199,8 +264,8 @@ func (c *EnvCommand) validate(context *kingpin.ParseContext) error {
199264
// TODO use app.FindModules
200265
func (c *EnvCommand) findModules(path string) (modulesPath string, isFound bool) {
201266
for _, v := range listSearchPaths() {
202-
if c.app.FindFolder(GetFullPath(path, v), ModulesDir) {
203-
return v + ModulesDir, true
267+
if c.app.FindFolder(GetFullPath(path, v), c.modulesDir) {
268+
return v + c.modulesDir, true
204269
}
205270
}
206271
return "", false

src/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

3-
const Version = "v0.4.2"
3+
const Version = "v0.5.0"
44

55
const CiEnvVar = "CI"
6+
const EnvVersionVar = "TF_ENV_VERSION"
67
const TerraformLocalEnvVar = "TF_LOCAL"
78
const TerraformEnvVar = "TF_ENV"
89
const ModulesDir = "aws-terraform-modules"
10+
const ModulesDirV2 = "aws-environment"
911
const ConfigFile = "config.tf"
1012
const ConfigModuleName = "config"
1113
const EnvironmentsDir = "environment"
@@ -17,6 +19,8 @@ const defaultEnvironmentConfig = "environment.env"
1719
// Project specific configuration
1820
const defaultProjectConfig = "terraform.env"
1921

22+
const defaultEnvironmentVersion = "1"
23+
2024
const WarningHeader = `######################################
2125
## DO NOT EDIT THIS FILE ##
2226
## Generated by tfconfig ##

src/utils.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ func (a *App) BoolResolver(text string) bool {
125125
return false
126126
}
127127

128+
func (a *App) isNewEnvVersion() bool {
129+
if strings.EqualFold(a.envVersion, "2") {
130+
return true
131+
} else {
132+
return false
133+
}
134+
}
135+
128136
func (a *App) projectEnvironmentConfigResolver(fileName string) (projectEnvironmentConfig string, isFound bool) {
129137
sourceEnvironmentProjectConfigPath, _ := filepath.Abs(fileName)
130138
rootEnvironmentProjectConfigPath, _ := filepath.Abs(strings.Join([]string{"..", fileName}, pathSeparator))
@@ -150,11 +158,11 @@ func (a *App) ParseTemplate(templateText string) *template.Template {
150158
return t
151159
}
152160

153-
func (a *App) findModules(path string) (modulesPath string, isFound bool) {
161+
func (a *App) findModules(path string, modulesDir string) (modulesPath string, isFound bool) {
154162
for _, v := range listSearchPaths() {
155163
searchPath, _ := filepath.Abs(filepath.Join(path, v))
156-
if a.FindFolder(searchPath, ModulesDir) {
157-
return filepath.Join(searchPath, ModulesDir), true
164+
if a.FindFolder(searchPath, modulesDir) {
165+
return filepath.Join(searchPath, modulesDir), true
158166
}
159167
}
160168
return "", false

0 commit comments

Comments
 (0)