From 64b1b03f8ff570a3d9e9c2ff5f95cc71ca61ab2a Mon Sep 17 00:00:00 2001 From: mcfarlm3 <58636946+mcfarlm3@users.noreply.github.com> Date: Wed, 6 Oct 2021 17:42:16 -0700 Subject: [PATCH] feat: added functionality for replication list command (#296) * feat: added functionality for replication list command * chore: ran make fmt * refactor: fixed comment for clarity * refactor: chained statements together to simplify code --- clients/remote/remote.go | 4 +-- clients/replication/replication.go | 54 ++++++++++++++++++++++++++++-- cmd/influx/replication.go | 44 ++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 9 deletions(-) diff --git a/clients/remote/remote.go b/clients/remote/remote.go index 89a32e9..f46c4fe 100644 --- a/clients/remote/remote.go +++ b/clients/remote/remote.go @@ -120,9 +120,7 @@ func (c Client) List(ctx context.Context, params *ListParams) error { } // set up query params - req := c.GetRemoteConnections(ctx) - - req = req.OrgID(orgID) + req := c.GetRemoteConnections(ctx).OrgID(orgID) if params.Name != "" { req = req.Name(params.Name) diff --git a/clients/replication/replication.go b/clients/replication/replication.go index e6865f8..c685164 100644 --- a/clients/replication/replication.go +++ b/clients/replication/replication.go @@ -2,6 +2,7 @@ package replication import ( "context" + "errors" "fmt" "github.com/influxdata/influx-cli/v2/api" @@ -56,6 +57,53 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error { return c.printReplication(printReplicationOpts{replication: &res}) } +type ListParams struct { + Name string + OrgID string + OrgName string + RemoteID string + LocalBucketID string +} + +func (c Client) List(ctx context.Context, params *ListParams) error { + + orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi) + if err != nil { + return err + } + + // set up params + req := c.GetReplications(ctx).OrgID(orgID) + + if params.Name != "" { + req = req.Name(params.Name) + } + + if params.RemoteID != "" { + req = req.RemoteID(params.RemoteID) + } + + if params.LocalBucketID != "" { + req = req.LocalBucketID(params.LocalBucketID) + } + + // send get request + res, err := req.Execute() + if err != nil { + return fmt.Errorf("failed to get replication streams: %w", err) + } + + // print replication stream info + printOpts := printReplicationOpts{} + if res.Replications != nil { + printOpts.replications = *res.Replications + } else { + return errors.New("no replication streams found for specified parameters") + } + + return c.printReplication(printOpts) +} + type printReplicationOpts struct { replication *api.Replication replications []api.Replication @@ -73,8 +121,8 @@ func (c Client) printReplication(opts printReplicationOpts) error { return c.PrintJSON(v) } - headers := []string{"ID", "Name", "Org ID", "Remote ID", "Local Bucket ID", "Remote Bucket ID", "Max Queue Bytes", - "Current Queue Bytes", "Latest Status Code"} + headers := []string{"ID", "Name", "Org ID", "Remote ID", "Local Bucket ID", "Remote Bucket ID", + "Current Queue Bytes", "Max Queue Bytes", "Latest Status Code"} if opts.deleted { headers = append(headers, "Deleted") } @@ -92,8 +140,8 @@ func (c Client) printReplication(opts printReplicationOpts) error { "Remote ID": r.GetRemoteID(), "Local Bucket ID": r.GetLocalBucketID(), "Remote Bucket ID": r.GetRemoteBucketID(), - "Max Queue Bytes": r.GetMaxQueueSizeBytes(), "Current Queue Bytes": r.GetCurrentQueueSizeBytes(), + "Max Queue Bytes": r.GetMaxQueueSizeBytes(), "Latest Status Code": r.GetLatestResponseCode(), } if opts.deleted { diff --git a/cmd/influx/replication.go b/cmd/influx/replication.go index 84a69d0..be0cf8f 100644 --- a/cmd/influx/replication.go +++ b/cmd/influx/replication.go @@ -105,14 +105,52 @@ func newReplicationDeleteCmd() cli.Command { } func newReplicationListCmd() cli.Command { + var params replication.ListParams return cli.Command{ Name: "list", Usage: "List all replication streams and corresponding metrics", Aliases: []string{"find", "ls"}, Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs), - Flags: commonFlags(), - Action: func(ctx *cli.Context) { - fmt.Println("replication list command was called") + Flags: append( + commonFlags(), + &cli.StringFlag{ + Name: "name, n", + Usage: "Filter results to only replication streams with a specific name", + Destination: ¶ms.Name, + }, + &cli.StringFlag{ + Name: "org-id", + Usage: "Local org ID", + EnvVar: "INFLUX_ORG_ID", + Destination: ¶ms.OrgID, + }, + &cli.StringFlag{ + Name: "org, o", + Usage: "Local org name", + EnvVar: "INFLUX_ORG", + Destination: ¶ms.OrgName, + }, + &cli.StringFlag{ + Name: "remote-id", + Usage: "Filter results to only replication streams for a specific remote connection", + Destination: ¶ms.RemoteID, + }, + &cli.StringFlag{ + Name: "local-bucket", + Usage: "Filter results to only replication streams for a specific local bucket", + Destination: ¶ms.LocalBucketID, + }, + ), + Action: func(ctx *cli.Context) error { + api := getAPI(ctx) + + client := replication.Client{ + CLI: getCLI(ctx), + ReplicationsApi: api.ReplicationsApi, + OrganizationsApi: api.OrganizationsApi, + } + + return client.List(getContext(ctx), ¶ms) }, } }