refactor: use workspace name in azurerm backend

main
sundowndev-snyk 2022-07-12 14:21:26 +04:00
parent f4a92e7b32
commit cb5ab70559
No known key found for this signature in database
GPG Key ID: A4A2BE47AC4C6A68
5 changed files with 28 additions and 2 deletions

View File

@ -32,7 +32,7 @@ func (b BackendBlock) SupplierConfig(workspace string) *config.SupplierConfig {
case "gcs":
return b.parseGCSBackend(workspace)
case "azurerm":
return b.parseAzurermBackend()
return b.parseAzurermBackend(workspace)
}
return nil
}
@ -79,10 +79,13 @@ func (b BackendBlock) parseGCSBackend(ws string) *config.SupplierConfig {
}
}
func (b BackendBlock) parseAzurermBackend() *config.SupplierConfig {
func (b BackendBlock) parseAzurermBackend(ws string) *config.SupplierConfig {
if b.ContainerName == "" || b.Key == "" {
return nil
}
if ws != DefaultStateName {
b.Key = fmt.Sprintf("%senv:%s", b.Key, ws)
}
return &config.SupplierConfig{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyAzureRM,

View File

@ -96,6 +96,15 @@ func TestBackend_SupplierConfig(t *testing.T) {
Path: "states/prod.terraform.tfstate",
},
},
{
name: "test with Azure backend block with non-default workspace",
filename: "testdata/azurerm_backend_workspace/azurerm_backend_block.tf",
want: &config.SupplierConfig{
Key: "tfstate",
Backend: "azurerm",
Path: "states/prod.terraform.tfstateenv:bar",
},
},
{
name: "test with unknown backend",
filename: "testdata/unknown_backend_block.tf",

View File

@ -0,0 +1 @@
!.terraform

View File

@ -0,0 +1 @@
bar

View File

@ -0,0 +1,12 @@
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "states"
key = "prod.terraform.tfstate"
}
}
provider "azurerm" {
features {}
}