cli/context/store: createOrUpdate(): accept name instead of ID

This allows callers to just pass the name, and handle the conversion to ID and
path internally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-28 17:38:12 +02:00
parent c3eb116f9c
commit 3b7f13a5e5
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 7 additions and 8 deletions

View File

@ -149,7 +149,6 @@ 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(name); err != nil {
return patchErrContextName(err, name)
}
@ -158,7 +157,7 @@ func (s *store) ResetTLSMaterial(name string, data *ContextTLSData) error {
}
for ep, files := range data.Endpoints {
for fileName, data := range files.Files {
if err := s.tls.createOrUpdate(id, ep, fileName, data); err != nil {
if err := s.tls.createOrUpdate(name, ep, fileName, data); err != nil {
return patchErrContextName(err, name)
}
}
@ -167,7 +166,6 @@ func (s *store) ResetTLSMaterial(name string, data *ContextTLSData) error {
}
func (s *store) ResetEndpointTLSMaterial(contextName string, endpointName string, data *EndpointTLSData) error {
id := contextdirOf(contextName)
if err := s.tls.removeAllEndpointData(contextName, endpointName); err != nil {
return patchErrContextName(err, contextName)
}
@ -175,7 +173,7 @@ func (s *store) ResetEndpointTLSMaterial(contextName string, endpointName string
return nil
}
for fileName, data := range data.Files {
if err := s.tls.createOrUpdate(id, endpointName, fileName, data); err != nil {
if err := s.tls.createOrUpdate(contextName, endpointName, fileName, data); err != nil {
return patchErrContextName(err, contextName)
}
}

View File

@ -23,7 +23,8 @@ func (s *tlsStore) filePath(contextID contextdir, endpointName, filename string)
return filepath.Join(s.root, string(contextID), endpointName, filename)
}
func (s *tlsStore) createOrUpdate(contextID contextdir, endpointName, filename string, data []byte) error {
func (s *tlsStore) createOrUpdate(name, endpointName, filename string, data []byte) error {
contextID := contextdirOf(name)
epdir := s.endpointDir(contextID, endpointName)
parentOfRoot := filepath.Dir(s.root)
if err := os.MkdirAll(parentOfRoot, 0755); err != nil {

View File

@ -15,12 +15,12 @@ func TestTlsCreateUpdateGetRemove(t *testing.T) {
_, err := testee.getData(contextID, "test-ep", "test-data")
assert.Equal(t, true, IsErrTLSDataDoesNotExist(err))
err = testee.createOrUpdate(contextID, "test-ep", "test-data", []byte("data"))
err = testee.createOrUpdate(contextName, "test-ep", "test-data", []byte("data"))
assert.NilError(t, err)
data, err := testee.getData(contextID, "test-ep", "test-data")
assert.NilError(t, err)
assert.Equal(t, string(data), "data")
err = testee.createOrUpdate(contextID, "test-ep", "test-data", []byte("data2"))
err = testee.createOrUpdate(contextName, "test-ep", "test-data", []byte("data2"))
assert.NilError(t, err)
data, err = testee.getData(contextID, "test-ep", "test-data")
assert.NilError(t, err)
@ -53,7 +53,7 @@ func TestTlsListAndBatchRemove(t *testing.T) {
contextID := contextdirOf(contextName)
for name, files := range all {
for _, file := range files {
err := testee.createOrUpdate(contextID, name, file, []byte("data"))
err := testee.createOrUpdate(contextName, name, file, []byte("data"))
assert.NilError(t, err)
}
}