Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(spaces): Move command spaces:info to oclif #2830

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/cli/src/commands/spaces/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import heredoc from 'tsheredoc'
import {renderInfo} from '../../lib/spaces/spaces'
import debug from 'debug'

const pgDebug = debug('pg')

export default class Info extends Command {
static topic = 'spaces'
static description = 'show info about a space'
static example = '$ heroku spaces:info my-space'

static flags = {
space: flags.string({char: 's', description: 'space to get info of'}),
json: flags.boolean({description: 'output in json format'}),
}

static args = {
space: Args.string({hidden: true}),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse(Info)
const spaceName = flags.space || args.space
if (!spaceName) {
ux.error(heredoc(`
Error: Missing 1 required arg:
space
See more help with --help
`))
}

let headers = {}
if (!flags.json) {
headers = {'Accept-Expansion': 'region'}
}

const {body: space} = await this.heroku.get<Heroku.Space>(`/spaces/${spaceName}`, {headers})
if (space.state === 'allocated') {
try {
const {body: outbound_ips} = await this.heroku.get<Heroku.SpaceNetworkAddressTranslation>(`/spaces/${spaceName}/nat`)
space.outbound_ips = outbound_ips
} catch (error) {
pgDebug(`Retrieving NAT details for the space failed with ${error}`)
}
}

renderInfo(space, flags.json)
}
}
23 changes: 23 additions & 0 deletions packages/cli/src/lib/spaces/spaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Heroku from '@heroku-cli/schema'
import {ux} from '@oclif/core'

export function displayShieldState(space: Heroku.Space) {
return space.shield ? 'on' : 'off'
Expand All @@ -9,3 +10,25 @@ export function displayNat(nat?: Required<Heroku.SpaceNetworkAddressTranslation>
if (nat.state !== 'enabled') return nat.state
return nat.sources.join(', ')
}

export function renderInfo(space: Heroku.Space, json: boolean) {
if (json) {
ux.log(JSON.stringify(space, null, 2))
} else {
ux.styledHeader(space.name || '')
ux.styledObject(
{
ID: space.id,
Team: space.team?.name,
Region: space.region?.description,
CIDR: space.cidr,
'Data CIDR': space.data_cidr,
State: space.state,
Shield: displayShieldState(space),
'Outbound IPs': displayNat(space.outbound_ips),
'Created at': space.created_at,
},
['ID', 'Team', 'Region', 'CIDR', 'Data CIDR', 'State', 'Shield', 'Outbound IPs', 'Created at'],
)
}
}
44 changes: 44 additions & 0 deletions packages/cli/test/fixtures/spaces/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as Heroku from '@heroku-cli/schema'

export const spaces: Record<string, Required<Heroku.Space>> = {
'non-shield-space': {
id: '1234',
name: 'my-unshielded-space',
shield: false,
region: {
description: 'virginia',
name: 'us',
},
team: {
name: 'my-team',
},
cidr: '10.0.0.0/16',
data_cidr: '172.23.0.0/20',
state: 'allocated',
organization: {
name: 'my-org',
},
created_at: '2016-01-06T03:23:13Z',
updated_at: '2016-01-06T03:23:13Z',
},
'shield-space': {
id: '1234',
name: 'my-shielded-space',
shield: true,
region: {
description: 'virginia',
name: 'us',
},
team: {
name: 'my-team',
},
cidr: '10.0.0.0/16',
data_cidr: '172.23.0.0/20',
state: 'allocated',
organization: {
name: 'my-org',
},
created_at: '2016-01-06T03:23:13Z',
updated_at: '2016-01-06T03:23:13Z',
},
}
164 changes: 164 additions & 0 deletions packages/cli/test/unit/commands/spaces/info.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import {stdout} from 'stdout-stderr'
import Cmd from '../../../../src/commands/spaces/info'
import runCommand from '../../../helpers/runCommand'
import * as nock from 'nock'
import heredoc from 'tsheredoc'
import expectOutput from '../../../helpers/utils/expectOutput'
import * as fixtures from '../../../fixtures/spaces/fixtures'

describe('spaces:info', function () {
const space = fixtures.spaces['non-shield-space']
const shieldSpace = fixtures.spaces['shield-space']

it('shows space info', async function () {
nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'region'}})
.get(`/spaces/${space.name}`)
.reply(200, space)

await runCommand(Cmd, [
'--space',
space.name,
])
expectOutput(stdout.output, heredoc(`
=== ${space.name}
ID: ${space.id}
Team: ${space.team.name}
Region: ${space.region.description}
CIDR: ${space.cidr}
Data CIDR: ${space.data_cidr}
State: ${space.state}
Shield: off
Created at: ${space.created_at}
`))
})

