Skip to content

Commit

Permalink
Allow for passing --app or --space to delete drains
Browse files Browse the repository at this point in the history
  • Loading branch information
eablack committed Oct 16, 2024
1 parent bdb30f1 commit 9c6b4d5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 15 deletions.
62 changes: 49 additions & 13 deletions packages/cli/src/commands/telemetry/remove.ts
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
}
}
68 changes: 66 additions & 2 deletions packages/cli/test/unit/commands/telemetry/remove.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import runCommand from '../../../helpers/runCommand'
import * as nock from 'nock'
import expectOutput from '../../../helpers/utils/expectOutput'
import heredoc from 'tsheredoc'
import {TelemetryDrain} from '../../../../src/lib/types/telemetry'
import {expect} from 'chai'

import {TelemetryDrain, TelemetryDrains} from '../../../../src/lib/types/telemetry'

describe('telemetry:remove', function () {
const appId = '87654321-5717-4562-b3fc-2c963f66afa6'
const spaceId = '12345678-5717-4562-b3fc-2c963f66afa6'
let appTelemetryDrain: TelemetryDrain
let appTelemetryDrainTwo: TelemetryDrain
let spaceTelemetryDrain: TelemetryDrain
let appTelemetryDrains: TelemetryDrains
let spaceTelemetryDrains: TelemetryDrains

beforeEach(function () {
spaceTelemetryDrain = {
Expand Down Expand Up @@ -39,6 +44,21 @@ describe('telemetry:remove', function () {
},
},
}
appTelemetryDrainTwo = {
id: '55555f64-5717-4562-b3fc-2c963f66afa6',
owner: {id: appId, type: 'app', name: 'myapp'},
capabilities: ['logs'],
exporter: {
type: 'otlphttp',
endpoint: 'https://api.papertrail.com/',
headers: {
'x-papertrail-team': 'your-api-key',
'x-papertrail-dataset': 'your-dataset',
},
},
}
appTelemetryDrains = [appTelemetryDrain, appTelemetryDrainTwo]
spaceTelemetryDrains = [spaceTelemetryDrain]
})

afterEach(function () {
Expand Down Expand Up @@ -68,7 +88,7 @@ describe('telemetry:remove', function () {
.reply(200, appTelemetryDrain)
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.delete(`/telemetry-drains/${appTelemetryDrain.id}`)
.reply(200, spaceTelemetryDrain)
.reply(200, appTelemetryDrain)

await runCommand(Cmd, [
appTelemetryDrain.id,
Expand All @@ -78,4 +98,48 @@ describe('telemetry:remove', function () {
Removing telemetry drain ${appTelemetryDrain.id}, which was configured for app ${appTelemetryDrain.owner.name}... done
`))
})

it('deletes all drains from an app', async function () {
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.get(`/apps/${appId}/telemetry-drains`)
.reply(200, appTelemetryDrains)
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.delete(`/telemetry-drains/${appTelemetryDrain.id}`)
.reply(200, appTelemetryDrain)
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.delete(`/telemetry-drains/${appTelemetryDrainTwo.id}`)
.reply(200, appTelemetryDrainTwo)

await runCommand(Cmd, [
'--app', appId,
])
expectOutput(stderr.output, heredoc(`
Removing all telemetry drains from app ${appId}...
Removing all telemetry drains from app ${appId}... done
`))
})

it('deletes all drains from a space', async function () {
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.get(`/spaces/${spaceId}/telemetry-drains`)
.reply(200, spaceTelemetryDrains)
nock('https://api.heroku.com', {reqheaders: {Accept: 'application/vnd.heroku+json; version=3.fir'}})
.delete(`/telemetry-drains/${spaceTelemetryDrain.id}`)
.reply(200, spaceTelemetryDrain)

await runCommand(Cmd, [
'--space', spaceId,
])
expectOutput(stderr.output, heredoc(`
Removing all telemetry drains from space ${spaceId}...
Removing all telemetry drains from space ${spaceId}... done
`))
})

it('requires a telemetry id, an app id, or a space id', async function () {
const errorMessage = 'Requires either --app or --space or a TELEMETRY_DRAIN_ID to be provided.'
await runCommand(Cmd, []).catch(error => {
expect(error.message).to.contain(errorMessage)
})
})
})

0 comments on commit 9c6b4d5

Please sign in to comment.