From d0398c423f881579dfa7971370960599a52ce22c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 28 Sep 2022 17:21:01 +0200 Subject: [PATCH] cli/context/store: removeAllContextData(): accept name instead of ID This allows callers to just pass the name, and handle the conversion to ID and path internally. This also fixes a test which incorrectly used "names" as pseudo-IDs. Signed-off-by: Sebastiaan van Stijn --- cli/context/store/store.go | 5 ++--- cli/context/store/tlsstore.go | 4 ++-- cli/context/store/tlsstore_test.go | 14 ++++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cli/context/store/store.go b/cli/context/store/store.go index d4657b99bd..03fac95515 100644 --- a/cli/context/store/store.go +++ b/cli/context/store/store.go @@ -136,11 +136,10 @@ func (s *store) CreateOrUpdate(meta Metadata) error { } func (s *store) Remove(name string) error { - id := contextdirOf(name) if err := s.meta.remove(name); err != nil { return patchErrContextName(err, name) } - return patchErrContextName(s.tls.removeAllContextData(id), name) + return patchErrContextName(s.tls.removeAllContextData(name), name) } func (s *store) GetMetadata(name string) (Metadata, error) { @@ -151,7 +150,7 @@ func (s *store) GetMetadata(name string) (Metadata, error) { func (s *store) ResetTLSMaterial(name string, data *ContextTLSData) error { id := contextdirOf(name) - if err := s.tls.removeAllContextData(id); err != nil { + if err := s.tls.removeAllContextData(name); err != nil { return patchErrContextName(err, name) } if data == nil { diff --git a/cli/context/store/tlsstore.go b/cli/context/store/tlsstore.go index 8267e87964..9b1eda5891 100644 --- a/cli/context/store/tlsstore.go +++ b/cli/context/store/tlsstore.go @@ -55,8 +55,8 @@ func (s *tlsStore) removeAllEndpointData(contextID contextdir, endpointName stri return os.RemoveAll(s.endpointDir(contextID, endpointName)) } -func (s *tlsStore) removeAllContextData(contextID contextdir) error { - return os.RemoveAll(s.contextDir(contextID)) +func (s *tlsStore) removeAllContextData(name string) error { + return os.RemoveAll(s.contextDir(contextdirOf(name))) } func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFiles, error) { diff --git a/cli/context/store/tlsstore_test.go b/cli/context/store/tlsstore_test.go index 5af9acda3b..80b5d0d5f2 100644 --- a/cli/context/store/tlsstore_test.go +++ b/cli/context/store/tlsstore_test.go @@ -45,26 +45,28 @@ func TestTlsListAndBatchRemove(t *testing.T) { "ep2": {"f1", "f2", "f3"}, } + const contextName = "test-ctx" + contextID := contextdirOf(contextName) for name, files := range all { for _, file := range files { - err := testee.createOrUpdate("test-ctx", name, file, []byte("data")) + err := testee.createOrUpdate(contextID, name, file, []byte("data")) assert.NilError(t, err) } } - resAll, err := testee.listContextData("test-ctx") + resAll, err := testee.listContextData(contextID) assert.NilError(t, err) assert.DeepEqual(t, resAll, all) - err = testee.removeAllEndpointData("test-ctx", "ep3") + err = testee.removeAllEndpointData(contextID, "ep3") assert.NilError(t, err) - resEp1ep2, err := testee.listContextData("test-ctx") + resEp1ep2, err := testee.listContextData(contextID) assert.NilError(t, err) assert.DeepEqual(t, resEp1ep2, ep1ep2) - err = testee.removeAllContextData("test-ctx") + err = testee.removeAllContextData(contextName) assert.NilError(t, err) - resEmpty, err := testee.listContextData("test-ctx") + resEmpty, err := testee.listContextData(contextID) assert.NilError(t, err) assert.DeepEqual(t, resEmpty, map[string]EndpointFiles{}) }