-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlifecycle.go
118 lines (106 loc) · 3.23 KB
/
lifecycle.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terra
import (
"fmt"
tkihcl "github.com/golingon/lingon/pkg/internal/hcl"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// IgnoreChanges takes a list of object attributes to include in the
// `ignore_changes` list for the lifecycle of a resource.
func IgnoreChanges(attrs ...Referencer) LifecyleIgnoreChanges {
refs := make(LifecyleIgnoreChanges, len(attrs))
for i, attr := range attrs {
ref, err := attr.InternalRef()
if err != nil {
panic(
fmt.Sprintf(
"IgnoreChanges: getting list of attributes: %s",
err.Error(),
),
)
}
refs[i] = ref
}
return refs
}
var _ tkihcl.Tokenizer = (*LifecyleIgnoreChanges)(nil)
// LifecyleIgnoreChanges is a list of references to attributes that we want to
// ignore in the `lifecycle` block
type LifecyleIgnoreChanges []Reference
// InternalTokens only returns the relative address of a reference.
// This is due to the specification for the `ignore_changes` list inside the
// `lifecycle` block. E.g.
//
// resource "aws_instance" "example" {
// # ...
// lifecycle {
// ignore_changes = [
// # Use a relative reference to the tags,
// # i.e. not `aws_instance.example.tags`
// tags,
// ]
// }
// }
func (l LifecyleIgnoreChanges) InternalTokens() (hclwrite.Tokens, error) {
if len(l) == 0 {
return nil, nil
}
elems := make([]hclwrite.Tokens, len(l))
for i, ref := range l {
tokens, err := tokensForSteps(ref.steps)
if err != nil {
return nil, fmt.Errorf(
"creating tokens for lifecycle ignore_changes: %w",
err,
)
}
elems[i] = tokens
}
return hclwrite.TokensForTuple(elems), nil
}
// ReplaceTriggeredBy takes a list of object attributes to add to the
// `replace_triggered_by` list for the lifecycle of a resource.
func ReplaceTriggeredBy(attrs ...Referencer) LifecycleReplaceTriggeredBy {
refs := make(LifecycleReplaceTriggeredBy, len(attrs))
for i, attr := range attrs {
ref, err := attr.InternalRef()
if err != nil {
panic(
fmt.Sprintf(
"ReplaceTriggeredBy: getting list of attributes: %s",
err.Error(),
),
)
}
refs[i] = ref
}
return refs
}
var _ tkihcl.Tokenizer = (*LifecycleReplaceTriggeredBy)(nil)
// LifecycleReplaceTriggeredBy is a list of references to attributes that we
// want to trigger a replacement on if those attributes themselves are replaced.
type LifecycleReplaceTriggeredBy []Reference
// InternalTokens returns the HCL tokens for the `replace_triggered_by` list
func (r LifecycleReplaceTriggeredBy) InternalTokens() (hclwrite.Tokens, error) {
if len(r) == 0 {
return nil, nil
}
elems := make([]hclwrite.Tokens, len(r))
for i, ref := range r {
toks, err := ref.InternalTokens()
if err != nil {
return nil, fmt.Errorf(
"creating tokens for lifecycle replace_triggered_by: %w", err,
)
}
elems[i] = toks
}
return hclwrite.TokensForTuple(elems), nil
}
type Lifecycle struct {
CreateBeforeDestroy BoolValue `hcl:"create_before_destroy,attr"`
PreventDestroy BoolValue `hcl:"prevent_destroy,attr"`
IgnoreChanges LifecyleIgnoreChanges `hcl:"ignore_changes,attr"`
ReplaceTriggeredBy LifecycleReplaceTriggeredBy `hcl:"replace_triggered_by,attr"`
}