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 12 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
4 changes: 2 additions & 2 deletions webui/src/components/Appliance/Memory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
</template>

<script>
import { computed, ref, watchEffect } from "vue";
import { computed } from "vue";
import { useBladeMemoryStore } from "../Stores/BladeMemoryStore";
import { useBladePortStore } from "../Stores/BladePortStore";
import { useBladeResourceStore } from "../Stores/BladeResourceStore";
Expand Down Expand Up @@ -568,4 +568,4 @@ export default {
};
},
};
</script>
</script>
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -323,7 +323,7 @@ export default {
if (this.selectedBlades.length === 0) {
console.log("No blades selected.");
} else {
for (var i = 0; i < this.selectedBlades.length; i++) {
for (let i = 0; i < this.selectedBlades.length; i++) {
try {
const newAddedBlade = await applianceStore.addDiscoveredBlades(
this.selectedBlades[i]
Expand All @@ -341,7 +341,7 @@ export default {
if (this.selectedHosts.length === 0) {
console.log("No hosts selected.");
} else {
for (var i = 0; i < this.selectedHosts.length; i++) {
for (let i = 0; i < this.selectedHosts.length; i++) {
try {
const newAddedHost = await hostStore.addDiscoveredHosts(
this.selectedHosts[i]
Expand Down
Loading
Loading