-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for passing --app or --space to delete drains
- Loading branch information
Showing
2 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,67 @@ | ||
import {Command} from '@heroku-cli/command' | ||
import {flags, Command} from '@heroku-cli/command' | ||
import {Args, ux} from '@oclif/core' | ||
import {TelemetryDrain} from '../../lib/types/telemetry' | ||
import heredoc from 'tsheredoc' | ||
|
||
export default class Remove extends Command { | ||
static topic = 'telemetry' | ||
static description = 'remove a telemetry drain' | ||
static args = { | ||
telemetry_drain_id: Args.string({required: true, description: 'ID of the drain to remove'}), | ||
}; | ||
telemetry_drain_id: Args.string({description: 'ID of the drain to remove'}), | ||
} | ||
|
||
static flags = { | ||
app: flags.app({description: 'name of the app to remove all drains from'}), | ||
space: flags.string({char: 's', description: 'name of the space to remove all drains from'}), | ||
} | ||
|
||
public async run(): Promise<void> { | ||
const {args} = await this.parse(Remove) | ||
const {args, flags} = await this.parse(Remove) | ||
const {app, space} = flags | ||
const {telemetry_drain_id} = args | ||
const {body: telemetryDrain} = await this.heroku.get<TelemetryDrain>(`/telemetry-drains/${telemetry_drain_id}`, { | ||
headers: { | ||
Accept: 'application/vnd.heroku+json; version=3.fir', | ||
}, | ||
}) | ||
if (!(app || space || telemetry_drain_id)) { | ||
ux.error(heredoc(` | ||
Requires either --app or --space or a TELEMETRY_DRAIN_ID to be provided. | ||
See more help with --help | ||
`)) | ||
} | ||
|
||
ux.action.start(`Removing telemetry drain ${telemetry_drain_id}, which was configured for ${telemetryDrain.owner.type} ${telemetryDrain.owner.name}`) | ||
if (telemetry_drain_id) { | ||
const telemetryDrain = await this.removeDrain(telemetry_drain_id) | ||
ux.action.start(`Removing telemetry drain ${telemetry_drain_id}, which was configured for ${telemetryDrain.owner.type} ${telemetryDrain.owner.name}`) | ||
} else if (app) { | ||
ux.action.start(`Removing all telemetry drains from app ${app}`) | ||
const {body: telemetryDrains} = await this.heroku.get<TelemetryDrain[]>(`/apps/${app}/telemetry-drains`, { | ||
headers: { | ||
Accept: 'application/vnd.heroku+json; version=3.fir', | ||
}, | ||
}) | ||
|
||
await this.heroku.delete<TelemetryDrain>(`/telemetry-drains/${telemetry_drain_id}`, { | ||
for (const telemetryDrain of telemetryDrains) { | ||
await this.removeDrain(telemetryDrain.id) | ||
} | ||
} else if (space) { | ||
ux.action.start(`Removing all telemetry drains from space ${space}`) | ||
const {body: telemetryDrains} = await this.heroku.get<TelemetryDrain[]>(`/spaces/${space}/telemetry-drains`, { | ||
headers: { | ||
Accept: 'application/vnd.heroku+json; version=3.fir', | ||
}, | ||
}) | ||
|
||
for (const telemetryDrain of telemetryDrains) { | ||
await this.removeDrain(telemetryDrain.id) | ||
} | ||
} | ||
|
||
ux.action.stop() | ||
} | ||
|
||
protected async removeDrain(telemetry_drain_id: string) { | ||
const {body: telemetryDrain} = await this.heroku.delete<TelemetryDrain>(`/telemetry-drains/${telemetry_drain_id}`, { | ||
headers: { | ||
Accept: 'application/vnd.heroku+json; version=3.fir', | ||
}, | ||
}) | ||
|
||
ux.action.stop() | ||
return telemetryDrain | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters