Skip to content

Commit

Permalink
refactor: use workspace name in azurerm backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev-snyk committed Jul 12, 2022
1 parent f4a92e7 commit cb5ab70
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/terraform/hcl/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions pkg/terraform/hcl/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.terraform
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
Original file line number Diff line number Diff line change
@@ -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 {}
}

0 comments on commit cb5ab70

Please sign in to comment.