feat: added functionality for replication create command (#289)

* feat: added functionality for replication create command

* refactor: changed replication create flag usage text and eliminated extra conditionals

* refactor: made changes to usage text and output table based on code review

* chore: fixed link to max queue size default value
This commit is contained in:
mcfarlm3
2021-10-06 10:48:57 -07:00
committed by GitHub
parent 857e2b356c
commit 4e8671dcc5
2 changed files with 168 additions and 3 deletions

View File

@ -0,0 +1,106 @@
package replication
import (
"context"
"fmt"
"github.com/influxdata/influx-cli/v2/api"
"github.com/influxdata/influx-cli/v2/clients"
)
type Client struct {
clients.CLI
api.ReplicationsApi
api.OrganizationsApi
}
type CreateParams struct {
Name string
Description string
OrgID string
OrgName string
RemoteID string
LocalBucketID string
RemoteBucketID string
MaxQueueSize int64
}
func (c Client) Create(ctx context.Context, params *CreateParams) error {
orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
if err != nil {
return err
}
// set up a struct with required params
body := api.ReplicationCreationRequest{
Name: params.Name,
OrgID: orgID,
RemoteID: params.RemoteID,
LocalBucketID: params.LocalBucketID,
RemoteBucketID: params.RemoteBucketID,
MaxQueueSizeBytes: params.MaxQueueSize,
}
// set optional params if specified
if params.Description != "" {
body.Description = &params.Description
}
// send post request
res, err := c.PostReplication(ctx).ReplicationCreationRequest(body).Execute()
if err != nil {
return fmt.Errorf("failed to create replication stream %q: %w", params.Name, err)
}
// print confirmation of new replication stream
return c.printReplication(printReplicationOpts{replication: &res})
}
type printReplicationOpts struct {
replication *api.Replication
replications []api.Replication
deleted bool
}
func (c Client) printReplication(opts printReplicationOpts) error {
if c.PrintAsJSON {
var v interface{}
if opts.replication != nil {
v = opts.replication
} else {
v = opts.replications
}
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"}
if opts.deleted {
headers = append(headers, "Deleted")
}
if opts.replication != nil {
opts.replications = append(opts.replications, *opts.replication)
}
var rows []map[string]interface{}
for _, r := range opts.replications {
row := map[string]interface{}{
"ID": r.GetId(),
"Name": r.GetName(),
"Org ID": r.GetOrgID(),
"Remote ID": r.GetRemoteID(),
"Local Bucket ID": r.GetLocalBucketID(),
"Remote Bucket ID": r.GetRemoteBucketID(),
"Max Queue Bytes": r.GetMaxQueueSizeBytes(),
"Current Queue Bytes": r.GetCurrentQueueSizeBytes(),
"Latest Status Code": r.GetLatestResponseCode(),
}
if opts.deleted {
row["Deleted"] = true
}
rows = append(rows, row)
}
return c.PrintTable(headers, rows...)
}