Skip to content
This repository was archived by the owner on Jul 16, 2018. It is now read-only.

Commit fc381d8

Browse files
committed
Add Create Environment
1 parent 5fc7ce6 commit fc381d8

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

cmds/cruds.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ const (
2424
`
2525
)
2626

27+
func NewCmdCreate() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "create",
30+
Short: "Create a resource type",
31+
Long: longhelp,
32+
}
33+
return cmd
34+
}
35+
2736
func NewCmdDelete() *cobra.Command {
2837
cmd := &cobra.Command{
2938
Use: "delete",

cmds/environ.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919

2020
"strings"
2121

22+
"strconv"
23+
2224
"github.com/fabric8io/gofabric8/client"
2325
"github.com/fabric8io/gofabric8/util"
2426
"github.com/spf13/cobra"
@@ -69,6 +71,70 @@ func NewCmdGetEnviron(f *cmdutil.Factory) *cobra.Command {
6971
return cmd
7072
}
7173

74+
func NewCmdCreateEnviron(f *cmdutil.Factory) (cmd *cobra.Command) {
75+
cmd = &cobra.Command{
76+
Use: "environ",
77+
Short: "Create environment from fabric8-environments configmap",
78+
Long: "gofabric8 create environ environKey namespace=string order=int ...",
79+
Run: func(cmd *cobra.Command, args []string) {
80+
var ev EnvironmentData
81+
var yamlData []byte
82+
ev.Order = -1
83+
84+
for _, kv := range args {
85+
split := strings.Split(kv, "=")
86+
k := split[0]
87+
v := split[1]
88+
89+
if strings.ToLower(k) == "name" {
90+
ev.Name = strings.ToLower(v)
91+
} else if strings.ToLower(k) == "namespace" {
92+
ev.Namespace = v
93+
} else if strings.ToLower(k) == "order" {
94+
conv, err := strconv.Atoi(v)
95+
if err != nil {
96+
util.Errorf("Cannot use %s from %s as number\n", v, k)
97+
return
98+
}
99+
ev.Order = conv
100+
} else {
101+
util.Errorf("Unkown key: %s\n", k)
102+
return
103+
}
104+
}
105+
106+
if ev.Name == "" || ev.Namespace == "" || ev.Order == -1 {
107+
util.Error("missing some key=value\n")
108+
cmd.Help()
109+
return
110+
}
111+
112+
detectedNS, c, _ := getOpenShiftClient(f)
113+
yamlData, err := yaml.Marshal(&ev)
114+
if err != nil {
115+
util.Fatalf("Failed to marshal configmap fabric8-environments error: %v\ntemplate: %s", err, string(yamlData))
116+
}
117+
118+
selector, err := k8api.LabelSelectorAsSelector(
119+
&k8api.LabelSelector{MatchLabels: map[string]string{"kind": "environments"}})
120+
cmdutil.CheckErr(err)
121+
122+
cfgmaps, err := c.ConfigMaps(detectedNS).List(api.ListOptions{LabelSelector: selector})
123+
cmdutil.CheckErr(err)
124+
125+
cfgmap := &cfgmaps.Items[0] // TODO(chmou): can we have more than one cfgmap with kind=environments label?
126+
cfgmap.Data[ev.Name] = string(yamlData)
127+
128+
_, err = c.ConfigMaps(detectedNS).Update(cfgmap)
129+
cmdutil.CheckErr(err)
130+
131+
//cfgmap, err := c.ConfigMaps(detectedNS).Create()
132+
133+
},
134+
}
135+
return
136+
}
137+
72138
// NewCmdDeleteEnviron is a command to delete an environ using: gofabric8 delete environ abcd
73139
func NewCmdDeleteEnviron(f *cmdutil.Factory) *cobra.Command {
74140
cmd := &cobra.Command{

gofabric8.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func main() {
130130
cmds.AddCommand(getcmd)
131131
getcmd.AddCommand(commands.NewCmdGetEnviron(f))
132132

133+
createcmd := commands.NewCmdCreate()
134+
cmds.AddCommand(createcmd)
135+
createcmd.AddCommand(commands.NewCmdCreateEnviron(f))
136+
133137
deletecmd := commands.NewCmdDelete()
134138
cmds.AddCommand(deletecmd)
135139
deletecmd.AddCommand(commands.NewCmdDeleteCluster(f))

0 commit comments

Comments
 (0)