-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfo.ts
155 lines (134 loc) · 6.04 KB
/
info.ts
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
155
import {HTTP, HTTPError} from '@heroku/http-call'
import color from '@heroku-cli/color'
import {Command} from '@heroku-cli/command'
import {DateTime} from 'luxon'
import {applyActionSpinner} from '../../async-actions'
import {getBorealisPgApiUrl, getBorealisPgAuthHeader} from '../../borealis-api'
import {
addonOptionName,
cliOptions,
appOptionName,
processAddonAttachmentInfo,
consoleColours,
} from '../../command-components'
import {createHerokuAuth, fetchAddonAttachmentInfo, removeHerokuAuth} from '../../heroku-api'
const keyColour = consoleColours.dataFieldName
const valueColour = consoleColours.dataFieldValue
const bytesPerGib = 1024 * 1024 * 1024
const supportedRegions: {[key: string]: string} = {
'us-east-1': 'N. Virginia (United States)',
'eu-west-1': 'Ireland (Europe)',
'eu-central-1': 'Frankfurt',
'ap-northeast-1': 'Tokyo',
'ap-southeast-2': 'Sydney',
'us-west-2': 'Oregon',
}
const dbTenancyTypes: {[key: string]: string} = {
isolated: 'Single Tenant',
shared: 'Multi-tenant',
}
const addonStatuses: {[key: string]: string} = {
available: 'Available',
awaiting: 'Provisioning',
configuring: 'Provisioning',
maintenance: 'Undergoing maintenance',
'maintenance-db-credentials-full-reset': 'Resetting DB credentials',
'maintenance-plan-change': 'Changing add-on plan',
'maintenance-restore-db-write-access': 'Restoring DB write access',
'maintenance-revoke-db-write-access': 'Revoking DB write access',
provisioning: 'Provisioning',
requested: 'Requested',
}
const storageComplianceStatuses: {[key: string]: string} = {
ok: 'OK',
'proximity-warning': 'Proximity Warning',
restricted: 'Restricted',
violating: 'Violating',
}
export default class AddonInfoCommand extends Command {
static description = 'shows information about a Borealis Isolated Postgres add-on database'
static flags = {
[addonOptionName]: cliOptions.addon,
[appOptionName]: cliOptions.app,
}
async run() {
const {flags} = await this.parse(AddonInfoCommand)
const authorization = await createHerokuAuth(this.heroku)
const attachmentInfo =
await fetchAddonAttachmentInfo(this.heroku, flags.addon, flags.app, this.error)
const {addonName} = processAddonAttachmentInfo(attachmentInfo, this.error)
try {
const response = await applyActionSpinner<HTTP<AddonInfo>>(
`Fetching information about add-on ${color.addon(addonName)}`,
HTTP.get(
getBorealisPgApiUrl(`/heroku/resources/${addonName}`),
{headers: {Authorization: getBorealisPgAuthHeader(authorization)}}),
)
this.printAddonInfo(response.body)
} finally {
await removeHerokuAuth(this.heroku, authorization.id as string)
}
}
private async printAddonInfo(addonInfo: AddonInfo) {
const region = supportedRegions[addonInfo.region] ?? addonInfo.region
const dbTenancyType = dbTenancyTypes[addonInfo.dbTenancyType] ?? addonInfo.dbTenancyType
const addonStatus = addonStatuses[addonInfo.status] ?? addonInfo.status
const storageComplianceStatus = storageComplianceStatuses[addonInfo.storageComplianceStatus] ??
addonInfo.storageComplianceStatus
const storageComplianceDeadline = addonInfo.storageComplianceDeadline ?
DateTime.fromISO(addonInfo.storageComplianceDeadline).toISO() as string :
'N/A'
const dbStorageMaxGib = addonInfo.dbStorageMaxBytes / bytesPerGib
const dbStorageMaxFractionDigits = (dbStorageMaxGib < 1) ? 2 : 0
const dbStorageMaxDisplay = dbStorageMaxGib.toFixed(dbStorageMaxFractionDigits) + ' GiB'
const dbStorageUsageGib = addonInfo.dbStorageUsageBytes / bytesPerGib
const dbStorageUsageFractionDigits = (dbStorageUsageGib < 1) ? 3 : 1
const dbStorageUsageDisplay = dbStorageUsageGib.toFixed(dbStorageUsageFractionDigits) + ' GiB'
const appDbName = addonInfo.appDbName ?? '(pending)'
const createdAt = DateTime.fromISO(addonInfo.createdAt).toISO() as string
const restoreSourceAddonName = addonInfo.restoreSourceAddonName ?? 'N/A'
this.log()
this.log(` ${keyColour('Add-on Name')}: ${valueColour(addonInfo.addonName)}`)
this.log(` ${keyColour('Status')}: ${valueColour(addonStatus)}`)
this.log(` ${keyColour('Region')}: ${valueColour(region)}`)
this.log(` ${keyColour('Plan Name')}: ${valueColour(addonInfo.planName)}`)
this.log(` ${keyColour('Environment')}: ${valueColour(dbTenancyType)}`)
this.log(` ${keyColour('PostgreSQL Version')}: ${valueColour(addonInfo.postgresVersion)}`)
this.log(` ${keyColour('Maximum Storage')}: ${valueColour(dbStorageMaxDisplay)}`)
this.log(` ${keyColour('Storage Used')}: ${valueColour(dbStorageUsageDisplay)}`)
this.log(` ${keyColour('Read-only Replicas')}: ${valueColour(addonInfo.replicaQuantity.toString())}`)
this.log(` ${keyColour('App DB Name')}: ${valueColour(appDbName)}`)
this.log(` ${keyColour('Created At')}: ${valueColour(createdAt)}`)
this.log(` ${keyColour('Restored/Cloned From Add-on')}: ${valueColour(restoreSourceAddonName)}`)
this.log(` ${keyColour('Storage Compliance Status')}: ${valueColour(storageComplianceStatus)}`)
this.log(` ${keyColour('Storage Compliance Deadline')}: ${valueColour(storageComplianceDeadline)}`)
}
async catch(err: any) {
/* istanbul ignore else */
if (err instanceof HTTPError) {
if (err.statusCode === 404) {
this.error('Add-on is not a Borealis Isolated Postgres add-on')
} else {
this.error('Add-on service is temporarily unavailable. Try again later.')
}
} else {
throw err
}
}
}
interface AddonInfo {
addonName: string;
appDbName: string | null;
createdAt: string;
dbStorageMaxBytes: number;
dbStorageUsageBytes: number;
dbTenancyType: string;
planName: string;
postgresVersion: string;
region: string;
replicaQuantity: number;
restoreSourceAddonName: string | null;
status: string;
storageComplianceDeadline: string | null;
storageComplianceStatus: string;
}