Skip to content

Commit

Permalink
migrate(W-14169383): spaces: Upgrade trusted-ips:remove
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby committed May 3, 2024
1 parent 274bc63 commit 3f67f5f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 74 deletions.
46 changes: 46 additions & 0 deletions packages/cli/src/commands/spaces/trusted-ips/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import heredoc from 'tsheredoc'

export default class Remove extends Command {
static aliases = ['trusted-ips:remove']
static topic = 'trusted-ips'
static description = 'Remove a range from the list of trusted IP ranges'
static help = 'Uses CIDR notation.'
static examples = [heredoc(`
$ heroku trusted-ips:remove --space my-space 192.168.2.0/24
Removed 192.168.2.0/24 from trusted IP ranges on my-space
`)]

static flags = {
space: flags.string({optional: false, description: 'space to remove rule from'}),
confirm: flags.string({description: 'set to space name to bypass confirm prompt'}),
}

static args = {
source: Args.string({required: true}),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse(Remove)
const space = flags.space
const url = `/spaces/${space}/inbound-ruleset`
const opts = {headers: {Accept: 'application/vnd.heroku+json; version=3.dogwood'}}
const {body: rules} = await this.heroku.get<Heroku.InboundRuleset>(url, opts)
if (rules.rules?.length === 0) {
throw new Error('No IP ranges are configured. Nothing to do.')
}

const originalLength = rules.rules?.length
rules.rules = rules.rules?.filter(r => r.source !== args.source)
if (rules.rules?.length === originalLength) {
throw new Error(`No IP range matching ${args.source} was found.`)
}

await this.heroku.put(url, {...opts, body: rules})
ux.log(`Removed ${color.cyan.bold(args.source)} from trusted IP ranges on ${color.cyan.bold(space)}`)
ux.warn('It may take a few moments for the changes to take effect.')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {expect} from '@oclif/test'
import * as nock from 'nock'
import {stdout} from 'stdout-stderr'
import heredoc from 'tsheredoc'
import Cmd from '../../../../../src/commands/spaces/trusted-ips/remove'
import runCommand from '../../../../helpers/runCommand'

describe('trusted-ips:remove', function () {
it('removes a CIDR entry from the trusted IP ranges', async function () {
const api = nock('https://api.heroku.com:443')
.get('/spaces/my-space/inbound-ruleset')
.reply(200, {
created_by: 'dickeyxxx',
rules: [
{source: '128.0.0.1/20', action: 'allow'},
{source: '127.0.0.1/20', action: 'allow'},
],
},
)
.put('/spaces/my-space/inbound-ruleset', {
created_by: 'dickeyxxx',
rules: [
{source: '128.0.0.1/20', action: 'allow'},
],
})
.reply(200, {rules: []})
await runCommand(Cmd, ['127.0.0.1/20', '--space', 'my-space'])
expect(stdout.output).to.eq(heredoc(`
Removed 127.0.0.1/20 from trusted IP ranges on my-space
`))
api.done()
})
})
41 changes: 0 additions & 41 deletions packages/spaces/commands/trusted-ips/remove.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ exports.commands = [
require('./commands/vpn/update'),
require('./commands/drains/get'),
require('./commands/trusted-ips'),
require('./commands/trusted-ips/remove'),
]
32 changes: 0 additions & 32 deletions packages/spaces/test/unit/commands/trusted-ips/remove.unit.test.js

This file was deleted.

0 comments on commit 3f67f5f

Please sign in to comment.