@@ -20,6 +20,7 @@ import (
2020
2121 "github.com/hashicorp/terraform/internal/states/remote"
2222 "github.com/hashicorp/terraform/internal/states/statemgr"
23+ "github.com/hashicorp/terraform/internal/tfdiags"
2324)
2425
2526const (
@@ -71,18 +72,20 @@ type RemoteClient struct {
7172 sessionCancel context.CancelFunc
7273}
7374
74- func (c * RemoteClient ) Get () (* remote.Payload , error ) {
75+ func (c * RemoteClient ) Get () (* remote.Payload , tfdiags.Diagnostics ) {
76+ var diags tfdiags.Diagnostics
77+
7578 c .mu .Lock ()
7679 defer c .mu .Unlock ()
7780
7881 kv := c .Client .KV ()
7982
8083 chunked , hash , chunks , pair , err := c .chunkedMode ()
8184 if err != nil {
82- return nil , err
85+ return nil , diags . Append ( err )
8386 }
8487 if pair == nil {
85- return nil , nil
88+ return nil , diags
8689 }
8790
8891 c .modifyIndex = pair .ModifyIndex
@@ -92,10 +95,10 @@ func (c *RemoteClient) Get() (*remote.Payload, error) {
9295 for _ , c := range chunks {
9396 pair , _ , err := kv .Get (c , nil )
9497 if err != nil {
95- return nil , err
98+ return nil , diags . Append ( err )
9699 }
97100 if pair == nil {
98- return nil , fmt .Errorf ("Key %q could not be found" , c )
101+ return nil , diags . Append ( fmt .Errorf ("Key %q could not be found" , c ) )
99102 }
100103 payload = append (payload , pair .Value [:]... )
101104 }
@@ -107,23 +110,23 @@ func (c *RemoteClient) Get() (*remote.Payload, error) {
107110 if len (payload ) >= 1 && payload [0 ] == '\x1f' {
108111 payload , err = uncompressState (payload )
109112 if err != nil {
110- return nil , err
113+ return nil , diags . Append ( err )
111114 }
112115 }
113116
114117 md5 := md5 .Sum (payload )
115118
116119 if hash != "" && fmt .Sprintf ("%x" , md5 ) != hash {
117- return nil , fmt .Errorf ("The remote state does not match the expected hash" )
120+ return nil , diags . Append ( fmt .Errorf ("The remote state does not match the expected hash" ) )
118121 }
119122
120123 return & remote.Payload {
121124 Data : payload ,
122125 MD5 : md5 [:],
123- }, nil
126+ }, diags
124127}
125128
126- func (c * RemoteClient ) Put (data []byte ) error {
129+ func (c * RemoteClient ) Put (data []byte ) tfdiags. Diagnostics {
127130 // The state can be stored in 4 different ways, based on the payload size
128131 // and whether the user enabled gzip:
129132 // - single entry mode with plain JSON: a single JSON is stored at
@@ -161,6 +164,8 @@ func (c *RemoteClient) Put(data []byte) error {
161164 // in chunked mode and we will need to remove the old chunks (whether or
162165 // not we were using gzip does not matter in that case).
163166
167+ var diags tfdiags.Diagnostics
168+
164169 c .mu .Lock ()
165170 defer c .mu .Unlock ()
166171
@@ -169,7 +174,7 @@ func (c *RemoteClient) Put(data []byte) error {
169174 // First we determine what mode we were using and to prepare the cleanup
170175 chunked , hash , _ , _ , err := c .chunkedMode ()
171176 if err != nil {
172- return err
177+ return diags . Append ( err )
173178 }
174179 cleanupOldChunks := func () {}
175180 if chunked {
@@ -188,7 +193,7 @@ func (c *RemoteClient) Put(data []byte) error {
188193 if compressedState , err := compressState (data ); err == nil {
189194 payload = compressedState
190195 } else {
191- return err
196+ return diags . Append ( err )
192197 }
193198 }
194199
@@ -256,10 +261,10 @@ func (c *RemoteClient) Put(data []byte) error {
256261
257262 if err = store (payload ); err == nil {
258263 // The payload was small enough to be stored
259- return nil
264+ return diags
260265 } else if ! strings .Contains (err .Error (), "too large" ) {
261266 // We failed for some other reason, report this to the user
262- return err
267+ return diags . Append ( err )
263268 }
264269
265270 // The payload was too large so we split it in multiple chunks
@@ -278,7 +283,7 @@ func (c *RemoteClient) Put(data []byte) error {
278283 }, nil )
279284
280285 if err != nil {
281- return err
286+ return diags . Append ( err )
282287 }
283288 }
284289
@@ -288,20 +293,21 @@ func (c *RemoteClient) Put(data []byte) error {
288293 "chunks" : chunkPaths ,
289294 })
290295 if err != nil {
291- return err
296+ return diags . Append ( err )
292297 }
293- return store (payload )
298+ return diags . Append ( store (payload ) )
294299}
295300
296- func (c * RemoteClient ) Delete () error {
301+ func (c * RemoteClient ) Delete () tfdiags.Diagnostics {
302+ var diags tfdiags.Diagnostics
297303 c .mu .Lock ()
298304 defer c .mu .Unlock ()
299305
300306 kv := c .Client .KV ()
301307
302308 chunked , hash , _ , _ , err := c .chunkedMode ()
303309 if err != nil {
304- return err
310+ return diags . Append ( err )
305311 }
306312
307313 _ , err = kv .Delete (c .Path , nil )
@@ -312,7 +318,7 @@ func (c *RemoteClient) Delete() error {
312318 kv .DeleteTree (path , nil )
313319 }
314320
315- return err
321+ return diags . Append ( err )
316322}
317323
318324func (c * RemoteClient ) lockPath () string {
0 commit comments