diff --git a/go.mod b/go.mod index be5367b97..a941796d5 100644 --- a/go.mod +++ b/go.mod @@ -21,9 +21,9 @@ 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-20200109210731-a1e09cc7638e + github.com/netfoundry/ziti-edge v0.0.0-20200113155507-fb8be7039465 github.com/netfoundry/ziti-fabric v0.0.0-20200108200335-8a3b0b19d2e6 - github.com/netfoundry/ziti-foundation v0.0.0-20200108154541-e614e4e75acd + github.com/netfoundry/ziti-foundation v0.0.0-20200110170009-41bb4fd29b11 github.com/netfoundry/ziti-sdk-golang v0.0.0-20200108160734-8bc089148c01 github.com/pborman/uuid v1.2.0 github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum index 1ae7d2608..c8503192a 100644 --- a/go.sum +++ b/go.sum @@ -218,16 +218,14 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/netfoundry/ziti-edge v0.0.0-20200109190215-a0024ae956ef h1:u1AAguvXj1cnsSq3qexSijcC+hfV/ShkYncONKct6wk= -github.com/netfoundry/ziti-edge v0.0.0-20200109190215-a0024ae956ef/go.mod h1:Z56jhag4epkWmt1SarB78ZIZaZWAZomZMxeSac26KnU= -github.com/netfoundry/ziti-edge v0.0.0-20200109210731-a1e09cc7638e h1:tl2stADLCR6+51rnyJHGztMZA4vfPHk+03PMd99lYlY= -github.com/netfoundry/ziti-edge v0.0.0-20200109210731-a1e09cc7638e/go.mod h1:Z56jhag4epkWmt1SarB78ZIZaZWAZomZMxeSac26KnU= +github.com/netfoundry/ziti-edge v0.0.0-20200113155507-fb8be7039465 h1:6l9+0zsLD+EB0chcsgxFKbN/QeQLfHM8o4OF9pFLCb8= +github.com/netfoundry/ziti-edge v0.0.0-20200113155507-fb8be7039465/go.mod h1:sfC4qwBoxJ2ffr3+y1QlCh+nToDeRQ5q17fxNj20I1c= github.com/netfoundry/ziti-fabric v0.0.0-20200108200335-8a3b0b19d2e6 h1:zR95MZtLIbuUqr/5LNj1unXuw0gz5WBTlWTv9pJ7+Ow= github.com/netfoundry/ziti-fabric v0.0.0-20200108200335-8a3b0b19d2e6/go.mod h1:pj5sWlqvzI6uY8x/qsJB9tgqjXqDfZa2HomPxKMurVc= github.com/netfoundry/ziti-foundation v0.0.0-20200107143132-d54269e96d45 h1:ZZxJIqJS/jto/TC8XB8Oo3CqFqERDYAH6wDU+Zg9Vlw= github.com/netfoundry/ziti-foundation v0.0.0-20200107143132-d54269e96d45/go.mod h1:27kvN9RZk7cpt2xba6owuXktNphq5hPK62CAiKnIS10= -github.com/netfoundry/ziti-foundation v0.0.0-20200108154541-e614e4e75acd h1:DiGZT6mBjt5S38v8PZZsNin208sbJZsEdYmYYkkgIJA= -github.com/netfoundry/ziti-foundation v0.0.0-20200108154541-e614e4e75acd/go.mod h1:27kvN9RZk7cpt2xba6owuXktNphq5hPK62CAiKnIS10= +github.com/netfoundry/ziti-foundation v0.0.0-20200110170009-41bb4fd29b11 h1:4MmXiSx63DVhx9/y29qNZqHrJXG32HL9NYanmits9HY= +github.com/netfoundry/ziti-foundation v0.0.0-20200110170009-41bb4fd29b11/go.mod h1:27kvN9RZk7cpt2xba6owuXktNphq5hPK62CAiKnIS10= github.com/netfoundry/ziti-sdk-golang v0.0.0-20200108160734-8bc089148c01 h1:5sA0w6wcT+GX71Eyqsuly1YvLqiQEVr07O6ZY6kj4WU= github.com/netfoundry/ziti-sdk-golang v0.0.0-20200108160734-8bc089148c01/go.mod h1:zdBrVmQLctojcR2hOsbvUMdYR9dvByQrDjocQVDWLRQ= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= diff --git a/ziti/cmd/ziti/cmd/edge_controller/create.go b/ziti/cmd/ziti/cmd/edge_controller/create.go index 40e20c32d..673d1e64c 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/create.go +++ b/ziti/cmd/ziti/cmd/edge_controller/create.go @@ -39,14 +39,14 @@ func newCreateCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Com }, } - cmd.AddCommand(newCreateAppWanCmd(f, out, errOut)) + cmd.AddCommand(newCreateAuthenticatorCmd(f, out, errOut)) + cmd.AddCommand(newCreateCaCmd(f, out, errOut)) + cmd.AddCommand(newCreateConfigCmd(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(newCreateServicePolicyCmd(f, out, errOut)) - cmd.AddCommand(newCreateAuthenticatorCmd(f, out, errOut)) - cmd.AddCommand(newCreateCaCmd(f, out, errOut)) return cmd } diff --git a/ziti/cmd/ziti/cmd/edge_controller/create_appwan.go b/ziti/cmd/ziti/cmd/edge_controller/create_appwan.go deleted file mode 100644 index 6024e66d1..000000000 --- a/ziti/cmd/ziti/cmd/edge_controller/create_appwan.go +++ /dev/null @@ -1,100 +0,0 @@ -/* - Copyright 2019 Netfoundry, Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package edge_controller - -import ( - "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" -) - -type appWanOptions struct { - commonOptions - identities []string - services []string -} - -// newCreateAppWanCmd creates the 'edge controller create appwan' command -func newCreateAppWanCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { - options := &appWanOptions{ - commonOptions: commonOptions{ - CommonOptions: common.CommonOptions{Factory: f, Out: out, Err: errOut}, - }, - } - - cmd := &cobra.Command{ - Use: "app-wan ", - Short: "creates an AppWan managed by the Ziti Edge Controller", - Long: "creates an AppWan managed by the Ziti Edge Controller", - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - options.Cmd = cmd - options.Args = args - err := runCreateAppWan(options) - cmdhelper.CheckErr(err) - }, - SuggestFor: []string{}, - } - - cmd.Flags().SetInterspersed(true) // allow interspersing positional args and flags - cmd.Flags().BoolVarP(&options.OutputJSONResponse, "output-json", "j", false, "Output the full JSON response from the Ziti Edge Controller") - cmd.Flags().StringSliceVarP(&options.identities, "identities", "i", nil, "Identities to add to the AppWan") - cmd.Flags().StringSliceVarP(&options.services, "services", "s", nil, "Services to add to the AppWan") - - return cmd -} - -func runCreateAppWan(o *appWanOptions) error { - - serviceData := gabs.New() - - identityIds, err := mapNamesToIDs("identities", o.identities...) - if err != nil { - return err - } - - serviceIds, err := mapNamesToIDs("services", o.services...) - if err != nil { - return err - } - - setJSONValue(serviceData, o.Args[0], "name") - setJSONValue(serviceData, identityIds, "identities") - setJSONValue(serviceData, serviceIds, "services") - - if _, err := fmt.Fprintf(o.Out, "%v\n", serviceData.String()); err != nil { - panic(err) - } - - result, err := createEntityOfType("app-wans", serviceData.String(), &o.commonOptions) - - if err != nil { - panic(err) - } - - appWanId := result.S("data", "id").Data() - - if _, err := fmt.Fprintf(o.Out, "%v\n", appWanId); err != nil { - panic(err) - } - - return err -} diff --git a/ziti/cmd/ziti/cmd/edge_controller/create_authenticator_updb.go b/ziti/cmd/ziti/cmd/edge_controller/create_authenticator_updb.go index f18f013bb..6b70377bb 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/create_authenticator_updb.go +++ b/ziti/cmd/ziti/cmd/edge_controller/create_authenticator_updb.go @@ -17,11 +17,11 @@ package edge_controller import ( - "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers" - "github.com/netfoundry/ziti-foundation/util/term" "errors" "fmt" "github.com/Jeffail/gabs" + "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers" + "github.com/netfoundry/ziti-foundation/util/term" "github.com/spf13/cobra" ) @@ -74,7 +74,6 @@ func newCreateAuthenticatorUpdb(idType string, options commonOptions) *cobra.Com } func runCreateIdentityPassword(idType string, options *createAuthenticatorUpdb) error { - if options.idOrName == "" { return errors.New("an identity must be specified") } @@ -86,18 +85,17 @@ func runCreateIdentityPassword(idType string, options *createAuthenticatorUpdb) } if options.password == "" { - options.password, err = term.PromptPassword("Enter password: ", false) + if options.password, err = term.PromptPassword("Enter password: ", false); err != nil { + return err + } } passwordData := gabs.New() setJSONValue(passwordData, options.password, "password") setJSONValue(passwordData, options.username, "username") - _, err = createEntityOfType(fmt.Sprintf("identities/%s/updb", id), passwordData.String(), &options.commonOptions) - - if err != nil { + if _, err = createEntityOfType(fmt.Sprintf("identities/%s/updb", id), passwordData.String(), &options.commonOptions); err != nil { return err } - return nil } diff --git a/ziti/cmd/ziti/cmd/edge_controller/create_config.go b/ziti/cmd/ziti/cmd/edge_controller/create_config.go new file mode 100644 index 000000000..5a1d2a885 --- /dev/null +++ b/ziti/cmd/ziti/cmd/edge_controller/create_config.go @@ -0,0 +1,91 @@ +/* + Copyright 2020 Netfoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package edge_controller + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/pkg/errors" + + "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" + "github.com/spf13/cobra" +) + +type createConfigOptions struct { + commonOptions +} + +// newCreateConfigCmd creates the 'edge controller create service-policy' command +func newCreateConfigCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { + options := &createConfigOptions{ + commonOptions: commonOptions{ + CommonOptions: common.CommonOptions{Factory: f, Out: out, Err: errOut}, + }, + } + + cmd := &cobra.Command{ + Use: "config ", + Short: "creates a config managed by the Ziti Edge Controller", + Long: "creates a config managed by the Ziti Edge Controller", + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + options.Cmd = cmd + options.Args = args + err := runCreateConfig(options) + cmdhelper.CheckErr(err) + }, + SuggestFor: []string{}, + } + + // allow interspersing positional args and flags + cmd.Flags().SetInterspersed(true) + cmd.Flags().BoolVarP(&options.OutputJSONResponse, "output-json", "j", false, "Output the full JSON response from the Ziti Edge Controller") + + return cmd +} + +// runCreateConfig create a new config on the Ziti Edge Controller +func runCreateConfig(o *createConfigOptions) error { + dataMap := map[string]interface{}{} + if err := json.Unmarshal([]byte(o.Args[1]), &dataMap); err != nil { + fmt.Printf("Attempted to parse: %v\n", o.Args[1]) + fmt.Printf("Failing parsing JSON: %+v\n", err) + return errors.Errorf("unable to parse data as json: %v", err) + } + + entityData := gabs.New() + setJSONValue(entityData, o.Args[0], "name") + setJSONValue(entityData, dataMap, "data") + result, err := createEntityOfType("configs", entityData.String(), &o.commonOptions) + + if err != nil { + panic(err) + } + + configId := result.S("data", "id").Data() + + if _, err := fmt.Fprintf(o.Out, "%v\n", configId); err != nil { + panic(err) + } + + return err +} diff --git a/ziti/cmd/ziti/cmd/edge_controller/create_service_policy.go b/ziti/cmd/ziti/cmd/edge_controller/create_service_policy.go index b3531ca1e..1f09e2905 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/create_service_policy.go +++ b/ziti/cmd/ziti/cmd/edge_controller/create_service_policy.go @@ -45,8 +45,8 @@ func newCreateServicePolicyCmd(f cmdutil.Factory, out io.Writer, errOut io.Write cmd := &cobra.Command{ Use: "service-policy ", - Short: "creates an service-policy managed by the Ziti Edge Controller", - Long: "creates an service-policy managed by the Ziti Edge Controller", + Short: "creates a service-policy managed by the Ziti Edge Controller", + Long: "creates a service-policy managed by the Ziti Edge Controller", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd diff --git a/ziti/cmd/ziti/cmd/edge_controller/delete.go b/ziti/cmd/ziti/cmd/edge_controller/delete.go index be3c8aff8..4fd2003c0 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/delete.go +++ b/ziti/cmd/ziti/cmd/edge_controller/delete.go @@ -53,6 +53,7 @@ func newDeleteCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Com cmd.AddCommand(newDeleteCmdForEntityType("api-session", runDeleteEntityOfType, newOptions())) cmd.AddCommand(newDeleteCmdForEntityType("ca", runDeleteEntityOfType, newOptions())) + cmd.AddCommand(newDeleteCmdForEntityType("config", runDeleteEntityOfType, newOptions())) cmd.AddCommand(newDeleteCmdForEntityType("edge-router", runDeleteEntityOfType, newOptions())) cmd.AddCommand(newDeleteCmdForEntityType("edge-router-policy", runDeleteEntityOfType, newOptions())) cmd.AddCommand(newDeleteCmdForEntityType("identity", runDeleteEntityOfType, newOptions())) diff --git a/ziti/cmd/ziti/cmd/edge_controller/list.go b/ziti/cmd/ziti/cmd/edge_controller/list.go index 36decfc3c..622e73b76 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/list.go +++ b/ziti/cmd/ziti/cmd/edge_controller/list.go @@ -17,6 +17,7 @@ package edge_controller import ( + "encoding/json" "fmt" "io" @@ -52,6 +53,7 @@ func newListCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Comma cmd.AddCommand(newListCmdForEntityType("api-sessions", runListApiSessions, newOptions())) cmd.AddCommand(newListCmdForEntityType("cas", runListCAs, newOptions())) + cmd.AddCommand(newListCmdForEntityType("configs", runListConfigs, newOptions())) cmd.AddCommand(newListCmdForEntityType("edge-routers", runListEdgeRouters, newOptions())) cmd.AddCommand(newListCmdForEntityType("edge-router-policies", runListEdgeRouterPolicies, newOptions())) cmd.AddCommand(newListCmdForEntityType("gateways", runListEdgeRouters, newOptions())) @@ -335,6 +337,28 @@ func runListCAs(o *commonOptions) error { return nil } +func runListConfigs(o *commonOptions) error { + children, err := listEntitiesOfTypeWithOptionalFilter("configs", o) + if err != nil { + return err + } + + for _, entity := range children { + id, _ := entity.Path("id").Data().(string) + name, _ := entity.Path("name").Data().(string) + data, _ := entity.Path("data").Data().(map[string]interface{}) + formattedData, err := json.MarshalIndent(data, " ", " ") + if err != nil { + return err + } + if _, err := fmt.Fprintf(o.Out, "id: %v\nname: %v\ndata: %v\n\n", id, name, string(formattedData)); err != nil { + return err + } + } + + return nil +} + func runListApiSessions(o *commonOptions) error { children, err := listEntitiesOfTypeWithOptionalFilter("api-sessions", o) if err != nil { diff --git a/ziti/cmd/ziti/cmd/edge_controller/update.go b/ziti/cmd/ziti/cmd/edge_controller/update.go index 4a2527f72..c765961ea 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/update.go +++ b/ziti/cmd/ziti/cmd/edge_controller/update.go @@ -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" "gopkg.in/resty.v1" "io" @@ -40,14 +40,22 @@ func newUpdateCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Com } cmd.AddCommand(newUpdateAuthenticatorCmd(f, out, errOut)) + cmd.AddCommand(newUpdateConfigCmd(f, out, errOut)) cmd.AddCommand(newUpdateCaCmd(f, out, errOut)) return cmd } -// updateEntityOfType updates an entity of the given type on the Ziti Edge Controller -func updateEntityOfType(entityType string, body string, options *commonOptions) (*gabs.Container, error) { +func putEntityOfType(entityType string, body string, options *commonOptions) (*gabs.Container, error) { + return updateEntityOfType(entityType, body, options, true) +} +func patchEntityOfType(entityType string, body string, options *commonOptions) (*gabs.Container, error) { + return updateEntityOfType(entityType, body, options, false) +} + +// updateEntityOfType updates an entity of the given type on the Ziti Edge Controller +func updateEntityOfType(entityType string, body string, options *commonOptions, put bool) (*gabs.Container, error) { session := &session{} err := session.Load() @@ -59,7 +67,7 @@ func updateEntityOfType(entityType string, body string, options *commonOptions) return nil, fmt.Errorf("host not specififed in cli config file. Exiting") } - jsonParsed, err := util.EdgeControllerUpdate(session.Host, session.Cert, session.Token, entityType, body, options.Out, options.OutputJSONResponse) + jsonParsed, err := util.EdgeControllerUpdate(session.Host, session.Cert, session.Token, entityType, body, options.Out, put, options.OutputJSONResponse) if err != nil { panic(err) diff --git a/ziti/cmd/ziti/cmd/edge_controller/update_authenticator_updb.go b/ziti/cmd/ziti/cmd/edge_controller/update_authenticator_updb.go index cd7eeb838..3df8e0bd4 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/update_authenticator_updb.go +++ b/ziti/cmd/ziti/cmd/edge_controller/update_authenticator_updb.go @@ -17,10 +17,10 @@ package edge_controller import ( - "github.com/netfoundry/ziti-foundation/util/term" "errors" "fmt" "github.com/Jeffail/gabs" + "github.com/netfoundry/ziti-foundation/util/term" "github.com/spf13/cobra" ) import cmdhelper "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers" @@ -99,7 +99,7 @@ func updateSelfPassword(current string, new string, options commonOptions) error setJSONValue(passwordData, current, "current") setJSONValue(passwordData, new, "new") - _, err = updateEntityOfType("current-identity/updb/password", passwordData.String(), &options) + _, err = putEntityOfType("current-identity/updb/password", passwordData.String(), &options) if err != nil { return err @@ -124,7 +124,7 @@ func setIdentityPassword(identity, password string, options commonOptions) error passwordData := gabs.New() setJSONValue(passwordData, password, "password") - _, err = updateEntityOfType(fmt.Sprintf("identities/%s/updb/password", id), passwordData.String(), &options) + _, err = putEntityOfType(fmt.Sprintf("identities/%s/updb/password", id), passwordData.String(), &options) if err != nil { return err diff --git a/ziti/cmd/ziti/cmd/edge_controller/update_ca.go b/ziti/cmd/ziti/cmd/edge_controller/update_ca.go index 6a3e06469..40d6450f9 100644 --- a/ziti/cmd/ziti/cmd/edge_controller/update_ca.go +++ b/ziti/cmd/ziti/cmd/edge_controller/update_ca.go @@ -17,11 +17,11 @@ 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" "github.com/netfoundry/ziti-cmd/ziti/cmd/ziti/cmd/helpers" - "fmt" - "github.com/Jeffail/gabs" "github.com/spf13/cobra" "gopkg.in/resty.v1" "io" @@ -122,7 +122,7 @@ func runUpdateCa(options updateCaOptions) error { setJSONValue(data, options.authEnabled, "isAuthEnabled") setJSONValue(data, tags, "tags") - _, err = updateEntityOfType("cas/"+id, data.String(), &options.commonOptions) + _, err = putEntityOfType("cas/"+id, data.String(), &options.commonOptions) if err != nil { return err diff --git a/ziti/cmd/ziti/cmd/edge_controller/update_config.go b/ziti/cmd/ziti/cmd/edge_controller/update_config.go new file mode 100644 index 000000000..59c700b90 --- /dev/null +++ b/ziti/cmd/ziti/cmd/edge_controller/update_config.go @@ -0,0 +1,106 @@ +/* + Copyright 2020 Netfoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package edge_controller + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/pkg/errors" + + "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" + "github.com/spf13/cobra" +) + +type updateConfigOptions struct { + commonOptions + name string + data string +} + +// newUpdateConfigCmd updates the 'edge controller update service-policy' command +func newUpdateConfigCmd(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { + options := &updateConfigOptions{ + commonOptions: commonOptions{ + CommonOptions: common.CommonOptions{Factory: f, Out: out, Err: errOut}, + }, + } + + cmd := &cobra.Command{ + Use: "config ", + Short: "updates a config managed by the Ziti Edge Controller", + Long: "updates a config managed by the Ziti Edge Controller", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + options.Cmd = cmd + options.Args = args + err := runUpdateConfig(options) + cmdhelper.CheckErr(err) + }, + SuggestFor: []string{}, + } + + // allow interspersing positional args and flags + cmd.Flags().SetInterspersed(true) + cmd.Flags().StringVarP(&options.name, "name", "n", "", "Set the name of the config") + cmd.Flags().StringVarP(&options.data, "data", "d", "", "Set the data of the config") + cmd.Flags().BoolVarP(&options.OutputJSONResponse, "output-json", "j", false, "Output the full JSON response from the Ziti Edge Controller") + + return cmd +} + +// runUpdateConfig update a new config on the Ziti Edge Controller +func runUpdateConfig(o *updateConfigOptions) error { + id, err := mapNameToID("configs", o.Args[0]) + if err != nil { + return err + } + entityData := gabs.New() + change := false + + if len(o.name) > 0 { + setJSONValue(entityData, o.name, "name") + change = true + } + + if len(o.data) > 0 { + dataMap := map[string]interface{}{} + if err := json.Unmarshal([]byte(o.data), &dataMap); err != nil { + fmt.Printf("Attempted to parse: %v\n", o.data) + fmt.Printf("Failing parsing JSON: %+v\n", err) + return errors.Errorf("unable to parse data as json: %v", err) + } + setJSONValue(entityData, dataMap, "data") + change = true + } + + if !change { + return errors.New("no change specified. must specify at least one attribute to change") + } + + _, err = patchEntityOfType(fmt.Sprintf("configs/%v", id), entityData.String(), &o.commonOptions) + + if err != nil { + panic(err) + } + + return err +} diff --git a/ziti/cmd/ziti/util/rest.go b/ziti/cmd/ziti/util/rest.go index 9028d99cc..0a908a713 100644 --- a/ziti/cmd/ziti/util/rest.go +++ b/ziti/cmd/ziti/util/rest.go @@ -576,19 +576,26 @@ func EdgeControllerDelete(baseUrl string, cert string, token string, entityType } // EdgeControllerUpdate will update entities of the given type in the given Edge Controller -func EdgeControllerUpdate(url string, cert string, token string, entityType string, body string, out io.Writer, logJSON bool) (*gabs.Container, error) { +func EdgeControllerUpdate(url string, cert string, token string, entityType string, body string, out io.Writer, put bool, logJSON bool) (*gabs.Container, error) { client := newClient() if cert != "" { client.SetRootCertificate(cert) } - resp, err := client. + request := client. R(). SetHeader("Content-Type", "application/json"). SetHeader(constants.ZitiSession, token). - SetBody(body). - Put(url + "/" + entityType) + SetBody(body) + + var err error + var resp *resty.Response + if put { + resp, err = request.Put(url + "/" + entityType) + } else { + resp, err = request.Patch(url + "/" + entityType) + } if err != nil { return nil, fmt.Errorf("unable to update %v instance in Ziti Edge Controller at %v. Error: %v", entityType, url, err)