-
Notifications
You must be signed in to change notification settings - Fork 91
/
plan.go
81 lines (66 loc) · 2.44 KB
/
plan.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
71
72
73
74
75
76
77
78
79
80
81
package tfsdk
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Plan represents a Terraform plan.
type Plan struct {
Raw tftypes.Value
Schema Schema
}
// Get populates the struct passed as `target` with the entire plan.
func (p Plan) Get(ctx context.Context, target interface{}) diag.Diagnostics {
return p.data().Get(ctx, target)
}
// GetAttribute retrieves the attribute found at `path` and populates the
// `target` with the value.
func (p Plan) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
return p.data().GetAtPath(ctx, path, target)
}
// PathMatches returns all matching path.Paths from the given path.Expression.
//
// If a parent path is null or unknown, which would prevent a full expression
// from matching, the parent path is returned rather than no match to prevent
// false positives.
func (p Plan) PathMatches(ctx context.Context, pathExpr path.Expression) (path.Paths, diag.Diagnostics) {
return p.data().PathMatches(ctx, pathExpr)
}
// Set populates the entire plan using the supplied Go value. The value `val`
// should be a struct whose values have one of the attr.Value types. Each field
// must be tagged with the corresponding schema field.
func (p *Plan) Set(ctx context.Context, val interface{}) diag.Diagnostics {
data := p.data()
diags := data.Set(ctx, val)
if diags.HasError() {
return diags
}
p.Raw = data.TerraformValue
return diags
}
// SetAttribute sets the attribute at `path` using the supplied Go value.
//
// The attribute path and value must be valid with the current schema. If the
// attribute path already has a value, it will be overwritten. If the attribute
// path does not have a value, it will be added, including any parent attribute
// paths as necessary.
//
// Lists can only have the next element added according to the current length.
func (p *Plan) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
data := p.data()
diags := data.SetAtPath(ctx, path, val)
if diags.HasError() {
return diags
}
p.Raw = data.TerraformValue
return diags
}
func (p Plan) data() *fwschemadata.Data {
return &fwschemadata.Data{
Description: fwschemadata.DataDescriptionPlan,
Schema: p.Schema,
TerraformValue: p.Raw,
}
}