Skip to content

Commit

Permalink
Update CLI for edge router policy changes. Pull in 0.7 edge changes. F…
Browse files Browse the repository at this point in the history
…ixes GH-9.
  • Loading branch information
plorenz committed Dec 11, 2019
1 parent c9b1fd6 commit e5fa340
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 256 deletions.
2 changes: 1 addition & 1 deletion common/version/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.1
0.7.0
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module github.com/netfoundry/ziti-cmd

go 1.13

// replace github.com/netfoundry/ziti-foundation => ../ziti-foundation
// replace github.com/netfoundry/ziti-fabric => ../ziti-fabric
//replace github.com/netfoundry/ziti-sdk-golang => ../ziti-sdk-golang
// replace github.com/netfoundry/ziti-edge => ../ziti-edge

require (
github.com/Jeffail/gabs v1.4.0
github.com/MakeNowJust/heredoc v1.0.0
Expand All @@ -16,10 +21,10 @@ require (
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19
github.com/michaelquigley/pfxlog v0.0.0-20190813191113-2be43bd0dccc
github.com/netfoundry/ziti-edge v0.0.0-20191209152826-1b734f68ffb4
github.com/netfoundry/ziti-fabric v0.0.0-20191203174113-af4eb6e60864
github.com/netfoundry/ziti-foundation v0.0.0-20191203170231-5c1204e0e65d
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209154431-2db321362451
github.com/netfoundry/ziti-edge v0.0.0-20191211205214-958bacc85eaf
github.com/netfoundry/ziti-fabric v0.0.0-20191209202137-4205e73d614e
github.com/netfoundry/ziti-foundation v0.0.0-20191209180049-dd15781cc4aa
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209205522-dd68e7c5ed49
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.8.1
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,21 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/netfoundry/ziti-edge v0.0.0-20191209152826-1b734f68ffb4 h1:AB+YmEEPrUEzll+EvVrjiYXsfjlxGuFPSMcX79UrwLY=
github.com/netfoundry/ziti-edge v0.0.0-20191209152826-1b734f68ffb4/go.mod h1:b9NqxGsJSVKqW8kurADYrvPrC6OfJZAIsaeMk7v5bSg=
github.com/netfoundry/ziti-edge v0.0.0-20191211205214-958bacc85eaf h1:ZTmtk6HdgWnJCQRsA5hrX4RNCgOHJsveEo0nCNHu/MU=
github.com/netfoundry/ziti-edge v0.0.0-20191211205214-958bacc85eaf/go.mod h1:iYyr7AlV7ocqajWxo+pFcQFiK/y5mJ7fzmiv365i/lY=
github.com/netfoundry/ziti-fabric v0.0.0-20191203174113-af4eb6e60864 h1:GGMpVeTIqeN+ZFC2MJX3qQNX+V9Zkda9lquhw1Toh+k=
github.com/netfoundry/ziti-fabric v0.0.0-20191203174113-af4eb6e60864/go.mod h1:ktqE4LYK1Z4MGBpP29ylnUvEsSNwk7jXBfq2K/kq4nw=
github.com/netfoundry/ziti-fabric v0.0.0-20191209202137-4205e73d614e h1:EdTaD5+hjMJpKRQW6yelbeqzrnSfpvXPb45XA0zUCe4=
github.com/netfoundry/ziti-fabric v0.0.0-20191209202137-4205e73d614e/go.mod h1:vJc7LEWv4KWvWkLqpWekH6aNe9uK5z0BrQ8NARlY21Q=
github.com/netfoundry/ziti-foundation v0.0.0-20191203170231-5c1204e0e65d h1:V6VZeV8JVUipJhZgDUNUhaJ2L6bqmosAf+SNUj6C1f0=
github.com/netfoundry/ziti-foundation v0.0.0-20191203170231-5c1204e0e65d/go.mod h1:27kvN9RZk7cpt2xba6owuXktNphq5hPK62CAiKnIS10=
github.com/netfoundry/ziti-foundation v0.0.0-20191209180049-dd15781cc4aa h1:6Ekd22eDTyTskrvQj2m+uTJ8rf3kDq1vrDWVLwF+oC0=
github.com/netfoundry/ziti-foundation v0.0.0-20191209180049-dd15781cc4aa/go.mod h1:27kvN9RZk7cpt2xba6owuXktNphq5hPK62CAiKnIS10=
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191206202611-43555d2de4f9/go.mod h1:WJ49orNIo3R813cG4hfB4mww8NU4mCzQBsKQldqs/ZQ=
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209154431-2db321362451 h1:6prHYMlluujLMhwhB+M4ZcXxIp4B7T6YPImKsMFRJXU=
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209154431-2db321362451/go.mod h1:WJ49orNIo3R813cG4hfB4mww8NU4mCzQBsKQldqs/ZQ=
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209205522-dd68e7c5ed49 h1:40QUJE6ow9GzW8N0kPaJ1ptYmt4axkFD7982IyVZSZQ=
github.com/netfoundry/ziti-sdk-golang v0.0.0-20191209205522-dd68e7c5ed49/go.mod h1:HaMjEeFnxNo91ew0RbWJ5IZRS04kD4QliFboTVFcLuQ=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oleiade/reflections v1.0.0 h1:0ir4pc6v8/PJ0yw5AEtMddfXpWBXg9cnG7SgSoJuCgY=
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
Expand Down
61 changes: 31 additions & 30 deletions ziti/cmd/ziti/cmd/edge_controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,38 @@ package edge_controller
import (
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"strings"
)

func mapNameToID(entityType string, val string) (string, error) {
// If we can parse it as a UUID, treat it as such
_, err := uuid.Parse(val)
if err == nil {
return val, nil
}

// Allow UUID formatted names to be recognized with a name: prefix
name := strings.TrimPrefix(val, "name:")
list, err := filterEntitiesOfType(entityType, fmt.Sprintf("name=\"%s\"", name), false)
if err != nil {
return "", err
}

if len(list) < 1 {
return "", errors.Errorf("no %v found for name %v", entityType, val)
}

if len(list) > 1 {
return "", errors.Errorf("multiple %v found for name %v, please use id instead", entityType, val)
}

entity := list[0]
entityId, _ := entity.Path("id").Data().(string)
fmt.Printf("Found %v with id %v for name %v\n", entityType, entityId, val)
return entityId, nil
}

func mapNamesToIDs(entityType string, list ...string) ([]string, error) {
var result []string
for _, val := range list {
Expand Down Expand Up @@ -49,37 +78,9 @@ func mapNamesToIDs(entityType string, list ...string) ([]string, error) {
}

func mapIdentityNameToID(nameOrId string) (string, error) {
ids, err := mapNamesToIDs("identities", nameOrId)

if err != nil {
return "", err
}

if len(ids) == 0 {
return "", fmt.Errorf("invalid identity name: %s", nameOrId)
}

if len(ids) > 1 {
return "", fmt.Errorf("too many identities with name: %s, use id", nameOrId)
}

return ids[0], nil
return mapNameToID("identities", nameOrId)
}

func mapCaNameToID(nameOrId string) (string, error) {
ids, err := mapNamesToIDs("cas", nameOrId)

if err != nil {
return "", err
}

if len(ids) == 0 {
return "", fmt.Errorf("invalid CA name: %s", nameOrId)
}

if len(ids) > 1 {
return "", fmt.Errorf("too many CAs with name: %s, use id", nameOrId)
}

return ids[0], nil
return mapNameToID("cas", nameOrId)
}
6 changes: 3 additions & 3 deletions ziti/cmd/ziti/cmd/edge_controller/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package edge_controller

import (
"fmt"
"github.com/Jeffail/gabs"
cmdutil "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/factory"
cmdhelper "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers"
"github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/util"
"fmt"
"github.com/Jeffail/gabs"
"github.com/spf13/cobra"
"io"
)
Expand All @@ -39,8 +39,8 @@ func newCreateCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Com
}

cmd.AddCommand(newCreateAppWanCmd(f, out, errOut))
cmd.AddCommand(newCreateClusterCmd(f, out, errOut))
cmd.AddCommand(newCreateEdgeRouterCmd(f, out, errOut))
cmd.AddCommand(newCreateEdgeRouterPolicyCmd(f, out, errOut))
cmd.AddCommand(newCreateIdentityCmd(f, out, errOut))
cmd.AddCommand(newCreateServiceCmd(f, out, errOut))
cmd.AddCommand(newCreateAuthenticatorCmd(f, out, errOut))
Expand Down
76 changes: 0 additions & 76 deletions ziti/cmd/ziti/cmd/edge_controller/create_cluster.go

This file was deleted.

37 changes: 8 additions & 29 deletions ziti/cmd/ziti/cmd/edge_controller/create_edge_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
package edge_controller

import (
"fmt"
"github.com/Jeffail/gabs"
"github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/common"
cmdutil "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/factory"
cmdhelper "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers"
"fmt"
"github.com/Jeffail/gabs"
"github.com/spf13/cobra"
"io"
"io/ioutil"
)

type createEdgeRouterOptions struct {
commonOptions
jwtOutputFile string
roleAttributes []string
jwtOutputFile string
}

func newCreateEdgeRouterCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
Expand All @@ -40,10 +41,11 @@ func newCreateEdgeRouterCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer)
}

cmd := &cobra.Command{
Use: "edge-router <name> [cluster]?",
Use: "edge-router <name>",
Aliases: []string{"gateway"},
Short: "creates an edge router managed by the Ziti Edge Controller",
Long: "creates an edge router managed by the Ziti Edge Controller",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
Expand All @@ -55,6 +57,7 @@ func newCreateEdgeRouterCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer)

// allow interspersing positional args and flags
cmd.Flags().SetInterspersed(true)
cmd.Flags().StringSliceVarP(&options.roleAttributes, "role-attributes", "r", nil, "Role attributes of the new edge router")
cmd.Flags().BoolVarP(&options.OutputJSONResponse, "output-json", "j", false, "Output the full JSON response from the Ziti Edge Controller")
cmd.Flags().StringVarP(&options.jwtOutputFile, "jwt-output-file", "o", "", "File to which to output the JWT used for enrolling the edge router")
return cmd
Expand All @@ -64,18 +67,7 @@ func newCreateEdgeRouterCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer)
func runCreateEdgeRouter(o *createEdgeRouterOptions) error {
routerData := gabs.New()
setJSONValue(routerData, o.Args[0], "name")
if len(o.Args) > 1 {
clusterIds, err := mapNamesToIDs("clusters", o.Args[1])
if err != nil {
return err
}
if len(clusterIds) == 0 {
return fmt.Errorf("cluster not found by id/name: %v", o.Args[1])
}
setJSONValue(routerData, clusterIds[0], "clusterId")
} else {
setJSONValue(routerData, getFirstCluster(o), "clusterId")
}
setJSONValue(routerData, o.roleAttributes, "roleAttributes")

result, err := createEntityOfType("edge-routers", routerData.String(), &o.commonOptions)

Expand Down Expand Up @@ -133,16 +125,3 @@ func getEdgeRouterJwt(o *createEdgeRouterOptions, id string) error {

return err
}

func getFirstCluster(o *createEdgeRouterOptions) string {
clusterList, err := listEntitiesOfType("clusters", &o.commonOptions)
if err != nil {
panic(err)
}

if len(clusterList) == 0 {
panic("no clusters available. please create cluster before creating a service.")
}

return clusterList[0].Path("id").Data().(string)
}
Loading

0 comments on commit e5fa340

Please sign in to comment.