Skip to content

Commit

Permalink
feat: add connewction status variable, and update functions, to datas…
Browse files Browse the repository at this point in the history
…tore objects

Also add new "contains" functions to allow for checking of existing datastore ids
  • Loading branch information
scott-howe-1 committed Sep 5, 2024
1 parent 87ac731 commit 5cc40bb
Showing 1 changed file with 112 additions and 11 deletions.
123 changes: 112 additions & 11 deletions pkg/common/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ func (c *DataStore) AddHost(creds *openapi.Credentials) {
c.SavedHosts[creds.CustomId] = NewHostDataStore(creds)
}

// ContainsAppliance: Checks to see if the supplied IDs represent a valid datastore Appliance
func (c *DataStore) ContainsAppliance(applianceId string) bool {
_, exists := c.SavedAppliances[applianceId]

return exists
}

// ContainsBlade: Checks to see if the supplied IDs represent a valid datastore Blade
func (c *DataStore) ContainsBlade(bladeId, applianceId string) bool {
appliance, exists := c.SavedAppliances[applianceId]
if !exists {
return false
}

_, exists = appliance.SavedBlades[bladeId]

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 @@ -66,7 +92,7 @@ func (c *DataStore) DeleteBlade(bladeId, applianceId string) error {
return nil
}

// DeleteHost: Delete an host from the data store
// DeleteHost: Delete a host from the data store
func (c *DataStore) DeleteHost(hostId string) {
delete(c.SavedHosts, hostId)
}
Expand All @@ -79,9 +105,62 @@ func (c *DataStore) InitDataStore(ctx context.Context, args []string) error {
return nil
}

type ApplianceUpdateRequest struct {
ApplianceId string
Status ConnectionStatus
}

// UpdateAppliance: Update an appliance's data
func (c *DataStore) UpdateAppliance(r *ApplianceUpdateRequest) error {
appliance, exists := c.SavedAppliances[r.ApplianceId]
if !exists {
return fmt.Errorf("appliance [%s] not found in data store appliance update", r.ApplianceId)
}

appliance.ConnectionStatus = r.Status

return nil
}

type BladeUpdateRequest struct {
ApplianceId string
BladeId string
Status ConnectionStatus
}

// UpdateBlade: Update an appliance's data
func (c *DataStore) UpdateBlade(r *BladeUpdateRequest) error {
appliance, exists := c.SavedAppliances[r.ApplianceId]
if !exists {
return fmt.Errorf("appliance [%s] not found in data store blade update", r.ApplianceId)
}

appliance.UpdateBlade(r)

return nil
}

type HostUpdateRequest struct {
HostId string
Status ConnectionStatus
}

// UpdateHost: Update a host's data
func (c *DataStore) UpdateHost(r *HostUpdateRequest) error {
host, exists := c.SavedHosts[r.HostId]
if !exists {
return fmt.Errorf("host [%s] not found in data store host update", r.HostId)
}

host.ConnectionStatus = r.Status

return nil
}

type ApplianceDataStore struct {
Credentials *openapi.Credentials `json:"credentials"`
SavedBlades map[string]*BladeDataStore `json:"saved-blades"`
Credentials *openapi.Credentials `json:"credentials"`
SavedBlades map[string]*BladeDataStore `json:"saved-blades"`
ConnectionStatus ConnectionStatus
}

func NewApplianceDataStore(creds *openapi.Credentials) *ApplianceDataStore {
Expand All @@ -91,31 +170,53 @@ func NewApplianceDataStore(creds *openapi.Credentials) *ApplianceDataStore {
}
}

func (c *ApplianceDataStore) AddBlade(creds *openapi.Credentials) {
c.SavedBlades[creds.CustomId] = NewBladeDataStore(creds)
func (a *ApplianceDataStore) AddBlade(creds *openapi.Credentials) {
a.SavedBlades[creds.CustomId] = NewBladeDataStore(creds)
}

func (c *ApplianceDataStore) DeleteBlade(bladeId string) {
delete(c.SavedBlades, bladeId)
func (a *ApplianceDataStore) DeleteBlade(bladeId string) {
delete(a.SavedBlades, bladeId)
}

func (a *ApplianceDataStore) UpdateBlade(r *BladeUpdateRequest) error {
blade, exists := a.SavedBlades[r.BladeId]
if !exists {
return fmt.Errorf("blade [%s] not found in data store blade update", r.BladeId)
}

blade.ConnectionStatus = r.Status

return nil
}

type ConnectionStatus string

const (
Active ConnectionStatus = "active"
Inactive ConnectionStatus = "inactive"
)

type BladeDataStore struct {
Credentials *openapi.Credentials `json:"credentials"`
Credentials *openapi.Credentials `json:"credentials"`
ConnectionStatus ConnectionStatus
}

func NewBladeDataStore(creds *openapi.Credentials) *BladeDataStore {
return &BladeDataStore{
Credentials: creds,
Credentials: creds,
ConnectionStatus: Active,
}
}

type HostDataStore struct {
Credentials *openapi.Credentials `json:"credentials"`
Credentials *openapi.Credentials `json:"credentials"`
ConnectionStatus ConnectionStatus
}

func NewHostDataStore(creds *openapi.Credentials) *HostDataStore {
return &HostDataStore{
Credentials: creds,
Credentials: creds,
ConnectionStatus: Active,
}
}

Expand Down

0 comments on commit 5cc40bb

Please sign in to comment.