MCPcopy Create free account
hub / github.com/hashicorp/vault / readThenWrite

Method readThenWrite

command/kv_patch.go:282–364  ·  view source on GitHub ↗
(client *api.Client, path string, newData map[string]interface{})

Source from the content-addressed store, hash-verified

280}
281
282func (c *KVPatchCommand) readThenWrite(client *api.Client, path string, newData map[string]interface{}) (*api.Secret, int) {
283 // First, do a read.
284 // Note that we don't want to see curl output for the read request.
285 curOutputCurl := client.OutputCurlString()
286 client.SetOutputCurlString(false)
287 outputPolicy := client.OutputPolicy()
288 client.SetOutputPolicy(false)
289 secret, err := kvReadRequest(client, path, nil)
290 if err != nil {
291 c.UI.Error(fmt.Sprintf("Error doing pre-read at %s: %s", path, err))
292 return nil, 2
293 }
294 client.SetOutputCurlString(curOutputCurl)
295 client.SetOutputPolicy(outputPolicy)
296
297 // Make sure a value already exists
298 if secret == nil || secret.Data == nil {
299 c.UI.Error(fmt.Sprintf("No value found at %s", path))
300 return nil, 2
301 }
302
303 // Verify metadata found
304 rawMeta, ok := secret.Data["metadata"]
305 if !ok || rawMeta == nil {
306 c.UI.Error(fmt.Sprintf("No metadata found at %s; patch only works on existing data", path))
307 return nil, 2
308 }
309 meta, ok := rawMeta.(map[string]interface{})
310 if !ok {
311 c.UI.Error(fmt.Sprintf("Metadata found at %s is not the expected type (JSON object)", path))
312 return nil, 2
313 }
314 if meta == nil {
315 c.UI.Error(fmt.Sprintf("No metadata found at %s; patch only works on existing data", path))
316 return nil, 2
317 }
318
319 // Verify old data found
320 rawData, ok := secret.Data["data"]
321 if !ok || rawData == nil {
322 c.UI.Error(fmt.Sprintf("No data found at %s; patch only works on existing data", path))
323 return nil, 2
324 }
325 data, ok := rawData.(map[string]interface{})
326 if !ok {
327 c.UI.Error(fmt.Sprintf("Data found at %s is not the expected type (JSON object)", path))
328 return nil, 2
329 }
330 if data == nil {
331 c.UI.Error(fmt.Sprintf("No data found at %s; patch only works on existing data", path))
332 return nil, 2
333 }
334
335 // Copy new data over
336 for k, v := range newData {
337 data[k] = v
338 }
339

Callers 2

RunMethod · 0.95
mergePatchMethod · 0.95

Calls 11

kvReadRequestFunction · 0.85
FormatFunction · 0.85
PrintRawFieldFunction · 0.85
OutputCurlStringMethod · 0.80
SetOutputCurlStringMethod · 0.80
OutputPolicyMethod · 0.80
SetOutputPolicyMethod · 0.80
LogicalMethod · 0.80
ErrorMethod · 0.65
WriteMethod · 0.45
InfoMethod · 0.45

Tested by

no test coverage detected