|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "log" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
7 | 8 |
|
8 | 9 | "github.com/apparentlymart/go-userdirs/userdirs" |
| 10 | + svchost "github.com/hashicorp/terraform-svchost" |
9 | 11 | "github.com/hashicorp/terraform-svchost/disco" |
10 | 12 |
|
11 | 13 | "github.com/hashicorp/terraform/addrs" |
12 | 14 | "github.com/hashicorp/terraform/command/cliconfig" |
13 | 15 | "github.com/hashicorp/terraform/internal/getproviders" |
| 16 | + "github.com/hashicorp/terraform/tfdiags" |
14 | 17 | ) |
15 | 18 |
|
16 | 19 | // providerSource constructs a provider source based on a combination of the |
17 | 20 | // CLI configuration and some default search locations. This will be the |
18 | 21 | // provider source used for provider installation in the "terraform init" |
19 | 22 | // command, unless overridden by the special -plugin-dir option. |
20 | | -func providerSource(services *disco.Disco) getproviders.Source { |
21 | | - // We're not yet using the CLI config here because we've not implemented |
22 | | - // yet the new configuration constructs to customize provider search |
23 | | - // locations. That'll come later. For now, we just always use the |
24 | | - // implicit default provider source. |
25 | | - return implicitProviderSource(services) |
| 23 | +func providerSource(configs []*cliconfig.ProviderInstallation, services *disco.Disco) (getproviders.Source, tfdiags.Diagnostics) { |
| 24 | + if len(configs) == 0 { |
| 25 | + // If there's no explicit installation configuration then we'll build |
| 26 | + // up an implicit one with direct registry installation along with |
| 27 | + // some automatically-selected local filesystem mirrors. |
| 28 | + return implicitProviderSource(services), nil |
| 29 | + } |
| 30 | + |
| 31 | + // There should only be zero or one configurations, which is checked by |
| 32 | + // the validation logic in the cliconfig package. Therefore we'll just |
| 33 | + // ignore any additional configurations in here. |
| 34 | + config := configs[0] |
| 35 | + return explicitProviderSource(config, services) |
| 36 | +} |
| 37 | + |
| 38 | +func explicitProviderSource(config *cliconfig.ProviderInstallation, services *disco.Disco) (getproviders.Source, tfdiags.Diagnostics) { |
| 39 | + var diags tfdiags.Diagnostics |
| 40 | + var searchRules []getproviders.MultiSourceSelector |
| 41 | + |
| 42 | + for _, sourceConfig := range config.Sources { |
| 43 | + source, moreDiags := providerSourceForCLIConfigLocation(sourceConfig.Location, services) |
| 44 | + diags = diags.Append(moreDiags) |
| 45 | + if moreDiags.HasErrors() { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + include, err := getproviders.ParseMultiSourceMatchingPatterns(sourceConfig.Include) |
| 50 | + if err != nil { |
| 51 | + diags = diags.Append(tfdiags.Sourceless( |
| 52 | + tfdiags.Error, |
| 53 | + "Invalid provider source inclusion patterns", |
| 54 | + fmt.Sprintf("CLI config specifies invalid provider inclusion patterns: %s.", err), |
| 55 | + )) |
| 56 | + } |
| 57 | + exclude, err := getproviders.ParseMultiSourceMatchingPatterns(sourceConfig.Include) |
| 58 | + if err != nil { |
| 59 | + diags = diags.Append(tfdiags.Sourceless( |
| 60 | + tfdiags.Error, |
| 61 | + "Invalid provider source exclusion patterns", |
| 62 | + fmt.Sprintf("CLI config specifies invalid provider exclusion patterns: %s.", err), |
| 63 | + )) |
| 64 | + } |
| 65 | + |
| 66 | + searchRules = append(searchRules, getproviders.MultiSourceSelector{ |
| 67 | + Source: source, |
| 68 | + Include: include, |
| 69 | + Exclude: exclude, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + return getproviders.MultiSource(searchRules), diags |
26 | 74 | } |
27 | 75 |
|
28 | 76 | // implicitProviderSource builds a default provider source to use if there's |
@@ -130,3 +178,36 @@ func implicitProviderSource(services *disco.Disco) getproviders.Source { |
130 | 178 |
|
131 | 179 | return getproviders.MultiSource(searchRules) |
132 | 180 | } |
| 181 | + |
| 182 | +func providerSourceForCLIConfigLocation(loc cliconfig.ProviderInstallationSourceLocation, services *disco.Disco) (getproviders.Source, tfdiags.Diagnostics) { |
| 183 | + if loc == cliconfig.ProviderInstallationDirect { |
| 184 | + return getproviders.NewMemoizeSource( |
| 185 | + getproviders.NewRegistrySource(services), |
| 186 | + ), nil |
| 187 | + } |
| 188 | + |
| 189 | + switch loc := loc.(type) { |
| 190 | + |
| 191 | + case cliconfig.ProviderInstallationFilesystemMirror: |
| 192 | + return getproviders.NewFilesystemMirrorSource(string(loc)), nil |
| 193 | + |
| 194 | + case cliconfig.ProviderInstallationNetworkMirror: |
| 195 | + host, err := svchost.ForComparison(string(loc)) |
| 196 | + if err != nil { |
| 197 | + var diags tfdiags.Diagnostics |
| 198 | + diags = diags.Append(tfdiags.Sourceless( |
| 199 | + tfdiags.Error, |
| 200 | + "Invalid hostname for provider installation source", |
| 201 | + fmt.Sprintf("Cannot parse %q as a hostname for a network provider mirror: %s.", string(loc), err), |
| 202 | + )) |
| 203 | + return nil, diags |
| 204 | + } |
| 205 | + return getproviders.NewNetworkMirrorSource(host), nil |
| 206 | + |
| 207 | + default: |
| 208 | + // We should not get here because the set of cases above should |
| 209 | + // be comprehensive for all of the |
| 210 | + // cliconfig.ProviderInstallationLocation implementations. |
| 211 | + panic(fmt.Sprintf("unexpected provider source location type %T", loc)) |
| 212 | + } |
| 213 | +} |
0 commit comments