Repository: nats-io/nsc
Pull request #20 (2018-12-04 15:11:23)

Allow account edit to change Limits.Conn
Signed-off-by: Derek Collison <derek@nats.io>


Patch:
diff --git a/cmd/editaccount.go b/cmd/editaccount.go
index 25371ce..52025f0 100644
--- a/cmd/editaccount.go
+++ b/cmd/editaccount.go
@@ -37,7 +37,7 @@ func createEditAccount() *cobra.Command {
 
 			cmd.Printf("Success! - edited account %q\n", params.AccountContextParams.Name)
 
-			Write("--", FormatJwt("Account", params.token))
+			_ = Write("--", FormatJwt("Account", params.token))
 
 			if params.claim.NotBefore > 0 {
 				cmd.Printf("Token valid on %s - %s\n",
@@ -49,12 +49,17 @@ func createEditAccount() *cobra.Command {
 					UnixToDate(params.claim.Expires),
 					HumanizedDate(params.claim.Expires))
 			}
+			if params.claim.Limits.Conn > 0 {
+				cmd.Printf("Maximum active connections set to %d\n",
+					params.claim.Limits.Conn)
+			}
 
 			return nil
 		},
 	}
 	cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
 	cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
+	cmd.Flags().Int64VarP(&params.conns.NumberValue, "conns", "", 0, "set maximum active connections for the account")
 
 	params.AccountContextParams.BindFlags(cmd)
 	params.TimeParams.BindFlags(cmd)
@@ -74,13 +79,14 @@ type EditAccountParams struct {
 	token  string
 	tags   []string
 	rmTags []string
+	conns  NumberParams
 }
 
 func (p *EditAccountParams) SetDefaults(ctx ActionCtx) error {
 	p.AccountContextParams.SetDefaults(ctx)
 	p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)
 
-	if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag") {
+	if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag", "conns") {
 		ctx.CurrentCmd().SilenceUsage = false
 		return fmt.Errorf("specify an edit option")
 	}
@@ -111,6 +117,10 @@ func (p *EditAccountParams) Load(ctx ActionCtx) error {
 
 func (p *EditAccountParams) PostInteractive(ctx ActionCtx) error {
 	var err error
+	if err = p.conns.Edit("max connections"); err != nil {
+		return err
+	}
+
 	if err = p.TimeParams.Edit(); err != nil {
 		return err
 	}
@@ -146,6 +156,10 @@ func (p *EditAccountParams) Run(ctx ActionCtx) error {
 	p.claim.Tags.Remove(p.rmTags...)
 	sort.Strings(p.claim.Tags)
 
+	if p.conns.NumberValue > 0 {
+		p.claim.Limits.Conn = p.conns.NumberValue
+	}
+
 	p.token, err = p.claim.Encode(p.signerKP)
 	if err != nil {
 		return err
diff --git a/cmd/numberparams.go b/cmd/numberparams.go
new file mode 100644
index 0000000..908314c
--- /dev/null
+++ b/cmd/numberparams.go
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018 The NATS Authors
+ * 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
+ *
+ * http://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 cmd
+
+import (
+	"fmt"
+
+	"github.com/nats-io/nsc/cli"
+)
+
+type NumberParams struct {
+	NumberValue int64
+}
+
+func (e *NumberParams) Valid() error {
+	// flag already insures this is a number
+	return nil
+}
+
+func (e *NumberParams) Edit(prompt string) error {
+	var err error
+	var nv int64
+	sv := fmt.Sprintf("%d", e.NumberValue)
+	sv, err = cli.Prompt(prompt, sv, true, func(s string) error {
+		nv, err = ParseNumber(s)
+		return err
+	})
+	if err != nil {
+		return err
+	}
+	e.NumberValue = nv
+	return nil
+}


Test patch:
diff --git a/cmd/editaccount_test.go b/cmd/editaccount_test.go
index 8e289b3..4781421 100644
--- a/cmd/editaccount_test.go
+++ b/cmd/editaccount_test.go
@@ -93,3 +93,18 @@ func Test_EditAccount_Times(t *testing.T) {
 	require.Equal(t, start, ac.NotBefore)
 	require.Equal(t, expiry, ac.Expires)
 }
+
+func Test_EditAccountMaxConns(t *testing.T) {
+	ts := NewTestStore(t, "edit account")
+	defer ts.Done(t)
+
+	ts.AddAccount(t, "A")
+
+	_, _, err := ExecuteCmd(createEditAccount(), "--conns", "10")
+	require.NoError(t, err)
+
+	ac, err := ts.Store.ReadAccountClaim("A")
+	require.NoError(t, err)
+	require.NotNil(t, ac)
+	require.Equal(t, int64(10), ac.Limits.Conn)
+}
diff --git a/cmd/numberparams_test.go b/cmd/numberparams_test.go
new file mode 100644
index 0000000..d54b186
--- /dev/null
+++ b/cmd/numberparams_test.go
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2018 The NATS Authors
+ * 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
+ *
+ * http://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 cmd
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+	"testing"
+
+	"github.com/spf13/cobra"
+	"github.com/stretchr/testify/require"
+)
+
+func numberEditorCmd(params *NumberParams) *cobra.Command {
+	cmd := &cobra.Command{
+		Use:           "env",
+		Short:         fmt.Sprintf("Prints and manage the %s environment", filepath.Base(os.Args[0])),
+		SilenceErrors: false,
+		SilenceUsage:  false,
+		Example:       "env",
+		RunE: func(cmd *cobra.Command, args []string) error {
+			if InteractiveFlag {
+				if err := params.Edit("enter a number"); err != nil {
+					return err
+				}
+			}
+			return nil
+		},
+	}
+	cmd.Flags().Int64VarP(&params.NumberValue, "number", "n", 0, "some number")
+
+	return cmd
+}
+
+func TestNumberParams_BindFlags(t *testing.T) {
+	var p NumberParams
+	cmd := numberEditorCmd(&p)
+	_, _, err := ExecuteCmd(cmd, "--number", "10")
+	require.NoError(t, err)
+
+	require.Equal(t, int64(10), p.NumberValue)
+}
+
+func TestNumberParams_Edit(t *testing.T) {
+	var p NumberParams
+	cmd := numberEditorCmd(&p)
+	_, _, err := ExecuteInteractiveCmd(cmd, []interface{}{"404"})
+	require.NoError(t, err)
+
+	require.Equal(t, int64(404), p.NumberValue)
+}