Skip to content

Commit

Permalink
TODO: hclog from slog
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Jun 13, 2024
1 parent 523f5b8 commit 2d4d133
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 71 deletions.
2 changes: 1 addition & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) {
// #nosec G204
tfc, err := tfclient.New(tfclient.Option{
Cmd: exec.Command(flagset.hflagTFClientPluginPath),
Logger: hclog.NewNullLogger(),
Logger: hclog.NewNullLogger(), // will be configured later in main
})
if err != nil {
return cfg, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/hashicorp/terraform-json v0.19.0
github.com/hexops/gotextdiff v1.0.3
github.com/magodo/armid v0.0.0-20230511151020-27880e5961c3
github.com/magodo/azlist v0.0.0-20231101012443-e30f2da262c1
github.com/magodo/azlist v0.0.0-20240613024003-b4529218cc6a
github.com/magodo/aztft v0.3.1-0.20240429022627-002cdc06267a
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6
github.com/magodo/terraform-client-go v0.0.0-20230323074119-02ceb732dd25
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magodo/armid v0.0.0-20230511151020-27880e5961c3 h1:ob6vk6PlChZvutcxcLnmPH/VNmJEuwz+TmCYCVtJqeA=
github.com/magodo/armid v0.0.0-20230511151020-27880e5961c3/go.mod h1:rR8E7zfGMbmfnSQvrkFiWYdhrfTqsVSltelnZB09BwA=
github.com/magodo/azlist v0.0.0-20231101012443-e30f2da262c1 h1:9SXoK38cfRZac8LwA3BmEhVjuU/JGPb6I3MKvpHA4To=
github.com/magodo/azlist v0.0.0-20231101012443-e30f2da262c1/go.mod h1:xefFDOxzRssOEjGoxvrO8jeTWlzHXbY6sCJYOt+Jh5k=
github.com/magodo/azlist v0.0.0-20240613024003-b4529218cc6a h1:CFX3fvA3ajq81F7fkSBF+RgrZEgyuNP8x2QWt4Zhw5k=
github.com/magodo/azlist v0.0.0-20240613024003-b4529218cc6a/go.mod h1:xefFDOxzRssOEjGoxvrO8jeTWlzHXbY6sCJYOt+Jh5k=
github.com/magodo/aztft v0.3.1-0.20240429022627-002cdc06267a h1:HYp5h1HOwwrFI6CVSeJTVfnXyO7Es6Kx8cyw3UlomDo=
github.com/magodo/aztft v0.3.1-0.20240429022627-002cdc06267a/go.mod h1:hqk4M4qig7+LTNKeHCNgi+hZfJBndnx4oSNAvP5KT0Y=
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6 h1:CElHO4hPXC+Eivy8sUC/WrnH3jmQzdF2x0lEXBEYul8=
Expand Down
34 changes: 17 additions & 17 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) e
// Ensure the state file is removed after this round import, preparing for the next round.
defer os.Remove(stateFile)

log.Printf("[DEBUG] Merging terraform state file %s (tfmerge)", stateFile)
log.Debug("Merging terraform state file (tfmerge)", "file", stateFile)
newState, err := tfmerge.Merge(ctx, meta.tf, meta.baseState, stateFile)
if err != nil {
return fmt.Errorf("failed to merge state file: %v", err)
Expand Down Expand Up @@ -667,12 +667,12 @@ func (meta *baseMeta) initImportDirs() error {
}

func (meta *baseMeta) initTF(ctx context.Context) error {
log.Printf("[INFO] Init Terraform")
log.Info("Init Terraform")
execPath, err := FindTerraform(ctx)
if err != nil {
return fmt.Errorf("error finding a terraform exectuable: %w", err)
}
log.Printf("[INFO] Find terraform binary at %s", execPath)
log.Info("Found terraform binary", "path", execPath)

newTF := func(dir string) (*tfexec.Terraform, error) {
tf, err := tfexec.NewTerraform(dir, execPath)
Expand Down Expand Up @@ -708,7 +708,7 @@ func (meta *baseMeta) initTF(ctx context.Context) error {
}

func (meta *baseMeta) initProvider(ctx context.Context) error {
log.Printf("[INFO] Init provider")
log.Info("Init provider")

module, diags := tfconfig.LoadModule(meta.outdir)
if diags.HasErrors() {
Expand All @@ -721,7 +721,7 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {
}

if module.ProviderConfigs[meta.providerName] == nil {
log.Printf("[INFO] Output directory doesn't contain provider setting, create one then")
log.Info("Output directory doesn't contain provider setting, create one then")
cfgFile := filepath.Join(meta.outdir, meta.outputFileNames.ProviderFileName)
// #nosec G306
if err := os.WriteFile(cfgFile, []byte(meta.buildProviderConfig()), 0644); err != nil {
Expand All @@ -730,7 +730,7 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {
}

if tfblock == nil {
log.Printf("[INFO] Output directory doesn't contain terraform block, create one then")
log.Info("Output directory doesn't contain terraform block, create one then")
cfgFile := filepath.Join(meta.outdir, meta.outputFileNames.TerraformFileName)
// #nosec G306
if err := os.WriteFile(cfgFile, []byte(meta.buildTerraformConfig(meta.backendType)), 0644); err != nil {
Expand All @@ -744,7 +744,7 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {
opts = append(opts, tfexec.BackendConfig(opt))
}

log.Printf(`[DEBUG] Run "terraform init" for the output directory %s`, meta.outdir)
log.Debug(`Run "terraform init" for the output directory`, "dir", meta.outdir)
if err := meta.tf.Init(ctx, opts...); err != nil {
return fmt.Errorf("error running terraform init for the output directory: %s", err)
}
Expand All @@ -766,9 +766,9 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {
return nil, fmt.Errorf("error creating terraform config: %w", err)
}
if meta.devProvider {
log.Printf(`[DEBUG] Skip running "terraform init" for the import directory (dev provider): %s`, meta.importBaseDirs[i])
log.Debug(`Skip running "terraform init" for the import directory (dev provider)`, "dir", meta.importBaseDirs[i])
} else {
log.Printf(`[DEBUG] Run "terraform init" for the import directory %s`, meta.importBaseDirs[i])
log.Debug(`Run "terraform init" for the import directory`, "dir", meta.importBaseDirs[i])
if err := meta.importTFs[i].Init(ctx); err != nil {
return nil, fmt.Errorf("error running terraform init: %s", err)
}
Expand All @@ -785,7 +785,7 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {

func (meta *baseMeta) importItem(ctx context.Context, item *ImportItem, importIdx int) {
if item.Skip() {
log.Printf("[INFO] Skipping %s", item.TFResourceId)
log.Info("Skipping resource", "tf_id", item.TFResourceId)
return
}

Expand All @@ -807,7 +807,7 @@ func (meta *baseMeta) importItem_tf(ctx context.Context, item *ImportItem, impor
// #nosec G306
if err := os.WriteFile(cfgFile, []byte(tpl), 0644); err != nil {
err := fmt.Errorf("generating resource template file for %s: %w", item.TFAddr, err)
log.Printf("[ERROR] %v", err)
log.Error("Failed to generate resource template file", "error", err, "tf_addr", item.TFAddr)
item.ImportError = err
return
}
Expand All @@ -819,13 +819,13 @@ func (meta *baseMeta) importItem_tf(ctx context.Context, item *ImportItem, impor
addr = meta.moduleAddr + "." + addr
}

log.Printf("[INFO] Importing %s as %s", item.TFResourceId, addr)
log.Info("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))

err := tf.Import(ctx, addr, item.TFResourceId)
if err != nil {
log.Printf("[ERROR] Importing %s: %v", item.TFAddr, err)
log.Error("Terraform import failed", "tf_addr", item.TFAddr, "error", err)
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", err))
} else {
Expand All @@ -838,7 +838,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()
log.Printf("[INFO] Importing %s as %s", item.TFResourceId, addr)
log.Info("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 All @@ -847,7 +847,7 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
ID: item.TFResourceId,
})
if diags.HasErrors() {
log.Printf("[ERROR] Importing %s: %v", item.TFAddr, diags)
log.Error("Terraform import failed", "tf_addr", item.TFAddr, "error", diags.Err())
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
item.ImportError = diags.Err()
Expand All @@ -856,7 +856,7 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
}
if len(importResp.ImportedResources) != 1 {
err := fmt.Errorf("expect 1 resource being imported, got=%d", len(importResp.ImportedResources))
log.Printf("[ERROR] %s", err)
log.Error(err.Error())
meta.tc.Trace(telemetry.Error, err.Error())
item.ImportError = err
item.Imported = false
Expand All @@ -869,7 +869,7 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
Private: res.Private,
})
if diags.HasErrors() {
log.Printf("[ERROR] Reading %s: %v", item.TFAddr, diags)
log.Error("Terraform read a resource failed", "tf_addr", item.TFAddr, "error", diags.Err())
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Reading %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
item.ImportError = diags.Err()
Expand Down
4 changes: 2 additions & 2 deletions internal/meta/meta_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MetaMap struct {
}

func NewMetaMap(cfg config.Config) (*MetaMap, error) {
log.Printf("[INFO] New map meta")
log.Info("New map meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand All @@ -42,7 +42,7 @@ func (meta MetaMap) ScopeName() string {
func (meta *MetaMap) ListResource(_ context.Context) (ImportList, error) {
var m resmap.ResourceMapping

log.Printf("[DEBUG] Read resource set from mapping file")
log.Debug("Read resource set from mapping file")
b, err := os.ReadFile(meta.mappingFile)
if err != nil {
return nil, fmt.Errorf("reading mapping file %s: %v", meta.mappingFile, err)
Expand Down
12 changes: 6 additions & 6 deletions internal/meta/meta_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MetaQuery struct {
}

func NewMetaQuery(cfg config.Config) (*MetaQuery, error) {
log.Printf("[INFO] New query meta")
log.Info("New query meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand All @@ -49,26 +49,26 @@ func (meta MetaQuery) ScopeName() string {
}

func (meta *MetaQuery) ListResource(ctx context.Context) (ImportList, error) {
log.Printf("[DEBUG] Query resource set")
log.Debug("Query resource set")
rset, err := meta.queryResourceSet(ctx, meta.argPredicate, meta.recursiveQuery)
if err != nil {
return nil, err
}
var rl []resourceset.TFResource
if meta.useAzAPI() {
log.Printf("[DEBUG] Azure Resource set map to TF resource set")
log.Debug("Azure Resource set map to TF resource set")
rl = rset.ToTFAzAPIResources()
} else {
log.Printf("[DEBUG] Populate resource set")
log.Debug("Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
log.Debug("Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}

log.Printf("[DEBUG] Azure Resource set map to TF resource set")
log.Debug("Azure Resource set map to TF resource set")
rl = rset.ToTFAzureRMResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/meta/meta_res.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MetaResource struct {
}

func NewMetaResource(cfg config.Config) (*MetaResource, error) {
log.Printf("[INFO] New resource meta")
log.Info("New resource meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -51,7 +51,7 @@ func (meta *MetaResource) ListResource(_ context.Context) (ImportList, error) {
},
},
}
log.Printf("[DEBUG] Azure Resource set map to TF resource set")
log.Debug("Azure Resource set map to TF resource set")

var rl []resourceset.TFResource
if meta.useAzAPI() {
Expand Down
11 changes: 5 additions & 6 deletions internal/meta/meta_rg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MetaResourceGroup struct {
}

func NewMetaResourceGroup(cfg config.Config) (*MetaResourceGroup, error) {
log.Printf("[INFO] New resource group meta")
log.Info("New resource group meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand All @@ -42,7 +42,7 @@ func (meta MetaResourceGroup) ScopeName() string {
}

func (meta *MetaResourceGroup) ListResource(ctx context.Context) (ImportList, error) {
log.Printf("[DEBUG] Query resource set")
log.Debug("Query resource set")
rset, err := meta.queryResourceSet(ctx, meta.resourceGroup)
if err != nil {
return nil, err
Expand All @@ -52,17 +52,16 @@ func (meta *MetaResourceGroup) ListResource(ctx context.Context) (ImportList, er
if meta.useAzAPI() {
rl = rset.ToTFAzAPIResources()
} else {

log.Printf("[DEBUG] Populate resource set")
log.Debug("Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
log.Debug("Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}

log.Printf("[DEBUG] Azure Resource set map to TF resource set")
log.Debug("Azure Resource set map to TF resource set")
rl = rset.ToTFAzureRMResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/resourceset/azure_resource_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (rset AzureResourceSet) ToTFAzureRMResources(parallelism int, cred azcore.T
wp.Run(func(v interface{}) error {
res := v.(result)
if res.err != nil {
log.Printf("[WARN] Failed to query resource type for %s: %v\n", res.resid, res.err)
log.Warn("Failed to query resource type", "id", res.resid, "error", res.err)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.resid,
Expand All @@ -52,7 +52,7 @@ func (rset AzureResourceSet) ToTFAzureRMResources(parallelism int, cred azcore.T
} else {
if !res.exact {
// It is not possible to return multiple result when API is used.
log.Printf("[WARN] No query result for resource type and TF id for %s\n", res.resid)
log.Warn("No query result for resource type and TF id", "id", res.resid)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.resid,
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m model) Init() tea.Cmd {

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if _, ok := msg.(spinner.TickMsg); !ok {
log.Printf("[DEBUG] UI update. STATUS: %s | MSG: %#v\n", m.status, msg)
log.Trace("UI update", "status", m.status, "msg", fmt.Sprintf("%#v", msg))
}

switch msg := msg.(type) {
Expand Down
Loading

0 comments on commit 2d4d133

Please sign in to comment.