From e6316c9de64409d5bc93527be2f8465da7a74b0e Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Wed, 13 Mar 2019 12:52:52 -0400 Subject: [PATCH] plugin/discovery: Parse warnings from TF Registry Terraform Registry (and other registry implementations) can now return an array of warnings with the versions response. These warnings are now displayed to the user during a `terraform init`. --- command/plugins_test.go | 2 +- plugin/discovery/get.go | 21 +++++++++++++++++++++ registry/response/terraform_provider.go | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/command/plugins_test.go b/command/plugins_test.go index c95d36330..4a4153df0 100644 --- a/command/plugins_test.go +++ b/command/plugins_test.go @@ -197,7 +197,7 @@ func (i *mockProviderInstaller) PurgeUnused(map[string]discovery.PluginMeta) (di return ret, nil } -type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, error) +type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) func (cb callbackPluginInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) { return cb(provider, req) diff --git a/plugin/discovery/get.go b/plugin/discovery/get.go index f9ad9a432..1702bff46 100644 --- a/plugin/discovery/get.go +++ b/plugin/discovery/get.go @@ -136,6 +136,17 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, t } return PluginMeta{}, diags, ErrorNoSuchProvider } + + // Add any warnings from the response to diags + for _, warning := range allVersions.Warnings { + hostname, err := i.hostname() + if err != nil { + return PluginMeta{}, diags, err + } + diag := tfdiags.SimpleWarning(fmt.Sprintf("%s: %s", hostname, warning)) + diags = diags.Append(diag) + } + if len(allVersions.Versions) == 0 { return PluginMeta{}, diags, ErrorNoSuitableVersion } @@ -423,6 +434,16 @@ func (i *ProviderInstaller) getProviderChecksum(urls *response.TerraformProvider return checksumForFile(shasums, urls.Filename), nil } +func (i *ProviderInstaller) hostname() (string, error) { + provider := regsrc.NewTerraformProvider("", i.OS, i.Arch) + svchost, err := provider.SvcHost() + if err != nil { + return "", err + } + + return svchost.ForDisplay(), nil +} + // list all versions available for the named provider func (i *ProviderInstaller) listProviderVersions(name string) (*response.TerraformProviderVersions, error) { provider := regsrc.NewTerraformProvider(name, i.OS, i.Arch) diff --git a/registry/response/terraform_provider.go b/registry/response/terraform_provider.go index 08d382a48..64e454a6c 100644 --- a/registry/response/terraform_provider.go +++ b/registry/response/terraform_provider.go @@ -32,6 +32,7 @@ type TerraformProviderVersion struct { type TerraformProviderVersions struct { ID string `json:"id"` Versions []*TerraformProviderVersion `json:"versions"` + Warnings []string `json:"warnings"` } // TerraformProviderPlatform is the Terraform-specific response structure for a