Skip to content

Commit

Permalink
CLI: add an option for renew command fail on non-fullfillable request…
Browse files Browse the repository at this point in the history
… to allow command chaining

Signed-off-by: saiaunghlyanhtet <[email protected]>
  • Loading branch information
saiaunghlyanhtet committed Dec 2, 2024
1 parent 4b456ff commit 82a65a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
19 changes: 17 additions & 2 deletions command/token_renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var (
type TokenRenewCommand struct {
*BaseCommand

flagAccessor bool
flagIncrement time.Duration
flagAccessor bool
flagIncrement time.Duration
flagFailIfNotFullfilled bool
}

func (c *TokenRenewCommand) Synopsis() string {
Expand Down Expand Up @@ -86,6 +87,15 @@ func (c *TokenRenewCommand) Flags() *FlagSets {
"numeric string with suffix like \"30s\" or \"5m\".",
})

f.BoolVar(&BoolVar{
Name: "fail-if-not-fullfilled",
Target: &c.flagFailIfNotFullfilled,
Default: false,
EnvVar: "",
Completion: complete.PredictNothing,
Usage: "Fail if the requested TTL increment cannot be fully fulfilled.",
})

return set
}

Expand Down Expand Up @@ -140,5 +150,10 @@ func (c *TokenRenewCommand) Run(args []string) int {
return 2
}

if c.flagFailIfNotFullfilled && secret.LeaseDuration < int(increment.Seconds()) {
c.UI.Info("Token renewal failed: requested increment could not be fully fulfilled")
return 1
}

return OutputSecret(c.UI, secret)
}
16 changes: 16 additions & 0 deletions command/token_renew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,42 @@ func TestTokenRenewCommand_Run(t *testing.T) {
args []string
out string
code int
fail bool
}{
{
"too_many_args",
[]string{"foo", "bar", "baz"},
"Too many arguments",
1,
false,
},
{
"default",
nil,
"",
0,
false,
},
{
"increment",
[]string{"-increment", "60s"},
"",
0,
false,
},
{
"increment_no_suffix",
[]string{"-increment", "60"},
"",
0,
false,
},
{
"fail_if_not_fullfilled",
[]string{"-increment", "30m", "--fail-if-not-fullfilled"},
"Token renewal failed: requested increment could not be fully fulfilled",
1,
true,
},
}

Expand All @@ -77,6 +89,10 @@ func TestTokenRenewCommand_Run(t *testing.T) {
ui, cmd := testTokenRenewCommand(t)
cmd.client = client

if tc.fail {
client.Auth().Token().Renew(token, 1)
}

code := cmd.Run(tc.args)
if code != tc.code {
t.Errorf("expected %d to be %d", code, tc.code)
Expand Down

0 comments on commit 82a65a9

Please sign in to comment.