feat: allow deleting replications with remotes (#417)
This commit is contained in:
parent
78ef3c127c
commit
0b6ce21cfe
@ -151,10 +151,18 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
return c.printRemote(printOpts)
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, remoteID string) error {
|
||||
connection, err := c.GetRemoteConnectionByID(ctx, remoteID).Execute()
|
||||
func (c Client) Get(ctx context.Context, remoteID string) (api.RemoteConnection, error) {
|
||||
conn, err := c.GetRemoteConnectionByID(ctx, remoteID).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete remote connection %q: %w", remoteID, err)
|
||||
return conn, fmt.Errorf("failed to find remote connection with ID %q: %w", remoteID, err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, remoteID string) error {
|
||||
connection, err := c.Get(ctx, remoteID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req := c.DeleteRemoteConnectionByID(ctx, remoteID)
|
||||
|
@ -190,6 +190,30 @@ func (c Client) Delete(ctx context.Context, replicationID string) error {
|
||||
return c.printReplication(printOpts)
|
||||
}
|
||||
|
||||
func (c Client) DeleteWithRemoteID(ctx context.Context, conn api.RemoteConnection) error {
|
||||
reps, err := c.GetReplications(ctx).OrgID(conn.OrgID).RemoteID(conn.Id).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find replication streams with remote ID %q: %w", conn.Id, err)
|
||||
}
|
||||
|
||||
if reps.Replications != nil {
|
||||
for _, rep := range reps.GetReplications() {
|
||||
if err := c.DeleteReplicationByID(ctx, rep.Id).Execute(); err != nil {
|
||||
return fmt.Errorf("failed to delete replication with ID %q: %w", rep.Id, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("no replications found for remote ID %q", conn.Id)
|
||||
}
|
||||
|
||||
printOpts := printReplicationOpts{
|
||||
replications: reps.GetReplications(),
|
||||
deleted: true,
|
||||
}
|
||||
|
||||
return c.printReplication(printOpts)
|
||||
}
|
||||
|
||||
type printReplicationOpts struct {
|
||||
replication *api.Replication
|
||||
replications []api.Replication
|
||||
|
@ -1,7 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/clients/remote"
|
||||
"github.com/influxdata/influx-cli/v2/clients/replication"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@ -82,6 +85,7 @@ func newRemoteCreateCmd() cli.Command {
|
||||
|
||||
func newRemoteDeleteCmd() cli.Command {
|
||||
var remoteID string
|
||||
var deleteReplications bool
|
||||
return cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "Delete an existing remote connection",
|
||||
@ -94,16 +98,40 @@ func newRemoteDeleteCmd() cli.Command {
|
||||
Required: true,
|
||||
Destination: &remoteID,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "delete-replications",
|
||||
Usage: "Forcefully deletes any replication queues that depend on this remote",
|
||||
Destination: &deleteReplications,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
api := getAPI(ctx)
|
||||
cli := getCLI(ctx)
|
||||
context := getContext(ctx)
|
||||
|
||||
client := remote.Client{
|
||||
CLI: getCLI(ctx),
|
||||
CLI: cli,
|
||||
RemoteConnectionsApi: api.RemoteConnectionsApi,
|
||||
}
|
||||
|
||||
return client.Delete(getContext(ctx), remoteID)
|
||||
if deleteReplications {
|
||||
conn, err := client.Get(context, remoteID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repClient := replication.Client{
|
||||
CLI: cli,
|
||||
ReplicationsApi: api.ReplicationsApi,
|
||||
}
|
||||
if err := repClient.DeleteWithRemoteID(context, conn); err != nil {
|
||||
fmt.Fprintln(cli.StdIO, err.Error())
|
||||
// do not exit, still try to delete the remote, as this flag could be
|
||||
// passed with no replications configured, which would error here
|
||||
}
|
||||
}
|
||||
|
||||
return client.Delete(context, remoteID)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user