remove legacy types from command package

This commit is contained in:
James Bardin 2020-11-18 13:41:33 -05:00
parent c33b8c7faa
commit e998882824
7 changed files with 41 additions and 36 deletions

View File

@ -12,8 +12,8 @@ import (
"time" "time"
multierror "github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/internal/legacy/terraform"
"github.com/hashicorp/terraform/states/statemgr" "github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/terraform"
) )
// LocalState manages a state storage that is local to the filesystem. // LocalState manages a state storage that is local to the filesystem.

View File

@ -41,6 +41,7 @@ import (
backendInit "github.com/hashicorp/terraform/backend/init" backendInit "github.com/hashicorp/terraform/backend/init"
backendLocal "github.com/hashicorp/terraform/backend/local" backendLocal "github.com/hashicorp/terraform/backend/local"
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
_ "github.com/hashicorp/terraform/internal/logging" _ "github.com/hashicorp/terraform/internal/logging"
) )
@ -404,7 +405,7 @@ func testStateFileWorkspaceDefault(t *testing.T, workspace string, s *states.Sta
// testStateFileRemote writes the state out to the remote statefile // testStateFileRemote writes the state out to the remote statefile
// in the cwd. Use `testCwd` to change into a temp cwd. // in the cwd. Use `testCwd` to change into a temp cwd.
func testStateFileRemote(t *testing.T, s *terraform.State) string { func testStateFileRemote(t *testing.T, s *legacy.State) string {
t.Helper() t.Helper()
path := filepath.Join(DefaultDataDir, DefaultStateFilename) path := filepath.Join(DefaultDataDir, DefaultStateFilename)
@ -418,7 +419,7 @@ func testStateFileRemote(t *testing.T, s *terraform.State) string {
} }
defer f.Close() defer f.Close()
if err := terraform.WriteState(s, f); err != nil { if err := legacy.WriteState(s, f); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -446,9 +447,9 @@ func testStateRead(t *testing.T, path string) *states.State {
// testDataStateRead reads a "data state", which is a file format resembling // testDataStateRead reads a "data state", which is a file format resembling
// our state format v3 that is used only to track current backend settings. // our state format v3 that is used only to track current backend settings.
// //
// This old format still uses *terraform.State, but should be replaced with // This old format still uses *legacy.State, but should be replaced with
// a more specialized type in a later release. // a more specialized type in a later release.
func testDataStateRead(t *testing.T, path string) *terraform.State { func testDataStateRead(t *testing.T, path string) *legacy.State {
t.Helper() t.Helper()
f, err := os.Open(path) f, err := os.Open(path)
@ -457,7 +458,7 @@ func testDataStateRead(t *testing.T, path string) *terraform.State {
} }
defer f.Close() defer f.Close()
s, err := terraform.ReadState(f) s, err := legacy.ReadState(f)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -719,7 +720,7 @@ func testInputMap(t *testing.T, answers map[string]string) func() {
// be returned about the backend configuration having changed and that // be returned about the backend configuration having changed and that
// "terraform init" must be run, since the test backend config cache created // "terraform init" must be run, since the test backend config cache created
// by this function contains the hash for an empty configuration. // by this function contains the hash for an empty configuration.
func testBackendState(t *testing.T, s *states.State, c int) (*terraform.State, *httptest.Server) { func testBackendState(t *testing.T, s *states.State, c int) (*legacy.State, *httptest.Server) {
t.Helper() t.Helper()
var b64md5 string var b64md5 string
@ -759,8 +760,8 @@ func testBackendState(t *testing.T, s *states.State, c int) (*terraform.State, *
configSchema := b.ConfigSchema() configSchema := b.ConfigSchema()
hash := backendConfig.Hash(configSchema) hash := backendConfig.Hash(configSchema)
state := terraform.NewState() state := legacy.NewState()
state.Backend = &terraform.BackendState{ state.Backend = &legacy.BackendState{
Type: "http", Type: "http",
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)), ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
Hash: uint64(hash), Hash: uint64(hash),
@ -772,10 +773,10 @@ func testBackendState(t *testing.T, s *states.State, c int) (*terraform.State, *
// testRemoteState is used to make a test HTTP server to return a given // testRemoteState is used to make a test HTTP server to return a given
// state file that can be used for testing legacy remote state. // state file that can be used for testing legacy remote state.
// //
// The return values are a *terraform.State instance that should be written // The return values are a *legacy.State instance that should be written
// as the "data state" (really: backend state) and the server that the // as the "data state" (really: backend state) and the server that the
// returned data state refers to. // returned data state refers to.
func testRemoteState(t *testing.T, s *states.State, c int) (*terraform.State, *httptest.Server) { func testRemoteState(t *testing.T, s *states.State, c int) (*legacy.State, *httptest.Server) {
t.Helper() t.Helper()
var b64md5 string var b64md5 string
@ -795,10 +796,10 @@ func testRemoteState(t *testing.T, s *states.State, c int) (*terraform.State, *h
resp.Write(buf.Bytes()) resp.Write(buf.Bytes())
} }
retState := terraform.NewState() retState := legacy.NewState()
srv := httptest.NewServer(http.HandlerFunc(cb)) srv := httptest.NewServer(http.HandlerFunc(cb))
b := &terraform.BackendState{ b := &legacy.BackendState{
Type: "http", Type: "http",
} }
b.SetConfig(cty.ObjectVal(map[string]cty.Value{ b.SetConfig(cty.ObjectVal(map[string]cty.Value{

View File

@ -31,6 +31,8 @@ import (
"github.com/hashicorp/terraform/tfdiags" "github.com/hashicorp/terraform/tfdiags"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/mitchellh/colorstring" "github.com/mitchellh/colorstring"
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
) )
// Meta are the meta-options that are available on all or most commands. // Meta are the meta-options that are available on all or most commands.
@ -147,7 +149,7 @@ type Meta struct {
configLoader *configload.Loader configLoader *configload.Loader
// backendState is the currently active backend state // backendState is the currently active backend state
backendState *terraform.BackendState backendState *legacy.BackendState
// Variables for the context (private) // Variables for the context (private)
variableArgs rawFlags variableArgs rawFlags

View File

@ -29,6 +29,7 @@ import (
backendInit "github.com/hashicorp/terraform/backend/init" backendInit "github.com/hashicorp/terraform/backend/init"
backendLocal "github.com/hashicorp/terraform/backend/local" backendLocal "github.com/hashicorp/terraform/backend/local"
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
) )
// BackendOpts are the options used to initialize a backend.Backend. // BackendOpts are the options used to initialize a backend.Backend.
@ -160,7 +161,7 @@ func (m *Meta) Backend(opts *BackendOpts) (backend.Enhanced, tfdiags.Diagnostics
// with inside backendFromConfig, because we still need that codepath // with inside backendFromConfig, because we still need that codepath
// to be able to recognize the lack of a config as distinct from // to be able to recognize the lack of a config as distinct from
// explicitly setting local until we do some more refactoring here. // explicitly setting local until we do some more refactoring here.
m.backendState = &terraform.BackendState{ m.backendState = &legacy.BackendState{
Type: "local", Type: "local",
ConfigRaw: json.RawMessage("{}"), ConfigRaw: json.RawMessage("{}"),
} }
@ -461,7 +462,7 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di
s := sMgr.State() s := sMgr.State()
if s == nil { if s == nil {
log.Printf("[TRACE] Meta.Backend: backend has not previously been initialized in this working directory") log.Printf("[TRACE] Meta.Backend: backend has not previously been initialized in this working directory")
s = terraform.NewState() s = legacy.NewState()
} else if s.Backend != nil { } else if s.Backend != nil {
log.Printf("[TRACE] Meta.Backend: working directory was previously initialized for %q backend", s.Backend.Type) log.Printf("[TRACE] Meta.Backend: working directory was previously initialized for %q backend", s.Backend.Type)
} else { } else {
@ -818,9 +819,9 @@ func (m *Meta) backend_C_r_s(c *configs.Backend, cHash int, sMgr *clistate.Local
// Store the metadata in our saved state location // Store the metadata in our saved state location
s := sMgr.State() s := sMgr.State()
if s == nil { if s == nil {
s = terraform.NewState() s = legacy.NewState()
} }
s.Backend = &terraform.BackendState{ s.Backend = &legacy.BackendState{
Type: c.Type, Type: c.Type,
ConfigRaw: json.RawMessage(configJSON), ConfigRaw: json.RawMessage(configJSON),
Hash: uint64(cHash), Hash: uint64(cHash),
@ -902,9 +903,9 @@ func (m *Meta) backend_C_r_S_changed(c *configs.Backend, cHash int, sMgr *clista
// Update the backend state // Update the backend state
s = sMgr.State() s = sMgr.State()
if s == nil { if s == nil {
s = terraform.NewState() s = legacy.NewState()
} }
s.Backend = &terraform.BackendState{ s.Backend = &legacy.BackendState{
Type: c.Type, Type: c.Type,
ConfigRaw: json.RawMessage(configJSON), ConfigRaw: json.RawMessage(configJSON),
Hash: uint64(cHash), Hash: uint64(cHash),
@ -996,7 +997,7 @@ func (m *Meta) backend_C_r_S_unchanged(c *configs.Backend, cHash int, sMgr *clis
// this function will conservatively assume that migration is required, // this function will conservatively assume that migration is required,
// expecting that the migration code will subsequently deal with the same // expecting that the migration code will subsequently deal with the same
// errors. // errors.
func (m *Meta) backendConfigNeedsMigration(c *configs.Backend, s *terraform.BackendState) bool { func (m *Meta) backendConfigNeedsMigration(c *configs.Backend, s *legacy.BackendState) bool {
if s == nil || s.Empty() { if s == nil || s.Empty() {
log.Print("[TRACE] backendConfigNeedsMigration: no cached config, so migration is required") log.Print("[TRACE] backendConfigNeedsMigration: no cached config, so migration is required")
return true return true

View File

@ -18,7 +18,6 @@ import (
tfplugin "github.com/hashicorp/terraform/plugin" tfplugin "github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/plugin/discovery" "github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/provisioners" "github.com/hashicorp/terraform/provisioners"
"github.com/hashicorp/terraform/terraform"
) )
// NOTE WELL: The logic in this file is primarily about plugin types OTHER THAN // NOTE WELL: The logic in this file is primarily about plugin types OTHER THAN
@ -120,7 +119,7 @@ func (m *Meta) pluginDirs(includeAutoInstalled bool) []string {
return dirs return dirs
} }
func (m *Meta) provisionerFactories() map[string]terraform.ProvisionerFactory { func (m *Meta) provisionerFactories() map[string]provisioners.Factory {
dirs := m.pluginDirs(true) dirs := m.pluginDirs(true)
plugins := discovery.FindPlugins("provisioner", dirs) plugins := discovery.FindPlugins("provisioner", dirs)
plugins, _ = plugins.ValidateVersions() plugins, _ = plugins.ValidateVersions()
@ -131,7 +130,7 @@ func (m *Meta) provisionerFactories() map[string]terraform.ProvisionerFactory {
// name here, even though the discovery interface forces us to pretend // name here, even though the discovery interface forces us to pretend
// that might not be true. // that might not be true.
factories := make(map[string]terraform.ProvisionerFactory) factories := make(map[string]provisioners.Factory)
// Wire up the internal provisioners first. These might be overridden // Wire up the internal provisioners first. These might be overridden
// by discovered provisioners below. // by discovered provisioners below.
@ -175,7 +174,7 @@ func internalPluginClient(kind, name string) (*plugin.Client, error) {
return plugin.NewClient(cfg), nil return plugin.NewClient(cfg), nil
} }
func provisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory { func provisionerFactory(meta discovery.PluginMeta) provisioners.Factory {
return func() (provisioners.Interface, error) { return func() (provisioners.Interface, error) {
cfg := &plugin.ClientConfig{ cfg := &plugin.ClientConfig{
Cmd: exec.Command(meta.Path), Cmd: exec.Command(meta.Path),
@ -191,7 +190,7 @@ func provisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory
} }
} }
func internalProvisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory { func internalProvisionerFactory(meta discovery.PluginMeta) provisioners.Factory {
return func() (provisioners.Interface, error) { return func() (provisioners.Interface, error) {
client, err := internalPluginClient("provisioner", meta.Name) client, err := internalPluginClient("provisioner", meta.Name)
if err != nil { if err != nil {

View File

@ -5,8 +5,9 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/backend/remote-state/inmem" "github.com/hashicorp/terraform/backend/remote-state/inmem"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
) )
// Since we can't unlock a local state file, just test that calling unlock // Since we can't unlock a local state file, just test that calling unlock
@ -24,7 +25,7 @@ func TestUnlock(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
err = terraform.WriteState(terraform.NewState(), f) err = legacy.WriteState(legacy.NewState(), f)
f.Close() f.Close()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)

View File

@ -13,8 +13,9 @@ import (
"github.com/hashicorp/terraform/backend/remote-state/inmem" "github.com/hashicorp/terraform/backend/remote-state/inmem"
"github.com/hashicorp/terraform/states" "github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statemgr" "github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
) )
func TestWorkspace_createAndChange(t *testing.T) { func TestWorkspace_createAndChange(t *testing.T) {
@ -379,14 +380,14 @@ func TestWorkspace_deleteWithState(t *testing.T) {
} }
// create a non-empty state // create a non-empty state
originalState := &terraform.State{ originalState := &legacy.State{
Modules: []*terraform.ModuleState{ Modules: []*legacy.ModuleState{
&terraform.ModuleState{ &legacy.ModuleState{
Path: []string{"root"}, Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{ Resources: map[string]*legacy.ResourceState{
"test_instance.foo": &terraform.ResourceState{ "test_instance.foo": &legacy.ResourceState{
Type: "test_instance", Type: "test_instance",
Primary: &terraform.InstanceState{ Primary: &legacy.InstanceState{
ID: "bar", ID: "bar",
}, },
}, },
@ -400,7 +401,7 @@ func TestWorkspace_deleteWithState(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer f.Close() defer f.Close()
if err := terraform.WriteState(originalState, f); err != nil { if err := legacy.WriteState(originalState, f); err != nil {
t.Fatal(err) t.Fatal(err)
} }