-
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-14179700): pg-v5: Upgrade pg:links:create (#2758)
* migrate(W-14179700): pg-v5: Upgrade pg:links:create * Update packages/cli/src/commands/pg/links/create.ts Co-authored-by: Santiago Bosio <[email protected]> * Moved help to description --------- Co-authored-by: Santiago Bosio <[email protected]>
- Loading branch information
1 parent
cff025d
commit f8deed5
Showing
8 changed files
with
177 additions
and
138 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
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,66 @@ | ||
import color from '@heroku-cli/color' | ||
import {Command, flags} from '@heroku-cli/command' | ||
import {Args, ux} from '@oclif/core' | ||
import heredoc from 'tsheredoc' | ||
import {addonResolver} from '../../../lib/addons/resolve' | ||
import {getAddon} from '../../../lib/pg/fetcher' | ||
import host from '../../../lib/pg/host' | ||
import type {AddOnAttachmentWithConfigVarsAndPlan, Link} from '../../../lib/pg/types' | ||
import {essentialPlan} from '../../../lib/pg/util' | ||
|
||
export default class Create extends Command { | ||
static topic = 'pg' | ||
static description = heredoc(` | ||
create a link between data stores | ||
Example: | ||
heroku pg:links:create HEROKU_REDIS_RED HEROKU_POSTGRESQL_CERULEAN | ||
`) | ||
|
||
static flags = { | ||
as: flags.string({description: 'name of link to create'}), | ||
app: flags.app({required: true}), | ||
remote: flags.remote(), | ||
} | ||
|
||
static args = { | ||
remote: Args.string({required: true}), | ||
database: Args.string({required: true}), | ||
} | ||
|
||
public async run(): Promise<void> { | ||
const {flags, args} = await this.parse(Create) | ||
const {app} = flags | ||
|
||
const service = async (remoteId: string) => { | ||
const addon = await addonResolver(this.heroku, app, remoteId) | ||
if (!addon.plan.name.match(/^heroku-(redis|postgresql)/)) | ||
throw new Error('Remote database must be heroku-redis or heroku-postgresql') | ||
return addon | ||
} | ||
|
||
const [db, target] = await Promise.all([ | ||
getAddon(this.heroku, app, args.database), | ||
service(args.remote), | ||
]) as [AddOnAttachmentWithConfigVarsAndPlan, AddOnAttachmentWithConfigVarsAndPlan] | ||
|
||
if (essentialPlan(db)) | ||
throw new Error('pg:links isn\u2019t available for Essential-tier databases.') | ||
if (essentialPlan(target)) | ||
throw new Error('pg:links isn\u2019t available for Essential-tier databases.') | ||
|
||
ux.action.start(`Adding link from ${color.yellow(target.name)} to ${color.yellow(db.name)}`) | ||
const {body: link} = await this.heroku.post<Link>(`/client/v11/databases/${db.id}/links`, { | ||
body: { | ||
target: target.name, | ||
as: flags.as, | ||
}, | ||
hostname: host(), | ||
}) | ||
|
||
if (link.message) { | ||
throw new Error(link.message) | ||
} | ||
|
||
ux.action.stop(`done, ${color.cyan(link.name)}`) | ||
} | ||
} |
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
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
90 changes: 90 additions & 0 deletions
90
packages/cli/test/unit/commands/pg/links/create.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,90 @@ | ||
import {stdout, stderr} from 'stdout-stderr' | ||
import heredoc from 'tsheredoc' | ||
import Cmd from '../../../../../src/commands/pg/links/create' | ||
import runCommand from '../../../../helpers/runCommand' | ||
|
||
import {expect} from 'chai' | ||
import * as nock from 'nock' | ||
|
||
describe('pg:links:create', async () => { | ||
let api: nock.Scope | ||
let pg: nock.Scope | ||
|
||
beforeEach(() => { | ||
api = nock('https://api.heroku.com') | ||
pg = nock('https://api.data.heroku.com') | ||
}) | ||
|
||
afterEach(() => { | ||
nock.cleanAll() | ||
api.done() | ||
pg.done() | ||
}) | ||
|
||
describe('on an essential database', () => { | ||
const addon = { | ||
id: 2, name: 'postgres-1', plan: {name: 'heroku-postgresql:basic'}, | ||
} | ||
|
||
it('errors when attempting to create a link', async () => { | ||
api.post('/actions/addon-attachments/resolve', { | ||
app: 'myapp', addon_attachment: 'heroku-postgres', addon_service: 'heroku-postgresql', | ||
}).reply(200, [{addon}]) | ||
|
||
api.post('/actions/addons/resolve', { | ||
app: 'myapp', | ||
addon: 'heroku-redis', | ||
}).reply(200, [addon]) | ||
|
||
try { | ||
await runCommand(Cmd, [ | ||
'--app', | ||
'myapp', | ||
'--as', | ||
'foobar', | ||
'heroku-redis', | ||
'heroku-postgres', | ||
]) | ||
} catch (error) { | ||
const {message} = error as { message: string } | ||
expect(message).to.equal('pg:links isn\u2019t available for Essential-tier databases.') | ||
} | ||
}) | ||
}) | ||
|
||
describe('on a production database', () => { | ||
const addon = { | ||
id: 1, name: 'postgres-1', plan: {name: 'heroku-postgresql:standard-0'}, | ||
} | ||
|
||
it('errors when attempting to create a link', async () => { | ||
api.post('/actions/addon-attachments/resolve', { | ||
app: 'myapp', | ||
addon_attachment: 'heroku-postgres', | ||
addon_service: 'heroku-postgresql', | ||
}).reply(200, [{addon}]) | ||
|
||
api.post('/actions/addons/resolve', { | ||
app: 'myapp', | ||
addon: 'heroku-redis', | ||
}).reply(200, [addon]) | ||
|
||
pg.post('/client/v11/databases/1/links', {target: 'postgres-1', as: 'foobar'}) | ||
.reply(200, {name: 'foobar'}) | ||
|
||
await runCommand(Cmd, [ | ||
'--app', | ||
'myapp', | ||
'--as', | ||
'foobar', | ||
'heroku-redis', | ||
'heroku-postgres', | ||
]) | ||
expect(stdout.output).to.equal('') | ||
expect(stderr.output).to.equal(heredoc(` | ||
Adding link from postgres-1 to postgres-1... | ||
Adding link from postgres-1 to postgres-1... done, foobar | ||
`)) | ||
}) | ||
}) | ||
}) |
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
68 changes: 0 additions & 68 deletions
68
packages/pg-v5/test/unit/commands/links/create.unit.test.js
This file was deleted.
Oops, something went wrong.