From 57a9177c19e12b59546c3ad82c3b0079f3e172ee Mon Sep 17 00:00:00 2001 From: Luca Berneking Date: Wed, 5 Jun 2024 14:43:31 +0200 Subject: [PATCH] Implement List for KV v1 --- kv_v1.go | 23 +++++++++++++++++++++++ kv_v1_test.go | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/kv_v1.go b/kv_v1.go index a68568d..74bf890 100644 --- a/kv_v1.go +++ b/kv_v1.go @@ -61,6 +61,29 @@ func (k *KVv1) Read(key string) (*KVv1ReadResponse, error) { return readRes, nil } +type KVv1ListResponse struct { + Data struct { + Keys []string `json:"keys"` + } `json:"data"` +} + +func (k *KVv1) List(key string) (*KVv1ListResponse, error) { + listRes := &KVv1ListResponse{} + + err := k.client.List( + []string{ + pathPrefix, + k.MountPoint, + url.PathEscape(key), + }, nil, listRes, nil, + ) + if err != nil { + return nil, err + } + + return listRes, nil +} + func (k *KVv1) Delete(key string) error { err := k.client.Delete( []string{ diff --git a/kv_v1_test.go b/kv_v1_test.go index d64512a..9e62558 100644 --- a/kv_v1_test.go +++ b/kv_v1_test.go @@ -79,3 +79,19 @@ func (s *KVv1TestSuite) TestCreateAndDelete() { _, readErr := s.client.Read("2b7ff26d-30b7-43ba-96d5-79b4baba9b39") require.Error(s.T(), readErr) } + +func (s *KVv1TestSuite) TestCreateAndList() { + testKeyValues := make(map[string]string) + testKeyValues["PrivateKey"] = "abcde" + + require.NoError(s.T(), s.client.Create("foo", testKeyValues)) + require.NoError(s.T(), s.client.Create("foo2", testKeyValues)) + + list, listErr := s.client.List("") + require.NoError(s.T(), listErr) + + require.Contains(s.T(), list.Data.Keys, "foo") + require.Contains(s.T(), list.Data.Keys, "foo2") + require.Len(s.T(), list.Data.Keys, 2) + +}