Skip to content

Commit ef74a43

Browse files
author
Joshua Anderson
committed
feat(client-go): add tags endpoint
1 parent ba24426 commit ef74a43

3 files changed

Lines changed: 246 additions & 0 deletions

File tree

client-go/cmd/tags.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/deis/deis/pkg/prettyprint"
8+
9+
"github.com/deis/deis/client-go/controller/api"
10+
"github.com/deis/deis/client-go/controller/models/config"
11+
)
12+
13+
// TagsList lists an app's tags.
14+
func TagsList(appID string) error {
15+
c, appID, err := load(appID)
16+
17+
if err != nil {
18+
return err
19+
}
20+
21+
config, err := config.List(c, appID)
22+
23+
fmt.Printf("=== %s Tags\n", appID)
24+
25+
tagMap := make(map[string]string)
26+
27+
for key, value := range config.Tags {
28+
tagMap[key] = value.(string)
29+
}
30+
31+
fmt.Print(prettyprint.PrettyTabs(tagMap, 5))
32+
33+
return nil
34+
}
35+
36+
// TagsSet sets an app's tags.
37+
func TagsSet(appID string, tags []string) error {
38+
c, appID, err := load(appID)
39+
40+
if err != nil {
41+
return err
42+
}
43+
44+
tagsMap := parseTags(tags)
45+
46+
fmt.Print("Applying tags... ")
47+
48+
quit := progress()
49+
configObj := api.Config{}
50+
configObj.Tags = tagsMap
51+
52+
_, err = config.Set(c, appID, configObj)
53+
54+
quit <- true
55+
<-quit
56+
57+
if err != nil {
58+
return err
59+
}
60+
61+
fmt.Print("done\n\n")
62+
63+
return TagsList(appID)
64+
}
65+
66+
// TagsUnset removes an app's tags.
67+
func TagsUnset(appID string, tags []string) error {
68+
c, appID, err := load(appID)
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
fmt.Print("Applying tags... ")
75+
76+
quit := progress()
77+
78+
configObj := api.Config{}
79+
80+
tagsMap := make(map[string]interface{})
81+
82+
for _, tag := range tags {
83+
tagsMap[tag] = nil
84+
}
85+
86+
configObj.Tags = tagsMap
87+
88+
_, err = config.Set(c, appID, configObj)
89+
90+
quit <- true
91+
<-quit
92+
93+
if err != nil {
94+
return err
95+
}
96+
97+
fmt.Print("done\n\n")
98+
99+
return TagsList(appID)
100+
}
101+
102+
func parseTags(tags []string) map[string]interface{} {
103+
tagMap := make(map[string]interface{})
104+
105+
for _, tag := range tags {
106+
key, value, err := parseTag(tag)
107+
108+
if err != nil {
109+
fmt.Println(err)
110+
continue
111+
}
112+
113+
tagMap[key] = value
114+
}
115+
116+
return tagMap
117+
}
118+
119+
func parseTag(tag string) (string, string, error) {
120+
parts := strings.Split(tag, "=")
121+
122+
if len(parts) != 2 {
123+
return "", "", fmt.Errorf(`%s is invalid, Must be in format key=value
124+
Examples: rack=1 evironment=production`, tag)
125+
}
126+
127+
return parts[0], parts[1], nil
128+
}

client-go/deis.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Use 'git push deis master' to deploy to an application.
8989
err = parser.Domains(argv)
9090
case "builds":
9191
err = parser.Builds(argv)
92+
case "tags":
93+
err = parser.Tags(argv)
9294
case "keys":
9395
err = parser.Keys(argv)
9496
case "users":

client-go/parser/tags.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package parser
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/deis/deis/client-go/cmd"
7+
docopt "github.com/docopt/docopt-go"
8+
)
9+
10+
// Tags routes tags commands to their specific function
11+
func Tags(argv []string) error {
12+
usage := `
13+
Valid commands for tags:
14+
15+
tags:list list tags for an app
16+
tags:set set tags for an app
17+
tags:unset unset tags for an app
18+
19+
Use 'deis help [command]' to learn more.
20+
`
21+
if len(argv) < 2 {
22+
return tagsList([]string{"tags:list"})
23+
}
24+
25+
switch argv[1] {
26+
case "list":
27+
return tagsList(combineCommand(argv))
28+
case "set":
29+
return tagsSet(combineCommand(argv))
30+
case "unset":
31+
return tagsUnset(combineCommand(argv))
32+
case "--help":
33+
fmt.Print(usage)
34+
return nil
35+
default:
36+
PrintUsage()
37+
return nil
38+
}
39+
}
40+
41+
func tagsList(argv []string) error {
42+
usage := `
43+
Lists tags for an application.
44+
45+
Usage: deis tags:list [options]
46+
47+
Options:
48+
-a --app=<app>
49+
the uniquely identifiable name of the application.
50+
`
51+
52+
args, err := docopt.Parse(usage, argv, true, "", false, true)
53+
54+
if err != nil {
55+
return err
56+
}
57+
58+
return cmd.TagsList(safeGetValue(args, "--app"))
59+
}
60+
61+
func tagsSet(argv []string) error {
62+
usage := `
63+
Sets tags for an application.
64+
65+
A tag is a key/value pair used to tag an application's containers and is passed to the
66+
scheduler. This is often used to restrict workloads to specific hosts matching the
67+
scheduler-configured metadata.
68+
69+
Usage: deis tags:set [options] <key>=<value>...
70+
71+
Arguments:
72+
<key> the tag key, for example: "environ" or "rack"
73+
<value> the tag value, for example: "prod" or "1"
74+
75+
Options:
76+
-a --app=<app>
77+
the uniquely identifiable name for the application.
78+
`
79+
80+
args, err := docopt.Parse(usage, argv, true, "", false, true)
81+
82+
if err != nil {
83+
return err
84+
}
85+
86+
app := safeGetValue(args, "--app")
87+
tags := args["<key>=<value>"].([]string)
88+
89+
return cmd.TagsSet(app, tags)
90+
}
91+
92+
func tagsUnset(argv []string) error {
93+
usage := `
94+
Unsets tags for an application.
95+
96+
Usage: deis tags:unset [options] <key>...
97+
98+
Arguments:
99+
<key> the tag key to unset, for example: "environ" or "rack"
100+
101+
Options:
102+
-a --app=<app>
103+
the uniquely identifiable name for the application.
104+
`
105+
106+
args, err := docopt.Parse(usage, argv, true, "", false, true)
107+
108+
if err != nil {
109+
return err
110+
}
111+
112+
app := safeGetValue(args, "--app")
113+
tags := args["<key>"].([]string)
114+
115+
return cmd.TagsUnset(app, tags)
116+
}

0 commit comments

Comments
 (0)