New dashboard improvements.
[src/xds/xds-agent.git] / lib / agent / projects.go
index 6804d35..966c231 100644 (file)
@@ -3,8 +3,10 @@ package agent
 import (
        "fmt"
        "log"
+       "strings"
        "time"
 
+       "github.com/franciscocpg/reflectme"
        "github.com/iotbzh/xds-agent/lib/apiv1"
        "github.com/iotbzh/xds-agent/lib/syncthing"
        "github.com/syncthing/syncthing/lib/sync"
@@ -66,6 +68,27 @@ func (p *Projects) Init(server *XdsServer) error {
        return nil
 }
 
+// ResolveID Complete a Project ID (helper for user that can use partial ID value)
+func (p *Projects) ResolveID(id string) (string, error) {
+       if id == "" {
+               return "", nil
+       }
+
+       match := []string{}
+       for iid := range p.projects {
+               if strings.HasPrefix(iid, id) {
+                       match = append(match, iid)
+               }
+       }
+
+       if len(match) == 1 {
+               return match[0], nil
+       } else if len(match) == 0 {
+               return id, fmt.Errorf("Unknown id")
+       }
+       return id, fmt.Errorf("Multiple IDs found with provided prefix: " + id)
+}
+
 // Get returns the folder config or nil if not existing
 func (p *Projects) Get(id string) *IPROJECT {
        if id == "" {
@@ -97,14 +120,14 @@ func (p *Projects) GetProjectArrUnsafe() []apiv1.ProjectConfig {
 }
 
 // Add adds a new folder
-func (p *Projects) Add(newF apiv1.ProjectConfig) (*apiv1.ProjectConfig, error) {
+func (p *Projects) Add(newF apiv1.ProjectConfig, fromSid string) (*apiv1.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); err != nil {
+       if err := p.events.Emit(apiv1.EVTProjectAdd, *prj, fromSid); err != nil {
                p.Log.Warningf("Cannot notify project deletion: %v", err)
        }
 
@@ -168,7 +191,7 @@ func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial b
                }
        } else {
                // Just update project config
-               if newPrj, err = fld.UpdateProject(newF); err != nil {
+               if newPrj, err = fld.Setup(newF); err != nil {
                        newF.Status = apiv1.StatusErrorConfig
                        log.Printf("ERROR Updating project: %v\n", err)
                        return newPrj, err
@@ -195,7 +218,7 @@ func (p *Projects) createUpdate(newF apiv1.ProjectConfig, create bool, initial b
 }
 
 // Delete deletes a specific folder
-func (p *Projects) Delete(id string) (apiv1.ProjectConfig, error) {
+func (p *Projects) Delete(id, fromSid string) (apiv1.ProjectConfig, error) {
        var err error
 
        pjMutex.Lock()
@@ -204,7 +227,7 @@ func (p *Projects) Delete(id string) (apiv1.ProjectConfig, error) {
        fld := apiv1.ProjectConfig{}
        fc, exist := p.projects[id]
        if !exist {
-               return fld, fmt.Errorf("unknown id")
+               return fld, fmt.Errorf("Unknown id")
        }
 
        prj := (*fc).GetProject()
@@ -216,7 +239,7 @@ func (p *Projects) Delete(id string) (apiv1.ProjectConfig, error) {
        delete(p.projects, id)
 
        // Notify client with event
-       if err := p.events.Emit(apiv1.EVTProjectDelete, *prj); err != nil {
+       if err := p.events.Emit(apiv1.EVTProjectDelete, *prj, fromSid); err != nil {
                p.Log.Warningf("Cannot notify project deletion: %v", err)
        }
 
@@ -240,3 +263,50 @@ func (p *Projects) IsProjectInSync(id string) (bool, error) {
        }
        return (*fc).IsInSync()
 }
+
+// Update Update some field of a project
+func (p *Projects) Update(id string, prj apiv1.ProjectConfig, fromSid string) (*apiv1.ProjectConfig, error) {
+
+       pjMutex.Lock()
+       defer pjMutex.Unlock()
+
+       fc, exist := p.projects[id]
+       if !exist {
+               return nil, fmt.Errorf("Unknown id")
+       }
+
+       // Copy current in a new object to change nothing in case of an error rises
+       newFld := apiv1.ProjectConfig{}
+       reflectme.Copy((*fc).GetProject(), &newFld)
+
+       // Only update some fields
+       dirty := false
+       for _, fieldName := range apiv1.ProjectConfigUpdatableFields {
+               valNew, err := reflectme.GetField(prj, fieldName)
+               if err == nil {
+                       valCur, err := reflectme.GetField(newFld, fieldName)
+                       if err == nil && valNew != valCur {
+                               err = reflectme.SetField(&newFld, fieldName, valNew)
+                               if err != nil {
+                                       return nil, err
+                               }
+                               dirty = true
+                       }
+               }
+       }
+
+       if !dirty {
+               return &newFld, nil
+       }
+
+       upPrj, err := (*fc).Update(newFld)
+       if err != nil {
+               return nil, err
+       }
+
+       // Notify client with event
+       if err := p.events.Emit(apiv1.EVTProjectChange, *upPrj, fromSid); err != nil {
+               p.Log.Warningf("Cannot notify project change: %v", err)
+       }
+       return upPrj, err
+}