From a097b29f24637eae34c3189e868771e9d6c38467 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Thu, 19 Dec 2024 12:00:23 -0500 Subject: [PATCH] fix: update UpdateGroupData method to support creating group data if it doesn't exist --- cmd/cloud-init-server/group_handlers.go | 2 +- internal/memstore/ciMemStore.go | 6 +++++- pkg/cistore/store.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/cloud-init-server/group_handlers.go b/cmd/cloud-init-server/group_handlers.go index 7f4a401..85d97b3 100644 --- a/cmd/cloud-init-server/group_handlers.go +++ b/cmd/cloud-init-server/group_handlers.go @@ -116,7 +116,7 @@ func (h CiHandler) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) { } // update group key-value data - err = h.store.UpdateGroupData(groupName, data) + err = h.store.UpdateGroupData(groupName, data, true) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/internal/memstore/ciMemStore.go b/internal/memstore/ciMemStore.go index 6f62f2e..3742fe6 100644 --- a/internal/memstore/ciMemStore.go +++ b/internal/memstore/ciMemStore.go @@ -67,9 +67,13 @@ func (m *MemStore) GetGroupData(groupName string) (cistore.GroupData, error) { } // UpdateGroupData is similar to AddGroupData but only works if the group exists -func (m *MemStore) UpdateGroupData(groupName string, groupData cistore.GroupData) error { +func (m *MemStore) UpdateGroupData(groupName string, groupData cistore.GroupData, create bool) error { m.GroupsMutex.RLock() defer m.GroupsMutex.RUnlock() + if create { + m.Groups[groupName] = groupData + return nil + } _, ok := m.Groups[groupName] if ok { diff --git a/pkg/cistore/store.go b/pkg/cistore/store.go index 28298aa..fdbf078 100644 --- a/pkg/cistore/store.go +++ b/pkg/cistore/store.go @@ -6,7 +6,7 @@ type Store interface { GetGroups() map[string]GroupData AddGroupData(groupName string, groupData GroupData) error GetGroupData(groupName string) (GroupData, error) - UpdateGroupData(groupName string, groupData GroupData) error + UpdateGroupData(groupName string, groupData GroupData, create bool) error RemoveGroupData(groupName string) error // Extended Instance Information API GetInstanceInfo(nodeName string) (OpenCHAMIInstanceInfo, error)