From 8a8f66928aa42cf0de0d269a013ea5424b0d7828 Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Fri, 9 Sep 2022 13:03:19 +0100 Subject: [PATCH] Added state_name so it allows to set the name of the state file. --- internal/commands/tfenv.go | 7 ++++--- internal/config/config.go | 1 + internal/config/infra.go | 1 + internal/schema/ize-spec.json | 8 ++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/commands/tfenv.go b/internal/commands/tfenv.go index 8481bf9b..dc56e518 100644 --- a/internal/commands/tfenv.go +++ b/internal/commands/tfenv.go @@ -78,14 +78,15 @@ func (o *TfenvOptions) Run() error { func GenerateTerraformFiles(project *config.Project, terraformStateBucketName string) error { pterm.DefaultSection.Printfln("Starting generate terraform files") - - stateKey := fmt.Sprintf("%v/terraform.tfstate", project.Env) - var tf config.Terraform + if project.Terraform != nil { tf = *project.Terraform["infra"] } + stateName := tf.StateName + stateKey := fmt.Sprintf("%v/%v.tfstate", project.Env, stateName) + if len(terraformStateBucketName) != 0 { tf.StateBucketName = terraformStateBucketName } diff --git a/internal/config/config.go b/internal/config/config.go index 81e12a1a..9da093c0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -235,6 +235,7 @@ func InitConfig() { viper.SetDefault("PREFER_RUNTIME", "native") viper.SetDefault("CUSTOM_PROMPT", false) viper.SetDefault("PLAIN_TEXT", false) + viper.SetDefault("terraform.infra.state_name", "terraform") home, err := os.UserHomeDir() if err != nil { diff --git a/internal/config/infra.go b/internal/config/infra.go index 571c46ac..ad075cd6 100644 --- a/internal/config/infra.go +++ b/internal/config/infra.go @@ -9,6 +9,7 @@ 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"` diff --git a/internal/schema/ize-spec.json b/internal/schema/ize-spec.json index efbe2289..c84b426f 100644 --- a/internal/schema/ize-spec.json +++ b/internal/schema/ize-spec.json @@ -188,6 +188,10 @@ "type": "string", "description": "(optional) Terraform state bucket name can be specified here. Normally it's generated and defaults to -tf-state" }, + "state_name": { + "type": "string", + "description": "(optional) Terraform state name that will be used in the .tfstate file. Normally it's 'terraform' or state name." + }, "root_domain_name" : { "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`" @@ -443,6 +447,10 @@ "type": "string", "description": "(optional) Terraform state bucket name can be specified here. Normally it's generated and defaults to -tf-state" }, + "state_name": { + "type": "string", + "description": "(optional) Terraform state name that will be used in the .tfstate file. Normally it's 'terraform' or state name." + }, "root_domain_name" : { "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`"