Skip to content

Commit cb5ab70

Browse files
refactor: use workspace name in azurerm backend
1 parent f4a92e7 commit cb5ab70

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

pkg/terraform/hcl/backend.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (b BackendBlock) SupplierConfig(workspace string) *config.SupplierConfig {
3232
case "gcs":
3333
return b.parseGCSBackend(workspace)
3434
case "azurerm":
35-
return b.parseAzurermBackend()
35+
return b.parseAzurermBackend(workspace)
3636
}
3737
return nil
3838
}
@@ -79,10 +79,13 @@ func (b BackendBlock) parseGCSBackend(ws string) *config.SupplierConfig {
7979
}
8080
}
8181

82-
func (b BackendBlock) parseAzurermBackend() *config.SupplierConfig {
82+
func (b BackendBlock) parseAzurermBackend(ws string) *config.SupplierConfig {
8383
if b.ContainerName == "" || b.Key == "" {
8484
return nil
8585
}
86+
if ws != DefaultStateName {
87+
b.Key = fmt.Sprintf("%senv:%s", b.Key, ws)
88+
}
8689
return &config.SupplierConfig{
8790
Key: state.TerraformStateReaderSupplier,
8891
Backend: backend.BackendKeyAzureRM,

pkg/terraform/hcl/backend_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ func TestBackend_SupplierConfig(t *testing.T) {
9696
Path: "states/prod.terraform.tfstate",
9797
},
9898
},
99+
{
100+
name: "test with Azure backend block with non-default workspace",
101+
filename: "testdata/azurerm_backend_workspace/azurerm_backend_block.tf",
102+
want: &config.SupplierConfig{
103+
Key: "tfstate",
104+
Backend: "azurerm",
105+
Path: "states/prod.terraform.tfstateenv:bar",
106+
},
107+
},
99108
{
100109
name: "test with unknown backend",
101110
filename: "testdata/unknown_backend_block.tf",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.terraform
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
backend "azurerm" {
3+
resource_group_name = "StorageAccount-ResourceGroup"
4+
storage_account_name = "abcd1234"
5+
container_name = "states"
6+
key = "prod.terraform.tfstate"
7+
}
8+
}
9+
10+
provider "azurerm" {
11+
features {}
12+
}

0 commit comments

Comments
 (0)