Adds logging for workspace handling

s3/request-logging
Graham Davison 3 years ago
parent f2ef4e155f
commit dac8d5935f

@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
baselogging "github.com/hashicorp/aws-sdk-go-base/v2/logging"
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/states/remote"
@ -25,6 +26,11 @@ func (b *Backend) Workspaces() ([]string, error) {
const maxKeys = 1000
ctx := context.TODO()
log := logger()
log = logWithOperation(log, operationBackendWorkspaces)
log = log.With(
logKeyBucket, b.bucketName,
)
prefix := ""
@ -32,6 +38,10 @@ func (b *Backend) Workspaces() ([]string, error) {
prefix = b.workspaceKeyPrefix + "/"
}
log = log.With(
logKeyBackendWorkspacePrefix, prefix,
)
params := &s3.ListObjectsV2Input{
Bucket: aws.String(b.bucketName),
Prefix: aws.String(prefix),
@ -40,6 +50,9 @@ func (b *Backend) Workspaces() ([]string, error) {
wss := []string{backend.DefaultStateName}
ctx, baselog := baselogging.NewHcLogger(ctx, log)
ctx = baselogging.RegisterLogger(ctx, baselog)
pages := s3.NewListObjectsV2Paginator(b.s3Client, params)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
@ -102,10 +115,18 @@ func (b *Backend) keyEnv(key string) string {
}
func (b *Backend) DeleteWorkspace(name string, _ bool) error {
log := logger()
log = logWithOperation(log, operationBackendDeleteWorkspace)
log = log.With(
logKeyBackendWorkspace, name,
)
if name == backend.DefaultStateName || name == "" {
return fmt.Errorf("can't delete default state")
}
log.Info("Deleting workspace")
client, err := b.remoteClient(name)
if err != nil {
return err

@ -4,6 +4,7 @@ import (
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/internal/states/statemgr"
)
@ -23,14 +24,17 @@ const (
)
const (
logKeyBackendOperation = "tf_backend.operation"
logKeyBackendOperation = "tf_backend.operation"
logKeyBackendRequestId = "tf_backend.req_id" // Using "req_id" to match pattern for provider logging
logKeyBackendWorkspace = "tf_backend.workspace"
logKeyBackendWorkspacePrefix = "tf_backend.workspace-prefix"
)
const (
operationBackendConfigSchema = "ConfigSchema"
operationBackendPrepareConfig = "PrepareConfig"
operationBackendConfigure = "Configure"
operationBackendStateMgr = "StateMgr"
// operationBackendConfigSchema = "ConfigSchema"
// operationBackendPrepareConfig = "PrepareConfig"
operationBackendConfigure = "Configure"
// operationBackendStateMgr = "StateMgr"
operationBackendDeleteWorkspace = "DeleteWorkspace"
operationBackendWorkspaces = "Workspaces"
)

Loading…
Cancel
Save