Skip to content

Commit

Permalink
add model registration funcs for adapters
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <[email protected]>
  • Loading branch information
MUzairS15 committed Jan 29, 2024
1 parent 99e3c1a commit f5f885a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions adapter/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,7 @@ func ErrGenerateComponents(err error) error {
func ErrCreatingComponents(err error) error {
return errors.New(ErrCreatingComponentsCode, errors.Alert, []string{"error creating components"}, []string{err.Error()}, []string{"Invalid Path or version passed in static configuration", "URL passed maybe incorrect", "Version passed maybe incorrect"}, []string{"Make sure to pass correct configuration", "Make sure the URL passed in the configuration is correct", "Make sure a valid version is passed in configuration"})
}

func ErrRegisterComponents(err error) error {
return errors.New(ErrCreatingComponentsCode, errors.Alert, []string{"error registering components"}, []string{err.Error()}, []string{"Invalid Path or version passed in configuration", "Server URL passed maybe incorrect", "Server is not reachable"}, []string{"Make sure to pass correct configuration", "Make sure the URL passed in the configuration is correct", "Make sure adapter is reachable to the server"})
}
70 changes: 70 additions & 0 deletions adapter/register.go
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
}

0 comments on commit f5f885a

Please sign in to comment.