Skip to content

Commit 3cd2fad

Browse files
ephemeral: add support for write-only attributes
1 parent cbfa072 commit 3cd2fad

File tree

10 files changed

+335
-12
lines changed

10 files changed

+335
-12
lines changed

internal/plans/changes.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ type Change struct {
582582
// collections/structures.
583583
Before, After cty.Value
584584

585+
// BeforeWriteOnlyPaths and AfterWriteOnlyPaths are paths for any values
586+
// in Before or After (respectively) that are considered to be write-only.
587+
BeforeWriteOnlyPaths, AfterWriteOnlyPaths []cty.Path
588+
585589
// Importing is present if the resource is being imported as part of this
586590
// change.
587591
//
@@ -645,8 +649,8 @@ func (c *Change) Encode(ty cty.Type) (*ChangeSrc, error) {
645649
After: afterDV,
646650
BeforeSensitivePaths: sensitiveAttrsBefore,
647651
AfterSensitivePaths: sensitiveAttrsAfter,
648-
BeforeWriteOnlyPaths: nil, // TODO: Add write-only paths
649-
AfterWriteOnlyPaths: nil, // TODO: Add write-only paths
652+
BeforeWriteOnlyPaths: c.BeforeWriteOnlyPaths,
653+
AfterWriteOnlyPaths: c.AfterWriteOnlyPaths,
650654
Importing: c.Importing.Encode(),
651655
GeneratedConfig: c.GeneratedConfig,
652656
}, nil

internal/plans/changes_src.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ func (c *ChangesSrc) Decode(schemas *schemarepo.Schemas) (*Changes, error) {
130130
rc.Before = marks.MarkPaths(rc.Before, marks.Sensitive, rcs.BeforeSensitivePaths)
131131
rc.After = marks.MarkPaths(rc.After, marks.Sensitive, rcs.AfterSensitivePaths)
132132

133+
rc.Before = marks.MarkPaths(rc.Before, marks.Ephemeral, rcs.BeforeWriteOnlyPaths)
134+
rc.After = marks.MarkPaths(rc.After, marks.Ephemeral, rcs.BeforeWriteOnlyPaths)
135+
136+
rc.BeforeWriteOnlyPaths = rcs.BeforeWriteOnlyPaths
137+
rc.AfterWriteOnlyPaths = rcs.AfterWriteOnlyPaths
138+
133139
changes.Resources = append(changes.Resources, rc)
134140
}
135141

internal/states/instance_object.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ type ResourceInstanceObject struct {
5050
// destroy operations, we need to record the status to ensure a resource
5151
// removed from the config will still be destroyed in the same manner.
5252
CreateBeforeDestroy bool
53+
54+
// AttrWriteOnlyPaths is an array of paths to mark as ephemeral coming out of
55+
// state, or to save as write_only paths when saving state
56+
AttrWriteOnlyPaths []cty.Path
5357
}
5458

5559
// ObjectStatus represents the status of a RemoteObject.
@@ -135,6 +139,7 @@ func (o *ResourceInstanceObject) Encode(ty cty.Type, schemaVersion uint64) (*Res
135139
SchemaVersion: schemaVersion,
136140
AttrsJSON: src,
137141
AttrSensitivePaths: sensitivePaths,
142+
AttrWriteOnlyPaths: o.AttrWriteOnlyPaths,
138143
Private: o.Private,
139144
Status: o.Status,
140145
Dependencies: dependencies,

internal/states/instance_object_src.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (os *ResourceInstanceObjectSrc) Decode(ty cty.Type) (*ResourceInstanceObjec
100100
default:
101101
val, err = ctyjson.Unmarshal(os.AttrsJSON, ty)
102102
val = marks.MarkPaths(val, marks.Sensitive, os.AttrSensitivePaths)
103+
val = marks.MarkPaths(val, marks.Ephemeral, os.AttrWriteOnlyPaths)
103104
if err != nil {
104105
return nil, err
105106
}
@@ -111,6 +112,7 @@ func (os *ResourceInstanceObjectSrc) Decode(ty cty.Type) (*ResourceInstanceObjec
111112
Dependencies: os.Dependencies,
112113
Private: os.Private,
113114
CreateBeforeDestroy: os.CreateBeforeDestroy,
115+
AttrWriteOnlyPaths: os.AttrWriteOnlyPaths,
114116
}, nil
115117
}
116118

internal/states/remote/state_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ func TestStatePersist(t *testing.T) {
129129
"attributes_flat": map[string]interface{}{
130130
"filename": "file.txt",
131131
},
132-
"schema_version": 0.0,
133-
"sensitive_attributes": []interface{}{},
132+
"schema_version": 0.0,
133+
"sensitive_attributes": []interface{}{},
134+
"write_only_attributes": []interface{}{},
134135
},
135136
},
136137
"mode": "managed",
@@ -167,8 +168,9 @@ func TestStatePersist(t *testing.T) {
167168
"attributes_flat": map[string]interface{}{
168169
"filename": "file.txt",
169170
},
170-
"schema_version": 0.0,
171-
"sensitive_attributes": []interface{}{},
171+
"schema_version": 0.0,
172+
"sensitive_attributes": []interface{}{},
173+
"write_only_attributes": []interface{}{},
172174
},
173175
},
174176
"mode": "managed",

internal/states/statefile/version4.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ func appendInstanceObjectStateV4(rs *states.Resource, is *states.ResourceInstanc
492492
// Marshal paths to JSON
493493
attributeSensitivePaths, pathsDiags := marshalPaths(obj.AttrSensitivePaths)
494494
diags = diags.Append(pathsDiags)
495+
attributeWriteOnlyPaths, pathsDiags := marshalPaths(obj.AttrWriteOnlyPaths)
496+
diags = diags.Append(pathsDiags)
495497

496498
return append(isV4s, instanceObjectStateV4{
497499
IndexKey: rawKey,
@@ -501,6 +503,7 @@ func appendInstanceObjectStateV4(rs *states.Resource, is *states.ResourceInstanc
501503
AttributesFlat: obj.AttrsFlat,
502504
AttributesRaw: obj.AttrsJSON,
503505
AttributeSensitivePaths: attributeSensitivePaths,
506+
AttributeWriteOnlyPaths: attributeWriteOnlyPaths,
504507
PrivateRaw: privateRaw,
505508
Dependencies: deps,
506509
CreateBeforeDestroy: obj.CreateBeforeDestroy,

0 commit comments

Comments
 (0)