func architectureRecursion()

in alzlib.go [805:841]


func architectureRecursion(parents mapset.Set[string], libArch *processor.LibArchitecture, arch *Architecture, az *AlzLib, depth int) error {
	if depth > 5 {
		return errors.New("architectureRecursion: recursion depth exceeded")
	}
	newParents := mapset.NewThreadUnsafeSet[string]()
	if len(libArch.ManagementGroups) == 0 {
		return errors.New("architectureRecursion: no management groups found")
	}
	for _, mg := range libArch.ManagementGroups {
		switch {
		case depth == 0 && mg.ParentId == nil:
			if err := arch.addMgFromProcessor(mg, az); err != nil {
				return fmt.Errorf("architectureRecursion: error adding management group %s: %w", mg.Id, err)
			}
		case depth > 0 && mg.ParentId != nil:
			if parents == nil {
				return errors.New("architectureRecursion: depth > 1 and parents set to nil")
			}
			if !parents.Contains(*mg.ParentId) {
				continue
			}
			if !arch.mgs[*mg.ParentId].exists && mg.Exists {
				return fmt.Errorf("architectureRecursion: error adding management group %s, which is configured as existing but the parent management group %s does not exist and would be created", mg.Id, *mg.ParentId)
			}
			if err := arch.addMgFromProcessor(mg, az); err != nil {
				return fmt.Errorf("architectureRecursion: error adding management group %s: %w", mg.Id, err)
			}
		default:
			continue
		}
		newParents.Add(mg.Id)
	}
	if newParents.Cardinality() > 0 {
		return architectureRecursion(newParents, libArch, arch, az, depth+1)
	}
	return nil
}