github.com/blend/go-sdk@v1.20220411.3/vault/kv1.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package vault 9 10 import ( 11 "context" 12 "encoding/json" 13 "path/filepath" 14 ) 15 16 // assert KV1 implements kv. 17 var ( 18 _ KV = &KV1{} 19 ) 20 21 // KV1 defines key value version 1 interactions 22 type KV1 struct { 23 Client *APIClient 24 } 25 26 // Put puts a value. 27 func (kv1 KV1) Put(ctx context.Context, path string, data Values, options ...CallOption) error { 28 contents, err := kv1.Client.jsonBody(data) 29 if err != nil { 30 return err 31 } 32 req := kv1.Client.createRequest(MethodPut, filepath.Join("/v1/", path), options...).WithContext(ctx) 33 req.Body = contents 34 res, err := kv1.Client.send(req, OptTraceVaultOperation("kv1.put"), OptTraceKeyName(path)) 35 if err != nil { 36 return err 37 } 38 defer res.Close() 39 return nil 40 } 41 42 // Get gets a value at a given key. 43 func (kv1 KV1) Get(ctx context.Context, path string, options ...CallOption) (Values, error) { 44 req := kv1.Client.createRequest(MethodGet, filepath.Join("/v1/", path), options...).WithContext(ctx) 45 res, err := kv1.Client.send(req, OptTraceVaultOperation("kv1.get"), OptTraceKeyName(path)) 46 if err != nil { 47 return nil, err 48 } 49 defer res.Close() 50 51 var response SecretV1 52 if err := json.NewDecoder(res).Decode(&response); err != nil { 53 return nil, err 54 } 55 return response.Data, nil 56 } 57 58 // Delete puts a key. 59 func (kv1 KV1) Delete(ctx context.Context, path string, options ...CallOption) error { 60 req := kv1.Client.createRequest(MethodDelete, filepath.Join("/v1/", path), options...).WithContext(ctx) 61 res, err := kv1.Client.send(req, OptTraceVaultOperation("kv1.delete"), OptTraceKeyName(path)) 62 if err != nil { 63 return err 64 } 65 defer res.Close() 66 return nil 67 } 68 69 // List returns a slice of key and subfolder names at this path. 70 func (kv1 KV1) List(ctx context.Context, path string, options ...CallOption) ([]string, error) { 71 req := kv1.Client.createRequest(MethodList, filepath.Join("/v1/", path), options...).WithContext(ctx) 72 res, err := kv1.Client.send(req, OptTraceVaultOperation("kv1.list")) 73 if err != nil { 74 return nil, err 75 } 76 defer res.Close() 77 78 var response SecretListV1 79 if err := json.NewDecoder(res).Decode(&response); err != nil { 80 return nil, err 81 } 82 return response.Data.Keys, nil 83 }