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

Commit

Permalink
Add Create Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
chmouel committed Jun 27, 2017
1 parent 5fc7ce6 commit fc381d8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmds/cruds.go
Expand Up @@ -24,6 +24,15 @@ const (
`
)

func NewCmdCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a resource type",
Long: longhelp,
}
return cmd
}

func NewCmdDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Expand Down
66 changes: 66 additions & 0 deletions cmds/environ.go
Expand Up @@ -19,6 +19,8 @@ import (

"strings"

"strconv"

"github.com/fabric8io/gofabric8/client"
"github.com/fabric8io/gofabric8/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,6 +71,70 @@ func NewCmdGetEnviron(f *cmdutil.Factory) *cobra.Command {
return cmd
}

func NewCmdCreateEnviron(f *cmdutil.Factory) (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "environ",
Short: "Create environment from fabric8-environments configmap",
Long: "gofabric8 create environ environKey namespace=string order=int ...",
Run: func(cmd *cobra.Command, args []string) {
var ev EnvironmentData
var yamlData []byte
ev.Order = -1

for _, kv := range args {
split := strings.Split(kv, "=")
k := split[0]
v := split[1]

if strings.ToLower(k) == "name" {
ev.Name = strings.ToLower(v)
} else if strings.ToLower(k) == "namespace" {
ev.Namespace = v
} else if strings.ToLower(k) == "order" {
conv, err := strconv.Atoi(v)
if err != nil {
util.Errorf("Cannot use %s from %s as number\n", v, k)
return
}
ev.Order = conv
} else {
util.Errorf("Unkown key: %s\n", k)
return
}
}

if ev.Name == "" || ev.Namespace == "" || ev.Order == -1 {
util.Error("missing some key=value\n")
cmd.Help()
return
}

detectedNS, c, _ := getOpenShiftClient(f)
yamlData, err := yaml.Marshal(&ev)
if err != nil {
util.Fatalf("Failed to marshal configmap fabric8-environments error: %v\ntemplate: %s", err, string(yamlData))
}

selector, err := k8api.LabelSelectorAsSelector(
&k8api.LabelSelector{MatchLabels: map[string]string{"kind": "environments"}})
cmdutil.CheckErr(err)

cfgmaps, err := c.ConfigMaps(detectedNS).List(api.ListOptions{LabelSelector: selector})
cmdutil.CheckErr(err)

cfgmap := &cfgmaps.Items[0] // TODO(chmou): can we have more than one cfgmap with kind=environments label?
cfgmap.Data[ev.Name] = string(yamlData)

_, err = c.ConfigMaps(detectedNS).Update(cfgmap)
cmdutil.CheckErr(err)

//cfgmap, err := c.ConfigMaps(detectedNS).Create()

},
}
return
}

// NewCmdDeleteEnviron is a command to delete an environ using: gofabric8 delete environ abcd
func NewCmdDeleteEnviron(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Expand Down
4 changes: 4 additions & 0 deletions gofabric8.go
Expand Up @@ -130,6 +130,10 @@ func main() {
cmds.AddCommand(getcmd)
getcmd.AddCommand(commands.NewCmdGetEnviron(f))

createcmd := commands.NewCmdCreate()
cmds.AddCommand(createcmd)
createcmd.AddCommand(commands.NewCmdCreateEnviron(f))

deletecmd := commands.NewCmdDelete()
cmds.AddCommand(deletecmd)
deletecmd.AddCommand(commands.NewCmdDeleteCluster(f))
Expand Down

0 comments on commit fc381d8

Please sign in to comment.