diff --git a/google-beta/acctest/resource_inventory_reader.go b/google-beta/acctest/resource_inventory_reader.go index 55a8df77f4..1a13841755 100644 --- a/google-beta/acctest/resource_inventory_reader.go +++ b/google-beta/acctest/resource_inventory_reader.go @@ -8,7 +8,7 @@ // // This code is generated by Magic Modules using the following: // -// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/acctest/resource_inventory_reader.go.tmpl +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/acctest/resource_inventory_reader.go // // DO NOT EDIT this file directly. Any changes made to this file will be // overwritten during the next generation cycle. @@ -33,8 +33,6 @@ var ( GlobalMetadataCache = MetadataCache{ mutex: &sync.Mutex{}, } - - providerName = "google-beta" ) // Metadata represents the structure of the metadata files @@ -170,31 +168,43 @@ func (mc *MetadataCache) Cache() map[string]Metadata { // getServicesDir returns the path to the services directory // It will attempt to find the project root relative to cwd func getServicesDir() (string, error) { + var startingDirs []string // Try to find project root root, err := findProjectRoot() - if err == nil { - servicesDir := filepath.Join(root, providerName, "services") - if _, err := os.Stat(servicesDir); err == nil { - return servicesDir, nil - } - } - - // Last resort: try relative to current directory - currentDir, err := os.Getwd() if err != nil { - return "", fmt.Errorf("failed to determine current directory: %v", err) + fmt.Printf("Error finding project root: %v", err) + // Fall back to dirs relative the current directory + currentDir, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to determine current directory: %v", err) + } + startingDirs = []string{ + filepath.Join(currentDir), + filepath.Join(currentDir, ".."), + filepath.Join(currentDir, "..", ".."), + } + } else { + startingDirs = append(startingDirs, root) } - // Try a few common relative paths - potentialPaths := []string{ - filepath.Join(currentDir, providerName, "services"), - filepath.Join(currentDir, "..", providerName, "services"), - filepath.Join(currentDir, "..", "..", providerName, "services"), - } + for _, dir := range startingDirs { + files, err := os.ReadDir(dir) + if err != nil { + return "", fmt.Errorf("Failed reading dir %q: %v", dir, err) + } - for _, path := range potentialPaths { - if _, err := os.Stat(path); err == nil { - return path, nil + for _, file := range files { + fi, err := file.Info() + if err != nil { + return "", fmt.Errorf("Failed getting info for file %q: %v", filepath.Join(dir, file.Name()), err) + } + + if fi.Mode().IsDir() && strings.HasPrefix(file.Name(), "google") { + servicesDir := filepath.Join(dir, file.Name(), "services") + if _, err := os.Stat(servicesDir); err == nil { + return servicesDir, nil + } + } } }