Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Add support for postgres credentials (#77)
Browse files Browse the repository at this point in the history
* Role flag for credential attachment

* Correct attach bug

* Attachment copy for credentials

* help text credential
  • Loading branch information
camillebaldock authored May 17, 2017
1 parent 3b52399 commit b1cd813
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
23 changes: 14 additions & 9 deletions commands/addons/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ function * run (context, heroku) {
let app = context.app
let addon = yield heroku.get(`/addons/${encodeURIComponent(context.args.addon_name)}`)

function createAttachment (app, as, confirm) {
function createAttachment (app, as, confirm, credential) {
let body = {
name: as,
app: {name: app},
addon: {name: addon.name},
confirm
}
if (credential) {
body.namespace = 'credential:' + credential
}
return cli.action(
`Attaching ${cli.color.addon(addon.name)}${as ? ' as ' + cli.color.attachment(as) : ''} to ${cli.color.app(app)}`,
`Attaching ${credential ? cli.color.addon(credential) + ' of ' : ''}${cli.color.addon(addon.name)}${as ? ' as ' + cli.color.attachment(as) : ''} to ${cli.color.app(app)}`,
heroku.request({
path: '/addon-attachments',
method: 'POST',
body: {
name: as,
app: {name: app},
addon: {name: addon.name},
confirm
}
body: body
})
)
}

let attachment = yield util.trapConfirmationRequired(context.app, context.flags.confirm, (confirm) => createAttachment(app, context.flags.as, confirm))
let attachment = yield util.trapConfirmationRequired(context.app, context.flags.confirm, (confirm) => createAttachment(app, context.flags.as, confirm, context.flags.credential))

yield cli.action(
`Setting ${cli.color.attachment(attachment.name)} config vars and restarting ${cli.color.app(app)}`,
Expand All @@ -48,6 +52,7 @@ module.exports = {
needsApp: true,
flags: [
{name: 'as', description: 'name for add-on attachment', hasValue: true},
{name: 'credential', description: 'credential name for scoped access to Heroku Postgres', hasValue: true},
{name: 'confirm', description: 'overwrite existing add-on attachment with same name', hasValue: true}
],
args: [{name: 'addon_name'}],
Expand Down
16 changes: 16 additions & 0 deletions test/commands/addons/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ Setting foo config vars and restarting myapp... done, v10
.then(() => expect(cli.stderr, 'to equal', `Attaching redis-123 as foo to myapp... !
Attaching redis-123 as foo to myapp... done
Setting foo config vars and restarting myapp... done, v10
`))
.then(() => api.done())
})

it('attaches in the credential namespace if the credential flag is specified', function () {
let api = nock('https://api.heroku.com:443')
.get('/addons/postgres-123')
.reply(200, {name: 'postgres-123'})
.post('/addon-attachments', {app: {name: 'myapp'}, addon: {name: 'postgres-123'}, namespace: 'credential:hello'})
.reply(201, {name: 'POSTGRES_HELLO'})
.get('/apps/myapp/releases')
.reply(200, [{version: 10}])
return cmd.run({app: 'myapp', args: {addon_name: 'postgres-123'}, flags: {credential: 'hello'}})
.then(() => expect(cli.stdout, 'to be empty'))
.then(() => expect(cli.stderr, 'to equal', `Attaching hello of postgres-123 to myapp... done
Setting POSTGRES_HELLO config vars and restarting myapp... done, v10
`))
.then(() => api.done())
})
Expand Down

0 comments on commit b1cd813

Please sign in to comment.