Skip to content

Commit

Permalink
refactor(redis-v5): Move redis:promite command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby committed Mar 19, 2024
1 parent a4f5b31 commit e919dde
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 127 deletions.
40 changes: 40 additions & 0 deletions packages/cli/src/commands/redis/promote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import apiFactory from '../../lib/redis/api'
export default class Promote extends Command {
static topic = 'redis'
static description = 'sets DATABASE as your REDIS_URL'
static flags = {
app: flags.app({required: true}),
}

static args = {
database: Args.string({required: false}),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse(Promote)
const api = apiFactory(flags.app, args.database, false, this.heroku)
const {body: addonsList} = await this.heroku.get<Required<Heroku.AddOn>[]>(`/apps/${flags.app}/addons`)
const addon = await api.getRedisAddon(addonsList)
const redisFilter = api.makeAddonsFilter('REDIS_URL')
const redis = redisFilter(addonsList) as Required<Heroku.AddOn>[]
if (redis.length === 1 && redis[0].config_vars.filter((c: string) => c.endsWith('_URL')).length === 1) {
const attachment = redis[0]
await this.heroku.post('/addon-attachments', {
body: {
app: flags.app, addon: attachment.id, confirm: flags.app,
},
})
}

ux.action.start(`Promoting ${addon.name} to REDIS_URL on ${flags.app}`)
await this.heroku.post('/addon-attachments', {
body: {
app: {name: flags.app}, addon: {name: addon.name}, confirm: flags.app, name: 'REDIS',
},
})
ux.action.stop()
}
}
6 changes: 4 additions & 2 deletions packages/cli/src/lib/redis/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ export default (app: string, database: string | undefined, json: boolean, heroku
return onResponse
},

async getRedisAddon(): Promise<Required<Heroku.AddOn>> {
const {body: addons} = await heroku.get<Required<Heroku.AddOn>[]>(`/apps/${app}/addons`)
async getRedisAddon(addons?: Heroku.AddOn[]): Promise<Required<Heroku.AddOn>> {
if (!addons) {
({body: addons} = await heroku.get<Heroku.AddOn[]>(`/apps/${app}/addons`))
}

const addonsFilter = this.makeAddonsFilter(database)
const redisAddons = addonsFilter(addons)
Expand Down
84 changes: 84 additions & 0 deletions packages/cli/test/unit/commands/redis/promote.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {expect} from '@oclif/test'
import * as nock from 'nock'
import {stderr, stdout} from 'stdout-stderr'
import Cmd from '../../../../src/commands/redis/promote'
import runCommand from '../../../helpers/runCommand'

describe('heroku redis:promote', () => {
require('../../lib/redis/shared.unit.test.ts').shouldHandleArgs(Cmd)
})

describe('heroku redis:promote', async () => {
beforeEach(async () => nock.cleanAll())

it('# promotes', async () => {
const app = nock('https://api.heroku.com:443')
.get('/apps/example/addons')
.reply(200, [
{
name: 'redis-silver-haiku',
addon_service: {name: 'heroku-redis'},
config_vars: ['REDIS_URL', 'HEROKU_REDIS_SILVER_URL'],
}, {
name: 'redis-gold-haiku',
addon_service: {name: 'heroku-redis'},
config_vars: ['HEROKU_REDIS_GOLD_URL'],
},
])

const attach = nock('https://api.heroku.com:443')
.post('/addon-attachments', {
app: {name: 'example'}, addon: {name: 'redis-gold-haiku'}, confirm: 'example', name: 'REDIS',
})
.reply(200, {})

await runCommand(Cmd, [
'--app',
'example',
'redis-gold-haiku',
])
app.done()
attach.done()
expect(stdout.output).to.equal('Promoting redis-gold-haiku to REDIS_URL on example\n')
expect(stderr.output).to.equal('')
})

it('# promotes and replaces attachment of existing REDIS_URL if necessary', async () => {
const app = nock('https://api.heroku.com:443')
.get('/apps/example/addons')
.reply(200, [
{
name: 'redis-silver-haiku', addon_service: {name: 'heroku-redis'}, config_vars: [
'REDIS_URL',
'REDIS_BASTIONS',
'REDIS_BASTION_KEY',
'REDIS_BASTION_REKEYS_AFTER',
],
}, {
name: 'redis-gold-haiku',
addon_service: {name: 'heroku-redis'},
config_vars: ['HEROKU_REDIS_GOLD_URL'],
},
])
const attachRedisUrl = nock('https://api.heroku.com:443')
.post('/addon-attachments', {
app: {name: 'example'}, addon: {name: 'redis-silver-haiku'}, confirm: 'example',
})
.reply(200, {})
const attach = nock('https://api.heroku.com:443')
.post('/addon-attachments', {
app: {name: 'example'}, addon: {name: 'redis-gold-haiku'}, confirm: 'example', name: 'REDIS',
})
.reply(200, {})
await runCommand(Cmd, [
'--app',
'example',
'redis-gold-haiku',
])
app.done()
attachRedisUrl.done()
attach.done()
expect(stdout.output).to.equal('Promoting redis-gold-haiku to REDIS_URL on example\n')
expect(stderr.output).to.equal('')
})
})
45 changes: 0 additions & 45 deletions packages/redis-v5/commands/promote.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/redis-v5/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
exports.commands = [
require('./commands/wait'),
require('./commands/promote'),
require('./commands/timeout'),
require('./commands/maintenance'),
]
Expand Down
79 changes: 0 additions & 79 deletions packages/redis-v5/test/unit/commands/promote.unit.test.js

This file was deleted.

0 comments on commit e919dde

Please sign in to comment.