|
4 | 4 | package features |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "fmt" |
7 | 8 | "os" |
8 | 9 | "strings" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | // EnhancedValidationEnabled returns whether the feature for Enhanced Validation is enabled. |
12 | 13 | // |
13 | 14 | // This functionality calls out to the Azure MetaData Service to cache the list of supported |
14 | | -// Azure Locations for the specified Endpoint - and then uses that to provide enhanced validation |
| 15 | +// Azure Locations and Resource Providers for the specified Endpoint - and then uses that to |
| 16 | +// provide enhanced validation. When enabled, invalid locations or resource providers are caught |
| 17 | +// at `terraform plan` time. When disabled, these errors are caught at `terraform apply` time |
| 18 | +// when Azure rejects the request. |
15 | 19 | // |
16 | | -// This is enabled by default as of version 2.20 of the Azure Provider, and can be disabled by |
17 | | -// setting the Environment Variable `ARM_PROVIDER_ENHANCED_VALIDATION` to `false`. |
| 20 | +// This is enabled by default in version 4.x and disabled by default as of version 5.0 of the |
| 21 | +// Azure Provider. The default can be overridden by setting the Environment Variable |
| 22 | +// `ARM_PROVIDER_ENHANCED_VALIDATION` to `true` or `false`. |
18 | 23 | func EnhancedValidationEnabled() bool { |
19 | 24 | value := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION") |
20 | 25 | if value == "" { |
21 | | - return true |
| 26 | + // In 5.0, default to disabled; in 4.x, default to enabled |
| 27 | + return !FivePointOh() |
22 | 28 | } |
23 | 29 |
|
24 | 30 | return strings.EqualFold(value, "true") |
25 | 31 | } |
| 32 | + |
| 33 | +// EnhancedValidationLocationsEnabled returns whether Enhanced Validation for Locations is enabled. |
| 34 | +// |
| 35 | +// This checks the `ARM_PROVIDER_ENHANCED_VALIDATION_LOCATIONS` environment variable first, |
| 36 | +// falling back to the legacy `ARM_PROVIDER_ENHANCED_VALIDATION` environment variable, then to the |
| 37 | +// version default (enabled in 4.x, disabled in 5.0). |
| 38 | +func EnhancedValidationLocationsEnabled() bool { |
| 39 | + // Check the locations-specific env var first |
| 40 | + value := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION_LOCATIONS") |
| 41 | + if value != "" { |
| 42 | + return strings.EqualFold(value, "true") |
| 43 | + } |
| 44 | + |
| 45 | + // In 4.x, fall back to the legacy environment variable |
| 46 | + if !FivePointOh() { |
| 47 | + return EnhancedValidationEnabled() |
| 48 | + } |
| 49 | + |
| 50 | + // In 5.0, default to disabled |
| 51 | + return false |
| 52 | +} |
| 53 | + |
| 54 | +// EnhancedValidationResourceProvidersEnabled returns whether Enhanced Validation for Resource Providers is enabled. |
| 55 | +// |
| 56 | +// This checks the `ARM_PROVIDER_ENHANCED_VALIDATION_RESOURCE_PROVIDERS` environment variable first, |
| 57 | +// falling back to the legacy `ARM_PROVIDER_ENHANCED_VALIDATION` environment variable, then to the |
| 58 | +// version default (enabled in 4.x, disabled in 5.0). |
| 59 | +func EnhancedValidationResourceProvidersEnabled() bool { |
| 60 | + // Check the resource-providers-specific env var first |
| 61 | + value := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION_RESOURCE_PROVIDERS") |
| 62 | + if value != "" { |
| 63 | + return strings.EqualFold(value, "true") |
| 64 | + } |
| 65 | + |
| 66 | + // In 4.x, fall back to the legacy environment variable |
| 67 | + if !FivePointOh() { |
| 68 | + return EnhancedValidationEnabled() |
| 69 | + } |
| 70 | + |
| 71 | + // In 5.0, default to disabled |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +// ValidateEnhancedValidationEnvVars validates the enhanced validation environment variables. |
| 76 | +// |
| 77 | +// In version 5.0, the legacy `ARM_PROVIDER_ENHANCED_VALIDATION` environment variable has been |
| 78 | +// removed - an error is returned if it is set, directing users to migrate to the specific |
| 79 | +// environment variables or the `enhanced_validation` provider block. |
| 80 | +// |
| 81 | +// In version 4.x, the legacy environment variable is still supported, but it cannot be set |
| 82 | +// at the same time as any of the specific environment variables. |
| 83 | +func ValidateEnhancedValidationEnvVars() error { |
| 84 | + legacyEnv := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION") |
| 85 | + if legacyEnv == "" { |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + // In 5.0, the legacy env var is no longer supported |
| 90 | + if FivePointOh() { |
| 91 | + return fmt.Errorf("the environment variable `ARM_PROVIDER_ENHANCED_VALIDATION` has been removed in v5.0 of the AzureRM Provider - please use the `enhanced_validation` provider block or the replacement environment variables `ARM_PROVIDER_ENHANCED_VALIDATION_LOCATIONS` and `ARM_PROVIDER_ENHANCED_VALIDATION_RESOURCE_PROVIDERS` instead") |
| 92 | + } |
| 93 | + |
| 94 | + // In 4.x, check for conflicts with specific env vars |
| 95 | + var conflicts []string |
| 96 | + if v := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION_LOCATIONS"); v != "" { |
| 97 | + conflicts = append(conflicts, "ARM_PROVIDER_ENHANCED_VALIDATION_LOCATIONS") |
| 98 | + } |
| 99 | + if v := os.Getenv("ARM_PROVIDER_ENHANCED_VALIDATION_RESOURCE_PROVIDERS"); v != "" { |
| 100 | + conflicts = append(conflicts, "ARM_PROVIDER_ENHANCED_VALIDATION_RESOURCE_PROVIDERS") |
| 101 | + } |
| 102 | + |
| 103 | + if len(conflicts) > 0 { |
| 104 | + return fmt.Errorf("the environment variable `ARM_PROVIDER_ENHANCED_VALIDATION` cannot be set at the same time as %v - please either use the legacy `ARM_PROVIDER_ENHANCED_VALIDATION` or the replacement environment variables, but not both", conflicts) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
0 commit comments