1 | 'use strict'
|
2 |
|
3 | function sslCertsPromise (app, heroku) {
|
4 | return heroku.request({
|
5 | path: `/apps/${app}/ssl-endpoints`,
|
6 | headers: {'Accept': 'application/vnd.heroku+json; version=3.ssl_cert'}
|
7 | }).then(function (data) {
|
8 | return data
|
9 | })
|
10 | }
|
11 |
|
12 | function sniCertsPromise (app, heroku) {
|
13 | return heroku.request({
|
14 | path: `/apps/${app}/sni-endpoints`,
|
15 | headers: {'Accept': 'application/vnd.heroku+json; version=3.sni_ssl_cert'}
|
16 | }).catch(function (err) {
|
17 | if (err.statusCode === 422 && err.body && err.body.id === 'space_app_not_supported') {
|
18 | return []
|
19 | }
|
20 | throw err
|
21 | }).then(function (data) {
|
22 | return data
|
23 | })
|
24 | }
|
25 |
|
26 | function meta (app, t, name) {
|
27 | var path, type, variant
|
28 | if (t === 'sni') {
|
29 | type = 'SNI'
|
30 | path = `/apps/${app}/sni-endpoints`
|
31 | variant = 'sni_ssl_cert'
|
32 | } else if (t === 'ssl') {
|
33 | type = 'Endpoint'
|
34 | path = `/apps/${app}/ssl-endpoints`
|
35 | variant = 'ssl_cert'
|
36 | } else {
|
37 | throw Error('Unknown type ' + type)
|
38 | }
|
39 | if (name) {
|
40 | path = `${path}/${name}`
|
41 | }
|
42 | return {path, type, variant}
|
43 | }
|
44 |
|
45 | function tagAndSort (app, allCerts) {
|
46 | allCerts.sni_certs.forEach(function (cert) {
|
47 | cert._meta = meta(app, 'sni', cert.name)
|
48 | })
|
49 |
|
50 | allCerts.ssl_certs.forEach(function (cert) {
|
51 | cert._meta = meta(app, 'ssl', cert.name)
|
52 | })
|
53 |
|
54 | return allCerts.ssl_certs.concat(allCerts.sni_certs).sort(function (a, b) {
|
55 | return a.name < b.name
|
56 | })
|
57 | }
|
58 |
|
59 | function * all (app, heroku) {
|
60 | let allCerts = yield {
|
61 | ssl_certs: sslCertsPromise(app, heroku),
|
62 | sni_certs: sniCertsPromise(app, heroku)
|
63 | }
|
64 |
|
65 | return tagAndSort(app, allCerts)
|
66 | }
|
67 |
|
68 | function * certsAndDomains (app, heroku) {
|
69 | let requests = yield {
|
70 | ssl_certs: sslCertsPromise(app, heroku),
|
71 | sni_certs: sniCertsPromise(app, heroku),
|
72 | domains: heroku.request({path: `/apps/${app}/domains`})
|
73 | }
|
74 |
|
75 | return {certs: tagAndSort(app, requests), domains: requests.domains}
|
76 | }
|
77 |
|
78 | function * hasAddon (app, heroku) {
|
79 | return yield heroku.request({
|
80 | path: `/apps/${app}/addons/ssl%3Aendpoint`
|
81 | }).then(function () {
|
82 | return true
|
83 | }).catch(function (err) {
|
84 | if (err.statusCode === 404 && err.body && err.body.id === 'not_found' && err.body.resource === 'addon') {
|
85 | return false
|
86 | } else {
|
87 | throw err
|
88 | }
|
89 | })
|
90 | }
|
91 |
|
92 | function * hasSpace (app, heroku) {
|
93 | return yield heroku.request({
|
94 | path: `/apps/${app}`
|
95 | }).then(function (data) {
|
96 | return !!data.space
|
97 | })
|
98 | }
|
99 |
|
100 | module.exports = {
|
101 | hasSpace,
|
102 | hasAddon,
|
103 | meta,
|
104 | all,
|
105 | certsAndDomains
|
106 | }
|