diff --git a/pkg/terraform/hcl/backend.go b/pkg/terraform/hcl/backend.go index 03b63ce2..324ec316 100644 --- a/pkg/terraform/hcl/backend.go +++ b/pkg/terraform/hcl/backend.go @@ -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, diff --git a/pkg/terraform/hcl/backend_test.go b/pkg/terraform/hcl/backend_test.go index 3076fd85..1b05fde5 100644 --- a/pkg/terraform/hcl/backend_test.go +++ b/pkg/terraform/hcl/backend_test.go @@ -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", diff --git a/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.gitignore b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.gitignore new file mode 100644 index 00000000..e920ea56 --- /dev/null +++ b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.gitignore @@ -0,0 +1 @@ +!.terraform diff --git a/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.terraform/environment b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.terraform/environment new file mode 100644 index 00000000..5716ca59 --- /dev/null +++ b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/.terraform/environment @@ -0,0 +1 @@ +bar diff --git a/pkg/terraform/hcl/testdata/azurerm_backend_workspace/azurerm_backend_block.tf b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/azurerm_backend_block.tf new file mode 100644 index 00000000..b47475ce --- /dev/null +++ b/pkg/terraform/hcl/testdata/azurerm_backend_workspace/azurerm_backend_block.tf @@ -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 {} +}