generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add model registration funcs for adapters
Signed-off-by: MUzairS15 <[email protected]>
- Loading branch information
MUzairS15
committed
Jan 29, 2024
1 parent
99e3c1a
commit f5f885a
Showing
2 changed files
with
74 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package adapter | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"sync" | ||
|
||
"github.com/layer5io/meshkit/models/meshmodel/core/types" | ||
) | ||
|
||
var ( | ||
basePath, _ = os.Getwd() | ||
MeshmodelComponents = filepath.Join(basePath, "templates", "meshmodel", "components") | ||
) | ||
|
||
// AvailableVersions denote the component versions available statically | ||
var AvailableVersions = map[string]bool{} | ||
|
||
type meshmodelDefinitionPathSet struct { | ||
meshmodelDefinitionPath string | ||
} | ||
|
||
func RegisterMeshModelComponents(uuid, runtime, host, port string) error { | ||
meshmodelRDP := []MeshModelRegistrantDefinitionPath{} | ||
pathSets, err := loadMeshmodelComponents(MeshmodelComponents) | ||
if err != nil { | ||
return ErrRegisterComponents(err) | ||
} | ||
portint, _ := strconv.Atoi(port) | ||
for _, pathSet := range pathSets { | ||
meshmodelRDP = append(meshmodelRDP, MeshModelRegistrantDefinitionPath{ | ||
EntityDefintionPath: pathSet.meshmodelDefinitionPath, | ||
Host: host, | ||
Port: portint, | ||
Type: types.ComponentDefinition, | ||
}) | ||
} | ||
|
||
return NewMeshModelRegistrant(meshmodelRDP, fmt.Sprintf("%s/api/meshmodel/components/register", runtime)). | ||
Register(uuid) | ||
} | ||
|
||
var versionLock sync.Mutex | ||
|
||
func loadMeshmodelComponents(basepath string) ([]meshmodelDefinitionPathSet, error) { | ||
res := []meshmodelDefinitionPathSet{} | ||
if err := filepath.Walk(basepath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
res = append(res, meshmodelDefinitionPathSet{ | ||
meshmodelDefinitionPath: path, | ||
}) | ||
versionLock.Lock() | ||
AvailableVersions[filepath.Base(filepath.Dir(path))] = true // Getting available versions already existing on file system | ||
versionLock.Unlock() | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |