diff --git a/adapter/error.go b/adapter/error.go index 1fbcf17..e9972af 100644 --- a/adapter/error.go +++ b/adapter/error.go @@ -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"}) +} \ No newline at end of file diff --git a/adapter/register.go b/adapter/register.go new file mode 100644 index 0000000..249b856 --- /dev/null +++ b/adapter/register.go @@ -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 +}