Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service): only discover devices and add the devices if there are no devices in data store #62

Merged
merged 13 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/cfm-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ func main() {
// Load datastore
datastore.DStore().Restore()
data := datastore.DStore().GetDataStore()

// Check if there are any devices in the data store
bladeExist := len(data.ApplianceData) != 0
hostExist := len(data.HostData) != 0

// If there are no devices in the data store, do discovery, otherwise skip
if !bladeExist && !hostExist {
// Discover devices before loading datastore
bladeDevices, errBlade := services.DiscoverDevices(ctx, defaultApiService, "blade")
hostDevices, errHost := services.DiscoverDevices(ctx, defaultApiService, "cxl-host")
// Add the discovered devices into datastore
if errBlade == nil && errHost == nil {
services.AddDiscoveredDevices(ctx, defaultApiService, bladeDevices, hostDevices)
}
// Update data
data = datastore.DStore().GetDataStore()
}

datastore.ReloadDataStore(ctx, defaultApiService, data)

// Set up CORS middleware (for webui)
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var DefaultBladeCredentials = &openapi.Credentials{
Port: 443,
Insecure: true,
Protocol: "https",
CustomId: "Discoverd_Blade_",
CustomId: "",
}

var DefaultHostCredentials = &openapi.Credentials{
Expand All @@ -38,5 +38,5 @@ var DefaultHostCredentials = &openapi.Credentials{
Port: 8082,
Insecure: true,
Protocol: "http",
CustomId: "Discoverd_Host_",
CustomId: "",
}
17 changes: 10 additions & 7 deletions services/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"fmt"
"log"
"strings"

"golang.org/x/net/context"

Expand All @@ -11,7 +12,7 @@ import (
"cfm/pkg/openapi"
)

// discoverDevices function to call the DiscoverDevices API
// DiscoverDevices function to call the DiscoverDevices API
func DiscoverDevices(ctx context.Context, apiService openapi.DefaultAPIServicer, deviceType string) (openapi.ImplResponse, error) {
resp, _ := apiService.DiscoverDevices(ctx, deviceType)
if resp.Code >= 300 {
Expand All @@ -35,7 +36,6 @@ func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServ
}

// Add blades
// Convert data type
bladeBodyBytes, ok := blades.Body.([]*openapi.DiscoveredDevice)
if !ok {
log.Fatalf("Response body is not []byte")
Expand All @@ -46,16 +46,17 @@ func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServ
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

// Remove the .local suffix (e.g. blade device name: granite00.local) from the device name by splitting it with . and assign it to the customId
deviceName := strings.SplitN(bladeDevice.Name, ".", 2)[0]
newCredentials.CustomId = deviceName

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")
Expand All @@ -65,8 +66,10 @@ func AddDiscoveredDevices(ctx context.Context, apiService openapi.DefaultAPIServ
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

// Remove the .local suffix (e.g. host device name: host00.local) from the device name by splitting it with . and assign it to the customId
deviceName := strings.SplitN(hostDevice.Name, ".", 2)[0]
newCredentials.CustomId = deviceName

datastore.DStore().GetDataStore().AddHostDatum(&newCredentials)
datastore.DStore().Store()
Expand Down
2 changes: 1 addition & 1 deletion webui/src/components/Dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
style="margin-bottom: 40px"
variant="tonal"
>
Click to discover new devices</v-btn
Discover new devices</v-btn
>
</v-col>
</v-row>
Expand Down
15 changes: 10 additions & 5 deletions webui/src/components/Stores/ApplianceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const useApplianceStore = defineStore('appliance', {
applianceIds: [] as { id: string, blades: { id: string, ipAddress: string, status: string | undefined }[] }[],
discoveredBlades: [] as DiscoveredDevice[],

prefixBladeId: "Discoverd_Blade_",
newBladeCredentials: {
username: "root",
password: "0penBmc",
Expand Down Expand Up @@ -140,6 +139,11 @@ export const useApplianceStore = defineStore('appliance', {
}
}

// Format the device name, remove the .local suffix (e.g. blade device name: granite00.local) from the device name by splitting it with .
for (var n = 0; n < this.discoveredBlades.length; n++) {
this.discoveredBlades[n].name = this.discoveredBlades[n].name!.split('.')[0]
}

return this.discoveredBlades
} catch (error) {
console.error("Error discovering new devices:", error);
Expand All @@ -163,12 +167,13 @@ export const useApplianceStore = defineStore('appliance', {
}
}

// Add the new discovered blade to the default appliance
let appliance = this.applianceIds.find(appliance => appliance.id === this.defaultApplianceId);

this.newBladeCredentials.customId = this.prefixBladeId + blade.address;
// Remove the .local suffix (e.g. blade device name: granite00.local) from the device name by splitting it with . and assign it to the customId
let deviceName = blade.name!.split('.')[0];
Meng-20 marked this conversation as resolved.
Show resolved Hide resolved
this.newBladeCredentials.customId = deviceName;
this.newBladeCredentials.ipAddress = blade.address + "";

// Add the new discovered blade to the default appliance
const responseOfBlade = await defaultApi.bladesPost(this.defaultApplianceId, this.newBladeCredentials);

if (responseOfBlade) {
Expand Down Expand Up @@ -234,4 +239,4 @@ export const useApplianceStore = defineStore('appliance', {
this.selectedApplianceId = applianceId;
},
}
})
})
17 changes: 12 additions & 5 deletions webui/src/components/Stores/HostStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const useHostStore = defineStore('host', {
hostIds: [] as { id: string, ipAddress: string, status: string | undefined }[],
discoveredHosts: [] as DiscoveredDevice[],

prefixHostId: "Discoverd_Host_",
newHostCredentials: {
username: "admin",
password: "admin12345",
Expand Down Expand Up @@ -103,6 +102,11 @@ export const useHostStore = defineStore('host', {
}
}

// Format the device name, remove the .local suffix (e.g. host device name: host00.local) from the device name by splitting it with .
for (var n = 0; n < this.discoveredHosts.length; n++) {
this.discoveredHosts[n].name = this.discoveredHosts[n].name!.split('.')[0]
}

return this.discoveredHosts
} catch (error) {
console.error("Error discovering new devices:", error);
Expand All @@ -111,13 +115,16 @@ export const useHostStore = defineStore('host', {

async addDiscoveredHosts(host: DiscoveredDevice) {
const defaultApi = new DefaultApi(undefined, API_BASE_PATH);
// Add all the new didcovered hosts
this.newHostCredentials.customId = this.prefixHostId + host.address;

// Remove the .local suffix (e.g. host device name: host00.local) from the device name by splitting it with . and assign it to the customId
let deviceName = host.name!.split('.')[0];
Meng-20 marked this conversation as resolved.
Show resolved Hide resolved
this.newHostCredentials.customId = deviceName;
this.newHostCredentials.ipAddress = host.address + "";

// Add the new didcovered host
const responseOfHost = await defaultApi.hostsPost(this.newHostCredentials);

// Update the applianceIds and appliances
// Update the hostIds and hosts
if (responseOfHost) {
const response = { id: responseOfHost.data.id, ipAddress: responseOfHost.data.ipAddress, status: responseOfHost.data.status }
this.hosts.push(responseOfHost.data);
Expand Down Expand Up @@ -241,4 +248,4 @@ export const useHostStore = defineStore('host', {
this.selectedHostStatus = status
}
}
})
})
Loading