Skip to content

Commit

Permalink
small refactoring of the ResolveImport() signature
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Sep 15, 2021
1 parent c151d81 commit 103855c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
38 changes: 17 additions & 21 deletions internal/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,20 @@ func (meta *Meta) ExportArmTemplate(ctx context.Context) error {
return nil
}

func (meta *Meta) ListAzureResourceIDs() []string {
func (meta *Meta) ImportList() ImportList {
var ids []string
for _, res := range meta.armTemplate.Resources {
ids = append(ids, res.ID(meta.subscriptionId, meta.resourceGroup))
}
ids = append(ids, armtemplate.ResourceGroupId.ID(meta.subscriptionId, meta.resourceGroup))
return ids

l := make(ImportList, 0, len(ids))
for _, id := range ids {
l = append(l, ImportItem{
ResourceID: id,
})
}
return l
}

type ImportList []ImportItem
Expand Down Expand Up @@ -220,30 +227,19 @@ func (item *ImportItem) TFAddr() string {
return item.TFResourceType + "." + item.TFResourceName
}

func (meta *Meta) ResolveImportList(ids []string, ctx context.Context) (ImportList, error) {

// schema, err := meta.tf.ProvidersSchema(ctx)
// if err != nil {
// return nil, fmt.Errorf("getting provider schema: %w", err)
// }
// tfResourceMap := schema.Schemas["registry.terraform.io/hashicorp/azurerm"].ResourceSchemas

func (meta *Meta) ResolveImportList(l ImportList) (ImportList, error) {
tfResourceMap := schema.ProviderSchemaInfo.ResourceSchemas

var list ImportList
var ol ImportList
// userResourceMap is used to track the resource types and resource names that are specified by users.
userResourceMap := map[string]map[string]bool{}
reader := bufio.NewReader(os.Stdin)
color.Cyan("\nPlease input either the Terraform resource type and name in the form of \"<resource type>.<resource name>\", or simply enter to skip\n")
for idx, id := range ids {
item := ImportItem{
ResourceID: id,
}
for idx, item := range l {
for {
fmt.Printf("[%d/%d] %q: ", idx+1, len(ids), id)
fmt.Printf("[%d/%d] %q: ", idx+1, len(l), item.ResourceID)
input, err := reader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("reading for resource %q: %w", id, err)
return nil, fmt.Errorf("reading for resource %q: %w", item.ResourceID, err)
}
input = strings.TrimSpace(input)
if input == "" {
Expand Down Expand Up @@ -276,10 +272,10 @@ func (meta *Meta) ResolveImportList(ids []string, ctx context.Context) (ImportLi
item.TFResourceName = rn
break
}
list = append(list, item)
ol = append(ol, item)
}

return list, nil
return ol, nil
}

func (meta *Meta) Import(ctx context.Context, list ImportList) (ImportList, error) {
Expand All @@ -302,7 +298,7 @@ func (meta *Meta) Import(ctx context.Context, list ImportList) (ImportList, erro
tpls = append(tpls, tpl)
}
if err := os.WriteFile(cfgFile, []byte(strings.Join(tpls, "\n")), 0644); err != nil {
return nil, fmt.Errorf("generating resource template cfgFile file: %w", err)
return nil, fmt.Errorf("generating resource template file: %w", err)
}
// Remove the temp Terraform config once resources are imported.
// This is due to the fact that "terraform add" will complain the resource to be added already exist in the config, even we are outputting to stdout.
Expand Down
11 changes: 6 additions & 5 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func Run(ctx context.Context, rg string) error {
return err
}

ids := meta.ListAzureResourceIDs()
l := meta.ImportList()

// Repeat importing resources here to avoid the user incorrectly maps an azure resource to an incorrect terraform resource
var importedList ImportList
for len(ids) != 0 {
l, err := meta.ResolveImportList(ids, ctx)
for len(l) != 0 {
var err error
l, err = meta.ResolveImportList(l)
if err != nil {
return err
}
Expand All @@ -45,9 +46,9 @@ func Run(ctx context.Context, rg string) error {
}

importErroredList := l.ImportErrored()
ids = make([]string, 0, len(importErroredList))
l = make(ImportList, 0, len(importErroredList))
for _, item := range importErroredList {
ids = append(ids, item.ResourceID)
l = append(l, item)
}
}

Expand Down

0 comments on commit 103855c

Please sign in to comment.