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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-28 17:21:01 +02:00
parent f843c42c05
commit d0398c423f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 12 additions and 11 deletions

View File

@ -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 {

View File

@ -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) {

View File

@ -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{})
}