-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Exported services CLI and docs #20331
Conversation
@tauhid621 I feel json structure is taking too much space and increased number of lines. We should also give user a table view option in the command. Please see the following json structure -
Comparing this to table structure - it feels concise.
Here is a patch for the same - diff --git a/api/exported_services.go b/api/exported_services.go
index 50483e9c8d..9a45d2104a 100644
--- a/api/exported_services.go
+++ b/api/exported_services.go
@@ -44,6 +44,6 @@ func (c *Client) ExportedServices(q *QueryOptions) ([]ResolvedExportedService, *
if err := decodeBody(resp, &expSvcs); err != nil {
return nil, nil, err
}
-
+
return expSvcs, qm, nil
}
diff --git a/command/exportedservices/exported_services.go b/command/exportedservices/exported_services.go
index c63af8d771..27394b447d 100644
--- a/command/exportedservices/exported_services.go
+++ b/command/exportedservices/exported_services.go
@@ -7,9 +7,8 @@ import (
"encoding/json"
"flag"
"fmt"
-
+ "github.com/hashicorp/consul/command/cli"
"github.com/hashicorp/consul/command/flags"
- "github.com/mitchellh/cli"
)
func New(ui cli.Ui) *cmd {
@@ -23,6 +22,7 @@ type cmd struct {
flags *flag.FlagSet
http *flags.HTTPFlags
help string
+ table bool
}
func (c *cmd) init() {
@@ -31,6 +31,7 @@ func (c *cmd) init() {
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
flags.Merge(c.flags, c.http.PartitionFlag())
+ c.flags.BoolVar(&c.table, "table", false, "Output in table format")
c.help = flags.Usage(help, c.flags)
}
@@ -51,6 +52,25 @@ func (c *cmd) Run(args []string) int {
return 1
}
+ if len(exportedServices) == 0 {
+ c.UI.Info("No exported services found")
+ return 0
+ }
+
+ if c.table {
+ tbl := cli.NewTable("Service", "Partition", "Namespace", "Consumers")
+ for _, expService := range exportedServices {
+ for _, peer := range expService.Consumers.Peers {
+ tbl.AddRow([]string{expService.Service, expService.Partition, expService.Namespace, fmt.Sprintf("Peer(%s)", peer)}, []string{})
+ }
+ for _, partition := range expService.Consumers.Partitions {
+ tbl.AddRow([]string{expService.Service, expService.Partition, expService.Namespace, fmt.Sprintf("Partition(%s)", partition)}, []string{})
+ }
+ }
+ c.UI.Table(tbl)
+ return 0
+ }
+
b, err := json.MarshalIndent(exportedServices, "", " ")
if err != nil {
c.UI.Error("Failed to encode output data")
diff --git a/command/exportedservices/exported_services_test.go b/command/exportedservices/exported_services_test.go
index 23ad13b678..83751ef266 100644
--- a/command/exportedservices/exported_services_test.go
+++ b/command/exportedservices/exported_services_test.go
@@ -79,4 +79,5 @@ func TestExportedServices(t *testing.T) {
require.Equal(t, "db", resp[0].Service)
require.Equal(t, "web", resp[1].Service)
require.Equal(t, []string{"east", "west"}, resp[0].Consumers.Peers)
+ require.Equal(t, []string{"east", "west"}, resp[1].Consumers.Peers)
} |
@absolutelightning Updated the code to have two format options. This is similar to other cli commands |
Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
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.
LGTM 🚀
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.
I reviewed the docs edits for the API and CLI command. Please implement the suggested changes.
Approving on behalf of consul-docs
Backport failed @tauhid621. Run: https://github.com/hashicorp/consul/actions/runs/7794306924 |
* Exported services CLI and docs * Changelog added * Added format option for pretty print * Update command/exportedservices/exported_services.go Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com> * Addressing PR comments, moving the command under services category * Add consumer peer and partition filter * Adding bexpr filter, change format of data --------- Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
* Exported services CLI and docs * Changelog added * Added format option for pretty print * Update command/exportedservices/exported_services.go Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com> * Addressing PR comments, moving the command under services category * Add consumer peer and partition filter * Adding bexpr filter, change format of data --------- Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
…#20331) (#20494) Exported services CLI and docs (#20331) * Exported services CLI and docs * Changelog added * Added format option for pretty print * Update command/exportedservices/exported_services.go * Addressing PR comments, moving the command under services category * Add consumer peer and partition filter * Adding bexpr filter, change format of data --------- Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
…#20331) (#20493) Exported services CLI and docs (#20331) * Exported services CLI and docs * Changelog added * Added format option for pretty print * Update command/exportedservices/exported_services.go * Addressing PR comments, moving the command under services category * Add consumer peer and partition filter * Adding bexpr filter, change format of data --------- Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
* Exported services CLI and docs * Changelog added * Added format option for pretty print * Update command/exportedservices/exported_services.go Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com> * Addressing PR comments, moving the command under services category * Add consumer peer and partition filter * Adding bexpr filter, change format of data --------- Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
Description
This PR adds a new cli command to list exported services
command:
consul services exported-services
The PR also adds the API docs for the exported services API.
CLI output
CE
Enterprise
CLI output json
Testing & Reproduction steps
Links
PR Checklist