Skip to content

Commit

Permalink
fix(ps): update larger dyno checking (#2741)
Browse files Browse the repository at this point in the history
* Update ps:type dyno checking

* Update ps:scale dyno checking

* Update ps:type test
  • Loading branch information
zwhitfield3 authored Mar 22, 2024
1 parent 450aa64 commit 853b94f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
36 changes: 15 additions & 21 deletions packages/apps-v5/src/commands/ps/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ async function run(context, heroku) {
})

async function parse(args) {
// checks for larger dyno sizes
// if the feature is not enabled
if (!largerDynoFeatureFlag.enabled) {
if (args.find(a => a.match(/=/))) {
// eslint-disable-next-line array-callback-return
compact(args.map(arg => {
let match = arg.match(/^([\w-]+)([=+-]\d+)(?::([\w-]+))?$/)
if (match === null) return
let size = match[3]

const largerDynoNames = /^(?!standard-[12]x$)(performance|private|shield)-(l-ram|xl|2xl)$/i
isLargerDyno = largerDynoNames.test(size)

if (isLargerDyno) {
const availableDynoSizes = 'eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l'
throw new Error(`No such size as ${size}. Use ${availableDynoSizes}.`)
}
}))
}
}

return compact(args.map(arg => {
let change = arg.match(/^([\w-]+)([=+-]\d+)(?::([\w-]+))?$/)
if (!change) return
Expand All @@ -56,6 +35,21 @@ async function run(context, heroku) {
}

let changes = await parse(context.args)

// checks for larger dyno sizes
// if the feature is not enabled
if (!largerDynoFeatureFlag.enabled) {
changes.forEach(({size}) => {
const largerDynoNames = /^(?!standard-[12]x$)(performance|private|shield)-(l-ram|xl|2xl)$/i
isLargerDyno = largerDynoNames.test(size)

if (isLargerDyno) {
const availableDynoSizes = 'eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l'
throw new Error(`No such size as ${size}. Use ${availableDynoSizes}.`)
}
})
}

if (changes.length === 0) {
let formation = await heroku.get(`/apps/${app}/formation`)

Expand Down
35 changes: 15 additions & 20 deletions packages/apps-v5/src/commands/ps/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,6 @@ async function run(context, heroku) {
let parse = async function (args) {
if (!args || args.length === 0) return []

// checks for larger dyno sizes
// if the feature is not enabled
if (!largerDynoFeatureFlag.enabled) {
if (args.find(a => a.match(/=/))) {
// eslint-disable-next-line array-callback-return
compact(args.map(arg => {
let match = arg.match(/^([a-zA-Z0-9_]+)=([\w-]+)$/)
let size = match[2]

const largerDynoNames = /^(?!standard-[12]x$)(performance|private|shield)-(l-ram|xl|2xl)$/i
isLargerDyno = largerDynoNames.test(size)

if (isLargerDyno) {
const availableDynoSizes = 'eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l'
throw new Error(`No such size as ${size}. Use ${availableDynoSizes}.`)
}
}))
}
}

let formation = await heroku.get(`/apps/${app}/formation`)
if (args.find(a => a.match(/=/))) {
return compact(args.map(arg => {
Expand Down Expand Up @@ -136,6 +116,21 @@ Types: ${cli.color.yellow(formation.map(f => f.type).join(', '))}`)
}

let changes = await parse(context.args)

// checks for larger dyno sizes
// if the feature is not enabled
if (!largerDynoFeatureFlag.enabled) {
changes.forEach(({size}) => {
const largerDynoNames = /^(?!standard-[12]x$)(performance|private|shield)-(l-ram|xl|2xl)$/i
isLargerDyno = largerDynoNames.test(size)

if (isLargerDyno) {
const availableDynoSizes = 'eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l'
throw new Error(`No such size as ${size}. Use ${availableDynoSizes}.`)
}
})
}

if (changes.length > 0) {
await cli.action(`Scaling dynos on ${cli.color.app(app)}`,
heroku.request({method: 'PATCH', path: `/apps/${app}/formation`, body: {updates: changes}}),
Expand Down
16 changes: 16 additions & 0 deletions packages/apps-v5/test/unit/commands/ps/type.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,32 @@ it('errors when user requests larger dynos and feature flag is NOT enabled', fun
let api = nock('https://api.heroku.com')
.get('/account/features/frontend-larger-dynos')
.reply(200, featureFlagPayload())
.get('/apps/myapp/formation')
.reply(200, [{type: 'web', quantity: 1, size: 'performance-l-ram'}, {type: 'web', quantity: 2, size: 'performance-l-ram'}])

return cmd.run({app: 'myapp', args: ['web=performance-l-ram']})
.catch(error => expect(error.message).to.eq('No such size as performance-l-ram. Use eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l.'))
.then(() => api.done())
})

it('errors when user requests larger dynos and feature flag is NOT enabled when chaning all type sizes', function () {
let api = nock('https://api.heroku.com')
.get('/account/features/frontend-larger-dynos')
.reply(200, featureFlagPayload())
.get('/apps/myapp/formation')
.reply(200, [{type: 'web', quantity: 1, size: 'performance-2xl'}, {type: 'web', quantity: 2, size: 'performance-2xl'}])

return cmd.run({app: 'myapp', args: ['performance-2xl']})
.catch(error => expect(error.message).to.eq('No such size as performance-2xl. Use eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l.'))
.then(() => api.done())
})

it("errors when user requests larger dynos and feature flag doesn't exist", function () {
let api = nock('https://api.heroku.com')
.get('/account/features/frontend-larger-dynos')
.reply(404, {})
.get('/apps/myapp/formation')
.reply(200, [{type: 'web', quantity: 1, size: 'performance-2xl'}, {type: 'web', quantity: 2, size: 'performance-2xl'}])

return cmd.run({app: 'myapp', args: ['web=performance-l-ram']})
.catch(error => expect(error.message).to.eq('No such size as performance-l-ram. Use eco, basic, standard-1x, standard-2x, performance-m, performance-l, private-s, private-m, private-l, shield-s, shield-m, shield-l.'))
Expand Down

0 comments on commit 853b94f

Please sign in to comment.