-
Notifications
You must be signed in to change notification settings - Fork 4.5k
cli: Add consul intention list command (based on PR #6825) #9468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hanshasselberg
merged 8 commits into
hashicorp:master
from
karras:feature_intention_list
Jan 12, 2021
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
daab8d6
5652 - Adds consul intention list command
snuggie12 7b3f14b
Fixed formatting in test
snuggie12 1d27397
Add namespace support to consul intention list
karras 4121a44
Remove references to Connect for consul intention list
karras ca37099
Fix consul intention list tests
karras b6aee29
Add consul intention list docs
karras 2787b8e
Change delim for intention list command to \x1f
karras 63deb7d
Add changelog entry for new consul intention list command
karras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package list | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/hashicorp/consul/command/flags" | ||
"github.com/mitchellh/cli" | ||
"github.com/ryanuber/columnize" | ||
) | ||
|
||
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 | ||
} | ||
|
||
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()) | ||
flags.Merge(c.flags, c.http.NamespaceFlags()) | ||
c.help = flags.Usage(help, c.flags) | ||
} | ||
|
||
func (c *cmd) Run(args []string) int { | ||
if err := c.flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
client, err := c.http.APIClient() | ||
if err != nil { | ||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) | ||
return 1 | ||
} | ||
|
||
ixns, _, err := client.Connect().Intentions(nil) | ||
if err != nil { | ||
c.UI.Error(fmt.Sprintf("Failed to retrieve the intentions list: %s", err)) | ||
return 1 | ||
} | ||
|
||
if len(ixns) == 0 { | ||
c.UI.Error(fmt.Sprintf("There are no intentions.")) | ||
return 2 | ||
} | ||
|
||
result := make([]string, 0, len(ixns)) | ||
header := "ID|Source|Action|Destination|Precedence" | ||
result = append(result, header) | ||
for _, ixn := range ixns { | ||
line := fmt.Sprintf("%s|%s|%s|%s|%d", | ||
ixn.ID, ixn.SourceName, ixn.Action, ixn.DestinationName, ixn.Precedence) | ||
result = append(result, line) | ||
} | ||
|
||
output := columnize.SimpleFormat(result) | ||
c.UI.Output(output) | ||
|
||
return 0 | ||
} | ||
|
||
func (c *cmd) Synopsis() string { | ||
return synopsis | ||
} | ||
|
||
func (c *cmd) Help() string { | ||
return c.help | ||
} | ||
|
||
const synopsis = "List intentions." | ||
const help = ` | ||
Usage: consul intention list | ||
|
||
List all intentions. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package list | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/agent" | ||
"github.com/hashicorp/consul/api" | ||
"github.com/mitchellh/cli" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIntentionListCommand_noTabs(t *testing.T) { | ||
t.Parallel() | ||
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { | ||
t.Fatal("help has tabs") | ||
} | ||
} | ||
|
||
func TestIntentionListCommand(t *testing.T) { | ||
t.Parallel() | ||
require := require.New(t) | ||
a := agent.NewTestAgent(t, ``) | ||
defer a.Shutdown() | ||
client := a.Client() | ||
|
||
// Create the intention | ||
var id string | ||
{ | ||
var err error | ||
//nolint:staticcheck | ||
id, _, err = client.Connect().IntentionCreate(&api.Intention{ | ||
SourceName: "web", | ||
DestinationName: "db", | ||
Action: api.IntentionActionAllow, | ||
}, nil) | ||
require.NoError(err) | ||
} | ||
|
||
// List all intentions | ||
ui := cli.NewMockUi() | ||
cmd := New(ui) | ||
args := []string{"-http-addr=" + a.HTTPAddr()} | ||
|
||
require.Equal(0, cmd.Run(args), ui.ErrorWriter.String()) | ||
require.Contains(ui.OutputWriter.String(), id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
layout: commands | ||
page_title: 'Commands: Intention List' | ||
sidebar_title: list | ||
--- | ||
|
||
# Consul Intention List | ||
|
||
Command: `consul intention list` | ||
|
||
The `intention list` command shows all intentions including ID and precedence. | ||
|
||
## Usage | ||
|
||
Usage: | ||
|
||
- `consul intention list` | ||
|
||
#### API Options | ||
|
||
@include 'http_api_options_client.mdx' | ||
|
||
#### Enterprise Options | ||
|
||
@include 'http_api_namespace_options.mdx' | ||
|
||
## Examples | ||
|
||
```shell-session | ||
$ consul intention list | ||
ID Source Action Destination Precedence | ||
web allow db 9 | ||
36a6cf15-5f0e-a388-163e-0f608009704a dashboard allow counting 9 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
\x1f
as the delimiter. See #6652 for more details.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I've quickly tested it in my Enterprise environment and it seems to look fine but may I ask you to do a short cross check?