@@ -4,29 +4,59 @@ import (
44 "fmt"
55 "os"
66 "path/filepath"
7+ "strconv"
78 "strings"
9+ "sync"
810
911 "github.com/layer5io/meshery-adapter-library/adapter"
1012 "github.com/layer5io/meshery-app-mesh/internal/config"
13+ "github.com/layer5io/meshkit/models/meshmodel/core/types"
1114)
1215
1316var (
14- basePath , _ = os .Getwd ()
15- // WorkloadPath contains the path to the workload schemas and definitions directory
16- WorkloadPath = filepath .Join (basePath , "templates" , "oam" , "workloads" )
17+ basePath , _ = os .Getwd ()
18+ WorkloadPath = filepath .Join (basePath , "templates" , "oam" , "workloads" )
19+ MeshmodelComponents = filepath .Join (basePath , "templates" , "meshmodel" , "components" )
20+ traitPath = filepath .Join (basePath , "templates" , "oam" , "traits" )
1721 // traitPath = filepath.Join(basePath, "templates", "oam", "traits")
1822 pathSets = []schemaDefinitionPathSet {}
1923)
2024
2125// AvailableVersions denote the component versions available statically
2226var AvailableVersions = map [string ]bool {}
27+ var availableVersionGlobalMutex sync.Mutex
2328
2429type schemaDefinitionPathSet struct {
2530 oamDefinitionPath string
2631 jsonSchemaPath string
2732 name string
2833}
2934
35+ type meshmodelDefinitionPathSet struct {
36+ meshmodelDefinitionPath string
37+ }
38+
39+ func RegisterMeshModelComponents (uuid , runtime , host , port string ) error {
40+ meshmodelRDP := []adapter.MeshModelRegistrantDefinitionPath {}
41+ pathSets , err := loadMeshmodelComponents (MeshmodelComponents )
42+ if err != nil {
43+ return err
44+ }
45+ portint , _ := strconv .Atoi (port )
46+ for _ , pathSet := range pathSets {
47+ meshmodelRDP = append (meshmodelRDP , adapter.MeshModelRegistrantDefinitionPath {
48+ EntityDefintionPath : pathSet .meshmodelDefinitionPath ,
49+ Host : host ,
50+ Port : portint ,
51+ Type : types .ComponentDefinition ,
52+ })
53+ }
54+
55+ return adapter .
56+ NewMeshModelRegistrant (meshmodelRDP , fmt .Sprintf ("%s/api/meshmodel/components/register" , runtime )).
57+ Register (uuid )
58+ }
59+
3060// RegisterWorkloads will register all of the workload definitions
3161// present in the path oam/workloads
3262//
@@ -56,6 +86,31 @@ func RegisterWorkloads(runtime, host string) error {
5686 Register ()
5787}
5888
89+ func loadMeshmodelComponents (basepath string ) ([]meshmodelDefinitionPathSet , error ) {
90+ res := []meshmodelDefinitionPathSet {}
91+ if err := filepath .Walk (basepath , func (path string , info os.FileInfo , err error ) error {
92+ if err != nil {
93+ return err
94+ }
95+
96+ if info .IsDir () {
97+ return nil
98+ }
99+
100+ res = append (res , meshmodelDefinitionPathSet {
101+ meshmodelDefinitionPath : path ,
102+ })
103+ availableVersionGlobalMutex .Lock ()
104+ AvailableVersions [filepath .Base (filepath .Dir (path ))] = true // Getting available versions already existing on file system
105+ availableVersionGlobalMutex .Unlock ()
106+ return nil
107+ }); err != nil {
108+ return nil , err
109+ }
110+
111+ return res , nil
112+ }
113+
59114// RegisterTraits will register all of the trait definitions
60115// present in the path oam/traits
61116//
@@ -107,7 +162,9 @@ func load(basePath string) ([]schemaDefinitionPathSet, error) {
107162 jsonSchemaPath : fmt .Sprintf ("%s.meshery.layer5io.schema.json" , nameWithPath ),
108163 name : filepath .Base (nameWithPath ),
109164 })
165+ availableVersionGlobalMutex .Lock ()
110166 AvailableVersions [filepath .Base (filepath .Dir (path ))] = true
167+ availableVersionGlobalMutex .Unlock ()
111168 }
112169
113170 return nil
0 commit comments