Skip to content

Commit

Permalink
📦 Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrieseberg committed Jun 29, 2016
1 parent 6850ce8 commit c42ec44
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 128 deletions.
6 changes: 1 addition & 5 deletions lib/plugins/Plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
"./aws/deploy/compile/functions/index.js",
"./aws/deploy/compile/events/schedule/index.js",
"./aws/deploy/compile/events/s3/index.js",
"./aws/deploy/compile/events/apiGateway/index.js",
"./azure/compile/index.js",
"./azure/deploy/index.js",
"./azure/invoke/index.js",
"./azure/remove/index.js"
"./aws/deploy/compile/events/apiGateway/index.js"
]
}
2 changes: 1 addition & 1 deletion lib/plugins/azure/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AzureCompileFunctions {
if (functionObject.events.azure.http_endpoint) {
functionJSON = HttpTrigger.buildFunctionJSON(functionObject);
} else if (functionObject.events.azure.scheduled_trigger) {
functionJSON = TimerTrigger.buildFunctionJSON(TimerTrigger);
functionJSON = TimerTrigger.buildFunctionJSON(functionObject);
}

this.serverless.service.resources.azure.functions[functionName] = functionJSON;
Expand Down
22 changes: 5 additions & 17 deletions lib/plugins/azure/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,17 @@ class AzureDeploy {
this.hooks = {
// Read the general template, merge with existing configuration,
// potentially add features
'deploy:initializeResources': () => {
return BbPromise.bind(this)
.then(this.initializeResources);
},

'deploy:initializeResources': () => BbPromise.bind(this).then(this.initializeResources),
'before:deploy:createProviderStacks': () => azureCli.setMode('arm'),

'deploy:createProviderStacks': () => {
// 1) Create resource group, deleting an existing one if it is
// in the way
return BbPromise.bind(this)
.then(this.createResourceGroup);
},

'deploy:deploy': () => {
'deploy:createProviderStacks': () => BbPromise.bind(this).then(this.createResourceGroup),
'deploy:deploy': () =>
// 1) Create resources and website with functions extension installed
// 2) Deploy functions to said webite
return BbPromise.bind(this)
BbPromise.bind(this)
// Optional: If you need to this in two steps, add a
.then(this.deployResources)
.then(this.deployFunctions)
.then(() => this.serverless.cli.log('Deployment successful!'));
},
.then(() => this.serverless.cli.log('Deployment successful!')),
};
}
}
Expand Down
9 changes: 4 additions & 5 deletions lib/plugins/azure/deploy/lib/deployFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = {
this.deployedFunctions.forEach(func => {
uploadPromises.push(this.uploadZipData(func, targetUrl, func.zipFileData));
});
this.createHostFile();
this.createHostFile(targetUrl);

return BbPromise.all(uploadPromises);
},
Expand All @@ -122,10 +122,9 @@ module.exports = {

fetch(createUrl)
.then(() => fetch(uploadUrl, {
method: 'PUT',
body: data,
})
)
method: 'PUT',
body: data,
}).catch(err => reject(err)))
.catch(err => reject(err));
});
},
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/azure/deploy/lib/initializeResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ module.exports = {
path.join(this.serverless.config.serverlessPath, 'templates', 'azure-arm.json'), 'utf8'
);
const azureResources = this.serverless.service.resources.azure;
const service = this.serverless.service.service;

