command/intentions/delete

This commit is contained in:
Mitchell Hashimoto 2018-05-11 22:19:39 -07:00
parent efa82278e2
commit 4caeaaaa21
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 119 additions and 25 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/hashicorp/consul/command/info"
"github.com/hashicorp/consul/command/intention"
ixncreate "github.com/hashicorp/consul/command/intention/create"
ixndelete "github.com/hashicorp/consul/command/intention/delete"
ixnget "github.com/hashicorp/consul/command/intention/get"
"github.com/hashicorp/consul/command/join"
"github.com/hashicorp/consul/command/keygen"
@ -71,6 +72,7 @@ func init() {
Register("info", func(ui cli.Ui) (cli.Command, error) { return info.New(ui), nil })
Register("intention", func(ui cli.Ui) (cli.Command, error) { return intention.New(), nil })
Register("intention create", func(ui cli.Ui) (cli.Command, error) { return ixncreate.New(ui), nil })
Register("intention delete", func(ui cli.Ui) (cli.Command, error) { return ixndelete.New(ui), nil })
Register("intention get", func(ui cli.Ui) (cli.Command, error) { return ixnget.New(ui), nil })
Register("join", func(ui cli.Ui) (cli.Command, error) { return join.New(ui), nil })
Register("keygen", func(ui cli.Ui) (cli.Command, error) { return keygen.New(ui), nil })

View File

@ -0,0 +1,86 @@
package delete
import (
"flag"
"fmt"
"io"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/intention/finder"
"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
// testStdin is the input for testing.
testStdin io.Reader
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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
}
// 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
}
// Get the intention ID to load
f := &finder.Finder{Client: client}
id, err := f.IDFromArgs(c.flags.Args())
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
}
// Read the intention
_, err = client.Connect().IntentionDelete(id, nil)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading the intention: %s", err))
return 1
}
c.UI.Output(fmt.Sprintf("Intention deleted."))
return 0
}
func (c *cmd) Synopsis() string {
return synopsis
}
func (c *cmd) Help() string {
return c.help
}
const synopsis = "Delete an intention."
const help = `
Usage: consul intention delete [options] SRC DST
Usage: consul intention delete [options] ID
Delete an intention. This cannot be reversed. The intention can be looked
up via an exact source/destination match or via the unique intention ID.
$ consul intention delete web db
`

View File

@ -1,6 +1,7 @@
package finder
import (
"fmt"
"strings"
"sync"
@ -20,6 +21,31 @@ type Finder struct {
ixns []*api.Intention // cached list of intentions
}
// ID returns the intention ID for the given CLI args. An error is returned
// if args is not 1 or 2 elements.
func (f *Finder) IDFromArgs(args []string) (string, error) {
switch len(args) {
case 1:
return args[0], nil
case 2:
ixn, err := f.Find(args[0], args[1])
if err != nil {
return "", err
}
if ixn == nil {
return "", fmt.Errorf(
"Intention with source %q and destination %q not found.",
args[0], args[1])
}
return ixn.ID, nil
default:
return "", fmt.Errorf("command requires exactly 1 or 2 arguments")
}
}
// Find finds the intention that matches the given src and dst. This will
// return nil when the result is not found.
func (f *Finder) Find(src, dst string) (*api.Intention, error) {

View File

@ -1,4 +1,4 @@
package create
package get
import (
"flag"
@ -50,30 +50,10 @@ func (c *cmd) Run(args []string) int {
}
// Get the intention ID to load
var id string
args = c.flags.Args()
switch len(args) {
case 1:
id = args[0]
case 2:
f := &finder.Finder{Client: client}
ixn, err := f.Find(args[0], args[1])
if err != nil {
c.UI.Error(fmt.Sprintf("Error looking up intention: %s", err))
return 1
}
if ixn == nil {
c.UI.Error(fmt.Sprintf(
"Intention with source %q and destination %q not found.",
args[0], args[1]))
return 1
}
id = ixn.ID
default:
c.UI.Error(fmt.Sprintf("Error: get requires exactly 1 or 2 arguments"))
f := &finder.Finder{Client: client}
id, err := f.IDFromArgs(c.flags.Args())
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
}