feat: added functionality for remote list
subcommand (#273)
* feat: add remote list subcommand functionality * refactor: removed repeated code and improved help text
This commit is contained in:
parent
9a008c6b26
commit
65cca47ded
@ -2,6 +2,7 @@ package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
@ -27,31 +28,15 @@ type CreateParams struct {
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
// get org id via org name
|
||||
if params.OrgID == "" {
|
||||
name := params.OrgName
|
||||
if name == "" {
|
||||
name = c.ActiveConfig.Org
|
||||
}
|
||||
res, err := c.GetOrgs(ctx).Org(name).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup ID of org %q: %w", name, err)
|
||||
}
|
||||
orgs := res.GetOrgs()
|
||||
if len(orgs) == 0 {
|
||||
return fmt.Errorf("no organization found with name %q", name)
|
||||
}
|
||||
params.OrgID = orgs[0].GetId()
|
||||
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.RemoteConnectionCreationRequest{
|
||||
Name: params.Name,
|
||||
OrgID: params.OrgID,
|
||||
OrgID: orgID,
|
||||
RemoteURL: params.RemoteURL,
|
||||
RemoteAPIToken: params.RemoteAPIToken,
|
||||
RemoteOrgID: params.RemoteOrgID,
|
||||
@ -67,10 +52,55 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create remote connection %q: %w", params.Name, err)
|
||||
}
|
||||
|
||||
// print confirmation of new connection
|
||||
return c.printRemote(printRemoteOpts{remote: &res})
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
Name string
|
||||
OrgID string
|
||||
OrgName string
|
||||
RemoteURL 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 query params
|
||||
req := c.GetRemoteConnections(ctx)
|
||||
|
||||
req = req.OrgID(orgID)
|
||||
|
||||
if params.Name != "" {
|
||||
req = req.Name(params.Name)
|
||||
}
|
||||
|
||||
if params.RemoteURL != "" {
|
||||
req = req.RemoteURL(params.RemoteURL)
|
||||
}
|
||||
|
||||
// send get request
|
||||
res, err := req.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get remote connections: %w", err)
|
||||
}
|
||||
|
||||
// print connections
|
||||
printOpts := printRemoteOpts{}
|
||||
if res.Remotes != nil {
|
||||
printOpts.remotes = *res.Remotes
|
||||
} else {
|
||||
return errors.New("no remote connections found for specified parameters")
|
||||
}
|
||||
|
||||
return c.printRemote(printOpts)
|
||||
}
|
||||
|
||||
type printRemoteOpts struct {
|
||||
remote *api.RemoteConnection
|
||||
remotes []api.RemoteConnection
|
||||
|
@ -78,10 +78,12 @@ func newRemoteCreateCmd() cli.Command {
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
api := getAPI(ctx)
|
||||
|
||||
client := remote.Client{
|
||||
CLI: getCLI(ctx),
|
||||
RemoteConnectionsApi: getAPI(ctx).RemoteConnectionsApi,
|
||||
OrganizationsApi: getAPI(ctx).OrganizationsApi,
|
||||
RemoteConnectionsApi: api.RemoteConnectionsApi,
|
||||
OrganizationsApi: api.OrganizationsApi,
|
||||
}
|
||||
|
||||
return client.Create(getContext(ctx), ¶ms)
|
||||
@ -102,14 +104,47 @@ func newRemoteDeleteCmd() cli.Command {
|
||||
}
|
||||
|
||||
func newRemoteListCmd() cli.Command {
|
||||
var params remote.ListParams
|
||||
return cli.Command{
|
||||
Name: "list",
|
||||
Usage: "List all remote connections",
|
||||
Aliases: []string{"find", "ls"},
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: commonFlags(),
|
||||
Action: func(ctx *cli.Context) {
|
||||
fmt.Println("remote list command was called")
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "Filter results to only connections 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-url",
|
||||
Usage: "Filter results to only connections for a specific remote URL",
|
||||
Destination: ¶ms.RemoteURL,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
api := getAPI(ctx)
|
||||
|
||||
client := remote.Client{
|
||||
CLI: getCLI(ctx),
|
||||
RemoteConnectionsApi: api.RemoteConnectionsApi,
|
||||
OrganizationsApi: api.OrganizationsApi,
|
||||
}
|
||||
|
||||
return client.List(getContext(ctx), ¶ms)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user