From f0fbbb91e5779f648ef92319bd6226013a58ea0c Mon Sep 17 00:00:00 2001 From: Scott Howe Date: Fri, 20 Sep 2024 13:44:34 -0500 Subject: [PATCH] refactor: rename host-related datastore items --- pkg/common/datastore/datastore.go | 72 +++++++++++++------------------ pkg/manager/host.go | 7 +-- pkg/manager/manager.go | 6 +-- 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/pkg/common/datastore/datastore.go b/pkg/common/datastore/datastore.go index e4e1df0..7118cdc 100644 --- a/pkg/common/datastore/datastore.go +++ b/pkg/common/datastore/datastore.go @@ -18,13 +18,13 @@ const ( type DataStore struct { SavedAppliances map[string]*ApplianceDataStore `json:"saved-appliances"` - SavedHosts map[string]*HostDataStore `json:"saved-hosts"` + HostData map[string]*HostDatum `json:"host-data"` } func NewDataStore() *DataStore { return &DataStore{ SavedAppliances: make(map[string]*ApplianceDataStore), - SavedHosts: make(map[string]*HostDataStore), + HostData: make(map[string]*HostDatum), } } @@ -45,9 +45,9 @@ func (c *DataStore) AddBlade(creds *openapi.Credentials, applianceId string) err return nil } -// AddHost: Add a new host to the data store -func (c *DataStore) AddHost(creds *openapi.Credentials) { - c.SavedHosts[creds.CustomId] = NewHostDataStore(creds) +// AddHostDatum: Add a new host datum to the data store +func (c *DataStore) AddHostDatum(creds *openapi.Credentials) { + c.HostData[creds.CustomId] = NewHostDatum(creds) } // ContainsAppliance: Checks to see if the supplied IDs represent a valid datastore Appliance @@ -69,13 +69,6 @@ func (c *DataStore) ContainsBlade(bladeId, applianceId string) bool { return exists } -// ContainsHost: Checks to see if the supplied IDs represent a valid datastore Host -func (c *DataStore) ContainsHost(hostId string) bool { - _, exists := c.SavedHosts[hostId] - - return exists -} - // DeleteAppliance: Delete an appliance from the data store func (c *DataStore) DeleteAppliance(applianceId string) { delete(c.SavedAppliances, applianceId) @@ -93,9 +86,19 @@ func (c *DataStore) DeleteBlade(bladeId, applianceId string) error { return nil } -// DeleteHost: Delete a host from the data store -func (c *DataStore) DeleteHost(hostId string) { - delete(c.SavedHosts, hostId) +// DeleteHostDatumById: Delete a host datum from the data store +func (c *DataStore) DeleteHostDatumById(hostId string) { + delete(c.HostData, hostId) +} + +// GetHostDatumById: Retrieve a host datum from the data store +func (c *DataStore) GetHostDatumById(hostId string) (*HostDatum, error) { + hostDatum, exists := c.HostData[hostId] + if !exists { + return nil, fmt.Errorf("host datum [%s] not found in data store", hostId) + } + + return hostDatum, nil } // Init: initialize the data store using command line args, ENV, or a file @@ -149,27 +152,6 @@ func (c *DataStore) UpdateBlade(ctx context.Context, r *BladeUpdateRequest) erro return appliance.UpdateBlade(ctx, r) } -type UpdateHostStatusRequest struct { - HostId string - Status common.ConnectionStatus -} - -// UpdateHost: Update a host's status -func (c *DataStore) UpdateHostStatus(ctx context.Context, r *UpdateHostStatusRequest) error { - logger := klog.FromContext(ctx) - - host, exists := c.SavedHosts[r.HostId] - if !exists { - err := fmt.Errorf("host [%s] not found in data store during host status update", r.HostId) - logger.Error(err, "failure: update host status") - return err - } - - host.ConnectionStatus = r.Status - - return nil -} - type ApplianceDataStore struct { Credentials *openapi.Credentials `json:"credentials"` SavedBlades map[string]*BladeDataStore `json:"saved-blades"` @@ -221,18 +203,22 @@ func NewBladeDataStore(creds *openapi.Credentials) *BladeDataStore { } } -type HostDataStore struct { +type HostDatum struct { Credentials *openapi.Credentials `json:"credentials"` ConnectionStatus common.ConnectionStatus `json:"connection-status"` } -func NewHostDataStore(creds *openapi.Credentials) *HostDataStore { - return &HostDataStore{ +func NewHostDatum(creds *openapi.Credentials) *HostDatum { + return &HostDatum{ Credentials: creds, ConnectionStatus: common.ONLINE, } } +func (h *HostDatum) SetConnectionStatus(status *common.ConnectionStatus) { + h.ConnectionStatus = *status +} + //////////////////////////////////////// ///////////// Helpers ////////////////// //////////////////////////////////////// @@ -271,11 +257,11 @@ func ReloadDataStore(ctx context.Context, s openapi.DefaultAPIServicer, c *DataS delete(c.SavedAppliances, applianceId) } - logger.V(2).Info("cfm-service: reloading saved hosts") - for hostId, host := range c.SavedHosts { - _, err = s.HostsPost(ctx, *host.Credentials) + logger.V(2).Info("cfm-service: restoring saved hosts") + for hostId, hostDatum := range c.HostData { + _, err = s.HostsPost(ctx, *hostDatum.Credentials) if err != nil { - logger.V(2).Info("cfm-service: host reload failure", "hostId", hostId) + logger.V(2).Info("cfm-service: host datum restore failure", "hostId", hostId) continue } } diff --git a/pkg/manager/host.go b/pkg/manager/host.go index 6885825..13c2ea0 100644 --- a/pkg/manager/host.go +++ b/pkg/manager/host.go @@ -440,11 +440,8 @@ func (h *Host) UpdateConnectionStatusBackend(ctx context.Context) { } // Update datastore status - r := datastore.UpdateHostStatusRequest{ - HostId: h.Id, - Status: common.ConnectionStatus(h.Status), - } - datastore.DStore().GetDataStore().UpdateHostStatus(ctx, &r) + hostDatum, _ := datastore.DStore().GetDataStore().GetHostDatumById(h.Id) + hostDatum.SetConnectionStatus(&h.Status) datastore.DStore().Store() logger.V(2).Info("update host status(backend)", "status", h.Status, "hostId", h.Id) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 51f9810..50d0f20 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -266,7 +266,7 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) { deviceCache.AddHost(host) // Add host to datastore - datastore.DStore().GetDataStore().AddHost(c) + datastore.DStore().GetDataStore().AddHostDatum(c) datastore.DStore().Store() logger.V(2).Info("success: add host", "hostId", host.Id) @@ -301,7 +301,7 @@ func DeleteHostById(ctx context.Context, hostId string) (*Host, error) { // Delete host from manager cache and datastore as well logger.V(2).Info("force host deletion after backend delete session failure", "hostId", host.Id) deviceCache.DeleteHostById(host.Id) - datastore.DStore().GetDataStore().DeleteHost(host.Id) + datastore.DStore().GetDataStore().DeleteHostDatumById(host.Id) datastore.DStore().Store() return nil, &common.RequestError{StatusCode: common.StatusHostDeleteSessionFailure, Err: newErr} @@ -311,7 +311,7 @@ func DeleteHostById(ctx context.Context, hostId string) (*Host, error) { h := deviceCache.DeleteHostById(host.Id) // delete host from datastore - datastore.DStore().GetDataStore().DeleteHost(host.Id) + datastore.DStore().GetDataStore().DeleteHostDatumById(host.Id) datastore.DStore().Store() logger.V(2).Info("success: delete host by id", "hostId", h.Id)