Skip to content

Commit

Permalink
Internal: Split the ImportCallback to PreImportHook and PostImportHoo…
Browse files Browse the repository at this point in the history
…k, also add a new parameter `startTime` (#541)
  • Loading branch information
magodo authored Jul 19, 2024
1 parent 26291d6 commit b5185d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
30 changes: 19 additions & 11 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

tfjson "github.com/hashicorp/terraform-json"

Expand Down Expand Up @@ -95,7 +96,8 @@ type baseMeta struct {
providerConfig map[string]cty.Value
fullConfig bool
parallelism int
importCallback config.ImportCallback
preImportHook config.ImportCallback
postImportHook config.ImportCallback
generateImportFile bool

hclOnly bool
Expand Down Expand Up @@ -221,7 +223,8 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) {
providerName: cfg.ProviderName,
fullConfig: cfg.FullConfig,
parallelism: cfg.Parallelism,
importCallback: cfg.ImportCallback,
preImportHook: cfg.PreImportHook,
postImportHook: cfg.PostImportHook,
generateImportFile: cfg.GenerateImportBlock,
hclOnly: cfg.HCLOnly,
tfclient: cfg.TFClient,
Expand Down Expand Up @@ -323,15 +326,19 @@ func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) e
i := i
wp.AddTask(func() (interface{}, error) {
for item := range itemsCh {
iitem := config.ImportItem{
AzureResourceID: item.AzureResourceID,
TFResourceId: item.TFResourceId,
ImportError: item.ImportError,
TFAddr: item.TFAddr,
}
startTime := time.Now()
if meta.preImportHook != nil {
meta.preImportHook(startTime, total, iitem)
}
meta.importItem(ctx, item, i)
if meta.importCallback != nil {
item := config.ImportItem{
AzureResourceID: item.AzureResourceID,
TFResourceId: item.TFResourceId,
ImportError: item.ImportError,
TFAddr: item.TFAddr,
}
meta.importCallback(total, item)
if meta.postImportHook != nil {
meta.postImportHook(startTime, total, iitem)
}
}
return i, nil
Expand Down Expand Up @@ -858,7 +865,7 @@ func (meta *baseMeta) importItem_tf(ctx context.Context, item *ImportItem, impor
func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, importIdx int) {
// Import resources
addr := item.TFAddr.String()
meta.Logger().Info("Importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
meta.Logger().Debug("Importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
// The actual resource type names in telemetry is redacted
meta.tc.Trace(telemetry.Info, fmt.Sprintf("Importing %s as %s", item.AzureResourceID.TypeString(), addr))

Expand Down Expand Up @@ -897,6 +904,7 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
return
}

meta.Logger().Debug("Finish importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
item.State = readResp.NewState
item.ImportError = nil
item.Imported = true
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"log/slog"
"time"

"github.com/Azure/aztfexport/internal/tfaddr"
"github.com/Azure/aztfexport/pkg/telemetry"
Expand All @@ -26,7 +27,7 @@ type ImportItem struct {
TFAddr tfaddr.TFAddr
}

type ImportCallback func(total int, item ImportItem)
type ImportCallback func(startTime time.Time, total int, item ImportItem)

type OutputFileNames struct {
// The filename for the generated "terraform.tf" (default)
Expand Down Expand Up @@ -73,8 +74,10 @@ type CommonConfig struct {
FullConfig bool
// Parallelism specifies the parallelism for the process
Parallelism int
// ImportCallback is a way to inspect each resource after being imported during ParallelImport
ImportCallback ImportCallback
// PreImportHook is called before each resource is imported during ParallelImport
PreImportHook ImportCallback
// PostImportHook is called after each resource is imported during ParallelImport
PostImportHook ImportCallback
// ModulePath specifies the path of the module (e.g. "module1.module2") where the resources will be imported and config generated.
// Note that only modules whose "source" is local path is supported. By default, it is the root module.
ModulePath string
Expand Down

0 comments on commit b5185d6

Please sign in to comment.