command/services/deregister: -id flag for deletion

This commit is contained in:
Mitchell Hashimoto 2018-10-01 08:53:30 -07:00
parent 2f97a618dc
commit 3425f123ef
No known key found for this signature in database
GPG Key ID: A3A9A8F4F25C3E56
2 changed files with 49 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/services"
"github.com/mitchellh/cli"
@ -16,14 +17,17 @@ func New(ui cli.Ui) *cmd {
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
flagId string
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.StringVar(&c.flagId, "id", "",
"ID to delete. This must not be set if arguments are given.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
@ -38,15 +42,20 @@ func (c *cmd) Run(args []string) int {
// Check for arg validation
args = c.flags.Args()
if len(args) == 0 {
c.UI.Error("Service deregistration requires at least one argument.")
if len(args) == 0 && c.flagId == "" {
c.UI.Error("Service deregistration requires at least one argument or -id.")
return 1
}
svcs, err := services.ServicesFromFiles(args)
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{
ID: c.flagId}}
if len(args) > 0 {
var err error
svcs, err = services.ServicesFromFiles(args)
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
}
}
// Create and test the HTTP client

View File

@ -93,6 +93,36 @@ func TestCommand_File_nameOnly(t *testing.T) {
require.NotNil(svcs["db"])
}
func TestCommand_Flag(t *testing.T) {
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
client := a.Client()
// Register a service
require.NoError(client.Agent().ServiceRegister(&api.AgentServiceRegistration{
Name: "web"}))
require.NoError(client.Agent().ServiceRegister(&api.AgentServiceRegistration{
Name: "db"}))
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-id", "web",
}
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
svcs, err := client.Agent().Services()
require.NoError(err)
require.Len(svcs, 1)
require.NotNil(svcs["db"])
}
func testFile(t *testing.T, suffix string) *os.File {
f := testutil.TempFile(t, "register-test-file")
if err := f.Close(); err != nil {