-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): add the discovered devices and change the connection s…
…tatus to unavailable for failed devices (#54) * feat: add the discovered devices and change the connection status to unavailable for failed ones * revert: revert the error handling * feat: add the failed devices after creating session and NewDevices * refactor: change all the FOUND status to UNAVAILABLE * refactor: change the locations for GetBladeDatumByIp and GetHostDatumByIp --------- Signed-off-by: Mengling Ding <[email protected]>
- Loading branch information
Showing
8 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
package common | ||
|
||
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 | ||
OFFLINE ConnectionStatus = "offline" // !avahi-found (after previously adding it) | ||
NOT_APPLICABLE ConnectionStatus = "n\\a" | ||
) | ||
|
||
var DefaultApplianceCredentials = &openapi.Credentials{ | ||
Username: "root", | ||
Password: "0penBmc", | ||
IpAddress: "127.0.0.1", | ||
Port: 8443, | ||
Insecure: true, | ||
Protocol: "https", | ||
CustomId: "CMA_Discovered_Blades", | ||
} | ||
|
||
var DefaultBladeCredentials = &openapi.Credentials{ | ||
Username: "root", | ||
Password: "0penBmc", | ||
IpAddress: "127.0.0.1", | ||
Port: 443, | ||
Insecure: true, | ||
Protocol: "https", | ||
CustomId: "Discoverd_Blade_", | ||
} | ||
|
||
var DefaultHostCredentials = &openapi.Credentials{ | ||
Username: "admin", | ||
Password: "admin12345", | ||
IpAddress: "127.0.0.1", | ||
Port: 8082, | ||
Insecure: true, | ||
Protocol: "http", | ||
CustomId: "Discoverd_Host_", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package services | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"cfm/pkg/common" | ||
"cfm/pkg/common/datastore" | ||
"cfm/pkg/openapi" | ||
) | ||
|
||
// discoverDevices function to call the DiscoverDevices API | ||
func DiscoverDevices(ctx context.Context, apiService openapi.DefaultAPIServicer, deviceType string) (openapi.ImplResponse, error) { | ||
resp, err := apiService.DiscoverDevices(ctx, deviceType) | ||
if err != nil { | ||
log.Printf("Error discovering devices of type %s: %v", deviceType, err) | ||
return resp, err | ||
} else { | ||
log.Printf("Discovered devices of type %s: %v", deviceType, resp) | ||
return resp, nil | ||
} | ||
} | ||
|
||
func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServicer, blades openapi.ImplResponse, hosts openapi.ImplResponse) { | ||
// Verify the existence of the default appliance; if it doesn't exist, add it | ||
datastore.DStore().Restore() | ||
data := datastore.DStore().GetDataStore() | ||
_, err := datastore.DStore().GetDataStore().GetApplianceDatumById(common.DefaultApplianceCredentials.CustomId) | ||
if err != nil { | ||
datastore.DStore().GetDataStore().AddApplianceDatum(common.DefaultApplianceCredentials) | ||
datastore.DStore().Store() | ||
} | ||
|
||
// Add blades | ||
// Convert data type | ||
bladeBodyBytes, ok := blades.Body.([]*openapi.DiscoveredDevice) | ||
if !ok { | ||
log.Fatalf("Response body is not []byte") | ||
} | ||
applianceDatum, _ := datastore.DStore().GetDataStore().GetApplianceDatumById(common.DefaultApplianceCredentials.CustomId) | ||
for _, bladeDevice := range bladeBodyBytes { | ||
_, exist := data.GetBladeDatumByIp(bladeDevice.Address) | ||
if !exist { | ||
newCredentials := *common.DefaultBladeCredentials | ||
newCredentials.IpAddress = bladeDevice.Address | ||
//Assign the actual ipAddress to customId to ensure its uniqueness | ||
newCredentials.CustomId = newCredentials.CustomId + bladeDevice.Address | ||
|
||
applianceDatum.AddBladeDatum(&newCredentials) | ||
datastore.DStore().Store() | ||
} | ||
} | ||
|
||
// Add cxl-hosts | ||
// Convert data type | ||
hostBodyBytes, ok := hosts.Body.([]*openapi.DiscoveredDevice) | ||
if !ok { | ||
log.Fatalf("Response body is not []byte") | ||
} | ||
for _, hostDevice := range hostBodyBytes { | ||
_, exist := data.GetHostDatumByIp(hostDevice.Address) | ||
if !exist { | ||
newCredentials := *common.DefaultHostCredentials | ||
newCredentials.IpAddress = hostDevice.Address | ||
//Assign the actual ipAddress to customId to ensure its uniqueness | ||
newCredentials.CustomId = newCredentials.CustomId + hostDevice.Address | ||
|
||
datastore.DStore().GetDataStore().AddHostDatum(&newCredentials) | ||
datastore.DStore().Store() | ||
} | ||
|
||
} | ||
} |