Skip to content

Commit

Permalink
Merge pull request #237 from magodo/list_skipped_resources
Browse files Browse the repository at this point in the history
Export `.aztfySkippedResources.txt` to list skipped resources, if any
  • Loading branch information
stemaMSFT authored Sep 27, 2022
2 parents 77f2c5d + e79f759 commit b4d6584
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/meta/meta_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions internal/meta/meta_group_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion internal/meta/meta_group_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions internal/ui/aztfyclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type ExportResourceMappingDoneMsg struct {
List meta.ImportList
}

type ExportSkippedResourcesDoneMsg struct {
List meta.ImportList
}

type GenerateCfgDoneMsg struct{}

type QuitMsg struct{}
Expand Down Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
statusImportErrorMsg
statusGeneratingCfg
statusExportResourceMapping
statusExportSkippedResources
statusSummary
statusQuitting
statusError
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b4d6584

Please sign in to comment.