Support short project id name if not ambiguous.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 6 Nov 2017 09:19:06 +0000 (10:19 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 6 Nov 2017 13:49:43 +0000 (14:49 +0100)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
lib/agent/apiv1-projects.go
lib/agent/projects.go

index 89218ab..c835967 100644 (file)
@@ -15,7 +15,12 @@ func (s *APIService) getProjects(c *gin.Context) {
 
 // getProject returns a specific project configuration
 func (s *APIService) getProject(c *gin.Context) {
-       prj := s.projects.Get(c.Param("id"))
+       id, err := s.projects.ResolveID(c.Param("id"))
+       if err != nil {
+               common.APIError(c, err.Error())
+               return
+       }
+       prj := s.projects.Get(id)
        if prj == nil {
                common.APIError(c, "Invalid id")
                return
@@ -45,22 +50,30 @@ func (s *APIService) addProject(c *gin.Context) {
 
 // syncProject force synchronization of project files
 func (s *APIService) syncProject(c *gin.Context) {
-       id := c.Param("id")
+       id, err := s.projects.ResolveID(c.Param("id"))
+       if err != nil {
+               common.APIError(c, err.Error())
+               return
+       }
 
        s.Log.Debugln("Sync project id: ", id)
 
-       err := s.projects.ForceSync(id)
+       err = s.projects.ForceSync(id)
        if err != nil {
                common.APIError(c, err.Error())
                return
        }
 
-       c.JSON(http.StatusOK, "")
+       c.JSON(http.StatusOK, nil)
 }
 
 // delProject deletes project from server config
 func (s *APIService) delProject(c *gin.Context) {
-       id := c.Param("id")
+       id, err := s.projects.ResolveID(c.Param("id"))
+       if err != nil {
+               common.APIError(c, err.Error())
+               return
+       }
 
        s.Log.Debugln("Delete project id ", id)
 
index 6804d35..f089882 100644 (file)
@@ -3,6 +3,7 @@ package agent
 import (
        "fmt"
        "log"
+       "strings"
        "time"
 
        "github.com/iotbzh/xds-agent/lib/apiv1"
@@ -66,6 +67,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 == "" {
@@ -204,7 +226,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()