-
Notifications
You must be signed in to change notification settings - Fork 11
/
expect_known_value.go
70 lines (52 loc) · 1.66 KB
/
expect_known_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plancheck
import (
"context"
"fmt"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)
// Resource Plan Check
var _ PlanCheck = expectKnownValue{}
type expectKnownValue struct {
resourceAddress string
attributePath tfjsonpath.Path
knownValue knownvalue.Check
}
// CheckPlan implements the plan check logic.
func (e expectKnownValue) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) {
var rc *tfjson.ResourceChange
if req.Plan == nil {
resp.Error = fmt.Errorf("plan is nil")
}
for _, resourceChange := range req.Plan.ResourceChanges {
if e.resourceAddress == resourceChange.Address {
rc = resourceChange
break
}
}
if rc == nil {
resp.Error = fmt.Errorf("%s - Resource not found in plan", e.resourceAddress)
return
}
result, err := tfjsonpath.Traverse(rc.Change.After, e.attributePath)
if err != nil {
resp.Error = err
return
}
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for attribute at path: %s.%s, err: %s", e.resourceAddress, e.attributePath.String(), err)
return
}
}
// ExpectKnownValue returns a plan check that asserts that the specified attribute at the given resource
// has a known type and value.
func ExpectKnownValue(resourceAddress string, attributePath tfjsonpath.Path, knownValue knownvalue.Check) PlanCheck {
return expectKnownValue{
resourceAddress: resourceAddress,
attributePath: attributePath,
knownValue: knownValue,
}
}