Skip to content

Commit

Permalink
Do not allow the '.' as the node name
Browse files Browse the repository at this point in the history
This commit sort the result from `congo list` and restrict the node
name which start with '.'
  • Loading branch information
chenglch committed Nov 13, 2017
1 parent 82f44cf commit c33eeb9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
49 changes: 48 additions & 1 deletion console/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"time"
"sort"
)

var (
Expand Down Expand Up @@ -52,6 +53,26 @@ func KeyValueToMap(value string, sep string) (map[string]interface{}, error) {
return m, nil
}

type nodeHost struct {
name string
host string
}
type nodeHostSlice []nodeHost
func (a nodeHostSlice) Len() int {
return len(a)
}

func (a nodeHostSlice) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

func (a nodeHostSlice) Less(i, j int) bool {
if strings.Compare(a[i].name, a[j].name) == -1 {
return true
}
return false
}

type CongoCli struct {
baseUrl string
cmd *cobra.Command
Expand Down Expand Up @@ -99,9 +120,15 @@ func (c *CongoCli) list(cmd *cobra.Command, args []string) {
fmt.Printf("Could not find any record.\n")
os.Exit(0)
}
tempNodes := make([]nodeHost, 0, len(nodes))
for _, v := range nodes {
node := v.(map[string]interface{})
fmt.Printf("%s (host: %s)\n", node["name"], node["host"])
tempNodes = append(tempNodes, nodeHost{name:node["name"].(string),
host: node["host"].(string)})
}
sort.Sort(nodeHostSlice(tempNodes))
for _, v := range tempNodes {
fmt.Printf("%s (host: %s)\n", v.name, v.host)
}
}

Expand All @@ -121,6 +148,10 @@ func (c *CongoCli) show(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Usage: congo show <node> \n")
os.Exit(1)
}
if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){
fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n")
os.Exit(1)
}
ret, err := congo.Show(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get resources detail, %s\n", err.Error())
Expand All @@ -146,6 +177,10 @@ func (c *CongoCli) logging(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Usage: congo logging <node> on/off \n")
os.Exit(1)
}
if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){
fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n")
os.Exit(1)
}
if args[1] != "on" && args[1] != "off" {
fmt.Fprintf(os.Stderr, "Usage: congo logging <node> on/off \n")
os.Exit(1)
Expand Down Expand Up @@ -173,6 +208,10 @@ func (c *CongoCli) delete(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Usage: congo delete <node>\n")
os.Exit(1)
}
if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){
fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n")
os.Exit(1)
}
_, err := congo.Delete(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
Expand Down Expand Up @@ -200,6 +239,10 @@ func (c *CongoCli) create(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Usage: congo create <node> driver=ssh ondemand=true --param key=val,key=val\n")
os.Exit(1)
}
if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){
fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n")
os.Exit(1)
}
attribs, err := KeyValueArrayToMap(args[1:], "=")
if err != nil {
fmt.Fprintf(os.Stderr, "Parse failed, err=%s\n", err.Error())
Expand Down Expand Up @@ -234,6 +277,10 @@ func (c *CongoCli) console(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Usage: congo console <node>\n")
os.Exit(1)
}
if strings.HasPrefix(".", args[0]) || strings.HasPrefix("/", args[0]){
fmt.Fprintf(os.Stderr, "Error: node name could not start with '.' or '/'\n")
os.Exit(1)
}
retry := true
common.NewTaskManager(100, 16)
for retry {
Expand Down
4 changes: 0 additions & 4 deletions console/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,7 @@ func (m *NodeManager) ShowNode(name string) (*Node, int, string) {
return nil, http.StatusBadRequest, fmt.Sprintf("The node %s is not exist.", name)
}
node = nodeManager.Nodes[name]
if err := node.RequireLock(true); err != nil {
return nil, http.StatusConflict, err.Error()
}
node.State = STATUS_MAP[node.GetStatus()]
node.Release(true)
} else {
nodeWithHost := m.stor.ListNodeWithHost()
if nodeWithHost == nil {
Expand Down

0 comments on commit c33eeb9

Please sign in to comment.