func()

in cmd/plan_command.go [80:171]


func (c *PlanCommand) Plan(terraform *tf.Terraform, isPlanOnly bool) []types.AzureResource {
	// get azapi resource from state
	log.Printf("[INFO] running terraform plan...")
	p, err := terraform.Plan(&c.varFile)
	if err != nil {
		log.Fatal(err)
	}

	migrationMessage := "The following resources will be migrated:\n"
	unsupportedMessage := "The following resources can't be migrated:\n"
	ignoreMessage := "The following resources will be ignored in migration:\n"
	ignoreSet := make(map[string]bool)
	if file, err := os.ReadFile(path.Join(terraform.GetWorkingDirectory(), "aztfmigrate.ignore")); err == nil {
		lines := strings.Split(string(file), "\n")
		for _, line := range lines {
			line = strings.TrimSpace(line)
			if len(line) == 0 {
				continue
			}
			ignoreSet[line] = true
			ignoreMessage += fmt.Sprintf("\t%s\n", line)
		}
	}

	res := make([]types.AzureResource, 0)

	for _, item := range types.ListResourcesFromPlan(p) {
		if item.TargetProvider() != c.TargetProvider {
			continue
		}
		if ignoreSet[item.OldAddress(nil)] {
			continue
		}
		if err := item.CoverageCheck(c.Strict); err != nil {
			unsupportedMessage += fmt.Sprintf("\t%s\n", err)
			continue
		}

		switch resource := item.(type) {
		case *types.AzapiResource:
			if len(resource.Instances) == 0 {
				continue
			}
			resourceId := resource.Instances[0].ResourceId
			resourceTypes, exact, err := azurerm.GetAzureRMResourceType(resourceId)
			if err != nil {
				log.Fatal(fmt.Errorf("failed to get resource type for %s: %w", resourceId, err))
			}
			if exact {
				resource.ResourceType = resourceTypes[0]
			} else if !isPlanOnly {
				resource.ResourceType = c.getUserInputResourceType(resourceId, resourceTypes)
			}

			if resource.ResourceType != "" {
				migrationMessage += fmt.Sprintf("\t%s will be replaced with %s\n", resource.OldAddress(nil), resource.NewAddress(nil))
			} else {
				migrationMessage += fmt.Sprintf("\t%s will be replaced with %v\n", resource.OldAddress(nil), strings.Join(resourceTypes, ", "))
			}
			res = append(res, resource)

		case *types.AzapiUpdateResource:
			resourceTypes, exact, err := azurerm.GetAzureRMResourceType(resource.Id)
			if err != nil {
				log.Fatal(fmt.Errorf("failed to get resource type for %s: %w", resource.Id, err))
			}

			if exact {
				resource.ResourceType = resourceTypes[0]
			} else if !isPlanOnly {
				resource.ResourceType = c.getUserInputResourceType(resource.Id, resourceTypes)
			}

			if resource.ResourceType != "" {
				migrationMessage += fmt.Sprintf("\t%s will be replaced with %s\n", resource.OldAddress(nil), resource.NewAddress(nil))
			} else {
				migrationMessage += fmt.Sprintf("\t%s will be replaced with %v\n", resource.OldAddress(nil), strings.Join(resourceTypes, ", "))
			}
			res = append(res, resource)

		case *types.AzurermResource:
			if len(resource.Instances) == 0 {
				continue
			}
			migrationMessage += fmt.Sprintf("\t%s will be replaced with %s\n", resource.OldAddress(nil), resource.NewAddress(nil))
			res = append(res, resource)
		}
	}

	log.Printf("[INFO]\n\nThe tool will perform the following actions:\n\n%s\n%s\n%s\n", migrationMessage, unsupportedMessage, ignoreMessage)
	return res
}