@@ -20,6 +20,7 @@ import (
20
20
21
21
"github.com/hashicorp/terraform/internal/states/remote"
22
22
"github.com/hashicorp/terraform/internal/states/statemgr"
23
+ "github.com/hashicorp/terraform/internal/tfdiags"
23
24
)
24
25
25
26
const (
@@ -71,18 +72,20 @@ type RemoteClient struct {
71
72
sessionCancel context.CancelFunc
72
73
}
73
74
74
- func (c * RemoteClient ) Get () (* remote.Payload , error ) {
75
+ func (c * RemoteClient ) Get () (* remote.Payload , tfdiags.Diagnostics ) {
76
+ var diags tfdiags.Diagnostics
77
+
75
78
c .mu .Lock ()
76
79
defer c .mu .Unlock ()
77
80
78
81
kv := c .Client .KV ()
79
82
80
83
chunked , hash , chunks , pair , err := c .chunkedMode ()
81
84
if err != nil {
82
- return nil , err
85
+ return nil , diags . Append ( err )
83
86
}
84
87
if pair == nil {
85
- return nil , nil
88
+ return nil , diags
86
89
}
87
90
88
91
c .modifyIndex = pair .ModifyIndex
@@ -92,10 +95,10 @@ func (c *RemoteClient) Get() (*remote.Payload, error) {
92
95
for _ , c := range chunks {
93
96
pair , _ , err := kv .Get (c , nil )
94
97
if err != nil {
95
- return nil , err
98
+ return nil , diags . Append ( err )
96
99
}
97
100
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 ) )
99
102
}
100
103
payload = append (payload , pair .Value [:]... )
101
104
}
@@ -107,23 +110,23 @@ func (c *RemoteClient) Get() (*remote.Payload, error) {
107
110
if len (payload ) >= 1 && payload [0 ] == '\x1f' {
108
111
payload , err = uncompressState (payload )
109
112
if err != nil {
110
- return nil , err
113
+ return nil , diags . Append ( err )
111
114
}
112
115
}
113
116
114
117
md5 := md5 .Sum (payload )
115
118
116
119
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" ) )
118
121
}
119
122
120
123
return & remote.Payload {
121
124
Data : payload ,
122
125
MD5 : md5 [:],
123
- }, nil
126
+ }, diags
124
127
}
125
128
126
- func (c * RemoteClient ) Put (data []byte ) error {
129
+ func (c * RemoteClient ) Put (data []byte ) tfdiags. Diagnostics {
127
130
// The state can be stored in 4 different ways, based on the payload size
128
131
// and whether the user enabled gzip:
129
132
// - single entry mode with plain JSON: a single JSON is stored at
@@ -161,6 +164,8 @@ func (c *RemoteClient) Put(data []byte) error {
161
164
// in chunked mode and we will need to remove the old chunks (whether or
162
165
// not we were using gzip does not matter in that case).
163
166
167
+ var diags tfdiags.Diagnostics
168
+
164
169
c .mu .Lock ()
165
170
defer c .mu .Unlock ()
166
171
@@ -169,7 +174,7 @@ func (c *RemoteClient) Put(data []byte) error {
169
174
// First we determine what mode we were using and to prepare the cleanup
170
175
chunked , hash , _ , _ , err := c .chunkedMode ()
171
176
if err != nil {
172
- return err
177
+ return diags . Append ( err )
173
178
}
174
179
cleanupOldChunks := func () {}
175
180
if chunked {
@@ -188,7 +193,7 @@ func (c *RemoteClient) Put(data []byte) error {
188
193
if compressedState , err := compressState (data ); err == nil {
189
194
payload = compressedState
190
195
} else {
191
- return err
196
+ return diags . Append ( err )
192
197
}
193
198
}
194
199
@@ -256,10 +261,10 @@ func (c *RemoteClient) Put(data []byte) error {
256
261
257
262
if err = store (payload ); err == nil {
258
263
// The payload was small enough to be stored
259
- return nil
264
+ return diags
260
265
} else if ! strings .Contains (err .Error (), "too large" ) {
261
266
// We failed for some other reason, report this to the user
262
- return err
267
+ return diags . Append ( err )
263
268
}
264
269
265
270
// The payload was too large so we split it in multiple chunks
@@ -278,7 +283,7 @@ func (c *RemoteClient) Put(data []byte) error {
278
283
}, nil )
279
284
280
285
if err != nil {
281
- return err
286
+ return diags . Append ( err )
282
287
}
283
288
}
284
289
@@ -288,20 +293,21 @@ func (c *RemoteClient) Put(data []byte) error {
288
293
"chunks" : chunkPaths ,
289
294
})
290
295
if err != nil {
291
- return err
296
+ return diags . Append ( err )
292
297
}
293
- return store (payload )
298
+ return diags . Append ( store (payload ) )
294
299
}
295
300
296
- func (c * RemoteClient ) Delete () error {
301
+ func (c * RemoteClient ) Delete () tfdiags.Diagnostics {
302
+ var diags tfdiags.Diagnostics
297
303
c .mu .Lock ()
298
304
defer c .mu .Unlock ()
299
305
300
306
kv := c .Client .KV ()
301
307
302
308
chunked , hash , _ , _ , err := c .chunkedMode ()
303
309
if err != nil {
304
- return err
310
+ return diags . Append ( err )
305
311
}
306
312
307
313
_ , err = kv .Delete (c .Path , nil )
@@ -312,7 +318,7 @@ func (c *RemoteClient) Delete() error {
312
318
kv .DeleteTree (path , nil )
313
319
}
314
320
315
- return err
321
+ return diags . Append ( err )
316
322
}
317
323
318
324
func (c * RemoteClient ) lockPath () string {
0 commit comments