Skip to content

Commit

Permalink
Merge pull request #505 from hazelops/IZE-509-ability-to-provide/set-…
Browse files Browse the repository at this point in the history
…backend.tf-with-file-name-value

IZE-509 added `terraform_config_file` parameter
  • Loading branch information
psihachina authored Oct 20, 2022
2 parents 42cf8c9 + 7d984af commit 0f37e31
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
14 changes: 10 additions & 4 deletions internal/commands/tfenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
Expand All @@ -12,9 +16,6 @@ import (
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
)

type TfenvOptions struct {
Expand Down Expand Up @@ -139,12 +140,17 @@ func GenerateTerraformFiles(name string, terraformStateBucketName string, projec
stackPath = project.EnvDir
}

if len(tf.TerraformConfigFile) == 0 {
tf.TerraformConfigFile = "backend.tf"
}

logrus.Debugf("backend opts: %s", backendOpts)
logrus.Debugf("state dir path: %s", stackPath)
logrus.Debugf("config file name: %s", tf.TerraformConfigFile)

err := template.GenerateBackendTf(
backendOpts,
stackPath,
filepath.Join(stackPath, tf.TerraformConfigFile),
)
if err != nil {
pterm.Error.Printfln("Generate terraform file for \"%s\" not completed", name)
Expand Down
17 changes: 9 additions & 8 deletions internal/config/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ type Infra struct {
}

type Terraform struct {
Version string `mapstructure:",omitempty"`
StateBucketRegion string `mapstructure:"state_bucket_region,omitempty"`
StateBucketName string `mapstructure:"state_bucket_name,omitempty"`
StateName string `mapstructure:"state_name,omitempty"`
RootDomainName string `mapstructure:"root_domain_name,omitempty"`
AwsRegion string `mapstructure:"aws_region,omitempty"`
AwsProfile string `mapstructure:"aws_profile,omitempty"`
DependsOn []string `mapstructure:"depends_on,omitempty"`
Version string `mapstructure:",omitempty"`
StateBucketRegion string `mapstructure:"state_bucket_region,omitempty"`
StateBucketName string `mapstructure:"state_bucket_name,omitempty"`
StateName string `mapstructure:"state_name,omitempty"`
RootDomainName string `mapstructure:"root_domain_name,omitempty"`
TerraformConfigFile string `mapstructure:"terraform_config_file,omitempty"`
AwsRegion string `mapstructure:"aws_region,omitempty"`
AwsProfile string `mapstructure:"aws_profile,omitempty"`
DependsOn []string `mapstructure:"depends_on,omitempty"`
}

type Tunnel struct {
Expand Down
5 changes: 5 additions & 0 deletions internal/schema/ize-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@
"type": "string",
"description": "(optional) Root domain name can be set here. This is the main domain that will be passed to the terraform. Generally if your app lives at 'api.dev.nutcorp.net' the root domain is `nutcorp.net`"
},
"terraform_config_file" : {
"type": "string",
"description": "(optional) Terraform config file name.",
"default": "backend.tf"
},
"aws_region" : {
"type": "string",
"description": "(optional) Terraform-specific AWS Region of this environment should be specified here. Normally global AWS_REGION is used."
Expand Down
15 changes: 6 additions & 9 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
)

const (
backend = "backend.tf"
vars = "terraform.tfvars"
ize = "ize.hcl"
vars = "terraform.tfvars"
ize = "ize.hcl"
)

func GenerateVarsTf(opts VarsOpts, path string) error {
Expand Down Expand Up @@ -210,11 +209,9 @@ func GenerateBackendTf(opts BackendOpts, path string) error {
backendBlock.Body().SetAttributeValue("dynamodb_table", cty.StringVal(opts.TERRAFORM_STATE_DYNAMODB_TABLE))
}

backendPath := filepath.Join(path, backend)

_, err := os.Stat(backendPath)
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
file, err := os.Create(backendPath)
file, err := os.Create(path)
if err != nil {
return err
}
Expand All @@ -230,15 +227,15 @@ func GenerateBackendTf(opts BackendOpts, path string) error {
}

newHash := md5.Sum(f.Bytes())
oldFile, err := os.ReadFile(backendPath)
oldFile, err := os.ReadFile(path)
if err != nil {
return err
}

oldHash := md5.Sum(oldFile)

if !reflect.DeepEqual(newHash, oldHash) {
file, err := os.Create(backendPath)
file, err := os.Create(path)
if err != nil {
return err
}
Expand Down

0 comments on commit 0f37e31

Please sign in to comment.