command/services

This commit is contained in:
Mitchell Hashimoto 2018-09-27 23:52:17 -07:00
parent 48feac2bee
commit 0997792cea
No known key found for this signature in database
GPG Key ID: A3A9A8F4F25C3E56
5 changed files with 219 additions and 0 deletions

View File

@ -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
}

View File

@ -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 }

View File

@ -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.
`

View File

@ -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 <subcommand> [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.
`

View File

@ -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")
}
}