diff --git a/pkg/common/common.go b/pkg/common/common.go index 8863ba0..1f470f8 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -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{ diff --git a/pkg/common/datastore/datastore.go b/pkg/common/datastore/datastore.go index 90a419a..f211d81 100644 --- a/pkg/common/datastore/datastore.go +++ b/pkg/common/datastore/datastore.go @@ -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 { @@ -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 @@ -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 { diff --git a/pkg/manager/appliance.go b/pkg/manager/appliance.go index d52a9bb..688aad2 100644 --- a/pkg/manager/appliance.go +++ b/pkg/manager/appliance.go @@ -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} } @@ -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} } diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 67569dc..aaaa07b 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -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} } @@ -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} diff --git a/services/discovery.go b/services/discovery.go index 12f679d..df0955d 100644 --- a/services/discovery.go +++ b/services/discovery.go @@ -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 @@ -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