From 4f141ae365590edb457c4fb2a1012e8cf520bf5f Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 20 Mar 2020 13:59:59 -0400 Subject: [PATCH] rename provider-specific functions (#24417) missingPlugins was hard-coded to work only with provider plugins, so I renamed it to clarify the usage. Also renamed a test provider from greater_than to greater-than as the underscore is an invalid provider name character and this will become a hard error in the near future. --- command/init.go | 6 +++--- command/init_test.go | 22 ++++++++++----------- command/plugins.go | 2 +- command/testdata/init-get-providers/main.tf | 8 ++++---- moduledeps/module.go | 20 +++++++++---------- moduledeps/module_test.go | 4 ++-- terraform/context.go | 2 +- 7 files changed, 31 insertions(+), 33 deletions(-) diff --git a/command/init.go b/command/init.go index 8cb0145ae..1a93c83d9 100644 --- a/command/init.go +++ b/command/init.go @@ -463,11 +463,11 @@ func (c *InitCommand) getProviders(earlyConfig *earlyconfig.Config, state *state return false, diags } - configReqs := configDeps.AllPluginRequirements() + configReqs := configDeps.AllProviderRequirements() // FIXME: This is weird because ConfigTreeDependencies was written before // we switched over to using earlyConfig as the main source of dependencies. // In future we should clean this up to be a more reasonable API. - stateReqs := terraform.ConfigTreeDependencies(nil, state).AllPluginRequirements() + stateReqs := terraform.ConfigTreeDependencies(nil, state).AllProviderRequirements() requirements := configReqs.Merge(stateReqs) if len(requirements) == 0 { @@ -479,7 +479,7 @@ func (c *InitCommand) getProviders(earlyConfig *earlyconfig.Config, state *state "\n[reset][bold]Initializing provider plugins...", )) - missing := c.missingPlugins(available, requirements) + missing := c.missingProviders(available, requirements) if c.getPlugins { if len(missing) > 0 { diff --git a/command/init_test.go b/command/init_test.go index 432f933b3..227d2e98f 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -788,7 +788,7 @@ func TestInit_getProvider(t *testing.T) { // looking for an exact version "exact": []string{"1.2.3"}, // config requires >= 2.3.3 - "greater_than": []string{"2.3.4", "2.3.3", "2.3.0"}, + "greater-than": []string{"2.3.4", "2.3.3", "2.3.0"}, // config specifies "between": []string{"3.4.5", "2.3.4", "1.2.3"}, }, @@ -817,9 +817,9 @@ func TestInit_getProvider(t *testing.T) { if _, err := os.Stat(exactPath); os.IsNotExist(err) { t.Fatal("provider 'exact' not downloaded") } - greaterThanPath := filepath.Join(c.pluginDir(), installer.FileName("greater_than", "2.3.4")) + greaterThanPath := filepath.Join(c.pluginDir(), installer.FileName("greater-than", "2.3.4")) if _, err := os.Stat(greaterThanPath); os.IsNotExist(err) { - t.Fatal("provider 'greater_than' not downloaded") + t.Fatal("provider 'greater-than' not downloaded") } betweenPath := filepath.Join(c.pluginDir(), installer.FileName("between", "2.3.4")) if _, err := os.Stat(betweenPath); os.IsNotExist(err) { @@ -893,7 +893,7 @@ func TestInit_findVendoredProviders(t *testing.T) { t.Fatal(err) } // the vendor path - greaterThanPath := filepath.Join(DefaultPluginVendorDir, "terraform-provider-greater_than_v2.3.4_x4") + greaterThanPath := filepath.Join(DefaultPluginVendorDir, "terraform-provider-greater-than_v2.3.4_x4") if err := ioutil.WriteFile(greaterThanPath, []byte("test bin"), 0755); err != nil { t.Fatal(err) } @@ -1020,7 +1020,7 @@ func TestInit_getUpgradePlugins(t *testing.T) { // looking for an exact version "exact": []string{"1.2.3"}, // config requires >= 2.3.3 - "greater_than": []string{"2.3.4", "2.3.3", "2.3.0"}, + "greater-than": []string{"2.3.4", "2.3.3", "2.3.0"}, // config specifies "between": []string{"3.4.5", "2.3.4", "1.2.3"}, }, @@ -1037,7 +1037,7 @@ func TestInit_getUpgradePlugins(t *testing.T) { if err != nil { t.Fatal(err) } - greaterThanUnwanted := filepath.Join(m.pluginDir(), installer.FileName("greater_than", "2.3.3")) + greaterThanUnwanted := filepath.Join(m.pluginDir(), installer.FileName("greater-than", "2.3.3")) err = ioutil.WriteFile(greaterThanUnwanted, []byte{}, os.ModePerm) if err != nil { t.Fatal(err) @@ -1084,8 +1084,8 @@ func TestInit_getUpgradePlugins(t *testing.T) { // includes both our old and new versions. "terraform-provider-exact_v0.0.1_x4", "terraform-provider-exact_v1.2.3_x4", - "terraform-provider-greater_than_v2.3.3_x4", - "terraform-provider-greater_than_v2.3.4_x4", + "terraform-provider-greater-than_v2.3.3_x4", + "terraform-provider-greater-than_v2.3.4_x4", } if !reflect.DeepEqual(gotFilenames, wantFilenames) { @@ -1112,7 +1112,7 @@ func TestInit_getProviderMissing(t *testing.T) { // looking for exact version 1.2.3 "exact": []string{"1.2.4"}, // config requires >= 2.3.3 - "greater_than": []string{"2.3.4", "2.3.3", "2.3.0"}, + "greater-than": []string{"2.3.4", "2.3.3", "2.3.0"}, // config specifies "between": []string{"3.4.5", "2.3.4", "1.2.3"}, }, @@ -1331,7 +1331,7 @@ func TestInit_pluginDirProviders(t *testing.T) { // add some dummy providers in our plugin dirs for i, name := range []string{ "terraform-provider-exact_v1.2.3_x4", - "terraform-provider-greater_than_v2.3.4_x4", + "terraform-provider-greater-than_v2.3.4_x4", "terraform-provider-between_v2.3.4_x4", } { @@ -1382,7 +1382,7 @@ func TestInit_pluginDirProvidersDoesNotGet(t *testing.T) { // add some dummy providers in our plugin dirs for i, name := range []string{ "terraform-provider-exact_v1.2.3_x4", - "terraform-provider-greater_than_v2.3.4_x4", + "terraform-provider-greater-than_v2.3.4_x4", } { if err := ioutil.WriteFile(filepath.Join(pluginPath[i], name), []byte("test bin"), 0755); err != nil { diff --git a/command/plugins.go b/command/plugins.go index 6a3a7f0ab..85c18d49a 100644 --- a/command/plugins.go +++ b/command/plugins.go @@ -290,7 +290,7 @@ func (m *Meta) internalProviders() map[addrs.Provider]providers.Factory { } // filter the requirements returning only the providers that we can't resolve -func (m *Meta) missingPlugins(avail discovery.PluginMetaSet, reqd discovery.PluginRequirements) discovery.PluginRequirements { +func (m *Meta) missingProviders(avail discovery.PluginMetaSet, reqd discovery.PluginRequirements) discovery.PluginRequirements { missing := make(discovery.PluginRequirements) candidates := avail.ConstrainVersions(reqd) diff --git a/command/testdata/init-get-providers/main.tf b/command/testdata/init-get-providers/main.tf index fdf27f329..71841670f 100644 --- a/command/testdata/init-get-providers/main.tf +++ b/command/testdata/init-get-providers/main.tf @@ -1,11 +1,11 @@ provider "exact" { - version = "1.2.3" + version = "1.2.3" } -provider "greater_than" { - version = ">= 2.3.3" +provider "greater-than" { + version = ">= 2.3.3" } provider "between" { - version = "> 1.0.0 , < 3.0.0" + version = "> 1.0.0 , < 3.0.0" } diff --git a/moduledeps/module.go b/moduledeps/module.go index 52b7c2f6e..53b0f0eb4 100644 --- a/moduledeps/module.go +++ b/moduledeps/module.go @@ -97,7 +97,7 @@ func (s sortModules) Swap(i, j int) { s.modules[i], s.modules[j] = s.modules[j], s.modules[i] } -// PluginRequirements produces a PluginRequirements structure that can +// ProviderRequirements produces a PluginRequirements structure that can // be used with discovery.PluginMetaSet.ConstrainVersions to identify // suitable plugins to satisfy the module's provider dependencies. // @@ -107,16 +107,14 @@ func (s sortModules) Swap(i, j int) { // // Requirements returned by this method include only version constraints, // and apply no particular SHA256 hash constraint. -func (m *Module) PluginRequirements() discovery.PluginRequirements { +func (m *Module) ProviderRequirements() discovery.PluginRequirements { ret := make(discovery.PluginRequirements) for pFqn, dep := range m.Providers { - // TODO: discovery.PluginRequirements should be refactored and use - // addrs.Provider as the map keys - provider := pFqn.LegacyString() - if existing, exists := ret[provider]; exists { - ret[provider].Versions = existing.Versions.Append(dep.Constraints) + providerStr := pFqn.LegacyString() + if existing, exists := ret[providerStr]; exists { + ret[providerStr].Versions = existing.Versions.Append(dep.Constraints) } else { - ret[provider] = &discovery.PluginConstraints{ + ret[providerStr] = &discovery.PluginConstraints{ Versions: dep.Constraints, } } @@ -124,16 +122,16 @@ func (m *Module) PluginRequirements() discovery.PluginRequirements { return ret } -// AllPluginRequirements calls PluginRequirements for the receiver and all +// AllProviderRequirements calls ProviderRequirements for the receiver and all // of its descendents, and merges the result into a single PluginRequirements // structure that would satisfy all of the modules together. // // Requirements returned by this method include only version constraints, // and apply no particular SHA256 hash constraint. -func (m *Module) AllPluginRequirements() discovery.PluginRequirements { +func (m *Module) AllProviderRequirements() discovery.PluginRequirements { var ret discovery.PluginRequirements m.WalkTree(func(path []string, parent *Module, current *Module) error { - ret = ret.Merge(current.PluginRequirements()) + ret = ret.Merge(current.ProviderRequirements()) return nil }) return ret diff --git a/moduledeps/module_test.go b/moduledeps/module_test.go index 7ec143b1c..316e4f701 100644 --- a/moduledeps/module_test.go +++ b/moduledeps/module_test.go @@ -188,7 +188,7 @@ func TestModuleSortChildren(t *testing.T) { } } -func TestModulePluginRequirements(t *testing.T) { +func TestModuleProviderRequirements(t *testing.T) { m := &Module{ Name: "root", Providers: Providers{ @@ -201,7 +201,7 @@ func TestModulePluginRequirements(t *testing.T) { }, } - reqd := m.PluginRequirements() + reqd := m.ProviderRequirements() if len(reqd) != 2 { t.Errorf("wrong number of elements in %#v; want 2", reqd) } diff --git a/terraform/context.go b/terraform/context.go index 1817aa541..146483365 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -173,7 +173,7 @@ func NewContext(opts *ContextOpts) (*Context, tfdiags.Diagnostics) { var providerFactories map[addrs.Provider]providers.Factory if opts.ProviderResolver != nil { deps := ConfigTreeDependencies(opts.Config, state) - reqd := deps.AllPluginRequirements() + reqd := deps.AllProviderRequirements() if opts.ProviderSHA256s != nil && !opts.SkipProviderVerify { reqd.LockExecutables(opts.ProviderSHA256s) }