it('shows space info --json', async function () {
nock('https://api.heroku.com:443')
.get(`/spaces/${space.name}`)
.reply(200, space)

await runCommand(Cmd, [
'--space',
space.name,
'--json',
])
expectOutput(stdout.output, JSON.stringify(space, null, 2))
})

it('shows allocated space with enabled nat', async function () {
nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'region'}})
.get(`/spaces/${space.name}`)
.reply(200, space)
nock('https://api.heroku.com')
.get(`/spaces/${space.name}/nat`)
.reply(200, {state: 'enabled', sources: ['123.456.789.123']})
await runCommand(Cmd, [
'--space',
space.name,
])

expectOutput(stdout.output, heredoc(`
=== ${space.name}
ID: ${space.id}
Team: ${space.team.name}
Region: ${space.region.description}
CIDR: ${space.cidr}
Data CIDR: ${space.data_cidr}
State: ${space.state}
Shield: off
Outbound IPs: 123.456.789.123
Created at: ${space.created_at}
`))
})

it('shows allocated space with disabled nat', async function () {
nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'region'}})
.get(`/spaces/${space.name}`)
.reply(200, space)
nock('https://api.heroku.com')
.get(`/spaces/${space.name}/nat`)
.reply(200, {state: 'disabled', sources: ['123.456.789.123']})

await runCommand(Cmd, [
'--space',
space.name,
])
expectOutput(stdout.output, heredoc(`
=== ${space.name}
ID: ${space.id}
Team: ${space.team.name}
Region: ${space.region.description}
CIDR: ${space.cidr}
Data CIDR: ${space.data_cidr}
State: ${space.state}
Shield: off
Outbound IPs: disabled
Created at: ${space.created_at}
`))
})

it('shows a space with Shield turned off', async function () {
nock('https://api.heroku.com:443')
.get(`/spaces/${space.name}`)
.reply(200, space)

await runCommand(Cmd, [
'--space',
space.name,
])
expectOutput(stdout.output, heredoc(`
=== ${space.name}
ID: ${space.id}
Team: ${space.team.name}
Region: ${space.region.description}
CIDR: ${space.cidr}
Data CIDR: ${space.data_cidr}
State: ${space.state}
Shield: off
Created at: ${space.created_at}
`))
})

it('shows a space with Shield turned on', async function () {
nock('https://api.heroku.com')
.get(`/spaces/${shieldSpace.name}`)
.reply(200, shieldSpace)
await runCommand(Cmd, [
'--space',
shieldSpace.name,
])

expectOutput(stdout.output, heredoc(`
=== ${shieldSpace.name}
ID: ${shieldSpace.id}
Team: ${shieldSpace.team.name}
Region: ${shieldSpace.region.description}
CIDR: ${shieldSpace.cidr}
Data CIDR: ${shieldSpace.data_cidr}
State: ${shieldSpace.state}
Shield: on
Created at: ${shieldSpace.created_at}
`))
})

it('test if nat API call fails ', async function () {
nock('https://api.heroku.com')
.get(`/spaces/${space.name}`)
.reply(200, space)
await runCommand(Cmd, [
'--space',
space.name,
])
expectOutput(stdout.output, heredoc(`
=== ${space.name}
ID: ${space.id}
Team: ${space.team.name}
Region: ${space.region.description}
CIDR: ${space.cidr}
Data CIDR: ${space.data_cidr}
State: ${space.state}
Shield: off
Created at: ${space.created_at}
`))
})
})
59 changes: 0 additions & 59 deletions packages/spaces/commands/info.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/spaces/commands/wait.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- mode: js; js-indent-level: 2; -*-
'use strict'
let cli = require('heroku-cli-util')
let info = require('./info')
const lib = require('../lib/spaces')
let wait = ms => new Promise(resolve => setTimeout(resolve, ms))

async function run(context, heroku) {
Expand Down Expand Up @@ -39,7 +39,7 @@ async function run(context, heroku) {

spinner.stop('done\n')

info.render(space, context.flags)
lib.renderInfo(space, context.flags)
_notify(context.flags.space || context.args.space)
}

Expand Down
1 change: 0 additions & 1 deletion packages/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports.topics = [
]

exports.commands = [
require('./commands/info'),
require('./commands/rename'),
require('./commands/wait'),
require('./commands/peering/info'),
Expand Down
Loading
Loading