Repository: skupperproject/skupper
Pull request #1688 (2024-09-26 02:45:57)

CollectorAPI Add new routerCount attribute to a SiteRecord


Patch:
diff --git a/cmd/network-console-collector/internal/api/types_gen.go b/cmd/network-console-collector/internal/api/types_gen.go
index 60b931f0..cbd14373 100644
--- a/cmd/network-console-collector/internal/api/types_gen.go
+++ b/cmd/network-console-collector/internal/api/types_gen.go
@@ -54,6 +54,7 @@ const (
 	SitePlatformTypeDocker     SitePlatformType = "docker"
 	SitePlatformTypeKubernetes SitePlatformType = "kubernetes"
 	SitePlatformTypePodman     SitePlatformType = "podman"
+	SitePlatformTypeSystemd    SitePlatformType = "systemd"
 	SitePlatformTypeUnknown    SitePlatformType = "unknown"
 )
 
@@ -575,7 +576,8 @@ type SiteRecord struct {
 	Platform SitePlatformType `json:"platform"`
 
 	// Provider Possible values are 'AWS', 'IBM', 'Azure' ecc. Can be any string or 'unknown'
-	Provider string `json:"provider"`
+	Provider    string `json:"provider"`
+	RouterCount int    `json:"routerCount"`
 
 	// SiteVersion The current skupper version installed. Can be any string or 'unknown'
 	SiteVersion string `json:"siteVersion"`
diff --git a/cmd/network-console-collector/internal/server/handlers.go b/cmd/network-console-collector/internal/server/handlers.go
index 273793fa..f8cf96bd 100644
--- a/cmd/network-console-collector/internal/server/handlers.go
+++ b/cmd/network-console-collector/internal/server/handlers.go
@@ -323,7 +323,7 @@ func (s *server) SitepairByID(w http.ResponseWriter, r *http.Request, id string)
 
 // (GET /api/v1alpha1/sites/)
 func (s *server) Sites(w http.ResponseWriter, r *http.Request) {
-	results := views.Sites(listByType[vanflow.SiteRecord](s.records))
+	results := views.NewSiteSliceProvider(s.graph)(listByType[vanflow.SiteRecord](s.records))
 	if err := handleCollection(w, r, &api.SiteListResponse{}, results); err != nil {
 		s.logWriteError(r, err)
 	}
@@ -331,7 +331,7 @@ func (s *server) Sites(w http.ResponseWriter, r *http.Request) {
 
 // (GET /api/v1alpha1/sites/{id}/)
 func (s *server) SiteById(w http.ResponseWriter, r *http.Request, id string) {
-	getRecord := fetchAndMap(s.records, views.Site, id)
+	getRecord := fetchAndMap(s.records, views.NewSiteProvider(s.graph), id)
 	if err := handleSingle(w, r, &api.SiteResponse{}, getRecord); err != nil {
 		s.logWriteError(r, err)
 	}
diff --git a/cmd/network-console-collector/internal/server/views/views.go b/cmd/network-console-collector/internal/server/views/views.go
index 06aa3a75..10fefd9f 100644
--- a/cmd/network-console-collector/internal/server/views/views.go
+++ b/cmd/network-console-collector/internal/server/views/views.go
@@ -594,38 +594,46 @@ func defaultRouter(id string) api.RouterRecord {
 	}
 }
 
-func Sites(entries []store.Entry) []api.SiteRecord {
-	results := make([]api.SiteRecord, 0, len(entries))
-	for _, e := range entries {
-		site, ok := e.Record.(vanflow.SiteRecord)
-		if !ok {
-			continue
+func NewSiteSliceProvider(graph collector.Graph) func(entries []store.Entry) []api.SiteRecord {
+	provider := NewSiteProvider(graph)
+	return func(entries []store.Entry) []api.SiteRecord {
+		results := make([]api.SiteRecord, 0, len(entries))
+		for _, e := range entries {
+			site, ok := e.Record.(vanflow.SiteRecord)
+			if !ok {
+				continue
+			}
+			results = append(results, provider(site))
 		}
-		results = append(results, Site(site))
+		return results
 	}
-	return results
 }
 
-func Site(site vanflow.SiteRecord) api.SiteRecord {
-	s := defaultSite(site.ID)
-	s.StartTime, s.EndTime = vanflowTimes(site.BaseRecord)
-	s.NameSpace = site.Namespace
+func NewSiteProvider(graph collector.Graph) func(vanflow.SiteRecord) api.SiteRecord {
+	return func(site vanflow.SiteRecord) api.SiteRecord {
+		s := defaultSite(site.ID)
+		s.StartTime, s.EndTime = vanflowTimes(site.BaseRecord)
+		s.NameSpace = site.Namespace
 
-	setOpt(&s.Name, site.Name)
-	setOpt(&s.Provider, site.Provider)
-	setOpt(&s.SiteVersion, site.Version)
-	if site.Platform != nil {
-		platform := *site.Platform
-		switch {
-		case strings.EqualFold(platform, string(api.SitePlatformTypeKubernetes)):
-			s.Platform = api.SitePlatformTypeKubernetes
-		case strings.EqualFold(platform, string(api.SitePlatformTypeDocker)):
-			s.Platform = api.SitePlatformTypeDocker
-		case strings.EqualFold(platform, string(api.SitePlatformTypePodman)):
-			s.Platform = api.SitePlatformTypePodman
+		setOpt(&s.Name, site.Name)
+		setOpt(&s.Provider, site.Provider)
+		setOpt(&s.SiteVersion, site.Version)
+		if site.Platform != nil {
+			platform := *site.Platform
+			switch {
+			case strings.EqualFold(platform, string(api.SitePlatformTypeKubernetes)):
+				s.Platform = api.SitePlatformTypeKubernetes
+			case strings.EqualFold(platform, string(api.SitePlatformTypeDocker)):
+				s.Platform = api.SitePlatformTypeDocker
+			case strings.EqualFold(platform, string(api.SitePlatformTypePodman)):
+				s.Platform = api.SitePlatformTypePodman
+			case strings.EqualFold(platform, string(api.SitePlatformTypeSystemd)):
+				s.Platform = api.SitePlatformTypeSystemd
+			}
 		}
+		s.RouterCount = len(graph.Site(site.ID).Routers())
+		return s
 	}
-	return s
 }
 
 func defaultSite(id string) api.SiteRecord {
diff --git a/cmd/network-console-collector/spec/openapi.yaml b/cmd/network-console-collector/spec/openapi.yaml
index b6ca594c..650be775 100644
--- a/cmd/network-console-collector/spec/openapi.yaml
+++ b/cmd/network-console-collector/spec/openapi.yaml
@@ -835,6 +835,7 @@ components:
         - kubernetes
         - podman
         - docker
+        - systemd
         - unknown
     processBindingType:
       type: string
@@ -865,6 +866,7 @@ components:
             - nameSpace
             - siteVersion
             - policy
+            - routerCount
           properties:
             provider:
               type: string
@@ -879,6 +881,8 @@ components:
             siteVersion:
               type: string
               description: The current skupper version installed. Can be any string or 'unknown'
+            routerCount:
+              type: integer
     ProcessRecord:
       allOf:
         - $ref: '#/components/schemas/baseRecord'


Test patch:
diff --git a/cmd/network-console-collector/internal/server/sites_test.go b/cmd/network-console-collector/internal/server/sites_test.go
index 4f4c1e26..f7b2092e 100644
--- a/cmd/network-console-collector/internal/server/sites_test.go
+++ b/cmd/network-console-collector/internal/server/sites_test.go
@@ -55,12 +55,17 @@ func TestSites(t *testing.T) {
 				vanflow.SiteRecord{BaseRecord: vanflow.NewBase("site-1"), Namespace: ptrTo("")},
 				vanflow.SiteRecord{BaseRecord: vanflow.NewBase("site-2"), Namespace: ptrTo("testns")},
 				vanflow.SiteRecord{BaseRecord: vanflow.NewBase("site-3")},
+				vanflow.RouterRecord{BaseRecord: vanflow.NewBase("router-2a"), Parent: ptrTo("site-2")},
+				vanflow.RouterRecord{BaseRecord: vanflow.NewBase("router-2b"), Parent: ptrTo("site-2")},
+				vanflow.RouterRecord{BaseRecord: vanflow.NewBase("router-2c"), Parent: ptrTo("site-2")},
+				vanflow.RouterRecord{BaseRecord: vanflow.NewBase("router-1a"), Parent: ptrTo("site-1")},
 			),
 			ExpectOK:             true,
 			ExpectCount:          1,
 			ExpectTimeRangeCount: 1,
 			ExpectResults: func(t *testing.T, results []api.SiteRecord) {
 				assert.Equal(t, results[0].Identity, "site-2")
+				assert.Equal(t, results[0].RouterCount, 3)
 			},
 			Parameters: map[string][]string{"nameSpace": {"testns"}},
 		},