Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-509 added terraform_config_file parameter #505

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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