Skip to content

Commit

Permalink
rename provider-specific functions (hashicorp#24417)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mildwonkey authored Mar 20, 2020
1 parent c8d6484 commit 4f141ae
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 33 deletions.
6 changes: 3 additions & 3 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"},
},
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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"},
},
Expand Down Expand Up @@ -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",
} {

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion command/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions command/testdata/init-get-providers/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
20 changes: 9 additions & 11 deletions moduledeps/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -107,33 +107,31 @@ 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,
}
}
}
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
Expand Down
4 changes: 2 additions & 2 deletions moduledeps/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4f141ae

Please sign in to comment.