diff --git a/internal/meta/base_meta.go b/internal/meta/base_meta.go index 2b17b27..67d16c4 100644 --- a/internal/meta/base_meta.go +++ b/internal/meta/base_meta.go @@ -92,6 +92,7 @@ type baseMeta struct { providerConfig map[string]cty.Value fullConfig bool parallelism int + importCallback config.ImportCallback generateImportFile bool hclOnly bool @@ -216,6 +217,7 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) { providerName: cfg.ProviderName, fullConfig: cfg.FullConfig, parallelism: cfg.Parallelism, + importCallback: cfg.ImportCallback, generateImportFile: cfg.GenerateImportBlock, hclOnly: cfg.HCLOnly, tfclient: cfg.TFClient, @@ -273,7 +275,8 @@ func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) e meta.tc.Trace(telemetry.Info, "ParallelImport Enter") defer meta.tc.Trace(telemetry.Info, "ParallelImport Leave") - itemsCh := make(chan *ImportItem, len(items)) + total := len(items) + itemsCh := make(chan *ImportItem, total) for _, item := range items { itemsCh <- item } @@ -313,6 +316,15 @@ func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) e wp.AddTask(func() (interface{}, error) { for item := range itemsCh { 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) + } } return i, nil }) diff --git a/pkg/config/config.go b/pkg/config/config.go index 678bd14..d4260e4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,13 +1,31 @@ package config import ( + "github.com/Azure/aztfexport/internal/tfaddr" "github.com/Azure/aztfexport/pkg/telemetry" "github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" + "github.com/magodo/armid" "github.com/magodo/terraform-client-go/tfclient" "github.com/zclconf/go-cty/cty" ) +type ImportItem struct { + // Azure resource Id + AzureResourceID armid.ResourceId + + // The TF resource id + TFResourceId string + + // Whether this azure resource failed to import into terraform (this might due to the TFResourceType doesn't match the resource) + ImportError error + + // The terraform resource + TFAddr tfaddr.TFAddr +} + +type ImportCallback func(total int, item ImportItem) + type OutputFileNames struct { // The filename for the generated "terraform.tf" (default) TerraformFileName string @@ -52,6 +70,8 @@ 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 // 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