Skip to content
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

Merged
merged 8 commits into from
Feb 6, 2024

Conversation

tauhid621
Copy link
Contributor

@tauhid621 tauhid621 commented Jan 24, 2024

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

$ ./bin/consul services exported-services                          
Service   Consumer Peers
backend   east, west
db        west
frontend  east, east-eu
web       east

Enterprise

$ ./bin/consul services exported-services                          
Service   Partition  Namespace  Consumer Peers  Consumer Partitions
backend   default    default    east, west      
db        default    default    west            partition-west
frontend  default    default    east, east-eu   
frontend  default    ns         east, east-eu   
web       default    default    east 

CLI output json

$ ./bin/consul services exported-services -format=json
[
    {
        "Service": "backend",
        "Consumers": {
            "Peers": [
                "east",
                "west"
            ]
        }
    },
    {
        "Service": "db",
        "Consumers": {
            "Peers": [
                "west"
            ]
        }
    },
    {
        "Service": "frontend",
        "Consumers": {
            "Peers": [
                "east",
                "east-eu"
            ]
        }
    },
    {
        "Service": "web",
        "Consumers": {
            "Peers": [
                "east"
            ]
        }
    }
]

Testing & Reproduction steps

  • Tests added
  • Manual testing

Links

PR Checklist

  • updated test coverage
  • external facing docs updated
  • appropriate backport labels added
  • not a security concern

@tauhid621 tauhid621 requested a review from a team as a code owner January 24, 2024 09:22
@github-actions github-actions bot added type/docs Documentation needs to be created/updated/clarified theme/cli Flags and documentation for the CLI interface labels Jan 24, 2024
@tauhid621 tauhid621 added the backport/1.17 This release series is no longer active on CE. Use backport/ent/1.17. label Jan 24, 2024
@tauhid621 tauhid621 added the consul-india PRs/Issues assigned to Consul India team label Jan 24, 2024
@absolutelightning
Copy link
Contributor

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

[
    {
        "Service": "consul",
        "Consumers": {
            "Peers": [
                "consul"
            ]
        }
    },
    {
        "Service": "batman",
        "Consumers": {
            "Peers": [
                "superman"
            ]
        }
    },
    {
        "Service": "service1",
        "Partition": "part",
        "Consumers": {
            "Peers": [
                "superman",
                "heman"
            ],
            "Partitions": [
                "part1",
                "part2"
            ]
        }
    },
    {
        "Service": "service1",
        "Partition": "part",
        "Consumers": {
            "Peers": [
                "superman",
                "heman"
            ],
            "Partitions": [
                "part1",
                "part2"
            ]
        }
    },
    {
        "Service": "service1",
        "Partition": "part",
        "Consumers": {
            "Peers": [
                "superman",
                "heman"
            ],
            "Partitions": [
                "part1",
                "part2"
            ]
        }
    }
]

Comparing this to table structure - it feels concise.

Service 	Partition	Namespace	Consumers
consul  	         	         	Peer(consul)
batman  	         	         	Peer(superman)
service1	part     	         	Peer(superman)
service1	part     	         	Peer(heman)
service1	part     	         	Partition(part1)
service1	part     	         	Partition(part2)
service1	part     	         	Peer(superman)
service1	part     	         	Peer(heman)
service1	part     	         	Partition(part1)
service1	part     	         	Partition(part2)
service1	part     	         	Peer(superman)
service1	part     	         	Peer(heman)
service1	part     	         	Partition(part1)
service1	part     	         	Partition(part2)

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

@tauhid621
Copy link
Contributor Author

@absolutelightning Updated the code to have two format options. This is similar to other cli commands
The default command prints the table version. I also had to use mitchellh/cli as it was difficult to test with the cli tool which you used.

Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com>
Copy link
Contributor

@absolutelightning absolutelightning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Copy link
Contributor

@boruszak boruszak left a 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

website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/api-docs/exported-services.mdx Outdated Show resolved Hide resolved
website/content/commands/exported-services.mdx Outdated Show resolved Hide resolved
website/content/commands/exported-services.mdx Outdated Show resolved Hide resolved
@tauhid621 tauhid621 merged commit 0c509a6 into main Feb 6, 2024
97 checks passed
@tauhid621 tauhid621 deleted the tauhid621_exported_services_docs_and_cli branch February 6, 2024 03:31
@hc-github-team-consul-core
Copy link
Collaborator

tauhid621 added a commit that referenced this pull request Feb 6, 2024
* 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>
tauhid621 added a commit that referenced this pull request Feb 6, 2024
* 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>
tauhid621 added a commit that referenced this pull request Feb 6, 2024
…#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>
tauhid621 added a commit that referenced this pull request Feb 6, 2024
…#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>
tauhid621 added a commit that referenced this pull request Feb 6, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/1.17 This release series is no longer active on CE. Use backport/ent/1.17. consul-india PRs/Issues assigned to Consul India team theme/cli Flags and documentation for the CLI interface type/docs Documentation needs to be created/updated/clarified
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants