1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 |
|
6 | function * run (context, heroku) {
|
7 | const fetcher = require('../../lib/fetcher')(heroku)
|
8 | const host = require('../../lib/host')
|
9 | const util = require('../../lib/util')
|
10 |
|
11 | const {app, args, flags} = context
|
12 | let cred = flags.name
|
13 |
|
14 | if (cred === 'default') {
|
15 | throw new Error('Default credential cannot be destroyed.')
|
16 | }
|
17 |
|
18 | let db = yield fetcher.addon(app, args.database)
|
19 | if (util.starterPlan(db)) {
|
20 | throw new Error(`Only one default credential is supported for Hobby tier databases.`)
|
21 | }
|
22 |
|
23 | let attachments = yield heroku.get(`/addons/${db.name}/addon-attachments`)
|
24 | let credAttachments = attachments.filter(a => a.namespace === `credential:${flags.name}`)
|
25 | if (credAttachments.length > 0) throw new Error(`Credential ${flags.name} must be detached from all other apps before destroying.`)
|
26 |
|
27 | yield cli.confirmApp(app, flags.confirm, `WARNING: Destructive action`)
|
28 |
|
29 | yield cli.action(`Destroying credential ${cli.color.cmd(cred)}`, co(function * () {
|
30 | yield heroku.delete(`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(cred)}`, {host: host(db)})
|
31 | }))
|
32 |
|
33 | cli.log(`The credential has been destroyed within ${db.name}.`)
|
34 | cli.log(`Database objects owned by ${cred} will be assigned to the default credential.`)
|
35 | }
|
36 |
|
37 | module.exports = {
|
38 | topic: 'pg',
|
39 | command: 'credentials:destroy',
|
40 | description: 'destroy credential within database',
|
41 | needsApp: true,
|
42 | needsAuth: true,
|
43 | help: `
|
44 | Example Usage:
|
45 | heroku pg:credentials:destroy postgresql-transparent-56874 --name cred-name -a woodstock-production
|
46 | `,
|
47 | args: [{name: 'database', optional: true}],
|
48 | flags: [
|
49 | {name: 'name', char: 'n', hasValue: true, required: true, description: 'unique identifier for the credential'},
|
50 | {name: 'confirm', char: 'c', hasValue: true}
|
51 | ],
|
52 | run: cli.command({preauth: true}, co.wrap(run))
|
53 | }
|