// Update basic properties
template.variables.siteName = `serverless-${this.serverless.service.service}-${this.options.stage}`;
template.variables.siteName = `serverless-${service}-${this.options.stage}`;
template.variables.location = this.options.region || template.variables.location;
template.variables.storageAccountType = this.options.storageAccountType
|| template.variables.storageAccountType;
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/azure/deploy/tests/deployFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe('deployFunctions', () => {
serverless.config.servicePath = tmpDirPath;

return azureDeployMock.zipFunctions().then(() => {
// eslint-disable-next-line no-unused-expressions
expect(azureDeployMock.deployedFunctions[0].zipFileData).to.be.not.empty;

// look into the zippedFileData
Expand Down Expand Up @@ -210,6 +211,7 @@ describe('deployFunctions', () => {
serverless.config.servicePath = tmpDirPath;

return azureDeployMock.zipFunctions().then(() => {
// eslint-disable-next-line no-unused-expressions
expect(azureDeployMock.deployedFunctions[0].zipFileData).to.be.not.empty;

// look into the zippedFileData
Expand Down Expand Up @@ -386,7 +388,7 @@ describe('deployFunctions', () => {
},
];
azureDeployMock.serverless.service = {
resources: { azure: { variables: { sitename: 'serverless-test-site', }, }, },
resources: { azure: { variables: { sitename: 'serverless-test-site' } } },
};
process.env.AZURE_USERNAME = 'test';
process.env.AZURE_PASSWORD = 'test';
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/azure/deploy/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('./createResourceGroup');
require('./deployFunctions');
require('./deployResources');
require('./initializeResources');
78 changes: 38 additions & 40 deletions lib/plugins/azure/utils/azureCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,65 @@
const BbPromise = require('bluebird');
const spawn = require('./spawn');

const SUCCEEDED = "Succeeded";
const STATUS_CHANGE_WAIT_TIME = 5 * 1000; // ms
const SUCCEEDED = 'Succeeded';
const STATUS_CHANGE_WAIT = 5 * 1000; // ms
const STATUS_START_TTL = 30;

function createResourceGroup(resourceGroup, location) {
return spawn(`azure group create --json -n "${resourceGroup}" -l "${location}"`).then(JSON.parse);
function showResourceGroup(resourceGroup) {
return spawn(`azure group show ${resourceGroup} --json`)
.then(JSON.parse);
}

function deleteResourceGroup(resourceGroup) {
return spawn(`azure group delete ${resourceGroup} --json -q`).then(() => {
return waitForResourceGroupDeleted(resourceGroup, STATUS_START_TTL);
function waitForGroupSucccess(group, ttl) {
return new BbPromise((resolve, reject) => {
showResourceGroup(group).then(result => {
if (result.properties.provisioningState === SUCCEEDED) {
return resolve(result);
}

if (ttl > 0) {
return setTimeout(() => waitForGroupSucccess(group, ttl - 1), STATUS_CHANGE_WAIT);
}

return reject(result);
})
.catch(() => setTimeout(() => waitForGroupSucccess(group, ttl - 1), STATUS_CHANGE_WAIT));
});
}

function deployResourceGroup(templatePath, parametersPath, resourceGroup, deploymentName) {
return spawn(`azure group deployment create --json -f ${templatePath} -e ${parametersPath} ${resourceGroup} ${deploymentName}`).then((result) => {
console.log('deploy finished');
return waitForResourceGroupSucceeded(resourceGroup, STATUS_START_TTL);
function waitForGroupDeleted(resourceGroup) {
return new BbPromise(resolve => {
showResourceGroup(resourceGroup)
.then(() => setTimeout(() => waitForGroupDeleted(resourceGroup), STATUS_CHANGE_WAIT))
.catch(() => resolve());
});
}

function showResourceGroup(resourceGroup) {
return spawn(`azure group show ${resourceGroup} --json`).then(JSON.parse);
function createResourceGroup(resourceGroup, location) {
return spawn(`azure group create --json -n "${resourceGroup}" -l "${location}"`)
.then(JSON.parse);
}

function setMode(mode) {
return spawn(`azure config mode ${mode}`);
function deleteResourceGroup(resourceGroup) {
return spawn(`azure group delete ${resourceGroup} --json -q`)
.then(() => waitForGroupDeleted(resourceGroup, STATUS_START_TTL));
}

function waitForResourceGroupSucceeded(resourceGroup, ttl) {
return new BbPromise((resolve, reject) => {
showResourceGroup(resourceGroup).then( (result) => {
if (result.properties.provisioningState === SUCCEEDED) {
resolve(result);
} else {
if (ttl > 0) {
return setTimeout( () => waitForResourceGroupSucceeded(resourceGroup, ttl - 1), STATUS_CHANGE_WAIT_TIME);
} else {
reject(result);
}
}
}).catch( () => {
return setTimeout( () => waitForResourceGroupSucceeded(resourceGroup, ttl - 1), STATUS_CHANGE_WAIT_TIME);
});
});
function deployResourceGroup(templatePath, parametersPath, resourceGroup, deploymentName) {
const params = `-f ${templatePath} -e ${parametersPath} ${resourceGroup} ${deploymentName}`;
const cmd = `azure group deployment create --json ${params}`;
return spawn(cmd)
.then(() => waitForGroupSucccess(resourceGroup, STATUS_START_TTL));
}

function waitForResourceGroupDeleted(resourceGroup, ttl) {
return new BbPromise((resolve, reject) => {
showResourceGroup(resourceGroup).then(JSON.parse).then( (result) => {
return setTimeout( () => waitForResourceGroupDeleted(resourceGroup), STATUS_CHANGE_WAIT_TIME);
}).catch( () => {
resolve();
});
});
function setMode(mode) {
return spawn(`azure config mode ${mode}`);
}

module.exports = {
createResourceGroup,
deleteResourceGroup,
deployResourceGroup,
showResourceGroup,
setMode
setMode,
};
6 changes: 4 additions & 2 deletions lib/plugins/azure/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const azureCli = require('./azureCli');

module.exports = {
azureCli: require('./azureCli')
};
azureCli,
};
119 changes: 65 additions & 54 deletions lib/plugins/azure/utils/tests/azureCli.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
'use strict';

const azureCli = require('../azureCli');
const assert = require('assert');

const TEST_RESOURCE_GROUP_NAME = 'test-rg-serverless';
const TEST_RESOURCE_GROUP_LOCATION = 'West US';
const TEST_RESOURCE_GROUP_DEPLOYMENT = TEST_RESOURCE_GROUP_NAME + '-deployment';
const TEST_TEMPLATE_PATH = "fixtures/test-deploy.json";
const TEST_PARAMETERS_PATH = "fixtures/test-parameters.json";

const SUCCEEDED = "Succeeded";

describe('azureCli', () => {

afterEach( (done) => {
setTimeout(done, 10000);
});

it('can create a resource group', (done) => {
return azureCli.createResourceGroup(TEST_RESOURCE_GROUP_NAME, TEST_RESOURCE_GROUP_LOCATION).then( (result) => {
assert.equal(result.name, TEST_RESOURCE_GROUP_NAME);
assert.equal(result.properties.provisioningState, SUCCEEDED);

assert.equal(result.location, "westus");
done();
});
});

it('can deploy a resource group', (done) => {
return azureCli.deployResourceGroup(TEST_TEMPLATE_PATH, TEST_PARAMETERS_PATH, TEST_RESOURCE_GROUP_NAME, TEST_RESOURCE_GROUP_DEPLOYMENT).then( (result) => {
assert.equal(result.name, TEST_RESOURCE_GROUP_NAME);
assert.equal(result.properties.provisioningState, SUCCEEDED);

done();
});
});

it('can show a resource group', (done) => {
return azureCli.showResourceGroup(TEST_RESOURCE_GROUP_NAME).then( (result) => {
assert.equal(result.name, TEST_RESOURCE_GROUP_NAME);
assert.equal(result.resources.length, 1);

done();
});
});

it('can delete resource group', (done) => {
return azureCli.deleteResourceGroup(TEST_RESOURCE_GROUP_NAME).then( () => {
azureCli.showResourceGroup(TEST_RESOURCE_GROUP_NAME).catch(() => {
done();
});
});
});

});
// Below tests call out to the real Azure Cli - and will
// create real resources.
// ----------------------------------------------------------------------------------------------

// const azureCli = require('../azureCli');
// const assert = require('assert');

// const RESOURCE_GROUP_NAME = 'test-rg-serverless';
// const RESOURCE_GROUP_LOCATION = 'West US';
// const RESOURCE_GROUP_DEPLOYMENT = `${RESOURCE_GROUP_NAME}-deployment`;
// const TEMPLATE_PATH = 'fixtures/test-deploy.json';
// const PARAMETERS_PATH = 'fixtures/test-parameters.json';
// const SUCCEEDED = 'Succeeded';

// describe('azureCli', () => {
// afterEach( (done) => {
// setTimeout(done, 10000);
// });


// it('can create a resource group', (done) => {
// return azureCli.createResourceGroup(RESOURCE_GROUP_NAME, RESOURCE_GROUP_LOCATION)
// .then(result => {
// assert.equal(result.name, RESOURCE_GROUP_NAME);
// assert.equal(result.properties.provisioningState, SUCCEEDED);

// assert.equal(result.location, 'westus');
// done();
// });
// });

// it('can deploy a resource group', (done) => {
// const groupName = RESOURCE_GROUP_NAME;
// const groupDeployment = RESOURCE_GROUP_DEPLOYMENT;
// const params = PARAMETERS_PATH;
// const template = TEMPLATE_PATH;
//
// return azureCli.deployResourceGroup(template, params, groupName, groupDeployment)
// .then(result => {
// assert.equal(result.name, RESOURCE_GROUP_NAME);
// assert.equal(result.properties.provisioningState, SUCCEEDED);

// done();
// });
// });

// it('can show a resource group', (done) => {
// return azureCli.showResourceGroup(RESOURCE_GROUP_NAME)
// .then(result => {
// assert.equal(result.name, RESOURCE_GROUP_NAME);
// assert.equal(result.resources.length, 1);

// done();
// });
// });

// it('can delete resource group', (done) => {
// return azureCli.deleteResourceGroup(TEST_RESOURCE_GROUP_NAME)
// .then(() => {
// azureCli.showResourceGroup(TEST_RESOURCE_GROUP_NAME).catch(() => {
// done();
// });
// });
// });
// });
1 change: 1 addition & 0 deletions lib/plugins/azure/utils/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./azureCli');
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
"lodash": "^4.13.1",
"minimist": "^1.2.0",
"moment": "^2.13.0",
"node-fetch": "^1.5.3",
"node-zip": "^1.1.1",
"replaceall": "^0.1.6",
"shelljs": "^0.6.0",
"traverse": "^0.6.6"
}
}
}
5 changes: 5 additions & 0 deletions tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ require('../lib/plugins/aws/deploy/compile/functions/tests');
require('../lib/plugins/aws/deploy/compile/events/s3/tests');
require('../lib/plugins/aws/deploy/compile/events/schedule/tests');
require('../lib/plugins/aws/deploy/compile/events/apiGateway/tests/all');

// Azure Plugin Tests
require('../lib/plugins/azure/compile/tests');
require('../lib/plugins/azure/deploy/tests');
require('../lib/plugins/azure/utils/tests');

0 comments on commit c42ec44

Please sign in to comment.