-
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.
migrate(W-14169383): spaces: Upgrade trusted-ips:remove
- Loading branch information
1 parent
a124379
commit 9f759a0
Showing
5 changed files
with
81 additions
and
74 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 = heredoc(` | ||
Remove a range from the list of trusted IP ranges | ||
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.') | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/cli/test/unit/commands/spaces/trusted-ips/remove.unit.test.ts
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 |
---|---|---|
@@ -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() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
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
32 changes: 0 additions & 32 deletions
32
packages/spaces/test/unit/commands/trusted-ips/remove.unit.test.js
This file was deleted.
Oops, something went wrong.