This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfetcher.js
154 lines (121 loc) · 4.87 KB
/
fetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
const co = require('co')
const debug = require('./debug')
const pgUtil = require('./util')
const getConfig = require('./config')
const cli = require('heroku-cli-util')
module.exports = heroku => {
function * attachment (app, passedDb, namespace = null) {
let db = passedDb || 'DATABASE_URL'
function matchesHelper (app, db) {
const {resolve} = require('@heroku-cli/plugin-addons')
debug(`fetching ${db} on ${app}`)
return resolve.appAttachment(heroku, app, db, {addon_service: 'heroku-postgresql', namespace: namespace})
.then(attached => ({matches: [attached]}))
.catch(function (err) {
if (err.statusCode === 422 && err.body && err.body.id === 'multiple_matches' && err.matches) {
return {matches: err.matches, err: err}
}
if (err.statusCode === 404 && err.body && err.body.id === 'not_found') {
return {matches: null, err: err}
}
throw err
})
}
let {matches, err} = yield matchesHelper(app, db)
// happy path where the resolver matches just one
if (matches && matches.length === 1) {
return matches[0]
}
// case for 404 where there are implicit attachments
if (!matches) {
let appConfigMatch = new RegExp('^(.+?)::(.+)').exec(db)
if (appConfigMatch) {
app = appConfigMatch[1]
db = appConfigMatch[2]
}
if (!db.endsWith('_URL')) {
db = db + '_URL'
}
let [config, attachments] = yield [
getConfig(heroku, app),
allAttachments(app)
]
if (attachments.length === 0) {
throw new Error(`${cli.color.app(app)} has no databases`)
}
matches = attachments.filter(attachment => config[db] && config[db] === config[pgUtil.getConfigVarName(attachment.config_vars)])
if (matches.length === 0) {
let validOptions = attachments.map(attachment => pgUtil.getConfigVarName(attachment.config_vars))
throw new Error(`Unknown database: ${passedDb}. Valid options are: ${validOptions.join(', ')}`)
}
}
// case for multiple attachments with passedDb
let first = matches[0]
// case for 422 where there are ambiguous attachments that are equivalent
if (matches.every((match) => first.addon.id === match.addon.id && first.app.id === match.app.id)) {
let config = yield getConfig(heroku, first.app.name)
if (matches.every((match) => config[pgUtil.getConfigVarName(first.config_vars)] === config[pgUtil.getConfigVarName(match.config_vars)])) {
return first
}
}
throw err
}
function * addon (app, db) {
return (yield attachment(app, db)).addon
}
function * database (app, db, namespace) {
let attached = yield attachment(app, db, namespace)
// would inline this as well but in some cases attachment pulls down config
// as well and we would request twice at the same time but I did not want
// to push this down into attachment because we do not always need config
let config = yield getConfig(heroku, attached.app.name)
return pgUtil.getConnectionDetails(attached, config)
}
function * allAttachments (app) {
let attachments = yield heroku.get(`/apps/${app}/addon-attachments`, {
headers: {'Accept-Inclusion': 'addon:plan,config_vars'}
})
return attachments.filter(a => a.addon.plan.name.startsWith('heroku-postgresql'))
}
function getAttachmentNamesByAddon (attachments) {
return attachments.reduce((results, a) => {
results[a.addon.id] = (results[a.addon.id] || []).concat(a.name)
return results
}, {})
}
function * all (app) {
const {uniqBy} = require('lodash')
debug(`fetching all DBs on ${app}`)
let attachments = yield allAttachments(app)
let addons = attachments.map(a => a.addon)
// Get the list of attachment names per addon here and add to each addon obj
let attachmentNamesByAddon = getAttachmentNamesByAddon(attachments)
addons = uniqBy(addons, 'id')
addons.forEach(addon => {
addon.attachment_names = attachmentNamesByAddon[addon.id]
})
return addons
}
function * arbitraryAppDB (app) {
// Since Postgres backups are tied to the app and not the add-on, but
// we require *an* add-on to interact with, make sure that that add-on
// is attached to the right app.
debug(`fetching arbitrary app db on ${app}`)
let addons = yield heroku.get(`/apps/${app}/addons`)
let addon = addons.find(a => a.app.name === app && a.plan.name.startsWith('heroku-postgresql'))
if (!addon) throw new Error(`No heroku-postgresql databases on ${app}`)
return addon
}
function * release (appName, id) {
return yield heroku.get(`/apps/${appName}/releases/${id}`)
}
return {
addon: co.wrap(addon),
attachment: co.wrap(attachment),
all: co.wrap(all),
database: co.wrap(database),
arbitraryAppDB: co.wrap(arbitraryAppDB),
release: co.wrap(release)
}
}