Skip to content

Commit

Permalink
ephemeral: add support for write-only attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Nov 21, 2024
1 parent cbfa072 commit 9f0ce65
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 12 deletions.
8 changes: 6 additions & 2 deletions internal/plans/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ type Change struct {
// collections/structures.
Before, After cty.Value

// BeforeWriteOnlyPaths and AfterWriteOnlyPaths are paths for any values
// in Before or After (respectively) that are considered to be write-only.
BeforeWriteOnlyPaths, AfterWriteOnlyPaths []cty.Path

// Importing is present if the resource is being imported as part of this
// change.
//
Expand Down Expand Up @@ -645,8 +649,8 @@ func (c *Change) Encode(ty cty.Type) (*ChangeSrc, error) {
After: afterDV,
BeforeSensitivePaths: sensitiveAttrsBefore,
AfterSensitivePaths: sensitiveAttrsAfter,
BeforeWriteOnlyPaths: nil, // TODO: Add write-only paths
AfterWriteOnlyPaths: nil, // TODO: Add write-only paths
BeforeWriteOnlyPaths: c.BeforeWriteOnlyPaths,
AfterWriteOnlyPaths: c.AfterWriteOnlyPaths,
Importing: c.Importing.Encode(),
GeneratedConfig: c.GeneratedConfig,
}, nil
Expand Down
6 changes: 6 additions & 0 deletions internal/plans/changes_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func (c *ChangesSrc) Decode(schemas *schemarepo.Schemas) (*Changes, error) {
rc.Before = marks.MarkPaths(rc.Before, marks.Sensitive, rcs.BeforeSensitivePaths)
rc.After = marks.MarkPaths(rc.After, marks.Sensitive, rcs.AfterSensitivePaths)

rc.Before = marks.MarkPaths(rc.Before, marks.Ephemeral, rcs.BeforeWriteOnlyPaths)
rc.After = marks.MarkPaths(rc.After, marks.Ephemeral, rcs.BeforeWriteOnlyPaths)

rc.BeforeWriteOnlyPaths = rcs.BeforeWriteOnlyPaths
rc.AfterWriteOnlyPaths = rcs.AfterWriteOnlyPaths

changes.Resources = append(changes.Resources, rc)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/states/instance_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type ResourceInstanceObject struct {
// destroy operations, we need to record the status to ensure a resource
// removed from the config will still be destroyed in the same manner.
CreateBeforeDestroy bool

// AttrWriteOnlyPaths is an array of paths to mark as ephemeral coming out of
// state, or to save as write_only paths when saving state
AttrWriteOnlyPaths []cty.Path
}

// ObjectStatus represents the status of a RemoteObject.
Expand Down Expand Up @@ -135,6 +139,7 @@ func (o *ResourceInstanceObject) Encode(ty cty.Type, schemaVersion uint64) (*Res
SchemaVersion: schemaVersion,
AttrsJSON: src,
AttrSensitivePaths: sensitivePaths,
AttrWriteOnlyPaths: o.AttrWriteOnlyPaths,
Private: o.Private,
Status: o.Status,
Dependencies: dependencies,
Expand Down
2 changes: 2 additions & 0 deletions internal/states/instance_object_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (os *ResourceInstanceObjectSrc) Decode(ty cty.Type) (*ResourceInstanceObjec
default:
val, err = ctyjson.Unmarshal(os.AttrsJSON, ty)
val = marks.MarkPaths(val, marks.Sensitive, os.AttrSensitivePaths)
val = marks.MarkPaths(val, marks.Ephemeral, os.AttrWriteOnlyPaths)
if err != nil {
return nil, err
}
Expand All @@ -111,6 +112,7 @@ func (os *ResourceInstanceObjectSrc) Decode(ty cty.Type) (*ResourceInstanceObjec
Dependencies: os.Dependencies,
Private: os.Private,
CreateBeforeDestroy: os.CreateBeforeDestroy,
AttrWriteOnlyPaths: os.AttrWriteOnlyPaths,
}, nil
}

Expand Down
10 changes: 6 additions & 4 deletions internal/states/remote/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func TestStatePersist(t *testing.T) {
"attributes_flat": map[string]interface{}{
"filename": "file.txt",
},
"schema_version": 0.0,
"sensitive_attributes": []interface{}{},
"schema_version": 0.0,
"sensitive_attributes": []interface{}{},
"write_only_attributes": []interface{}{},
},
},
"mode": "managed",
Expand Down Expand Up @@ -167,8 +168,9 @@ func TestStatePersist(t *testing.T) {
"attributes_flat": map[string]interface{}{
"filename": "file.txt",
},
"schema_version": 0.0,
"sensitive_attributes": []interface{}{},
"schema_version": 0.0,
"sensitive_attributes": []interface{}{},
"write_only_attributes": []interface{}{},
},
},
"mode": "managed",
Expand Down
3 changes: 3 additions & 0 deletions internal/states/statefile/version4.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ func appendInstanceObjectStateV4(rs *states.Resource, is *states.ResourceInstanc
// Marshal paths to JSON
attributeSensitivePaths, pathsDiags := marshalPaths(obj.AttrSensitivePaths)
diags = diags.Append(pathsDiags)
attributeWriteOnlyPaths, pathsDiags := marshalPaths(obj.AttrWriteOnlyPaths)
diags = diags.Append(pathsDiags)

return append(isV4s, instanceObjectStateV4{
IndexKey: rawKey,
Expand All @@ -501,6 +503,7 @@ func appendInstanceObjectStateV4(rs *states.Resource, is *states.ResourceInstanc
AttributesFlat: obj.AttrsFlat,
AttributesRaw: obj.AttrsJSON,
AttributeSensitivePaths: attributeSensitivePaths,
AttributeWriteOnlyPaths: attributeWriteOnlyPaths,
PrivateRaw: privateRaw,
Dependencies: deps,
CreateBeforeDestroy: obj.CreateBeforeDestroy,
Expand Down
Loading

0 comments on commit 9f0ce65

Please sign in to comment.