Skip to content

Commit

Permalink
feat: add the failed devices after creating session and NewDevices
Browse files Browse the repository at this point in the history
  • Loading branch information
Meng-20 committed Nov 22, 2024
1 parent 433c878 commit 0c1fbb4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
8 changes: 4 additions & 4 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import "cfm/pkg/openapi"
type ConnectionStatus string

const (
ONLINE ConnectionStatus = "online"
FOUND ConnectionStatus = "found"
OFFLINE ConnectionStatus = "offline"
ONLINE ConnectionStatus = "online" // avahi-found, found root service, created backend session, added to service map
UNAVAILABLE ConnectionStatus = "unavailable" // avahi-found AND detect root service AND !created backend session AND !added to service map
FOUND ConnectionStatus = "found" // avahi-found AND detect root service
OFFLINE ConnectionStatus = "offline" // !avahi-found (after previously adding it)
NOT_APPLICABLE ConnectionStatus = "n\\a"
UNAVAILABLE ConnectionStatus = "unavailable"
)

var DefaultApplianceCredentials = &openapi.Credentials{
Expand Down
6 changes: 3 additions & 3 deletions pkg/common/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (a *ApplianceDatum) GetBladeDatumById(ctx context.Context, bladeId string)
}

// Verify if the blade exists using the ipAddress
func (c *DataStore) CheckBladeExist(IpAddress string) (*string, bool) {
func (c *DataStore) GetBladeDatumByIp(IpAddress string) (*string, bool) {
for _, appliance := range c.ApplianceData {
for bladeId, blade := range appliance.BladeData {
if blade.Credentials.IpAddress == IpAddress {
Expand All @@ -124,7 +124,7 @@ func (c *DataStore) CheckBladeExist(IpAddress string) (*string, bool) {
}

// Verify if the host exists using the ipAddress
func (c *DataStore) CheckHostExist(IpAddress string) (*string, bool) {
func (c *DataStore) GetHostDatumByIp(IpAddress string) (*string, bool) {
for hostId, host := range c.HostData {
if host.Credentials.IpAddress == IpAddress {
return &hostId, true
Expand Down Expand Up @@ -180,7 +180,7 @@ func ReloadDataStore(ctx context.Context, s openapi.DefaultAPIServicer, c *DataS
logger := klog.FromContext(ctx)

logger.V(2).Info("cfm-service: restoring saved appliances")

for applianceId, applianceDatum := range c.ApplianceData {
_, err = s.AppliancesPost(ctx, *applianceDatum.Credentials)
if err != nil {
Expand Down
36 changes: 26 additions & 10 deletions pkg/manager/appliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ func (a *Appliance) AddBlade(ctx context.Context, c *openapi.Credentials) (*Blad
if err != nil || response == nil {
newErr := fmt.Errorf("create session failure at [%s:%d] using interface [%s]: %w", c.IpAddress, c.Port, backendName, err)
logger.Error(newErr, "failure: add blade")

// Continue adding the failed blade to the datastore, but update the connection status to unavailable
newBlade := &Blade{
Id: c.CustomId,
Uri: GetCfmUriBladeId(a.Id, c.CustomId),
Status: common.UNAVAILABLE,
ApplianceId: a.Id,
}
a.Blades[newBlade.Id] = newBlade

applianceDatum, _ := datastore.DStore().GetDataStore().GetApplianceDatumById(newBlade.ApplianceId)
applianceDatum.AddBladeDatum(c)
unavailabelBlade, _ := applianceDatum.GetBladeDatumById(ctx, c.CustomId)
unavailabelBlade.ConnectionStatus = common.UNAVAILABLE
datastore.DStore().Store()

return nil, &common.RequestError{StatusCode: common.StatusBladeCreateSessionFailure, Err: newErr}
}

Expand Down Expand Up @@ -150,20 +166,20 @@ func (a *Appliance) AddBlade(ctx context.Context, c *openapi.Credentials) (*Blad
logger.Error(newErr, "failure: add blade")

// Continue adding the failed blade to the datastore, but update the connection status to unavailable
applianceDatum, _ := datastore.DStore().GetDataStore().GetApplianceDatumById(a.Id)
applianceDatum.AddBladeDatum(c)
OfflineBlade, _ := applianceDatum.GetBladeDatumById(ctx, c.CustomId)
OfflineBlade.ConnectionStatus = common.UNAVAILABLE
datastore.DStore().Store()

var newBlade = &Blade{
Id: OfflineBlade.Credentials.CustomId,
Uri: GetCfmUriBladeId(a.Id, OfflineBlade.Credentials.CustomId),
Status: OfflineBlade.ConnectionStatus,
newBlade := &Blade{
Id: c.CustomId,
Uri: GetCfmUriBladeId(a.Id, c.CustomId),
Status: common.UNAVAILABLE,
ApplianceId: a.Id,
}
a.Blades[newBlade.Id] = newBlade

applianceDatum, _ := datastore.DStore().GetDataStore().GetApplianceDatumById(newBlade.ApplianceId)
applianceDatum.AddBladeDatum(c)
unavailabelBlade, _ := applianceDatum.GetBladeDatumById(ctx, c.CustomId)
unavailabelBlade.ConnectionStatus = common.UNAVAILABLE
datastore.DStore().Store()

return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr}
}

Expand Down
27 changes: 24 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) {
if err != nil || response == nil {
newErr := fmt.Errorf("create session failure at [%s:%d] using interface [%s]: %w", c.IpAddress, c.Port, backendName, err)
logger.Error(newErr, "failure: add host")

// Continue adding the failed host to the datastore, but update the connection status to unavailable
host := &Host{
Id: c.CustomId,
Uri: GetCfmUriHostId(c.CustomId),
Status: common.UNAVAILABLE,
}
deviceCache.AddHost(host, false)

datastore.DStore().GetDataStore().AddHostDatum(c)
unavailableHost, _ := datastore.DStore().GetDataStore().GetHostDatumById(c.CustomId)
unavailableHost.ConnectionStatus = common.UNAVAILABLE
datastore.DStore().Store()

return nil, &common.RequestError{StatusCode: common.StatusHostCreateSessionFailure, Err: newErr}
}

Expand Down Expand Up @@ -332,10 +346,17 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) {
newErr := fmt.Errorf("new host object creation failure: %w", err)
logger.Error(newErr, "failure: add host")

// Continue adding the failed host to the datastore, but update the connection status to offline
// Continue adding the failed host to the datastore, but update the connection status to unavailable
host := &Host{
Id: c.CustomId,
Uri: GetCfmUriHostId(c.CustomId),
Status: common.UNAVAILABLE,
}
deviceCache.AddHost(host, false)

datastore.DStore().GetDataStore().AddHostDatum(c)
offlineHost, _ := datastore.DStore().GetDataStore().GetHostDatumById(c.CustomId)
offlineHost.ConnectionStatus = common.UNAVAILABLE
unavailableHost, _ := datastore.DStore().GetDataStore().GetHostDatumById(c.CustomId)
unavailableHost.ConnectionStatus = common.UNAVAILABLE
datastore.DStore().Store()

return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr}
Expand Down
4 changes: 2 additions & 2 deletions services/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServ
}
applianceDatum, _ := datastore.DStore().GetDataStore().GetApplianceDatumById(common.DefaultApplianceCredentials.CustomId)
for _, bladeDevice := range bladeBodyBytes {
_, exist := data.CheckBladeExist(bladeDevice.Address)
_, exist := data.GetBladeDatumByIp(bladeDevice.Address)
if !exist {
newCredentials := *common.DefaultBladeCredentials
newCredentials.IpAddress = bladeDevice.Address
Expand All @@ -59,7 +59,7 @@ func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServ
log.Fatalf("Response body is not []byte")
}
for _, hostDevice := range hostBodyBytes {
_, exist := data.CheckHostExist(hostDevice.Address)
_, exist := data.GetHostDatumByIp(hostDevice.Address)
if !exist {
newCredentials := *common.DefaultHostCredentials
newCredentials.IpAddress = hostDevice.Address
Expand Down

0 comments on commit 0c1fbb4

Please sign in to comment.