diff --git a/clients/remote/remote.go b/clients/remote/remote.go index a5fc71f..e584d3c 100644 --- a/clients/remote/remote.go +++ b/clients/remote/remote.go @@ -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) diff --git a/clients/replication/replication.go b/clients/replication/replication.go index 14862eb..2e28060 100644 --- a/clients/replication/replication.go +++ b/clients/replication/replication.go @@ -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 diff --git a/cmd/influx/remote.go b/cmd/influx/remote.go index 7a5e4ea..350e8b8 100644 --- a/cmd/influx/remote.go +++ b/cmd/influx/remote.go @@ -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) }, } }