Skip to content

Commit

Permalink
refactor(redis-v5): Move redis:promote command to CLI (#2726)
Browse files Browse the repository at this point in the history
* refactor(redis-v5): Move redis:promite command to CLI

* Fixes afer branch update

* Update packages/cli/src/commands/redis/promote.ts

Co-authored-by: Santiago Bosio <santiago.bosio@gmail.com>

* Addressed PR comments

* Revert "Addressed PR comments"

This reverts commit 855efff.

---------

Co-authored-by: Santiago Bosio <santiago.bosio@gmail.com>
  • Loading branch information
justinwilaby and sbosio authored Mar 20, 2024
1 parent eeb76f7 commit fdf50b1
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 128 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/addons/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class Open extends Command {
return this.sudo(ctx)
}

let attachment: void | AddOnAttachment
let attachment: void | AddOnAttachment | null = null
try {
attachment = await attachmentResolver(this.heroku, app, addon)
} catch (error) {
Expand Down
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}),
remote: flags.remote(),
}

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: {name: flags.app}, addon: {name: attachment.name}, confirm: flags.app,
},
})
}

ux.log(`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',
},
})
}
}
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 @@ -116,8 +116,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?: Required<Heroku.AddOn>[]): Promise<Required<Heroku.AddOn>> {
if (!addons) {
({body: addons} = await heroku.get<Required<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'),
]

exports.topic = {
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 fdf50b1

Please sign in to comment.