diff --git a/command/license_get.go b/command/license_get.go index a5a7d7e51..8dcc7abeb 100644 --- a/command/license_get.go +++ b/command/license_get.go @@ -2,19 +2,15 @@ package command import ( "fmt" - - "github.com/mitchellh/cli" ) -var _ cli.Command = &LicenseGetCommand{} - type LicenseGetCommand struct { Meta } func (c *LicenseGetCommand) Help() string { helpText := ` -Usage: nomad license put [options] +Usage: nomad license get [options] Gets a new license in Servers and Clients General Options: @@ -44,16 +40,22 @@ func (c *LicenseGetCommand) Run(args []string) int { flags.Usage = func() { c.Ui.Output(c.Help()) } flags.BoolVar(&signed, "signed", false, "Gets the signed license blob instead of a parsed license") + if err := flags.Parse(args); err != nil { + c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err)) + return 1 + } + client, err := c.Meta.Client() if err != nil { c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) - return 0 + return 1 } if signed { resp, _, err := client.Operator().LicenseGetSigned(nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error getting signed license: %v", err)) + return 1 } c.Ui.Output(resp) return 0 @@ -61,8 +63,8 @@ func (c *LicenseGetCommand) Run(args []string) int { resp, _, err := client.Operator().LicenseGet(nil) if err != nil { - c.Ui.Error(fmt.Sprintf("Error putting license: %v", err)) - return 0 + c.Ui.Error(fmt.Sprintf("Error getting license: %v", err)) + return 1 } return OutputLicenseReply(c.Ui, resp) diff --git a/command/license_get_test.go b/command/license_get_test.go new file mode 100644 index 000000000..ae3a67c82 --- /dev/null +++ b/command/license_get_test.go @@ -0,0 +1,27 @@ +package command + +import ( + "testing" + + "github.com/mitchellh/cli" + "github.com/stretchr/testify/require" +) + +var _ cli.Command = &LicenseGetCommand{} + +func TestCommand_LicenseGet_OSSErr(t *testing.T) { + t.Parallel() + + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &LicenseGetCommand{Meta: Meta{Ui: ui}} + + if code := cmd.Run([]string{"-address=" + url}); code != 1 { + require.Equal(t, 1, code) + } + + require.Contains(t, ui.ErrorWriter.String(), "Nomad Enterprise only endpoint") + +} diff --git a/command/license_reset.go b/command/license_reset.go new file mode 100644 index 000000000..57ce1f96f --- /dev/null +++ b/command/license_reset.go @@ -0,0 +1,54 @@ +package command + +import ( + "fmt" +) + +type LicenseResetCommand struct { + Meta +} + +func (c *LicenseResetCommand) Help() string { + helpText := ` +Usage: nomad license reset [options] + +Resets the Nomad Server and Clients Enterprise license to the builtin one if +it is still valid. IF the builtin license is invalid, the current one stays +active. + +General Options: + + ` + generalOptionsUsage() + + return helpText +} + +func (c *LicenseResetCommand) Synopsis() string { + return "Install a new Nomad Enterprise License" +} + +func (c *LicenseResetCommand) Name() string { return "license reset" } + +func (c *LicenseResetCommand) Run(args []string) int { + flags := c.Meta.FlagSet(c.Name(), FlagSetClient) + flags.Usage = func() { c.Ui.Output(c.Help()) } + + if err := flags.Parse(args); err != nil { + c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err)) + return 1 + } + + client, err := c.Meta.Client() + if err != nil { + c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) + return 1 + } + + resp, _, err := client.Operator().LicenseReset(nil) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error resetting license: %v", err)) + return 1 + } + + return OutputLicenseReply(c.Ui, resp) +} diff --git a/command/license_reset_test.go b/command/license_reset_test.go new file mode 100644 index 000000000..8b20aa0b4 --- /dev/null +++ b/command/license_reset_test.go @@ -0,0 +1,27 @@ +package command + +import ( + "testing" + + "github.com/mitchellh/cli" + "github.com/stretchr/testify/require" +) + +var _ cli.Command = &LicenseResetCommand{} + +func TestCommand_LicenseReset_OSSErr(t *testing.T) { + t.Parallel() + + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &LicenseResetCommand{Meta: Meta{Ui: ui}} + + if code := cmd.Run([]string{"-address=" + url}); code != 1 { + require.Equal(t, 1, code) + } + + require.Contains(t, ui.ErrorWriter.String(), "Nomad Enterprise only endpoint") + +}