/
expect_known_output_value.go
68 lines (50 loc) · 1.5 KB
/
expect_known_output_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
// 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 = expectKnownOutputValue{}
type expectKnownOutputValue struct {
outputAddress string
knownValue knownvalue.Check
}
// CheckPlan implements the plan check logic.
func (e expectKnownOutputValue) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) {
var change *tfjson.Change
if req.Plan == nil {
resp.Error = fmt.Errorf("plan is nil")
}
for address, oc := range req.Plan.OutputChanges {
if e.outputAddress == address {
change = oc
break
}
}
if change == nil {
resp.Error = fmt.Errorf("%s - Output not found in plan", e.outputAddress)
return
}
result, err := tfjsonpath.Traverse(change.After, tfjsonpath.Path{})
if err != nil {
resp.Error = err
return
}
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for output at path: %s, err: %s", e.outputAddress, err)
return
}
}
// ExpectKnownOutputValue returns a plan check that asserts that the specified value
// has a known type, and value.
func ExpectKnownOutputValue(outputAddress string, knownValue knownvalue.Check) PlanCheck {
return expectKnownOutputValue{
outputAddress: outputAddress,
knownValue: knownValue,
}
}