command/services/register: flag-based registration

This commit is contained in:
Mitchell Hashimoto 2018-10-01 09:16:14 -07:00
parent 939708138f
commit bf83309124
No known key found for this signature in database
GPG Key ID: A3A9A8F4F25C3E56
2 changed files with 107 additions and 7 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"
@ -22,14 +23,31 @@ type cmd struct {
help string
// flags
flagMeta map[string]string
flagId string
flagName string
flagAddress string
flagPort int
flagTags []string
flagMeta map[string]string
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.StringVar(&c.flagId, "id", "",
"ID of the service to register for arg-based registration. If this "+
"isn't set, it will default to the -name value.")
c.flags.StringVar(&c.flagName, "name", "",
"Name of the service to register for arg-based registration.")
c.flags.StringVar(&c.flagAddress, "address", "",
"Address of the service to register for arg-based registration.")
c.flags.IntVar(&c.flagPort, "port", 0,
"Port of the service to register for arg-based registration.")
c.flags.Var((*flags.FlagMapValue)(&c.flagMeta), "meta",
"Metadata to set on the intention, formatted as key=value. This flag "+
"may be specified multiple times to set multiple meta fields.")
c.flags.Var((*flags.AppendSliceValue)(&c.flagTags), "tag",
"Tag to add to the service. This flag can be specified multiple "+
"times to set multiple tags.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
@ -42,17 +60,32 @@ func (c *cmd) Run(args []string) int {
return 1
}
svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{
ID: c.flagId,
Name: c.flagName,
Address: c.flagAddress,
Port: c.flagPort,
Tags: c.flagTags,
Meta: c.flagMeta,
}}
// Check for arg validation
args = c.flags.Args()
if len(args) == 0 {
c.UI.Error("Service registration requires at least one argument.")
if len(args) == 0 && c.flagName == "" {
c.UI.Error("Service registration requires at least one argument or flags.")
return 1
} else if len(args) > 0 && c.flagName != "" {
c.UI.Error("Service registration requires arguments or -id, not both.")
return 1
}
svcs, err := services.ServicesFromFiles(args)
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
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

@ -18,6 +18,47 @@ func TestCommand_noTabs(t *testing.T) {
}
}
func TestCommand_Validation(t *testing.T) {
t.Parallel()
ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct {
args []string
output string
}{
"no args or id": {
[]string{},
"at least one",
},
"args and -name": {
[]string{"-name", "web", "foo.json"},
"not both",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
require := require.New(t)
c.init()
// Ensure our buffer is always clear
if ui.ErrorWriter != nil {
ui.ErrorWriter.Reset()
}
if ui.OutputWriter != nil {
ui.OutputWriter.Reset()
}
require.Equal(1, c.Run(tc.args))
output := ui.ErrorWriter.String()
require.Contains(output, tc.output)
})
}
}
func TestCommand_File(t *testing.T) {
t.Parallel()
@ -51,6 +92,32 @@ func TestCommand_File(t *testing.T) {
require.NotNil(svc)
}
func TestCommand_Flags(t *testing.T) {
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
client := a.Client()
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-name", "web",
}
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
svcs, err := client.Agent().Services()
require.NoError(err)
require.Len(svcs, 1)
svc := svcs["web"]
require.NotNil(svc)
}
func testFile(t *testing.T, suffix string) *os.File {
f := testutil.TempFile(t, "register-test-file")
if err := f.Close(); err != nil {