|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tools/preview-api-version-linter/sdk" |
| 11 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tools/preview-api-version-linter/version" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + EXCEPTIONS_FILE = "internal/tools/preview-api-version-linter/exceptions.yml" |
| 16 | + HISTORICAL_EXCEPTIONS_FILE = "internal/tools/preview-api-version-linter/historical-exceptions.yml" |
| 17 | + VENDOR_DIR = "vendor" |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + historicalExceptions, err := version.ParseHistoricalExceptions(filepath.FromSlash(HISTORICAL_EXCEPTIONS_FILE)) |
| 22 | + if err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "failed to parse historical exceptions file %s:\n\n%s\n\n", HISTORICAL_EXCEPTIONS_FILE, err) |
| 24 | + printErrorFooterAndExit() |
| 25 | + } |
| 26 | + exceptions, err := version.ParseExceptions(filepath.FromSlash(EXCEPTIONS_FILE)) |
| 27 | + if err != nil { |
| 28 | + fmt.Fprintf(os.Stderr, "failed to parse exceptions file %s:\n\n%s\n\n", EXCEPTIONS_FILE, err) |
| 29 | + printErrorFooterAndExit() |
| 30 | + } |
| 31 | + |
| 32 | + previewVersions := map[version.Version]bool{} |
| 33 | + for _, sdkType := range sdk.SdkTypes { |
| 34 | + if err := populatePreviewVersions(sdkType, previewVersions); err != nil { |
| 35 | + fmt.Fprintf(os.Stderr, "failed to populate preview versions for SDK type %s:\n\n%s\n\n", sdkType.Module, err) |
| 36 | + printErrorFooterAndExit() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + unusedExceptions := []string{} |
| 41 | + for _, exception := range historicalExceptions { |
| 42 | + if !previewVersions[exception] { |
| 43 | + unusedExceptions = append(unusedExceptions, fmt.Sprintf("module: %s, service: %s, version: %s", exception.Module, exception.Service, exception.Version)) |
| 44 | + } else { |
| 45 | + delete(previewVersions, exception) |
| 46 | + } |
| 47 | + } |
| 48 | + failIfAnyUnusuedExceptions(unusedExceptions, HISTORICAL_EXCEPTIONS_FILE) |
| 49 | + |
| 50 | + for _, exception := range exceptions { |
| 51 | + if !previewVersions[exception] { |
| 52 | + unusedExceptions = append(unusedExceptions, fmt.Sprintf("module: %s, service: %s, version: %s", exception.Module, exception.Service, exception.Version)) |
| 53 | + } else { |
| 54 | + delete(previewVersions, exception) |
| 55 | + } |
| 56 | + } |
| 57 | + failIfAnyUnusuedExceptions(unusedExceptions, EXCEPTIONS_FILE) |
| 58 | + |
| 59 | + if len(previewVersions) > 0 { |
| 60 | + invalidPreviewVersions := []string{} |
| 61 | + for svcVer := range previewVersions { |
| 62 | + invalidPreviewVersions = append(invalidPreviewVersions, fmt.Sprintf("module: %s, service: %s, version: %s", svcVer.Module, svcVer.Service, svcVer.Version)) |
| 63 | + } |
| 64 | + sort.Strings(invalidPreviewVersions) |
| 65 | + |
| 66 | + fmt.Fprintf(os.Stderr, "❌ Invalid use of preview ARM API versions detected in `vendor` folder:\n\n") |
| 67 | + for _, v := range invalidPreviewVersions { |
| 68 | + fmt.Fprintf(os.Stderr, "%s\n", v) |
| 69 | + } |
| 70 | + fmt.Fprintf(os.Stderr, ` |
| 71 | +Preview versions are prone to sudden breaking changes which can result in a less than ideal user experience, |
| 72 | +please use stable version instead. |
| 73 | +
|
| 74 | +`) |
| 75 | + printErrorFooterAndExit() |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Printf("✅ No invalid use of preview ARM API versions detected in %s folder.\n", VENDOR_DIR) |
| 79 | +} |
| 80 | + |
| 81 | +func populatePreviewVersions(sdkType sdk.SdkType, previewServiceVersions map[version.Version]bool) error { |
| 82 | + return filepath.WalkDir(filepath.FromSlash(VENDOR_DIR+"/"+sdkType.Module), func(path string, dir os.DirEntry, err error) error { |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + if dir.IsDir() { |
| 87 | + matches := sdkType.ServiceAndVersionRegex.FindStringSubmatch(filepath.ToSlash(path)) |
| 88 | + if len(matches) == 3 { |
| 89 | + previewServiceVersions[version.Version{ |
| 90 | + Module: sdkType.Module, |
| 91 | + Service: strings.ToLower(matches[1]), |
| 92 | + Version: strings.ToLower(matches[2]), |
| 93 | + }] = true |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func printErrorFooterAndExit() { |
| 101 | + fmt.Fprintf(os.Stderr, `More information: https://github.com/hashicorp/terraform-provider-azurerm/blob/main/contributing/topics/guide-api-version.md |
| 102 | +
|
| 103 | +To rerun this check locally, use: go run internal/tools/preview-api-version-linter/main.go |
| 104 | +`) |
| 105 | + os.Exit(1) |
| 106 | +} |
| 107 | + |
| 108 | +func failIfAnyUnusuedExceptions(unusedExceptions []string, exceptionsFile string) { |
| 109 | + if len(unusedExceptions) > 0 { |
| 110 | + fmt.Fprintf(os.Stderr, "❌ Unused exceptions detected in `%s` file, remove these entries:\n\n", exceptionsFile) |
| 111 | + for _, unusedException := range unusedExceptions { |
| 112 | + fmt.Fprintln(os.Stderr, unusedException) |
| 113 | + } |
| 114 | + fmt.Fprintf(os.Stderr, "\n") |
| 115 | + printErrorFooterAndExit() |
| 116 | + } |
| 117 | +} |
0 commit comments