From 7c7d90a781082c6bd22d12a5e2451ca61a5198af Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 29 Nov 2017 11:19:00 +0100 Subject: [PATCH] Renamed apiv1 lib to xaapiv1. xa prefix (for Xds Agent) is safest to avoid confusion with xs prefix (for Xds Server) Signed-off-by: Sebastien Douheret --- Makefile | 3 +- glide.yaml | 4 ++ lib/agent/apiv1-config.go | 12 ++-- lib/agent/apiv1-events.go | 6 +- lib/agent/apiv1-projects.go | 6 +- lib/agent/apiv1-version.go | 10 +-- lib/agent/events.go | 16 ++--- lib/agent/project-interface.go | 10 +-- lib/agent/project-pathmap.go | 17 ++--- lib/agent/project-st.go | 31 +++++---- lib/agent/projects.go | 41 +++++------ lib/agent/webserver.go | 4 +- lib/agent/xdsserver.go | 137 +++++++++---------------------------- lib/syncthing/stfolder.go | 8 +-- lib/{apiv1 => xaapiv1}/config.go | 2 +- lib/{apiv1 => xaapiv1}/events.go | 10 +-- lib/{apiv1 => xaapiv1}/exec.go | 2 +- lib/{apiv1 => xaapiv1}/projects.go | 2 +- lib/{apiv1 => xaapiv1}/sdks.go | 2 +- lib/{apiv1 => xaapiv1}/version.go | 2 +- 20 files changed, 130 insertions(+), 195 deletions(-) rename lib/{apiv1 => xaapiv1}/config.go (97%) rename lib/{apiv1 => xaapiv1}/events.go (90%) rename lib/{apiv1 => xaapiv1}/exec.go (99%) rename lib/{apiv1 => xaapiv1}/projects.go (98%) rename lib/{apiv1 => xaapiv1}/sdks.go (94%) rename lib/{apiv1 => xaapiv1}/version.go (95%) diff --git a/Makefile b/Makefile index 3f4457c..802aede 100644 --- a/Makefile +++ b/Makefile @@ -140,7 +140,8 @@ vendor: tools/glide glide.yaml vendor/debug: vendor (cd vendor/github.com/iotbzh && \ - rm -rf xds-common && ln -s ../../../../xds-common ) + rm -rf xds-common && ln -s ../../../../xds-common && \ + rm -rf xds-server && ln -s ../../../../xds-server ) .PHONY: tools/glide tools/glide: diff --git a/glide.yaml b/glide.yaml index 1630276..0569d76 100644 --- a/glide.yaml +++ b/glide.yaml @@ -25,5 +25,9 @@ import: version: ^0.1.0 subpackages: - golib/common +- package: github.com/iotbzh/xds-server + version: 5c1d023f41472660952ee559e2484cd6bbe688d3 + subpackages: + - lib/xsapiv1 - package: github.com/franciscocpg/reflectme version: ^0.1.9 diff --git a/lib/agent/apiv1-config.go b/lib/agent/apiv1-config.go index 2197720..40f65df 100644 --- a/lib/agent/apiv1-config.go +++ b/lib/agent/apiv1-config.go @@ -5,7 +5,7 @@ import ( "sync" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" "github.com/iotbzh/xds-agent/lib/xdsconfig" common "github.com/iotbzh/xds-common/golib" ) @@ -24,7 +24,7 @@ func (s *APIService) getConfig(c *gin.Context) { // SetConfig sets configuration func (s *APIService) setConfig(c *gin.Context) { - var cfgArg apiv1.APIConfig + var cfgArg xaapiv1.APIConfig if c.BindJSON(&cfgArg) != nil { common.APIError(c, "Invalid arguments") return @@ -65,16 +65,16 @@ func (s *APIService) setConfig(c *gin.Context) { c.JSON(http.StatusOK, s._getConfig()) } -func (s *APIService) _getConfig() apiv1.APIConfig { - cfg := apiv1.APIConfig{ +func (s *APIService) _getConfig() xaapiv1.APIConfig { + cfg := xaapiv1.APIConfig{ Version: s.Config.Version, APIVersion: s.Config.APIVersion, VersionGitTag: s.Config.VersionGitTag, - Servers: []apiv1.ServerCfg{}, + Servers: []xaapiv1.ServerCfg{}, } for _, svr := range s.xdsServers { - cfg.Servers = append(cfg.Servers, apiv1.ServerCfg{ + cfg.Servers = append(cfg.Servers, xaapiv1.ServerCfg{ ID: svr.ID, URL: svr.BaseURL, APIURL: svr.APIURL, diff --git a/lib/agent/apiv1-events.go b/lib/agent/apiv1-events.go index cb7cde1..c40b83f 100644 --- a/lib/agent/apiv1-events.go +++ b/lib/agent/apiv1-events.go @@ -4,7 +4,7 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" common "github.com/iotbzh/xds-common/golib" ) @@ -15,7 +15,7 @@ func (s *APIService) eventsList(c *gin.Context) { // eventsRegister Registering for events that will be send over a WS func (s *APIService) eventsRegister(c *gin.Context) { - var args apiv1.EventRegisterArgs + var args xaapiv1.EventRegisterArgs if c.BindJSON(&args) != nil || args.Name == "" { common.APIError(c, "Invalid arguments") @@ -39,7 +39,7 @@ func (s *APIService) eventsRegister(c *gin.Context) { // eventsRegister Registering for events that will be send over a WS func (s *APIService) eventsUnRegister(c *gin.Context) { - var args apiv1.EventUnRegisterArgs + var args xaapiv1.EventUnRegisterArgs if c.BindJSON(&args) != nil || args.Name == "" { common.APIError(c, "Invalid arguments") diff --git a/lib/agent/apiv1-projects.go b/lib/agent/apiv1-projects.go index 5784896..281886f 100644 --- a/lib/agent/apiv1-projects.go +++ b/lib/agent/apiv1-projects.go @@ -4,7 +4,7 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" common "github.com/iotbzh/xds-common/golib" ) @@ -31,7 +31,7 @@ func (s *APIService) getProject(c *gin.Context) { // addProject adds a new project to server config func (s *APIService) addProject(c *gin.Context) { - var cfgArg apiv1.ProjectConfig + var cfgArg xaapiv1.ProjectConfig if c.BindJSON(&cfgArg) != nil { common.APIError(c, "Invalid arguments") return @@ -93,7 +93,7 @@ func (s *APIService) updateProject(c *gin.Context) { return } - var cfgArg apiv1.ProjectConfig + var cfgArg xaapiv1.ProjectConfig if c.BindJSON(&cfgArg) != nil { common.APIError(c, "Invalid arguments") return diff --git a/lib/agent/apiv1-version.go b/lib/agent/apiv1-version.go index c73010e..be72672 100644 --- a/lib/agent/apiv1-version.go +++ b/lib/agent/apiv1-version.go @@ -5,13 +5,13 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" ) // getInfo : return various information about server func (s *APIService) getVersion(c *gin.Context) { - response := apiv1.XDSVersion{ - Client: apiv1.VersionData{ + response := xaapiv1.XDSVersion{ + Client: xaapiv1.VersionData{ ID: "", Version: s.Config.Version, APIVersion: s.Config.APIVersion, @@ -19,9 +19,9 @@ func (s *APIService) getVersion(c *gin.Context) { }, } - svrVer := []apiv1.VersionData{} + svrVer := []xaapiv1.VersionData{} for _, svr := range s.xdsServers { - res := apiv1.VersionData{} + res := xaapiv1.VersionData{} if err := svr.GetVersion(&res); err != nil { errMsg := fmt.Sprintf("Cannot retrieve version of XDS server ID %s : %v", svr.ID, err.Error()) s.Log.Warning(errMsg) diff --git a/lib/agent/events.go b/lib/agent/events.go index ccf8ddc..a45d404 100644 --- a/lib/agent/events.go +++ b/lib/agent/events.go @@ -4,7 +4,7 @@ import ( "fmt" "time" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" ) // EventDef Definition on one event @@ -21,7 +21,7 @@ type Events struct { // NewEvents creates an instance of Events func NewEvents(ctx *Context) *Events { evMap := make(map[string]*EventDef) - for _, ev := range apiv1.EVTAllList { + for _, ev := range xaapiv1.EVTAllList { evMap[ev] = &EventDef{ sids: make(map[string]int), } @@ -34,13 +34,13 @@ func NewEvents(ctx *Context) *Events { // GetList returns the list of all supported events func (e *Events) GetList() []string { - return apiv1.EVTAllList + return xaapiv1.EVTAllList } // Register Used by a client/session to register to a specific (or all) event(s) func (e *Events) Register(evName, sessionID string) error { - evs := apiv1.EVTAllList - if evName != apiv1.EVTAll { + evs := xaapiv1.EVTAllList + if evName != xaapiv1.EVTAll { if _, ok := e.eventsMap[evName]; !ok { return fmt.Errorf("Unsupported event type name") } @@ -54,8 +54,8 @@ func (e *Events) Register(evName, sessionID string) error { // UnRegister Used by a client/session to unregister event(s) func (e *Events) UnRegister(evName, sessionID string) error { - evs := apiv1.EVTAllList - if evName != apiv1.EVTAll { + evs := xaapiv1.EVTAllList + if evName != xaapiv1.EVTAll { if _, ok := e.eventsMap[evName]; !ok { return fmt.Errorf("Unsupported event type name") } @@ -92,7 +92,7 @@ func (e *Events) Emit(evName string, data interface{},fromSid string) error { } continue } - msg := apiv1.EventMsg{ + msg := xaapiv1.EventMsg{ Time: time.Now().String(), FromSessionID: fromSid, Type: evName, diff --git a/lib/agent/project-interface.go b/lib/agent/project-interface.go index 0d6bb1a..7191210 100644 --- a/lib/agent/project-interface.go +++ b/lib/agent/project-interface.go @@ -1,14 +1,14 @@ package agent -import "github.com/iotbzh/xds-agent/lib/apiv1" +import "github.com/iotbzh/xds-agent/lib/xaapiv1" // IPROJECT Project interface type IPROJECT interface { - Add(cfg apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) // Add a new project - Setup(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) // Local setup of the project + Add(cfg xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) // Add a new project + Setup(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) // Local setup of the project Delete() error // Delete a project - GetProject() *apiv1.ProjectConfig // Get project public configuration - Update(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) // Update project configuration + GetProject() *xaapiv1.ProjectConfig // Get project public configuration + Update(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) // Update project configuration GetServer() *XdsServer // Get XdsServer that holds this project Sync() error // Force project files synchronization IsInSync() (bool, error) // Check if project files are in-sync diff --git a/lib/agent/project-pathmap.go b/lib/agent/project-pathmap.go index 3c87770..1880fa3 100644 --- a/lib/agent/project-pathmap.go +++ b/lib/agent/project-pathmap.go @@ -6,8 +6,9 @@ import ( "os" "strings" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" common "github.com/iotbzh/xds-common/golib" + "github.com/iotbzh/xds-server/lib/xsapiv1" ) // IPROJECT interface implementation for native/path mapping projects @@ -16,7 +17,7 @@ import ( type PathMap struct { *Context server *XdsServer - folder *XdsFolderConfig + folder *xsapiv1.FolderConfig } // NewProjectPathMap Create a new instance of PathMap @@ -24,13 +25,13 @@ func NewProjectPathMap(ctx *Context, svr *XdsServer) *PathMap { p := PathMap{ Context: ctx, server: svr, - folder: &XdsFolderConfig{}, + folder: &xsapiv1.FolderConfig{}, } return &p } // Add a new project -func (p *PathMap) Add(cfg apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *PathMap) Add(cfg xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { var err error var file *os.File errMsg := "ClientPath sanity check error (%d): %v" @@ -92,24 +93,24 @@ func (p *PathMap) Delete() error { } // GetProject Get public part of project config -func (p *PathMap) GetProject() *apiv1.ProjectConfig { +func (p *PathMap) GetProject() *xaapiv1.ProjectConfig { prj := p.server.FolderToProject(*p.folder) prj.ServerID = p.server.ID return &prj } // Setup Setup local project config -func (p *PathMap) Setup(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *PathMap) Setup(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { p.folder = p.server.ProjectToFolder(prj) np := p.GetProject() - if err := p.events.Emit(apiv1.EVTProjectChange, np, ""); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectChange, np, ""); err != nil { return np, err } return np, nil } // Update Update some field of a project -func (p *PathMap) Update(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *PathMap) Update(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { if p.folder.ID != prj.ID { return nil, fmt.Errorf("Invalid id") } diff --git a/lib/agent/project-st.go b/lib/agent/project-st.go index c4e8fce..d5939e6 100644 --- a/lib/agent/project-st.go +++ b/lib/agent/project-st.go @@ -3,8 +3,9 @@ package agent import ( "fmt" - "github.com/iotbzh/xds-agent/lib/apiv1" st "github.com/iotbzh/xds-agent/lib/syncthing" + "github.com/iotbzh/xds-agent/lib/xaapiv1" + "github.com/iotbzh/xds-server/lib/xsapiv1" ) // IPROJECT interface implementation for syncthing projects @@ -13,7 +14,7 @@ import ( type STProject struct { *Context server *XdsServer - folder *XdsFolderConfig + folder *xsapiv1.FolderConfig eventIDs []int } @@ -22,13 +23,13 @@ func NewProjectST(ctx *Context, svr *XdsServer) *STProject { p := STProject{ Context: ctx, server: svr, - folder: &XdsFolderConfig{}, + folder: &xsapiv1.FolderConfig{}, } return &p } // Add a new project -func (p *STProject) Add(cfg apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *STProject) Add(cfg xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { var err error // Add project/folder into XDS Server @@ -51,7 +52,7 @@ func (p *STProject) Add(cfg apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { locPrj, err := p.SThg.FolderConfigGet(id) if err != nil { - svrPrj.Status = apiv1.StatusErrorConfig + svrPrj.Status = xaapiv1.StatusErrorConfig return nil, err } if svrPrj.ID != locPrj.ID { @@ -73,14 +74,14 @@ func (p *STProject) Delete() error { } // GetProject Get public part of project config -func (p *STProject) GetProject() *apiv1.ProjectConfig { +func (p *STProject) GetProject() *xaapiv1.ProjectConfig { prj := p.server.FolderToProject(*p.folder) prj.ServerID = p.server.ID return &prj } // Setup Setup local project config -func (p *STProject) Setup(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *STProject) Setup(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { // Update folder p.folder = p.server.ProjectToFolder(prj) svrPrj := p.GetProject() @@ -106,7 +107,7 @@ func (p *STProject) Setup(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) } // Update Update some field of a project -func (p *STProject) Update(prj apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) { +func (p *STProject) Update(prj xaapiv1.ProjectConfig) (*xaapiv1.ProjectConfig, error) { if p.folder.ID != prj.ID { return nil, fmt.Errorf("Invalid id") @@ -146,7 +147,7 @@ func (p *STProject) IsInSync() (bool, error) { // callback use to update (XDS Server) folder IsInSync status func (p *STProject) _cbServerFolderChanged(pData interface{}, data interface{}) error { - evt := data.(XdsEventFolderChange) + evt := data.(xsapiv1.EventMsg) // Only process event that concerns this project/folder ID if p.folder.ID != evt.Folder.ID { @@ -159,7 +160,7 @@ func (p *STProject) _cbServerFolderChanged(pData interface{}, data interface{}) p.folder.DataCloudSync.STSvrIsInSync = evt.Folder.IsInSync p.folder.DataCloudSync.STSvrStatus = evt.Folder.Status - if err := p.events.Emit(apiv1.EVTProjectChange, p.server.FolderToProject(*p.folder), ""); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectChange, p.server.FolderToProject(*p.folder), ""); err != nil { p.Log.Warningf("Cannot notify project change (from server): %v", err) } } @@ -180,15 +181,15 @@ func (p *STProject) _cbLocalSTEvents(ev st.Event, data *st.EventsCBData) { to := ev.Data["to"] switch to { case "scanning", "syncing": - sts = apiv1.StatusSyncing + sts = xaapiv1.StatusSyncing case "idle": - sts = apiv1.StatusEnable + sts = xaapiv1.StatusEnable } inSync = (to == "idle") case st.EventFolderPaused: - if sts == apiv1.StatusEnable { - sts = apiv1.StatusPause + if sts == xaapiv1.StatusEnable { + sts = xaapiv1.StatusPause } inSync = false } @@ -198,7 +199,7 @@ func (p *STProject) _cbLocalSTEvents(ev st.Event, data *st.EventsCBData) { p.folder.DataCloudSync.STLocIsInSync = inSync p.folder.DataCloudSync.STLocStatus = sts - if err := p.events.Emit(apiv1.EVTProjectChange, p.server.FolderToProject(*p.folder), ""); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectChange, p.server.FolderToProject(*p.folder), ""); err != nil { p.Log.Warningf("Cannot notify project change (local): %v", err) } } diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 966c231..1d21931 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -7,8 +7,9 @@ import ( "time" "github.com/franciscocpg/reflectme" - "github.com/iotbzh/xds-agent/lib/apiv1" "github.com/iotbzh/xds-agent/lib/syncthing" + "github.com/iotbzh/xds-agent/lib/xaapiv1" + "github.com/iotbzh/xds-server/lib/xsapiv1" "github.com/syncthing/syncthing/lib/sync" ) @@ -45,7 +46,7 @@ func (p *Projects) Init(server *XdsServer) error { if svr.Disabled { continue } - xFlds := []XdsFolderConfig{} + xFlds := []xsapiv1.FolderConfig{} if err := svr.GetFolders(&xFlds); err != nil { errMsg += fmt.Sprintf("Cannot retrieve folders config of XDS server ID %s : %v \n", svr.ID, err.Error()) continue @@ -102,7 +103,7 @@ func (p *Projects) Get(id string) *IPROJECT { } // GetProjectArr returns the config of all folders as an array -func (p *Projects) GetProjectArr() []apiv1.ProjectConfig { +func (p *Projects) GetProjectArr() []xaapiv1.ProjectConfig { pjMutex.Lock() defer pjMutex.Unlock() @@ -110,8 +111,8 @@ func (p *Projects) GetProjectArr() []apiv1.ProjectConfig { } // GetProjectArrUnsafe Same as GetProjectArr without mutex protection -func (p *Projects) GetProjectArrUnsafe() []apiv1.ProjectConfig { - conf := []apiv1.ProjectConfig{} +func (p *Projects) GetProjectArrUnsafe() []xaapiv1.ProjectConfig { + conf := []xaapiv1.ProjectConfig{} for _, v := range p.projects { prj := (*v).GetProject() conf = append(conf, *prj) @@ -120,14 +121,14 @@ func (p *Projects) GetProjectArrUnsafe() []apiv1.ProjectConfig { } // Add adds a new folder -func (p *Projects) Add(newF apiv1.ProjectConfig, fromSid string) (*apiv1.ProjectConfig, error) { +func (p *Projects) Add(newF xaapiv1.ProjectConfig, fromSid string) (*xaapiv1.ProjectConfig, error) { prj, err := p.createUpdate(newF, true, false) if err != nil { return prj, err } // Notify client with event - if err := p.events.Emit(apiv1.EVTProjectAdd, *prj, fromSid); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectAdd, *prj, fromSid); err != nil { p.Log.Warningf("Cannot notify project deletion: %v", err) } @@ -135,7 +136,7 @@ func (p *Projects) Add(newF apiv1.ProjectConfig, fromSid string) (*apiv1.Project } // CreateUpdate creates or update a folder -func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial bool) (*apiv1.ProjectConfig, error) { +func (p *Projects) createUpdate(newF xaapiv1.ProjectConfig, create bool, initial bool) (*xaapiv1.ProjectConfig, error) { var err error pjMutex.Lock() @@ -167,7 +168,7 @@ func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial b var fld IPROJECT switch newF.Type { // SYNCTHING - case apiv1.TypeCloudSync: + case xaapiv1.TypeCloudSync: if p.SThg != nil { fld = NewProjectST(p.Context, svr) } else { @@ -175,24 +176,24 @@ func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial b } // PATH MAP - case apiv1.TypePathMap: + case xaapiv1.TypePathMap: fld = NewProjectPathMap(p.Context, svr) default: return nil, fmt.Errorf("Unsupported folder type") } - var newPrj *apiv1.ProjectConfig + var newPrj *xaapiv1.ProjectConfig if create { // Add project on server if newPrj, err = fld.Add(newF); err != nil { - newF.Status = apiv1.StatusErrorConfig + newF.Status = xaapiv1.StatusErrorConfig log.Printf("ERROR Adding project: %v\n", err) return newPrj, err } } else { // Just update project config if newPrj, err = fld.Setup(newF); err != nil { - newF.Status = apiv1.StatusErrorConfig + newF.Status = xaapiv1.StatusErrorConfig log.Printf("ERROR Updating project: %v\n", err) return newPrj, err } @@ -218,13 +219,13 @@ func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial b } // Delete deletes a specific folder -func (p *Projects) Delete(id, fromSid string) (apiv1.ProjectConfig, error) { +func (p *Projects) Delete(id, fromSid string) (xaapiv1.ProjectConfig, error) { var err error pjMutex.Lock() defer pjMutex.Unlock() - fld := apiv1.ProjectConfig{} + fld := xaapiv1.ProjectConfig{} fc, exist := p.projects[id] if !exist { return fld, fmt.Errorf("Unknown id") @@ -239,7 +240,7 @@ func (p *Projects) Delete(id, fromSid string) (apiv1.ProjectConfig, error) { delete(p.projects, id) // Notify client with event - if err := p.events.Emit(apiv1.EVTProjectDelete, *prj, fromSid); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectDelete, *prj, fromSid); err != nil { p.Log.Warningf("Cannot notify project deletion: %v", err) } @@ -265,7 +266,7 @@ func (p *Projects) IsProjectInSync(id string) (bool, error) { } // Update Update some field of a project -func (p *Projects) Update(id string, prj apiv1.ProjectConfig, fromSid string) (*apiv1.ProjectConfig, error) { +func (p *Projects) Update(id string, prj xaapiv1.ProjectConfig, fromSid string) (*xaapiv1.ProjectConfig, error) { pjMutex.Lock() defer pjMutex.Unlock() @@ -276,12 +277,12 @@ func (p *Projects) Update(id string, prj apiv1.ProjectConfig, fromSid string) (* } // Copy current in a new object to change nothing in case of an error rises - newFld := apiv1.ProjectConfig{} + newFld := xaapiv1.ProjectConfig{} reflectme.Copy((*fc).GetProject(), &newFld) // Only update some fields dirty := false - for _, fieldName := range apiv1.ProjectConfigUpdatableFields { + for _, fieldName := range xaapiv1.ProjectConfigUpdatableFields { valNew, err := reflectme.GetField(prj, fieldName) if err == nil { valCur, err := reflectme.GetField(newFld, fieldName) @@ -305,7 +306,7 @@ func (p *Projects) Update(id string, prj apiv1.ProjectConfig, fromSid string) (* } // Notify client with event - if err := p.events.Emit(apiv1.EVTProjectChange, *upPrj, fromSid); err != nil { + if err := p.events.Emit(xaapiv1.EVTProjectChange, *upPrj, fromSid); err != nil { p.Log.Warningf("Cannot notify project change: %v", err) } return upPrj, err diff --git a/lib/agent/webserver.go b/lib/agent/webserver.go index 13572b4..c9f3acd 100644 --- a/lib/agent/webserver.go +++ b/lib/agent/webserver.go @@ -11,7 +11,7 @@ import ( "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "github.com/googollee/go-socket.io" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" ) // WebServer . @@ -233,7 +233,7 @@ func (s *WebServer) socketHandler(c *gin.Context) { so.On("disconnection", func() { s.Log.Debugf("WS disconnected (WSID=%s, SID=%s)", so.Id(), sess.ID) - s.events.UnRegister(apiv1.EVTAll, sess.ID) + s.events.UnRegister(xaapiv1.EVTAll, sess.ID) s.sessions.UpdateIOSocket(sess.ID, nil) }) }) diff --git a/lib/agent/xdsserver.go b/lib/agent/xdsserver.go index e2c38c1..620bae9 100644 --- a/lib/agent/xdsserver.go +++ b/lib/agent/xdsserver.go @@ -11,9 +11,10 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/iotbzh/xds-agent/lib/xaapiv1" "github.com/iotbzh/xds-agent/lib/xdsconfig" common "github.com/iotbzh/xds-common/golib" + "github.com/iotbzh/xds-server/lib/xsapiv1" uuid "github.com/satori/go.uuid" sio_client "github.com/sebd71/go-socket.io-client" ) @@ -28,7 +29,7 @@ type XdsServer struct { ConnRetry int Connected bool Disabled bool - ServerConfig *XdsServerConfig + ServerConfig *xsapiv1.APIConfig // Events management CBOnError func(error) @@ -43,80 +44,6 @@ type XdsServer struct { apiRouter *gin.RouterGroup } -// XdsServerConfig Data return by GET /config -type XdsServerConfig struct { - ID string `json:"id"` - Version string `json:"version"` - APIVersion string `json:"apiVersion"` - VersionGitTag string `json:"gitTag"` - SupportedSharing map[string]bool `json:"supportedSharing"` - Builder XdsBuilderConfig `json:"builder"` -} - -// XdsBuilderConfig represents the builder container configuration -type XdsBuilderConfig struct { - IP string `json:"ip"` - Port string `json:"port"` - SyncThingID string `json:"syncThingID"` -} - -// XdsFolderType XdsServer folder type -type XdsFolderType string - -const ( - // XdsTypePathMap Path Mapping folder type - XdsTypePathMap = "PathMap" - // XdsTypeCloudSync Cloud synchronization (AKA syncthing) folder type - XdsTypeCloudSync = "CloudSync" - // XdsTypeCifsSmb CIFS (AKA samba) folder type - XdsTypeCifsSmb = "CIFS" -) - -// XdsFolderConfig XdsServer folder config -type XdsFolderConfig struct { - ID string `json:"id"` - Label string `json:"label"` - ClientPath string `json:"path"` - Type XdsFolderType `json:"type"` - Status string `json:"status"` - IsInSync bool `json:"isInSync"` - DefaultSdk string `json:"defaultSdk"` - ClientData string `json:"clientData"` // free form field that can used by client - - // Specific data depending on which Type is used - DataPathMap XdsPathMapConfig `json:"dataPathMap,omitempty"` - DataCloudSync XdsCloudSyncConfig `json:"dataCloudSync,omitempty"` -} - -// XdsPathMapConfig Path mapping specific data -type XdsPathMapConfig struct { - ServerPath string `json:"serverPath"` - CheckFile string `json:"checkFile"` - CheckContent string `json:"checkContent"` -} - -// XdsCloudSyncConfig CloudSync (AKA Syncthing) specific data -type XdsCloudSyncConfig struct { - SyncThingID string `json:"syncThingID"` - STSvrStatus string `json:"-"` - STSvrIsInSync bool `json:"-"` - STLocStatus string `json:"-"` - STLocIsInSync bool `json:"-"` -} - -// XdsEventRegisterArgs arguments used to register to XDS server events -type XdsEventRegisterArgs struct { - Name string `json:"name"` - ProjectID string `json:"filterProjectID"` -} - -// XdsEventFolderChange Folder change event structure -type XdsEventFolderChange struct { - Time string `json:"time"` - Type string `json:"type"` - Folder XdsFolderConfig `json:"folder"` -} - // EventCB Event emitter callback type EventCB func(privData interface{}, evtData interface{}) error @@ -203,12 +130,12 @@ func (xs *XdsServer) SetLoggerOutput(out io.Writer) { } // SendCommand Send a command to XDS Server -func (xs *XdsServer) SendCommand(cmd string, body []byte) (*http.Response, error) { +func (xs *XdsServer) SendCommand(cmd string, body []byte, res interface{}) error { url := cmd if !strings.HasPrefix("/", cmd) { url = "/" + cmd } - return xs.client.HTTPPostWithRes(url, string(body)) + return xs.client.Post(url, string(body), res) } // GetVersion Send Get request to retrieve XDS Server version @@ -217,12 +144,12 @@ func (xs *XdsServer) GetVersion(res interface{}) error { } // GetFolders Send GET request to get current folder configuration -func (xs *XdsServer) GetFolders(folders *[]XdsFolderConfig) error { +func (xs *XdsServer) GetFolders(folders *[]xsapiv1.FolderConfig) error { return xs.client.Get("/folders", folders) } // FolderAdd Send POST request to add a folder -func (xs *XdsServer) FolderAdd(fld *XdsFolderConfig, res interface{}) error { +func (xs *XdsServer) FolderAdd(fld *xsapiv1.FolderConfig, res interface{}) error { err := xs.client.Post("/folders", fld, res) if err != nil { return fmt.Errorf("FolderAdd error: %s", err.Error()) @@ -241,7 +168,7 @@ func (xs *XdsServer) FolderSync(id string) error { } // FolderUpdate Send PUT request to update a folder -func (xs *XdsServer) FolderUpdate(fld *XdsFolderConfig, resFld *XdsFolderConfig) error { +func (xs *XdsServer) FolderUpdate(fld *xsapiv1.FolderConfig, resFld *xsapiv1.FolderConfig) error { return xs.client.Put("/folders/"+fld.ID, fld, resFld) } @@ -325,7 +252,7 @@ func (xs *XdsServer) PassthroughPost(url string) { func (xs *XdsServer) EventRegister(evName string, id string) error { return xs.client.Post( "/events/register", - XdsEventRegisterArgs{ + xsapiv1.EventRegisterArgs{ Name: evName, ProjectID: id, }, @@ -345,10 +272,10 @@ func (xs *XdsServer) EventOn(evName string, privData interface{}, f EventCB) (uu // Register listener only the first time evn := evName - // FIXME: use generic type: data interface{} instead of data XdsEventFolderChange + // FIXME: use generic type: data interface{} instead of data xsapiv1.EventMsg var err error if evName == "event:folder-state-change" { - err = xs.ioSock.On(evn, func(data XdsEventFolderChange) error { + err = xs.ioSock.On(evn, func(data xsapiv1.EventMsg) error { xs.sockEventsLock.Lock() sEvts := make([]*caller, len(xs.sockEvents[evn])) copy(sEvts, xs.sockEvents[evn]) @@ -408,25 +335,25 @@ func (xs *XdsServer) EventOff(evName string, id uuid.UUID) error { } // ProjectToFolder Convert Project structure to Folder structure -func (xs *XdsServer) ProjectToFolder(pPrj apiv1.ProjectConfig) *XdsFolderConfig { +func (xs *XdsServer) ProjectToFolder(pPrj xaapiv1.ProjectConfig) *xsapiv1.FolderConfig { stID := "" - if pPrj.Type == XdsTypeCloudSync { + if pPrj.Type == xsapiv1.TypeCloudSync { stID, _ = xs.SThg.IDGet() } // TODO: limit ClientData size and gzip it (see https://golang.org/pkg/compress/gzip/) - fPrj := XdsFolderConfig{ + fPrj := xsapiv1.FolderConfig{ ID: pPrj.ID, Label: pPrj.Label, ClientPath: pPrj.ClientPath, - Type: XdsFolderType(pPrj.Type), + Type: xsapiv1.FolderType(pPrj.Type), Status: pPrj.Status, IsInSync: pPrj.IsInSync, DefaultSdk: pPrj.DefaultSdk, ClientData: pPrj.ClientData, - DataPathMap: XdsPathMapConfig{ + DataPathMap: xsapiv1.PathMapConfig{ ServerPath: pPrj.ServerPath, }, - DataCloudSync: XdsCloudSyncConfig{ + DataCloudSync: xsapiv1.CloudSyncConfig{ SyncThingID: stID, STLocIsInSync: pPrj.IsInSync, STLocStatus: pPrj.Status, @@ -439,36 +366,36 @@ func (xs *XdsServer) ProjectToFolder(pPrj apiv1.ProjectConfig) *XdsFolderConfig } // FolderToProject Convert Folder structure to Project structure -func (xs *XdsServer) FolderToProject(fPrj XdsFolderConfig) apiv1.ProjectConfig { +func (xs *XdsServer) FolderToProject(fPrj xsapiv1.FolderConfig) xaapiv1.ProjectConfig { inSync := fPrj.IsInSync sts := fPrj.Status - if fPrj.Type == XdsTypeCloudSync { + if fPrj.Type == xsapiv1.TypeCloudSync { inSync = fPrj.DataCloudSync.STSvrIsInSync && fPrj.DataCloudSync.STLocIsInSync sts = fPrj.DataCloudSync.STSvrStatus switch fPrj.DataCloudSync.STLocStatus { - case apiv1.StatusErrorConfig, apiv1.StatusDisable, apiv1.StatusPause: + case xaapiv1.StatusErrorConfig, xaapiv1.StatusDisable, xaapiv1.StatusPause: sts = fPrj.DataCloudSync.STLocStatus break - case apiv1.StatusSyncing: - if sts != apiv1.StatusErrorConfig && sts != apiv1.StatusDisable && sts != apiv1.StatusPause { - sts = apiv1.StatusSyncing + case xaapiv1.StatusSyncing: + if sts != xaapiv1.StatusErrorConfig && sts != xaapiv1.StatusDisable && sts != xaapiv1.StatusPause { + sts = xaapiv1.StatusSyncing } break - case apiv1.StatusEnable: + case xaapiv1.StatusEnable: // keep STSvrStatus break } } - pPrj := apiv1.ProjectConfig{ + pPrj := xaapiv1.ProjectConfig{ ID: fPrj.ID, ServerID: xs.ID, Label: fPrj.Label, ClientPath: fPrj.ClientPath, ServerPath: fPrj.DataPathMap.ServerPath, - Type: apiv1.ProjectType(fPrj.Type), + Type: xaapiv1.ProjectType(fPrj.Type), Status: sts, IsInSync: inSync, DefaultSdk: fPrj.DefaultSdk, @@ -523,7 +450,7 @@ func (xs *XdsServer) _reconnect() error { // Established HTTP and WS connection and retrieve XDSServer config func (xs *XdsServer) _connect(reConn bool) error { - xdsCfg := XdsServerConfig{} + xdsCfg := xsapiv1.APIConfig{} if err := xs.client.Get("/config", &xdsCfg); err != nil { xs.Connected = false if !reConn { @@ -532,12 +459,12 @@ func (xs *XdsServer) _connect(reConn bool) error { return err } - if reConn && xs.ID != xdsCfg.ID { - xs.Log.Warningf("Reconnected to server but ID differs: old=%s, new=%s", xs.ID, xdsCfg.ID) + if reConn && xs.ID != xdsCfg.ServerUID { + xs.Log.Warningf("Reconnected to server but ID differs: old=%s, new=%s", xs.ID, xdsCfg.ServerUID) } // Update local XDS config - xs.ID = xdsCfg.ID + xs.ID = xdsCfg.ServerUID xs.ServerConfig = &xdsCfg // Establish WS connection and register listen @@ -618,7 +545,7 @@ func (xs *XdsServer) _SocketConnect() error { // Send event to notify changes func (xs *XdsServer) _NotifyState() { - evSts := apiv1.ServerCfg{ + evSts := xaapiv1.ServerCfg{ ID: xs.ID, URL: xs.BaseURL, APIURL: xs.APIURL, @@ -626,7 +553,7 @@ func (xs *XdsServer) _NotifyState() { ConnRetry: xs.ConnRetry, Connected: xs.Connected, } - if err := xs.events.Emit(apiv1.EVTServerConfig, evSts, ""); err != nil { + if err := xs.events.Emit(xaapiv1.EVTServerConfig, evSts, ""); err != nil { xs.Log.Warningf("Cannot notify XdsServer state change: %v", err) } } diff --git a/lib/syncthing/stfolder.go b/lib/syncthing/stfolder.go index 196e3c7..2273f1a 100644 --- a/lib/syncthing/stfolder.go +++ b/lib/syncthing/stfolder.go @@ -20,7 +20,7 @@ type FolderChangeArg struct { // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database /* -func (s *SyncThing) FolderLoadFromStConfig(f *[]XdsFolderConfig) error { +func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error { defaultSdk := "" // cannot know which was the default sdk @@ -40,15 +40,15 @@ func (s *SyncThing) FolderLoadFromStConfig(f *[]XdsFolderConfig) error { } for _, stFld := range stCfg.Folders { - *f = append(*f, XdsFolderConfig{ + *f = append(*f, xsapiv1.FolderConfig{ ID: stFld.ID, Label: stFld.Label, ClientPath: strings.TrimRight(stFld.Path, "/"), - Type: XdsTypeCloudSync, + Type: xsapiv1.TypeCloudSync, Status: StatusDisable, DefaultSdk: defaultSdk, RootPath: "", - DataCloudSync: XdsCloudSyncConfig{SyncThingID: devID}, + DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID}, }) } diff --git a/lib/apiv1/config.go b/lib/xaapiv1/config.go similarity index 97% rename from lib/apiv1/config.go rename to lib/xaapiv1/config.go index f5c1fb5..ae853dd 100644 --- a/lib/apiv1/config.go +++ b/lib/xaapiv1/config.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 // APIConfig parameters (json format) of /config command type APIConfig struct { diff --git a/lib/apiv1/events.go b/lib/xaapiv1/events.go similarity index 90% rename from lib/apiv1/events.go rename to lib/xaapiv1/events.go index b2fda62..12c8cb2 100644 --- a/lib/apiv1/events.go +++ b/lib/xaapiv1/events.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 import ( "encoding/json" @@ -24,10 +24,10 @@ const ( // Supported Events type EVTAll = EventTypePrefix + "all" - EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type apiv1.ServerCfg - EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type apiv1.ProjectConfig - EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type apiv1.ProjectConfig - EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type apiv1.ProjectConfig + EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type xaapiv1.ServerCfg + EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type xaapiv1.ProjectConfig + EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type xaapiv1.ProjectConfig + EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig ) // EVTAllList List of all supported events diff --git a/lib/apiv1/exec.go b/lib/xaapiv1/exec.go similarity index 99% rename from lib/apiv1/exec.go rename to lib/xaapiv1/exec.go index 914ed79..b96be79 100644 --- a/lib/apiv1/exec.go +++ b/lib/xaapiv1/exec.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 type ( // ExecArgs JSON parameters of /exec command diff --git a/lib/apiv1/projects.go b/lib/xaapiv1/projects.go similarity index 98% rename from lib/apiv1/projects.go rename to lib/xaapiv1/projects.go index b1e64c8..51c5837 100644 --- a/lib/apiv1/projects.go +++ b/lib/xaapiv1/projects.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 // ProjectType definition type ProjectType string diff --git a/lib/apiv1/sdks.go b/lib/xaapiv1/sdks.go similarity index 94% rename from lib/apiv1/sdks.go rename to lib/xaapiv1/sdks.go index dfa44e2..63ef357 100644 --- a/lib/apiv1/sdks.go +++ b/lib/xaapiv1/sdks.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 // SDK Define a cross tool chain used to build application type SDK struct { diff --git a/lib/apiv1/version.go b/lib/xaapiv1/version.go similarity index 95% rename from lib/apiv1/version.go rename to lib/xaapiv1/version.go index 956f890..5fed4e2 100644 --- a/lib/apiv1/version.go +++ b/lib/xaapiv1/version.go @@ -1,4 +1,4 @@ -package apiv1 +package xaapiv1 // VersionData type VersionData struct { -- 2.16.6