Skip to content

Commit 5eb089d

Browse files
authored
terraform: Add deprecation Go documentation comments (#171)
Reference: #165 Except in certain known provider testing use cases without replacement (yet), this deprecates various `terraform` package functionality. The `terraform` package contains legacy Terraform core logic which has been copied to terraform-plugin-sdk and terraform-plugin-testing over the years and continually exported due to the complexity of rewriting developer functionality using the machine-readable interfaces for Terraform, such as JSON state. Much of this now-deprecated `terraform` package logic should have been omitted when this Go module was created, but that step was missed, so it is left as-is with deprecation notices following the Go module versioning guidelines. In reality, the entire `terraform` package will be removed in a future major version, however this change pragmatically leaves certain functionality without deprecation notices for now until replacement functionality is available so developers will not need to silence linting tools without it being actionable yet. This change opts to not include a changelog entry here because it could be more confusing for developers, who might feel like they need to go check for usage when it likely affects only a very small (if any) portion of the population. Their usage externally would be unexpected as all of these are unintentionally part of the exported Go API having been just copied Terraform core "internal" code from some historical time and it is not likely any future changes will occur to their logic since they fall outside the intention of the Go module being acceptance testing of Terraform Providers. If/when it becomes time to deprecate the `terraform.State` type, which is externally prevalent due to `helper/resource.TestCheckFunc`, that certainly warrants a changelog entry (and maybe even website documentation and upgrade tooling) with its replacement. This change attempted to be careful and leave other somewhat prevalent functionality under the `TestCheckFunc` expected use case, such as `(terraform.State).RootModule()`, also without deprecation comments for now since there is no replacement yet. If there are other similar parts of this now-deprecated functionality which adversely affect provider acceptance testing use cases, the deprecation comments can be removed until replacement functionality is in place. To that end, if there is a valid use case for this now-deprecated functionality, developers should create a GitHub issue for tracking.
1 parent c132058 commit 5eb089d

File tree

11 files changed

+283
-73
lines changed

11 files changed

+283
-73
lines changed

helper/resource/state_shim.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type shimmedState struct {
2121
}
2222

2323
func shimStateFromJson(jsonState *tfjson.State) (*terraform.State, error) {
24-
state := terraform.NewState()
24+
state := terraform.NewState() //nolint:staticcheck // legacy usage
2525
state.TFVersion = jsonState.TerraformVersion
2626

2727
if jsonState.Values == nil {
@@ -124,7 +124,7 @@ func (ss *shimmedState) shimStateModule(sm *tfjson.StateModule) error {
124124
}
125125
}
126126

127-
mod := ss.state.AddModule(path)
127+
mod := ss.state.AddModule(path) //nolint:staticcheck // legacy usage
128128
for _, res := range sm.Resources {
129129
resourceState, err := shimResourceState(res)
130130
if err != nil {

helper/resource/testing.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ func modulePrimaryInstanceState(ms *terraform.ModuleState, name string) (*terraf
16101610
// modulePathPrimaryInstanceState returns the primary instance state for the
16111611
// given resource name in a given module path.
16121612
func modulePathPrimaryInstanceState(s *terraform.State, mp addrs.ModuleInstance, name string) (*terraform.InstanceState, error) {
1613-
ms := s.ModuleByPath(mp)
1613+
ms := s.ModuleByPath(mp) //nolint:staticcheck // legacy usage
16141614
if ms == nil {
16151615
return nil, fmt.Errorf("No module found at: %s", mp)
16161616
}
@@ -1621,7 +1621,7 @@ func modulePathPrimaryInstanceState(s *terraform.State, mp addrs.ModuleInstance,
16211621
// primaryInstanceState returns the primary instance state for the given
16221622
// resource name in the root module.
16231623
func primaryInstanceState(s *terraform.State, name string) (*terraform.InstanceState, error) {
1624-
ms := s.RootModule()
1624+
ms := s.RootModule() //nolint:staticcheck // legacy usage
16251625
return modulePrimaryInstanceState(ms, name)
16261626
}
16271627

@@ -1640,7 +1640,7 @@ func indexesIntoTypeSet(key string) bool {
16401640
func checkIfIndexesIntoTypeSet(key string, f TestCheckFunc) TestCheckFunc {
16411641
return func(s *terraform.State) error {
16421642
err := f(s)
1643-
if err != nil && s.IsBinaryDrivenTest && indexesIntoTypeSet(key) {
1643+
if err != nil && indexesIntoTypeSet(key) {
16441644
return fmt.Errorf("Error in test check: %s\nTest check address %q likely indexes into TypeSet\nThis is currently not possible in the SDK", err, key)
16451645
}
16461646
return err
@@ -1650,7 +1650,7 @@ func checkIfIndexesIntoTypeSet(key string, f TestCheckFunc) TestCheckFunc {
16501650
func checkIfIndexesIntoTypeSetPair(keyFirst, keySecond string, f TestCheckFunc) TestCheckFunc {
16511651
return func(s *terraform.State) error {
16521652
err := f(s)
1653-
if err != nil && s.IsBinaryDrivenTest && (indexesIntoTypeSet(keyFirst) || indexesIntoTypeSet(keySecond)) {
1653+
if err != nil && (indexesIntoTypeSet(keyFirst) || indexesIntoTypeSet(keySecond)) {
16541654
return fmt.Errorf("Error in test check: %s\nTest check address %q or %q likely indexes into TypeSet\nThis is currently not possible in the SDK", err, keyFirst, keySecond)
16551655
}
16561656
return err

helper/resource/testing_new.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func getState(ctx context.Context, t testing.T, wd *plugintest.WorkingDir) (*ter
356356
}
357357

358358
func stateIsEmpty(state *terraform.State) bool {
359-
return state.Empty() || !state.HasResources()
359+
return state.Empty() || !state.HasResources() //nolint:staticcheck // legacy usage
360360
}
361361

362362
func planIsEmpty(plan *tfjson.Plan) bool {
@@ -375,7 +375,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
375375

376376
// Build the state. The state is just the resource with an ID. There
377377
// are no attributes. We only set what is needed to perform a refresh.
378-
state := terraform.NewState()
378+
state := terraform.NewState() //nolint:staticcheck // legacy usage
379379
state.RootModule().Resources = make(map[string]*terraform.ResourceState)
380380
state.RootModule().Resources[c.IDRefreshName] = &terraform.ResourceState{}
381381

helper/resource/testing_new_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
112112
if step.Check != nil {
113113
logging.HelperResourceTrace(ctx, "Using TestStep Check")
114114

115-
state.IsBinaryDrivenTest = true
116115
if step.Destroy {
117116
if err := step.Check(stateBeforeApplication); err != nil {
118117
return fmt.Errorf("Check failed: %w", err)
@@ -244,6 +243,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
244243
return err
245244
}
246245

246+
//nolint:staticcheck // legacy usage
247247
if state.Empty() {
248248
return nil
249249
}

helper/resource/testing_new_import_state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
147147
continue
148148
}
149149

150-
is := r.Primary.DeepCopy()
150+
is := r.Primary.DeepCopy() //nolint:staticcheck // legacy usage
151151
is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type
152152
states = append(states, is)
153153
}

0 commit comments

Comments
 (0)