Skip to content

Commit

Permalink
Provide backend config values to TF code (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-dima authored May 17, 2024
1 parent 20c4407 commit 16d1520
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 10 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ gcp-org:
- `inventory_path` = relative path to the folder with jsons
- `cmd_to_exec` = name of the binary to execute (`tofu` or `terraform`)
- `backend` = Config values for backend provider. All the child key:values will be provided to `init` and `$tofugu_state_path` will be replaced by generated path.
For example, it will look like `tofu init -backend-config=bucket=gcp-tfstates -backend-config=prefix=account_free-tier/free_instance.tfstate`
For example, when you will execute `tofugu cook ...... -- init`, TofuGu actually will execute `init -backend-config=bucket=gcp-tfstates -backend-config=prefix=account_free-tier/free_instance.tfstate`

At least
```yaml
Expand Down Expand Up @@ -185,9 +185,9 @@ Examples:
- [Shared module for VPC creation](examples/tofies/shared-modules/create_vpc)
- [Shared module for VPC creation used in code](examples/tofies/demo-org/vpc/main.tf#L3)
## Remote state in S3
## Remote state (Terraform Backend where state data files are stored)
AWS, Google Cloud and some other backends are supported! You could configure any backend provider in `tofugu config file`
AWS, Google Cloud and some other backends are supported! You could configure any backend provider in [TofuGu Config file](#hometofugu)
[For AWS S3 your terraform code (`tofi`) should contains at least:](examples/tofies/demo-org/vpc/versions.tf#L4):
```
Expand All @@ -209,6 +209,35 @@ If for the `demo-org` config `bucket` is NOT set, then `$tofugu_state_path` will
This could be useful, if you want to store by default tfstate for all the organisations in the same/default bucket `default-tfstates` but for some specific organisation you need to store tfstates in dedicated bucket `demo-org-tfstates`
## Data Source Configuration (data "terraform_remote_state")
To simplify "Data Source Configuration" (`data "terraform_remote_state" "tfstate" { }`) will be nice to have backend config values as tfvars.
`var.tofugu_backend_config` will contain all the parameters from [TofuGu config (backend Section)](#hometofugu)
[For example, for AWS S3](examples/tofies/demo-org/vpc/data.tf):
```
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = var.tofugu_backend_config.bucket
key = "network/terraform.tfstate"
region = var.tofugu_backend_config.region
}
}
```
[And for GCS](examples/tofies/gcp-org/free_instance/data.tf):
```
data "terraform_remote_state" "free_instance" {
backend = "gcs"
config = {
bucket = var.tofugu_backend_config.bucket
prefix = "account_free-tier/free_instance.tfstate"
}
}
```
You will set `key/prefix` to another tofie's tfstate, which outputs you want to use.
## $HOME/.tofurc
Expand Down
7 changes: 6 additions & 1 deletion cmd/cook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ var cookCmd = &cobra.Command{
tofuguStruct.ParseTofiManifest("tofi_manifest.json")
tofuguStruct.ParseDimensions()

backendConfig := tofuguStruct.SetupBackendConfig()
backendTofuguConfig := tofuguStruct.SetupBackendConfig()

tofuguStruct.PrepareTemp()

tofuguStruct.GenerateVarsByDims()
tofuguStruct.GenerateVarsByDimOptional("defaults")
tofuguStruct.GenerateVarsByEnvVars()
tofuguStruct.GenerateVarsByDimAndData("config", "backend", backendTofuguConfig)

//Local variables for child execution
forceCleanTempDir, _ := cmd.Flags().GetBool("clean")
var backendConfig []string
for param, value := range backendTofuguConfig {
backendConfig = append(backendConfig, "-backend-config="+param+"="+value.(string))
}
cmdArgs := args
if args[0] == "init" {
cmdArgs = append(cmdArgs, backendConfig...)
Expand Down
8 changes: 8 additions & 0 deletions examples/tofies/demo-org/vpc/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# data "terraform_remote_state" "network" {
# backend = "s3"
# config = {
# bucket = var.tofugu_backend_config.bucket
# key = "network/terraform.tfstate"
# region = var.tofugu_backend_config.region
# }
# }
7 changes: 7 additions & 0 deletions examples/tofies/gcp-org/free_instance/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# data "terraform_remote_state" "free_instance" {
# backend = "gcs"
# config = {
# bucket = var.tofugu_backend_config.bucket
# prefix = "account_free-tier/free_instance.tfstate"
# }
# }
11 changes: 5 additions & 6 deletions utils/externals.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func (tofuguStruct *Tofugu) GetObjectFromViperByOrgOrDefault(keyName string) map
}
}

func (tofuguStruct *Tofugu) SetupBackendConfig() []string {
var backendFinalConfig []string

func (tofuguStruct *Tofugu) SetupBackendConfig() map[string]interface{} {
var stateS3Path string
if !viper.IsSet(tofuguStruct.OrgName + ".backend") {
stateS3Path = stateS3Path + "org_" + tofuguStruct.OrgName + "/"
Expand All @@ -52,12 +50,13 @@ func (tofuguStruct *Tofugu) SetupBackendConfig() []string {
if len(backendTofuguConfig) == 0 {
log.Println("Tofugu: no backend config provied!")
}

var backendTofuguConfigMap = make(map[string]interface{}, len(backendTofuguConfig))
for param, value := range backendTofuguConfig {
replacedVar := strings.Replace(value.(string), "$tofugu_state_path", tofuguStruct.StateS3Path, 1)
backendFinalConfig = append(backendFinalConfig, "-backend-config="+param+"="+replacedVar)
backendTofuguConfigMap[param] = strings.Replace(value.(string), "$tofugu_state_path", tofuguStruct.StateS3Path, 1)
}

return backendFinalConfig
return backendTofuguConfigMap
}

func (tofuguStruct *Tofugu) GetDimData(dimensionKey string, dimensionValue string, skipOnNotFound bool) map[string]interface{} {
Expand Down
8 changes: 8 additions & 0 deletions utils/generatevars.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (tofuguStruct *Tofugu) GenerateVarsByDimOptional(optionType string) {
}
}

func (tofuguStruct *Tofugu) GenerateVarsByDimAndData(optionType string, dimKey string, dimensionJsonMap map[string]interface{}) {
targetAutoTfvarMap := map[string]interface{}{
"tofugu_" + dimKey + "_" + optionType: dimensionJsonMap,
}
writeTfvarsMaps(targetAutoTfvarMap, dimKey+"_"+optionType, tofuguStruct.CmdWorkTempDir)
log.Println("TofuGu attached " + optionType + " in var.tofugu_" + dimKey + "_" + optionType)
}

func (tofuguStruct *Tofugu) GenerateVarsByEnvVars() {
targetAutoTfvarMap := make(map[string]interface{})

Expand Down
4 changes: 4 additions & 0 deletions utils/preparetemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

func (tofuguStruct *Tofugu) PrepareTemp() {
if tofuguStruct.StateS3Path == "" {
log.Fatalf("StateS3Path is empty \n")
}

tmpFolderNameSuffix := tofuguStruct.OrgName + tofuguStruct.StateS3Path + tofuguStruct.TofiName
cmdTempDirFullPath := os.TempDir() + "/tofugu-" + GetMD5Hash(tmpFolderNameSuffix)

Expand Down

0 comments on commit 16d1520

Please sign in to comment.