From b315e79cfe918e3726545f8375ebca73bace0da5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 27 Sep 2018 23:52:17 -0700 Subject: [PATCH] command/services --- command/services/register/config.go | 21 +++++ command/services/register/config_test.go | 43 +++++++++ command/services/register/register.go | 107 +++++++++++++++++++++++ command/services/services.go | 35 ++++++++ command/services/services_test.go | 13 +++ 5 files changed, 219 insertions(+) create mode 100644 command/services/register/config.go create mode 100644 command/services/register/config_test.go create mode 100644 command/services/register/register.go create mode 100644 command/services/services.go create mode 100644 command/services/services_test.go diff --git a/command/services/register/config.go b/command/services/register/config.go new file mode 100644 index 0000000000..fc01672710 --- /dev/null +++ b/command/services/register/config.go @@ -0,0 +1,21 @@ +package register + +import ( + "fmt" + "github.com/hashicorp/consul/agent/config" + "github.com/hashicorp/consul/api" + "github.com/mitchellh/mapstructure" +) + +// configToAgentService converts a ServiceDefinition struct to an +// AgentServiceRegistration API struct. +func configToAgentService(svc *config.ServiceDefinition) (*api.AgentServiceRegistration, error) { + var result api.AgentServiceRegistration + var m map[string]interface{} + err := mapstructure.Decode(svc, &m) + if err == nil { + println(fmt.Sprintf("%#v", m)) + err = mapstructure.Decode(m, &result) + } + return &result, err +} diff --git a/command/services/register/config_test.go b/command/services/register/config_test.go new file mode 100644 index 0000000000..17349a2f80 --- /dev/null +++ b/command/services/register/config_test.go @@ -0,0 +1,43 @@ +package register + +import ( + "testing" + + "github.com/hashicorp/consul/agent/config" + "github.com/hashicorp/consul/api" + "github.com/stretchr/testify/require" +) + +func TestConfigToAgentService(t *testing.T) { + cases := []struct { + Name string + Input *config.ServiceDefinition + Output *api.AgentServiceRegistration + }{ + { + "Basic service with port", + &config.ServiceDefinition{ + Name: strPtr("web"), + Tags: []string{"leader"}, + Port: intPtr(1234), + }, + &api.AgentServiceRegistration{ + Name: "web", + Tags: []string{"leader"}, + Port: 1234, + }, + }, + } + + for _, tc := range cases { + t.Run(tc.Name, func(t *testing.T) { + require := require.New(t) + actual, err := configToAgentService(tc.Input) + require.NoError(err) + require.Equal(tc.Output, actual) + }) + } +} + +func intPtr(v int) *int { return &v } +func strPtr(v string) *string { return &v } diff --git a/command/services/register/register.go b/command/services/register/register.go new file mode 100644 index 0000000000..0ceb24c40d --- /dev/null +++ b/command/services/register/register.go @@ -0,0 +1,107 @@ +package register + +import ( + "flag" + + //"github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" +) + +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c +} + +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags + help string + + // flags + flagMeta map[string]string +} + +func (c *cmd) init() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + 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.http = &flags.HTTPFlags{} + flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.ServerFlags()) + c.help = flags.Usage(help, c.flags) +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { + return 1 + } + + // Check for arg validation + args = c.flags.Args() + if len(args) == 0 { + c.UI.Error("Service registration requires at least one argument.") + return 1 + } + + /* + ixns, err := c.ixnsFromArgs(args) + if err != nil { + c.UI.Error(fmt.Sprintf("Error: %s", err)) + return 1 + } + + // Create and test the HTTP client + /* + client, err := c.http.APIClient() + if err != nil { + c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) + return 1 + } + */ + + return 0 +} + +func (c *cmd) Synopsis() string { + return synopsis +} + +func (c *cmd) Help() string { + return c.help +} + +const synopsis = "Create intentions for service connections." +const help = ` +Usage: consul intention create [options] SRC DST +Usage: consul intention create [options] -file FILE... + + Create one or more intentions. The data can be specified as a single + source and destination pair or via a set of files when the "-file" flag + is specified. + + $ consul intention create web db + + To consume data from a set of files: + + $ consul intention create -file one.json two.json + + When specifying the "-file" flag, "-" may be used once to read from stdin: + + $ echo "{ ... }" | consul intention create -file - + + An "allow" intention is created by default (whitelist). To create a + "deny" intention, the "-deny" flag should be specified. + + If a conflicting intention is found, creation will fail. To replace any + conflicting intentions, specify the "-replace" flag. This will replace any + conflicting intentions with the intention specified in this command. + Metadata and any other fields of the previous intention will not be + preserved. + + Additional flags and more advanced use cases are detailed below. +` diff --git a/command/services/services.go b/command/services/services.go new file mode 100644 index 0000000000..0e050d77d3 --- /dev/null +++ b/command/services/services.go @@ -0,0 +1,35 @@ +package services + +import ( + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" +) + +func New() *cmd { + return &cmd{} +} + +type cmd struct{} + +func (c *cmd) Run(args []string) int { + return cli.RunResultHelp +} + +func (c *cmd) Synopsis() string { + return synopsis +} + +func (c *cmd) Help() string { + return flags.Usage(help, nil) +} + +const synopsis = "Interact with services" +const help = ` +Usage: consul services [options] [args] + + This command has subcommands for interacting with services. The subcommands + default to working with services registered with the local agent. Please see + the "consul catalog" command for interacting with the entire catalog. + + For more examples, ask for subcommand help or view the documentation. +` diff --git a/command/services/services_test.go b/command/services/services_test.go new file mode 100644 index 0000000000..c7521718a3 --- /dev/null +++ b/command/services/services_test.go @@ -0,0 +1,13 @@ +package services + +import ( + "strings" + "testing" +) + +func TestCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New().Help(), '\t') { + t.Fatal("help has tabs") + } +}