Skip to content

Commit

Permalink
refactor: rename host-related datastore items
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-howe-1 committed Sep 20, 2024
1 parent df366ed commit f0fbbb9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
72 changes: 29 additions & 43 deletions pkg/common/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 //////////////////
////////////////////////////////////////
Expand Down Expand Up @@ -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
}
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/manager/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand All @@ -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)
Expand Down

0 comments on commit f0fbbb9

Please sign in to comment.