driftctl/pkg/terraform/hcl/backend.go

95 lines
2.5 KiB
Go
Raw Normal View History

2022-06-24 08:12:32 +00:00
package hcl
import (
"fmt"
"path"
"github.com/hashicorp/hcl/v2"
"github.com/snyk/driftctl/pkg/iac/config"
"github.com/snyk/driftctl/pkg/iac/terraform/state"
"github.com/snyk/driftctl/pkg/iac/terraform/state/backend"
)
type BackendBlock struct {
2022-07-11 13:47:28 +00:00
Name string `hcl:"name,label"`
Path string `hcl:"path,optional"`
WorkspaceDir string `hcl:"workspace_dir,optional"`
Bucket string `hcl:"bucket,optional"`
Key string `hcl:"key,optional"`
Region string `hcl:"region,optional"`
Prefix string `hcl:"prefix,optional"`
ContainerName string `hcl:"container_name,optional"`
WorkspaceKeyPrefix string `hcl:"workspace_key_prefix,optional"`
Remain hcl.Body `hcl:",remain"`
2022-06-24 08:12:32 +00:00
}
func (b BackendBlock) SupplierConfig(workspace string) *config.SupplierConfig {
2022-06-24 08:12:32 +00:00
switch b.Name {
case "local":
return b.parseLocalBackend()
case "s3":
return b.parseS3Backend(workspace)
2022-06-24 08:12:32 +00:00
case "gcs":
return b.parseGCSBackend(workspace)
2022-06-24 08:12:32 +00:00
case "azurerm":
return b.parseAzurermBackend(workspace)
2022-06-24 08:12:32 +00:00
}
return nil
}
func (b BackendBlock) parseLocalBackend() *config.SupplierConfig {
if b.Path == "" {
return nil
}
return &config.SupplierConfig{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyFile,
Path: path.Join(b.WorkspaceDir, b.Path),
}
}
func (b BackendBlock) parseS3Backend(ws string) *config.SupplierConfig {
2022-06-24 08:12:32 +00:00
if b.Bucket == "" || b.Key == "" {
return nil
}
2022-07-12 10:14:55 +00:00
keyPrefix := b.WorkspaceKeyPrefix
if ws != DefaultStateName {
if b.WorkspaceKeyPrefix == "" {
b.WorkspaceKeyPrefix = "env:"
}
keyPrefix = path.Join(b.WorkspaceKeyPrefix, ws)
2022-07-11 13:47:28 +00:00
}
2022-07-12 10:14:55 +00:00
2022-06-24 08:12:32 +00:00
return &config.SupplierConfig{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyS3,
2022-07-12 10:14:55 +00:00
Path: path.Join(b.Bucket, keyPrefix, b.Key),
2022-06-24 08:12:32 +00:00
}
}
func (b BackendBlock) parseGCSBackend(ws string) *config.SupplierConfig {
2022-06-24 08:12:32 +00:00
if b.Bucket == "" || b.Prefix == "" {
return nil
}
return &config.SupplierConfig{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyGS,
Path: fmt.Sprintf("%s.tfstate", path.Join(b.Bucket, b.Prefix, ws)),
2022-06-24 08:12:32 +00:00
}
}
func (b BackendBlock) parseAzurermBackend(ws string) *config.SupplierConfig {
2022-06-24 08:12:32 +00:00
if b.ContainerName == "" || b.Key == "" {
return nil
}
if ws != DefaultStateName {
b.Key = fmt.Sprintf("%senv:%s", b.Key, ws)
}
2022-06-24 08:12:32 +00:00
return &config.SupplierConfig{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyAzureRM,
Path: path.Join(b.ContainerName, b.Key),
}
}