Skip to content

Commit

Permalink
Cfg: Add ImportCallback to allow caller inspect import status
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Jun 28, 2024
1 parent dd9c5fa commit c0ac45f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type baseMeta struct {
providerConfig map[string]cty.Value
fullConfig bool
parallelism int
importCallback config.ImportCallback
generateImportFile bool

hclOnly bool
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
})
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c0ac45f

Please sign in to comment.