|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package statecheck |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "reflect" |
| 10 | + |
| 11 | + tfjson "github.com/hashicorp/terraform-json" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 14 | +) |
| 15 | + |
| 16 | +var _ StateCheck = expectIdentityValueMatchesStateAtPath{} |
| 17 | + |
| 18 | +type expectIdentityValueMatchesStateAtPath struct { |
| 19 | + resourceAddress string |
| 20 | + identityAttrPath tfjsonpath.Path |
| 21 | + stateAttrPath tfjsonpath.Path |
| 22 | +} |
| 23 | + |
| 24 | +// CheckState implements the state check logic. |
| 25 | +func (e expectIdentityValueMatchesStateAtPath) CheckState(ctx context.Context, req CheckStateRequest, resp *CheckStateResponse) { |
| 26 | + var resource *tfjson.StateResource |
| 27 | + |
| 28 | + if req.State == nil { |
| 29 | + resp.Error = fmt.Errorf("state is nil") |
| 30 | + |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + if req.State.Values == nil { |
| 35 | + resp.Error = fmt.Errorf("state does not contain any state values") |
| 36 | + |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if req.State.Values.RootModule == nil { |
| 41 | + resp.Error = fmt.Errorf("state does not contain a root module") |
| 42 | + |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + for _, r := range req.State.Values.RootModule.Resources { |
| 47 | + if e.resourceAddress == r.Address { |
| 48 | + resource = r |
| 49 | + |
| 50 | + break |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if resource == nil { |
| 55 | + resp.Error = fmt.Errorf("%s - Resource not found in state", e.resourceAddress) |
| 56 | + |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if resource.IdentitySchemaVersion == nil || len(resource.IdentityValues) == 0 { |
| 61 | + resp.Error = fmt.Errorf("%s - Identity not found in state. Either the resource does not support identity or the Terraform version running the test does not support identity. (must be v1.12+)", e.resourceAddress) |
| 62 | + |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + identityResult, err := tfjsonpath.Traverse(resource.IdentityValues, e.identityAttrPath) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + resp.Error = err |
| 70 | + |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + stateResult, err := tfjsonpath.Traverse(resource.AttributeValues, e.stateAttrPath) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + resp.Error = err |
| 78 | + |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if !reflect.DeepEqual(identityResult, stateResult) { |
| 83 | + resp.Error = fmt.Errorf( |
| 84 | + "expected identity (%[1]s.%[2]s) and state value (%[1]s.%[3]s) to match, but they differ: identity value: %[4]v, state value: %[5]v", |
| 85 | + e.resourceAddress, |
| 86 | + e.identityAttrPath.String(), |
| 87 | + e.stateAttrPath.String(), |
| 88 | + identityResult, |
| 89 | + stateResult, |
| 90 | + ) |
| 91 | + |
| 92 | + return |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// ExpectIdentityValueMatchesStateAtPath returns a state check that asserts that the specified identity attribute at the given resource |
| 97 | +// matches the specified attribute in state. This is useful when an identity attribute is in sync with a state attribute of a different path. |
| 98 | +// |
| 99 | +// This state check can only be used with managed resources that support resource identity. Resource identity is only supported in Terraform v1.12+ |
| 100 | +func ExpectIdentityValueMatchesStateAtPath(resourceAddress string, identityAttrPath, stateAttrPath tfjsonpath.Path) StateCheck { |
| 101 | + return expectIdentityValueMatchesStateAtPath{ |
| 102 | + resourceAddress: resourceAddress, |
| 103 | + identityAttrPath: identityAttrPath, |
| 104 | + stateAttrPath: stateAttrPath, |
| 105 | + } |
| 106 | +} |
0 commit comments