diff --git a/commands/addons/attach.js b/commands/addons/attach.js index 95b6885..9f7ff7f 100644 --- a/commands/addons/attach.js +++ b/commands/addons/attach.js @@ -29,6 +29,13 @@ function * run (context, heroku) { ) } + if (context.flags.credential) { + let credentialConfig = yield heroku.get(`/addons/${addon.name}/config/credential:${context.flags.credential}`) + if (credentialConfig.length === 0) { + throw new Error(`Could not find credential ${context.flags.credential} for database ${addon.name}`) + } + } + let attachment = yield util.trapConfirmationRequired(context.app, context.flags.confirm, (confirm) => createAttachment(app, context.flags.as, confirm, context.flags.credential)) yield cli.action( diff --git a/test/commands/addons/attach.js b/test/commands/addons/attach.js index 220678c..7c3d115 100644 --- a/test/commands/addons/attach.js +++ b/test/commands/addons/attach.js @@ -72,6 +72,8 @@ Setting foo config vars and restarting myapp... done, v10 let api = nock('https://api.heroku.com:443') .get('/addons/postgres-123') .reply(200, {name: 'postgres-123'}) + .get('/addons/postgres-123/config/credential:hello') + .reply(200, [{some: 'config'}]) .post('/addon-attachments', {app: {name: 'myapp'}, addon: {name: 'postgres-123'}, namespace: 'credential:hello'}) .reply(201, {name: 'POSTGRES_HELLO'}) .get('/apps/myapp/releases') @@ -83,4 +85,18 @@ Setting POSTGRES_HELLO config vars and restarting myapp... done, v10 `)) .then(() => api.done()) }) + + it('errors if the credential flag is specified but that credential does not exist for that addon', function () { + nock('https://api.heroku.com:443') + .get('/addons/postgres-123') + .reply(200, {name: 'postgres-123'}) + .get('/addons/postgres-123/config/credential:hello') + .reply(200, []) + + return expect(cmd.run({ + app: 'myapp', + args: {addon_name: 'postgres-123'}, + flags: {credential: 'hello'} + }), 'to be rejected with error satisfying', new Error('Could not find credential hello for database postgres-123')) + }) })