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.
Merge pull request #150 from MUzairS15/MUzairS15/model/registration
[models] Add adapter model registration funcs.
- Loading branch information
Showing
5 changed files
with
80 additions
and
235 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
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
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 | ||
} |