diff --git a/build/services/auto-scaler-service.js b/build/services/auto-scaler-service.js new file mode 100644 index 00000000..281b7818 --- /dev/null +++ b/build/services/auto-scaler-service.js @@ -0,0 +1,14 @@ +function createAutoscaller() { + return async () => { + return true + }; +} + +async function scalingoClient({ scalingoToken, scalingoApiUrl }) { + const client = await scalingo.clientFromToken(scalingoToken, { apiUrl: scalingoApiUrl }); +} + +module.exports = { + createAutoscaller, + scalingoClient +}; diff --git a/common/services/scalingo-client.js b/common/services/scalingo-client.js index 926182ba..9f66812e 100644 --- a/common/services/scalingo-client.js +++ b/common/services/scalingo-client.js @@ -130,6 +130,18 @@ class ScalingoClient { throw new Error(`Impossible to create ${app.name}, ${e.name}`); } } + + async updateAutoscaler(appname, updateParams) { + const autoscalers = await this.client.Autoscalers.for(appname); + + if (!Array.isArray(autoscalers) || !autoscalers.length) { + throw new Error(`Aucun autoscaller trouvé pour l'application '${appname}'`) + } + + const autoscaler = autoscalers[0]; + + await this.client.Autoscalers.update(appname, autoscaler.id, updateParams); + } } async function _isUrlReachable(url) { diff --git a/index.js b/index.js index e5bf4da3..6815b978 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const server = require('./server'); const { createCronJob } = require('./common/services/cron-job'); const githubServices = require('./common/services/github'); const { deploy } = require('./run/services/deploy'); -const ecoModeService = require('./build/services/eco-mode-service'); +const = require('./build/services/eco-mode-service'); const logger = require('./common/services/logger'); const init = async () => { diff --git a/test/unit/build/services/auto-scaler_test.js b/test/unit/build/services/auto-scaler_test.js new file mode 100644 index 00000000..0ebb0275 --- /dev/null +++ b/test/unit/build/services/auto-scaler_test.js @@ -0,0 +1,13 @@ +const { expect, sinon } = require('../../../test-helper'); +const { createAutoscaller } = require('../../../../build/services/auto-scaler-service.js'); +describe('Unit | Services | AutoScaler ', function () { + describe('#startAutoScaler', function () { + it.only('should start the job', async function () { + // given + // when + const ret = createAutoscaller({}); + // then + expect(await ret()).to.be.true; + }); + }); +}); diff --git a/test/unit/common/services/scalingo-client_test.js b/test/unit/common/services/scalingo-client_test.js index c811d4c6..7b58498b 100644 --- a/test/unit/common/services/scalingo-client_test.js +++ b/test/unit/common/services/scalingo-client_test.js @@ -566,4 +566,149 @@ describe('Scalingo client', () => { expect(actual.message).to.equal('Impossible to create pix-application-recette, foo'); }); }); + + describe('#Scalingo.updateAutoscaler', () => { + it('should update autoscaller for one application', async () => { + // given + const forAutoscallerStub = sinon.stub(); + const updateAutoscallerStub = sinon.stub(); + + forAutoscallerStub.resolves([ + { + id: 'au-123456789' + } + ]) + + updateAutoscallerStub.resolves() + + const clientStub = { + clientFromToken: async () => { + return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } }; + }, + }; + + const scalingoClient = await ScalingoClient.getInstance('recette', clientStub); + + const autoscalerUpdateParams = { + min_containers: 1, + max_containers: 2, + }; + + // when + await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams); + + // then + expect(forAutoscallerStub.calledOnceWith('pix-application-recette')).to.be.true; + expect(updateAutoscallerStub.calledOnceWith( + 'pix-application-recette', + 'au-123456789', + autoscalerUpdateParams)).to.be.true; + }); + + it('should throw when autoscaler not found', async () => { + // given + const forAutoscallerStub = sinon.stub(); + const updateAutoscallerStub = sinon.stub(); + + forAutoscallerStub.resolves([]) + + updateAutoscallerStub.resolves() + + const clientStub = { + clientFromToken: async () => { + return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } }; + }, + }; + + const scalingoClient = await ScalingoClient.getInstance('recette', clientStub); + + const autoscalerUpdateParams = { + min_containers: 1, + max_containers: 2, + }; + + // when + let actual; + try { + await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams); + } catch (error) { + actual = error; + } + + // then + expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'"); + }); + + it('should throw when autoscaler api return undefined', async () => { + // given + const forAutoscallerStub = sinon.stub(); + const updateAutoscallerStub = sinon.stub(); + + forAutoscallerStub.resolves(undefined) + + updateAutoscallerStub.resolves() + + const clientStub = { + clientFromToken: async () => { + return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } }; + }, + }; + + const scalingoClient = await ScalingoClient.getInstance('recette', clientStub); + + const autoscalerUpdateParams = { + min_containers: 1, + max_containers: 2, + }; + + // when + let actual; + try { + await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams); + } catch (error) { + actual = error; + } + + // then + expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'"); + }); + + it('should throw when autoscaler api return null', async () => { + // given + const forAutoscallerStub = sinon.stub(); + const updateAutoscallerStub = sinon.stub(); + + forAutoscallerStub.resolves(null) + + updateAutoscallerStub.resolves() + + const clientStub = { + clientFromToken: async () => { + return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } }; + }, + }; + + const scalingoClient = await ScalingoClient.getInstance('recette', clientStub); + + const autoscalerUpdateParams = { + min_containers: 1, + max_containers: 2, + }; + + // when + let actual; + try { + await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams); + } catch (error) { + actual = error; + } + + // then + expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'"); + }); + + it('should throw when update autoscaler', async () => { + + }); + }); });