Skip to content

Commit c597f9d

Browse files
authored
Add checks and plan timestamp for Terraform v1.5 (#87)
* Add checks and plan timestamp for Terraform v1.5 * add copywrite headers * add module into dynamic address
1 parent 2a2fd06 commit c597f9d

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

checks.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfjson
5+
6+
// CheckKind is a string representation of the type of conditional check
7+
// referenced in a check result.
8+
type CheckKind string
9+
10+
const (
11+
// CheckKindResource indicates the check result is from a pre- or
12+
// post-condition on a resource or data source.
13+
CheckKindResource CheckKind = "resource"
14+
15+
// CheckKindOutputValue indicates the check result is from an output
16+
// post-condition.
17+
CheckKindOutputValue CheckKind = "output_value"
18+
19+
// CheckKindCheckBlock indicates the check result is from a check block.
20+
CheckKindCheckBlock CheckKind = "check"
21+
)
22+
23+
// CheckStatus is a string representation of the status of a given conditional
24+
// check.
25+
type CheckStatus string
26+
27+
const (
28+
// CheckStatusPass indicates the check passed.
29+
CheckStatusPass CheckStatus = "pass"
30+
31+
// CheckStatusFail indicates the check failed.
32+
CheckStatusFail CheckStatus = "fail"
33+
34+
// CheckStatusError indicates the check errored. This is distinct from
35+
// CheckStatusFail in that it represents a logical or configuration error
36+
// within the check block that prevented the check from executing, as
37+
// opposed to the check was attempted and evaluated to false.
38+
CheckStatusError CheckStatus = "error"
39+
40+
// CheckStatusUnknown indicates the result of the check was not known. This
41+
// could be because a value within the check could not be known at plan
42+
// time, or because the overall plan failed for an unrelated reason before
43+
// this check could be executed.
44+
CheckStatusUnknown CheckStatus = "unknown"
45+
)
46+
47+
// CheckStaticAddress details the address of the object that performed a given
48+
// check. The static address points to the overall resource, as opposed to the
49+
// dynamic address which contains the instance key for any resource that has
50+
// multiple instances.
51+
type CheckStaticAddress struct {
52+
// ToDisplay is a formatted and ready to display representation of the
53+
// address.
54+
ToDisplay string `json:"to_display"`
55+
56+
// Kind represents the CheckKind of this check.
57+
Kind CheckKind `json:"kind"`
58+
59+
// Module is the module part of the address. This will be empty for any
60+
// resources in the root module.
61+
Module string `json:"module,omitempty"`
62+
63+
// Mode is the ResourceMode of the resource that contains this check. This
64+
// field is only set is Kind equals CheckKindResource.
65+
Mode ResourceMode `json:"mode,omitempty"`
66+
67+
// Type is the resource type for the resource that contains this check. This
68+
// field is only set if Kind equals CheckKindResource.
69+
Type string `json:"type,omitempty"`
70+
71+
// Name is the name of the resource, check block, or output that contains
72+
// this check.
73+
Name string `json:"name,omitempty"`
74+
}
75+
76+
// CheckDynamicAddress contains the InstanceKey field for any resources that
77+
// have multiple instances. A complete address can be built by combining the
78+
// CheckStaticAddress with the CheckDynamicAddress.
79+
type CheckDynamicAddress struct {
80+
// ToDisplay is a formatted and ready to display representation of the
81+
// full address, including the additional information from the relevant
82+
// CheckStaticAddress.
83+
ToDisplay string `json:"to_display"`
84+
85+
// Module is the module part of the address. This address will include the
86+
// instance key for any module expansions resulting from foreach or count
87+
// arguments. This field will be empty for any resources within the root
88+
// module.
89+
Module string `json:"module,omitempty"`
90+
91+
// InstanceKey is the instance key for any instances of a given resource.
92+
//
93+
// InstanceKey will be empty if there was no foreach or count argument
94+
// defined on the containing object.
95+
InstanceKey string `json:"instance_key,omitempty"`
96+
}
97+
98+
// CheckResultStatic is the container for a "checkable object".
99+
//
100+
// A "checkable object" is a resource or data source, an output, or a check
101+
// block.
102+
type CheckResultStatic struct {
103+
// Address is the absolute address of the "checkable object"
104+
Address CheckStaticAddress `json:"address"`
105+
106+
// Status is the overall status for all the checks within this object.
107+
Status CheckStatus `json:"status"`
108+
109+
// Instances contains the results for dynamic object that belongs to this
110+
// static object. For example, any instances created from an object using
111+
// the foreach or count meta arguments.
112+
//
113+
// Check blocks and outputs will only contain a single instance, while
114+
// resources can contain 1 to many.
115+
Instances []CheckResultDynamic `json:"instances,omitempty"`
116+
}
117+
118+
// CheckResultDynamic describes the check result for a dynamic object that
119+
// results from the expansion of the containing object.
120+
type CheckResultDynamic struct {
121+
// Address is the relative address of this instance given the Address in the
122+
// parent object.
123+
Address CheckDynamicAddress `json:"address"`
124+
125+
// Status is the overall status for the checks within this dynamic object.
126+
Status CheckStatus `json:"status"`
127+
128+
// Problems describes any additional optional details about this check if
129+
// the check failed.
130+
//
131+
// This will not include the errors resulting from this check block, as they
132+
// will be exposed as diagnostics in the original terraform execution. It
133+
// may contain any failure messages even if the overall status is
134+
// CheckStatusError, however, as the instance could contain multiple checks
135+
// that returned a mix of error and failure statuses.
136+
Problems []CheckResultProblem `json:"problems,omitempty"`
137+
}
138+
139+
// CheckResultProblem describes one of potentially several problems that led to
140+
// a check being classied as CheckStatusFail.
141+
type CheckResultProblem struct {
142+
// Message is the condition error message provided by the original check
143+
// author.
144+
Message string `json:"message"`
145+
}

plan.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ type Plan struct {
6666
// RelevantAttributes represents any resource instances and their
6767
// attributes which may have contributed to the planned changes
6868
RelevantAttributes []ResourceAttribute `json:"relevant_attributes,omitempty"`
69+
70+
// Checks contains the results of any conditional checks executed, or
71+
// planned to be executed, during this plan.
72+
Checks []CheckResultStatic `json:"checks,omitempty"`
73+
74+
// Timestamp contains the static timestamp that Terraform considers to be
75+
// the time this plan executed, in UTC.
76+
Timestamp string `json:"timestamp,omitempty"`
6977
}
7078

7179
// ResourceAttribute describes a full path to a resource attribute

state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type State struct {
3535

3636
// The values that make up the state.
3737
Values *StateValues `json:"values,omitempty"`
38+
39+
// Checks contains the results of any conditional checks when Values was
40+
// last updated.
41+
Checks *CheckResultStatic `json:"checks,omitempty"`
3842
}
3943

4044
// UseJSONNumber controls whether the State will be decoded using the

0 commit comments

Comments
 (0)