diff --git a/core/cmd/bridge_commands.go b/core/cmd/bridge_commands.go index 398d466c43a..cd314b23218 100644 --- a/core/cmd/bridge_commands.go +++ b/core/cmd/bridge_commands.go @@ -128,6 +128,29 @@ func (s *Shell) CreateBridge(c *cli.Context) (err error) { return s.renderAPIResponse(resp, &BridgePresenter{}) } +func (s *Shell) UpdateBridge(c *cli.Context) (err error) { + if !c.Args().Present() { + return s.errorOut(errors.New("must pass the name of the bridge to be updated")) + } + bridgeName := c.Args().First() + buf, err := getBufferFromJSON(c.Args().Get(1)) + if err != nil { + return s.errorOut(err) + } + + resp, err := s.HTTP.Patch(s.ctx(), "/v2/bridge_types/"+bridgeName, buf) + if err != nil { + return s.errorOut(err) + } + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + err = multierr.Append(err, cerr) + } + }() + + return s.renderAPIResponse(resp, &BridgePresenter{}) +} + // RemoveBridge removes a specific Bridge by name. func (s *Shell) RemoveBridge(c *cli.Context) (err error) { if !c.Args().Present() { diff --git a/core/cmd/bridge_commands_test.go b/core/cmd/bridge_commands_test.go index f05aac52cd9..04352b0d5d1 100644 --- a/core/cmd/bridge_commands_test.go +++ b/core/cmd/bridge_commands_test.go @@ -3,6 +3,7 @@ package cmd_test import ( "bytes" "flag" + "fmt" "testing" "time" @@ -191,3 +192,44 @@ func TestShell_RemoveBridge(t *testing.T) { assert.Equal(t, bt.URL.String(), p.URL) assert.Equal(t, bt.Confirmations, p.Confirmations) } +func TestShell_UpdateBridge(t *testing.T) { + t.Parallel() + + app := startNewApplicationV2(t, nil) + client, _ := app.NewShellAndRenderer() + name := testutils.RandomizeName("updatebridge") + + bt := &bridges.BridgeType{ + Name: bridges.MustParseBridgeName(name), + URL: cltest.WebURL(t, "https://testing.com/bridges"), + Confirmations: 0, + } + require.NoError(t, app.BridgeORM().CreateBridgeType(testutils.Context(t), bt)) + tests := []struct { + name string + args []string + errored bool + }{ + {"NoArgs", []string{}, true}, + {"OnlyName", []string{name}, true}, + {"ValidUpdate", []string{name, fmt.Sprintf(`{ "name": "%s", "url": "http://localhost:3000/updated" }`, name)}, false}, + {"InvalidJSON", []string{name, `{ "url": "http://localhost:3000/updated"`}, true}, + } + + for _, tt := range tests { + test := tt + t.Run(test.name, func(t *testing.T) { + set := flag.NewFlagSet("bridge", 0) + flagSetApplyFromAction(client.UpdateBridge, set, "") + + require.NoError(t, set.Parse(test.args)) + + c := cli.NewContext(nil, set, nil) + if test.errored { + assert.Error(t, client.UpdateBridge(c)) + } else { + assert.Nil(t, client.UpdateBridge(c)) + } + }) + } +}