Skip to content
Draft
1 change: 1 addition & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ postpublish
postbump
postrelease
postrun
preassessment
preauth
prerun
preupdate
Expand Down
130 changes: 6 additions & 124 deletions src/commands/data/pg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import createPool from '../../../lib/data/createPool.js'
import {parseProvisionOpts} from '../../../lib/data/parseProvisionOpts.js'
import PoolConfig from '../../../lib/data/poolConfig.js'
import {ExtendedPostgresLevelInfo} from '../../../lib/data/types.js'
import {fetchLevelsAndPricing, renderPricingInfo} from '../../../lib/data/utils.js'
import {fetchLevelsAndPricing} from '../../../lib/data/utils.js'
import notify from '../../../lib/notify.js'

const heredoc = tsheredoc.default
// eslint-disable-next-line import/no-named-as-default-member
const {Separator, prompt} = inquirer
const {prompt} = inquirer

export default class DataPgCreate extends BaseCommand {
static baseFlags = BaseCommand.baseFlagsWithoutPrompt()
Expand Down Expand Up @@ -209,79 +209,9 @@ export default class DataPgCreate extends BaseCommand {
} while (oneMore)
}

private async highAvailabilityStep(): Promise<string> {
process.stderr.write(
'The leader pool has high availability enabled and includes a standby instance for redundancy.\n'
+ 'If you disable high availability, you remove the standby and you won\'t have redundancy on your database.\n\n',
)

const leaderPricing = this.extendedLevelsInfo!.find(level => level.name === this.leaderLevel)?.pricing
const {action} = await this.prompt<{action: string}>({
choices: [
{name: 'Keep high availability (HA)', value: 'keep'},
{
name: 'Remove high availability' + (
renderPricingInfo(leaderPricing) === 'free'
? ''
: ` ${color.yellowBright(`-${renderPricingInfo(leaderPricing).replace('~', '')}`)}`
),
value: 'remove',
},
new Separator(),
{name: 'Go back', value: 'back'},
],
message: 'Do you want to keep the high availability standby instance?',
name: 'action',
type: 'list',
})
process.stderr.write('\n')

return action
}

private async leaderConfirmationStep(): Promise<string> {
const leaderLevelInfo = this.extendedLevelsInfo!.find(level => level.name === this.leaderLevel)
const totalPrice = this.highAvailability
? renderPricingInfo(leaderLevelInfo?.pricing, 2)
: renderPricingInfo(leaderLevelInfo?.pricing)
const instancePrice = renderPricingInfo(leaderLevelInfo?.pricing)
process.stderr.write(heredoc`
${`${color.green('✓ Configure Leader Pool')} ${totalPrice}`}
${color.dim(
`${this.leaderLevel} ${leaderLevelInfo?.vcpu} ${color.inverse('vCPU')} `
+ `${leaderLevelInfo?.memory_in_gb} GB ${color.inverse('MEM')} `
+ instancePrice,
)}
`)
if (this.highAvailability) {
process.stderr.write(color.dim(` Standby (High Availability) ${instancePrice}\n`))
}

process.stderr.write('\n')

const {action} = await this.prompt<{action: string}>({
choices: [
{name: 'Confirm', value: 'confirm'},
{name: 'Go back', value: 'back'},
],
message: 'Confirm provisioning?',
name: 'action',
type: 'list',
})
process.stderr.write('\n')

return action
}

private async leaderLevelStep(): Promise<void> {
private async leaderPoolConfig(): Promise<void> {
const poolConfig = new PoolConfig(this.extendedLevelsInfo!, this.followerInstanceCount)
const level = await poolConfig.levelStep('Leader')
process.stderr.write('\n')

this.leaderLevel = level
}

private async leaderPoolConfig(): Promise<void> {
process.stderr.write(heredoc`

Create a Heroku Postgres Advanced database
Expand All @@ -294,56 +224,8 @@ export default class DataPgCreate extends BaseCommand {
${color.dim(' Configure Follower Pool(s)')}\n
`)

let configReady = false
let currentStep = 'leaderLevel'

while (!configReady) {
switch (currentStep) {
case 'leaderLevel': {
await this.leaderLevelStep()
currentStep = 'highAvailability'
break
}

case 'highAvailability': {
switch (await this.highAvailabilityStep()) {
case 'keep': {
this.highAvailability = true
currentStep = 'confirmation'
break
}

case 'remove': {
this.highAvailability = false
currentStep = 'confirmation'
break
}

case 'back': {
currentStep = 'leaderLevel'
break
}
}

break
}

case 'confirmation': {
switch (await this.leaderConfirmationStep()) {
case 'confirm': {
configReady = true
break
}

case 'back': {
currentStep = 'highAvailability'
break
}
}

break
}
}
}
const {highAvailability, level} = await poolConfig.leaderInteractiveConfig()
this.leaderLevel = level
this.highAvailability = highAvailability
}
}
Loading
Loading