Skip to content

Commit

Permalink
Add Set[Pre|Post]ImportHook to BaseMeta interface (#548)
Browse files Browse the repository at this point in the history
* Add Set[Pre|Post]ImportHook to BaseMeta interface

* Modify callback signature
  • Loading branch information
magodo authored Aug 6, 2024
1 parent 5743ff5 commit 155b4b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
15 changes: 13 additions & 2 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type BaseMeta interface {
// CleanUpWorkspace is a weired method that is only meant to be used internally by aztfexport, which under the hood will remove everything in the output directory, except the generated TF config.
// This method does nothing if HCLOnly in the Config is not set.
CleanUpWorkspace(ctx context.Context) error

SetPreImportHook(config.ImportCallback)
SetPostImportHook(config.ImportCallback)
}

var _ BaseMeta = &baseMeta{}
Expand Down Expand Up @@ -390,11 +393,11 @@ func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) e
}
startTime := time.Now()
if meta.preImportHook != nil {
meta.preImportHook(startTime, total, iitem)
meta.preImportHook(startTime, iitem)
}
meta.importItem(ctx, item, i)
if meta.postImportHook != nil {
meta.postImportHook(startTime, total, iitem)
meta.postImportHook(startTime, iitem)
}
}
return i, nil
Expand Down Expand Up @@ -588,6 +591,14 @@ func (meta baseMeta) CleanUpWorkspace(_ context.Context) error {
return nil
}

func (meta *baseMeta) SetPreImportHook(cb config.ImportCallback) {
meta.preImportHook = cb
}

func (meta *baseMeta) SetPostImportHook(cb config.ImportCallback) {
meta.postImportHook = cb
}

func (meta baseMeta) generateCfg(ctx context.Context, l ImportList, cfgTrans ...TFConfigTransformer) error {
cfginfos, err := meta.stateToConfig(ctx, l)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/meta/meta_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"io"
"log/slog"
"time"

"github.com/Azure/aztfexport/pkg/config"
)

type MetaGroupDummy struct {
rg string
providerName string
}

func NewGroupMetaDummy(rg string, providerName string) MetaGroupDummy {
return MetaGroupDummy{rg: rg, providerName: providerName}
func NewGroupMetaDummy(rg string, providerName string) *MetaGroupDummy {
return &MetaGroupDummy{rg: rg, providerName: providerName}
}

func (m MetaGroupDummy) Logger() *slog.Logger {
Expand Down Expand Up @@ -96,3 +98,9 @@ func (m MetaGroupDummy) CleanUpWorkspace(_ context.Context) error {
time.Sleep(500 * time.Millisecond)
return nil
}

func (meta *MetaGroupDummy) SetPreImportHook(cb config.ImportCallback) {
}

func (meta *MetaGroupDummy) SetPostImportHook(cb config.ImportCallback) {
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ImportItem struct {
TFAddr tfaddr.TFAddr
}

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

type OutputFileNames struct {
// The filename for the generated "terraform.tf" (default)
Expand Down
1 change: 1 addition & 0 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meta
import (
"context"
"fmt"

"github.com/Azure/aztfexport/internal/meta"
"github.com/Azure/aztfexport/pkg/config"
)
Expand Down

0 comments on commit 155b4b7

Please sign in to comment.