Skip to content

Commit

Permalink
Renaming variable (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jan 22, 2024
1 parent 31f8d5e commit 031df21
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
12 changes: 6 additions & 6 deletions statecheck/expect_known_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type expectKnownValue struct {

// CheckState implements the state check logic.
func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest, resp *CheckStateResponse) {
var rc *tfjson.StateResource
var resource *tfjson.StateResource

if req.State == nil {
resp.Error = fmt.Errorf("state is nil")
Expand All @@ -44,21 +44,21 @@ func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest,
return
}

for _, resourceChange := range req.State.Values.RootModule.Resources {
if e.resourceAddress == resourceChange.Address {
rc = resourceChange
for _, r := range req.State.Values.RootModule.Resources {
if e.resourceAddress == r.Address {
resource = r

break
}
}

if rc == nil {
if resource == nil {
resp.Error = fmt.Errorf("%s - Resource not found in state", e.resourceAddress)

return
}

result, err := tfjsonpath.Traverse(rc.AttributeValues, e.attributePath)
result, err := tfjsonpath.Traverse(resource.AttributeValues, e.attributePath)

if err != nil {
resp.Error = err
Expand Down
12 changes: 6 additions & 6 deletions statecheck/expect_sensitive_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type expectSensitiveValue struct {

// CheckState implements the state check logic.
func (e expectSensitiveValue) CheckState(ctx context.Context, req CheckStateRequest, resp *CheckStateResponse) {
var rc *tfjson.StateResource
var resource *tfjson.StateResource

if req.State == nil {
resp.Error = fmt.Errorf("state is nil")
Expand All @@ -42,23 +42,23 @@ func (e expectSensitiveValue) CheckState(ctx context.Context, req CheckStateRequ
return
}

for _, resourceChange := range req.State.Values.RootModule.Resources {
if e.resourceAddress == resourceChange.Address {
rc = resourceChange
for _, r := range req.State.Values.RootModule.Resources {
if e.resourceAddress == r.Address {
resource = r

break
}
}

if rc == nil {
if resource == nil {
resp.Error = fmt.Errorf("%s - Resource not found in state", e.resourceAddress)

return
}

var data map[string]any

err := json.Unmarshal(rc.SensitiveValues, &data)
err := json.Unmarshal(resource.SensitiveValues, &data)

if err != nil {
resp.Error = fmt.Errorf("could not unmarshal SensitiveValues: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ import (
"context"
"fmt"

tfjson "github.com/hashicorp/terraform-json"

"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

var _ StateCheck = expectKnownValue{}
var _ statecheck.StateCheck = expectKnownValue{}

type expectKnownValue struct {
resourceAddress string
attributePath tfjsonpath.Path
knownValue knownvalue.Check
}

func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest, resp *CheckStateResponse) {
var rc *tfjson.StateResource
func (e expectKnownValue) CheckState(ctx context.Context, req statecheck.CheckStateRequest, resp *statecheck.CheckStateResponse) {
var resource *tfjson.StateResource

if req.State == nil {
resp.Error = fmt.Errorf("state is nil")
Expand All @@ -45,21 +49,21 @@ func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest,
resp.Error = fmt.Errorf("state does not contain a root module")
}

for _, resourceChange := range req.State.Values.RootModule.Resources {
if e.resourceAddress == resourceChange.Address {
rc = resourceChange
for _, r := range req.State.Values.RootModule.Resources {
if e.resourceAddress == r.Address {
resource = r

break
}
}

if rc == nil {
if resource == nil {
resp.Error = fmt.Errorf("%s - Resource not found in state", e.resourceAddress)

return
}

result, err := tfjsonpath.Traverse(rc.AttributeValues, e.attributePath)
result, err := tfjsonpath.Traverse(resource.AttributeValues, e.attributePath)

if err != nil {
resp.Error = err
Expand All @@ -72,7 +76,7 @@ func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest,
}
}

func ExpectKnownValue(resourceAddress string, attributePath tfjsonpath.Path, knownValue knownvalue.Check) StateCheck {
func ExpectKnownValue(resourceAddress string, attributePath tfjsonpath.Path, knownValue knownvalue.Check) statecheck.StateCheck {
return expectKnownValue{
resourceAddress: resourceAddress,
attributePath: attributePath,
Expand All @@ -82,13 +86,14 @@ func ExpectKnownValue(resourceAddress string, attributePath tfjsonpath.Path, kno
```

And example usage:

```go
package example_test

import (
"testing"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
Expand All @@ -97,15 +102,15 @@ import (
func TestExpectKnownValue_CheckState_Bool(t *testing.T) {
t.Parallel()

r.Test(t, r.TestCase{
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []r.TestStep{
Steps: []resource.TestStep{
{
Config: `resource "test_resource" "one" {
bool_attribute = true
}
`,
ConfigStateChecks: r.ConfigStateChecks{
ConfigStateChecks: resource.ConfigStateChecks{
statecheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("bool_attribute"),
Expand Down

0 comments on commit 031df21

Please sign in to comment.