diff --git a/internal/meta/meta_group.go b/internal/meta/meta_group.go index 2ebb06f..828f3b3 100644 --- a/internal/meta/meta_group.go +++ b/internal/meta/meta_group.go @@ -5,12 +5,14 @@ import ( ) const ResourceMappingFileName = ".aztfyResourceMapping.json" +const SkippedResourcesFileName = ".aztfySkippedResources.txt" type GroupMeta interface { meta ScopeName() string ListResource() (ImportList, error) ExportResourceMapping(l ImportList) error + ExportSkippedResources(l ImportList) error } func NewGroupMeta(cfg config.GroupConfig) (GroupMeta, error) { diff --git a/internal/meta/meta_group_dummy.go b/internal/meta/meta_group_dummy.go index 093d866..70028a8 100644 --- a/internal/meta/meta_group_dummy.go +++ b/internal/meta/meta_group_dummy.go @@ -66,3 +66,8 @@ func (m MetaGroupDummy) ExportResourceMapping(l ImportList) error { time.Sleep(500 * time.Millisecond) return nil } + +func (m MetaGroupDummy) ExportSkippedResources(l ImportList) error { + time.Sleep(500 * time.Millisecond) + return nil +} diff --git a/internal/meta/meta_group_impl.go b/internal/meta/meta_group_impl.go index ee846bb..599b947 100644 --- a/internal/meta/meta_group_impl.go +++ b/internal/meta/meta_group_impl.go @@ -129,7 +129,7 @@ func (meta *MetaGroupImpl) ListResource() (ImportList, error) { func (meta MetaGroupImpl) ExportResourceMapping(l ImportList) error { m := resmap.ResourceMapping{} for _, item := range l { - if item.TFAddr.Type == "" { + if item.Skip() { continue } m[strings.ToUpper(item.AzureResourceID.String())] = resmap.ResourceMapEntity{ @@ -149,6 +149,29 @@ func (meta MetaGroupImpl) ExportResourceMapping(l ImportList) error { return nil } +func (meta MetaGroupImpl) ExportSkippedResources(l ImportList) error { + var sl []string + for _, item := range l { + if item.Skip() { + sl = append(sl, "- "+item.AzureResourceID.String()) + } + } + if len(sl) == 0 { + return nil + } + + output := filepath.Join(meta.Workspace(), SkippedResourcesFileName) + if err := os.WriteFile(output, []byte(fmt.Sprintf(`Following resources are marked to be skipped: + +%s + +They are either not Terraform candidate resources (e.g. not managed by users), or not supported by the Terraform AzureRM provider yet. +`, strings.Join(sl, "\n"))), 0644); err != nil { + return fmt.Errorf("writing the skipped resources to %s: %v", output, err) + } + return nil +} + func ptr[T any](v T) *T { return &v } diff --git a/internal/run.go b/internal/run.go index b2dbb02..a5ca0db 100644 --- a/internal/run.go +++ b/internal/run.go @@ -151,10 +151,21 @@ func BatchImport(cfg config.GroupConfig, continueOnError bool) error { } } + msg.SetStatus("Exporting Resource Mapping file...") + if err := c.ExportResourceMapping(list); err != nil { + return fmt.Errorf("exporting Resource Mapping file: %v", err) + } + + msg.SetStatus("Exporting Skipped Resource file...") + if err := c.ExportSkippedResources(list); err != nil { + return fmt.Errorf("exporting Skipped Resource file: %v", err) + } + msg.SetStatus("Generating Terraform configurations...") if err := c.GenerateCfg(list); err != nil { return fmt.Errorf("generating Terraform configuration: %v", err) } + return nil } diff --git a/internal/ui/aztfyclient/client.go b/internal/ui/aztfyclient/client.go index 4fdcef5..2b1bbbf 100644 --- a/internal/ui/aztfyclient/client.go +++ b/internal/ui/aztfyclient/client.go @@ -40,6 +40,10 @@ type ExportResourceMappingDoneMsg struct { List meta.ImportList } +type ExportSkippedResourcesDoneMsg struct { + List meta.ImportList +} + type GenerateCfgDoneMsg struct{} type QuitMsg struct{} @@ -122,6 +126,15 @@ func ExportResourceMapping(c meta.GroupMeta, l meta.ImportList) tea.Cmd { } } +func ExportSkippedResources(c meta.GroupMeta, l meta.ImportList) tea.Cmd { + return func() tea.Msg { + if err := c.ExportSkippedResources(l); err != nil { + return ErrMsg(err) + } + return ExportSkippedResourcesDoneMsg{List: l} + } +} + func CleanTFState(addr string) tea.Cmd { return func() tea.Msg { return CleanTFStateMsg{addr} diff --git a/internal/ui/ui.go b/internal/ui/ui.go index cfb554c..ab947fe 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -39,6 +39,7 @@ const ( statusImportErrorMsg statusGeneratingCfg statusExportResourceMapping + statusExportSkippedResources statusSummary statusQuitting statusError @@ -153,6 +154,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.status = statusExportResourceMapping return m, aztfyclient.ExportResourceMapping(m.meta, msg.List) case aztfyclient.ExportResourceMappingDoneMsg: + m.status = statusExportSkippedResources + return m, aztfyclient.ExportSkippedResources(m.meta, msg.List) + case aztfyclient.ExportSkippedResourcesDoneMsg: m.status = statusGeneratingCfg return m, aztfyclient.GenerateCfg(m.meta, msg.List) case aztfyclient.GenerateCfgDoneMsg: @@ -215,6 +219,8 @@ func (m model) View() string { s += m.spinner.View() + " Generating Terraform Configurations..." case statusExportResourceMapping: s += m.spinner.View() + " Exporting Resource Mapping..." + case statusExportSkippedResources: + s += m.spinner.View() + " Exporting Skipped Resources..." case statusSummary: s += summaryView(m) case statusError: