Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 90 additions & 25 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.130",
"@types/open": "^6.1.0",
"@types/request": "^2.48.1",
"@types/serverless": "^1.18.2",
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ This way only one plugin needs to be added to the service in order to get access
whole provider implementation.
*/

import * as Serverless from 'serverless';
import Serverless from 'serverless';
import AzureProvider from './provider/azureProvider';
import { AzureInvoke } from './plugins/invoke/azureInvoke';
import { AzureLogs } from './plugins/logs/azureLogs';
import { AzureRemove } from './plugins/remove/azureRemove';
import { AzurePackage } from './plugins/package/azurePackage';
import { AzurePackageFunction } from './plugins/package/azurePackageFunction';
import { AzureDeployPlugin } from './plugins/deploy/azureDeployPlugin';
import { AzureDeployFunctionPlugin } from './plugins/deploy/azureDeployFunctionPlugin';
import { AzureLoginPlugin } from './plugins/login/loginPlugin';
import { AzureApimServicePlugin } from './plugins/apim/apimServicePlugin';
import { AzureApimFunctionPlugin } from './plugins/apim/apimFunctionPlugin';
Expand All @@ -23,14 +21,12 @@ export class AzureIndex {

// To be refactored
this.serverless.pluginManager.addPlugin(AzurePackage);
this.serverless.pluginManager.addPlugin(AzurePackageFunction);
this.serverless.pluginManager.addPlugin(AzureInvoke);
this.serverless.pluginManager.addPlugin(AzureLogs);
this.serverless.pluginManager.addPlugin(AzureRemove);
// Refactored
this.serverless.pluginManager.addPlugin(AzureLoginPlugin);
this.serverless.pluginManager.addPlugin(AzureDeployPlugin);
this.serverless.pluginManager.addPlugin(AzureDeployFunctionPlugin);
this.serverless.pluginManager.addPlugin(AzureApimServicePlugin);
this.serverless.pluginManager.addPlugin(AzureApimFunctionPlugin);
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/apim/apimFunctionPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import { ApimService } from '../../services/apimService';

export class AzureApimFunctionPlugin {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/apim/apimServicePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import { ApimService } from '../../services/apimService';

export class AzureApimServicePlugin {
Expand All @@ -11,6 +11,11 @@ export class AzureApimServicePlugin {
}

private async deploy() {
const apimConfig = this.serverless.service.provider['apim'];
if (!apimConfig) {
return Promise.resolve();
}

this.serverless.cli.log('Starting APIM service deployment');

const apimService = new ApimService(this.serverless, this.options);
Expand Down
27 changes: 0 additions & 27 deletions src/plugins/deploy/azureDeployFunctionPlugin.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/deploy/azureDeployPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import { ResourceService } from '../../services/resourceService';
import { FunctionAppService } from '../../services/functionAppService';

Expand Down
6 changes: 2 additions & 4 deletions src/plugins/invoke/azureInvoke.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as Serverless from 'serverless';
import { getAdminKey } from '../../shared/getAdminKey';
import Serverless from 'serverless';
import { join, isAbsolute } from 'path';
import AzureProvider from '../../provider/azureProvider';

export class AzureInvoke {
public hooks: { [eventName: string]: Promise<any> };
private provider: AzureProvider;
private getAdminKey = getAdminKey;

constructor(private serverless: Serverless, private options: Serverless.Options) {
this.provider = (this.serverless.getProvider('azure') as any) as AzureProvider;
Expand All @@ -24,7 +22,7 @@ export class AzureInvoke {
}

this.hooks = {
'before:invoke:invoke': this.getAdminKey.bind(this),
'before:invoke:invoke': this.provider.getAdminKey.bind(this),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move getAdminKey to this class? Doesn't look like it's being used elsewhere

'invoke:invoke': this.invoke.bind(this)
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/login/loginPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as open from 'open';
import open from 'open';
import { interactiveLoginWithAuthResponse, loginWithServicePrincipalSecretWithAuthResponse } from '@azure/ms-rest-nodeauth';
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import AzureProvider from '../../provider/azureProvider';

export class AzureLoginPlugin {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/logs/azureLogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import AzureProvider from '../../provider/azureProvider';

export class AzureLogs {
Expand Down
37 changes: 1 addition & 36 deletions src/plugins/package/azurePackage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import { existsSync, renameSync, statSync } from 'fs';
import { join } from "path";
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import AzureProvider from '../../provider/azureProvider';
import { createEventsBindings } from '../../shared/bindings';
import { getFunctionMetaData } from '../../shared/utils';
Expand All @@ -13,7 +11,6 @@ export class AzurePackage {
constructor(private serverless: Serverless, private options: Serverless.Options) {
this.hooks = {
'package:setupProviderConfiguration': this.setupProviderConfiguration.bind(this),
'before:webpack:package:packageModules': this.webpackFunctionJson.bind(this)
};
}

Expand All @@ -29,36 +26,4 @@ export class AzurePackage {

return Promise.all(createEventsPromises);
}

private async webpackFunctionJson(): Promise<any> {
if (existsSync('.webpack')) {
const webpackJsonPromises = this.serverless.service.getAllFunctions()
.map((functionName) => this.moveJsonFile(functionName));

return Promise.all(webpackJsonPromises);
}

return Promise.resolve();
}

private async moveJsonFile(functionName): Promise<any> {
const dirPath = join(this.serverless.config.servicePath, '.webpack', functionName);
const jsonFileName = `${functionName}-function.json`;
const jsonFileSrcPath = join(this.serverless.config.servicePath, jsonFileName);
const jsonFileDestPath = join(dirPath, jsonFileName);

return new Promise((resolve) => {
if (existsSync(dirPath) && statSync(dirPath).isDirectory()) {
if (existsSync(jsonFileSrcPath)) {
this.serverless.cli.log(`Moving ${jsonFileName} to .webpack directory.`);
renameSync(jsonFileSrcPath, jsonFileDestPath);
}
else {
this.serverless.cli.log(`Warning: No generated ${functionName}-function.json file was found. It will not be included in the package.`);
}
}

resolve();
});
}
}
26 changes: 0 additions & 26 deletions src/plugins/package/azurePackageFunction.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/remove/azureRemove.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import { ResourceService } from '../../services/resourceService';

export class AzureRemove {
Expand Down
6 changes: 3 additions & 3 deletions src/provider/azureProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import fs from 'fs';
import { join } from 'path';
import * as request from 'request';
import * as Serverless from 'serverless';
import request from 'request';
import Serverless from 'serverless';
import config from '../config';

let functionAppName;
Expand Down
32 changes: 14 additions & 18 deletions src/services/apimService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import { ApiManagementClient } from '@azure/arm-apimanagement';
import { FunctionAppService } from './functionAppService';
import { BaseService } from './baseService';
Expand All @@ -22,11 +22,7 @@ export class ApimService extends BaseService {
/**
* Deploys the APIM top level api
*/
async deployApi() {
if (!this.config) {
return;
}

public async deployApi() {
const functionApp = await this.functionAppService.get();

await this.ensureApi();
Expand All @@ -37,12 +33,8 @@ export class ApimService extends BaseService {
/**
* Deploys all the functions of the serverless service to APIM
*/
async deployFunctions() {
if (!this.config) {
return;
}

this.serverless.cli.log('Starting to deploy API Operations');
public async deployFunctions() {
this.serverless.cli.log('-> Deploying API Operations');

const deployApiTasks = this.serverless.service
.getAllFunctions()
Expand All @@ -55,7 +47,7 @@ export class ApimService extends BaseService {
* Deploys the specified serverless function to APIM
* @param options
*/
async deployFunction(options) {
public async deployFunction(options) {
const functionConfig = this.serverless.service['functions'][options.function];

if (!functionConfig.apim) {
Expand All @@ -75,7 +67,9 @@ export class ApimService extends BaseService {
/**
* Deploys the APIM API referenced by the serverless service
*/
async ensureApi() {
private async ensureApi() {
this.serverless.cli.log('-> Deploying API')

try {
await this.apimClient.api.createOrUpdate(this.resourceGroup, this.config.resourceId, this.config.name, {
isCurrent: true,
Expand All @@ -96,7 +90,8 @@ export class ApimService extends BaseService {
* Deploys the APIM Backend referenced by the serverless service
* @param functionAppUrl The host name for the deployed function app
*/
async ensureBackend(functionApp) {
private async ensureBackend(functionApp) {
this.serverless.cli.log('-> Deploying API Backend')
try {
const functionAppResourceId = `https://management.azure.com${functionApp.id}`;

Expand All @@ -122,8 +117,8 @@ export class ApimService extends BaseService {
* @param serverless The serverless framework
* @param options The plugin options
*/
async deployOperation(options) {
this.serverless.cli.log(`-> Deploying API operation ${options.function}`);
private async deployOperation(options) {
this.serverless.cli.log(`--> Deploying API operation ${options.function}`);

try {
const client = new ApiManagementClient(this.credentials, this.subscriptionId);
Expand Down Expand Up @@ -167,7 +162,8 @@ export class ApimService extends BaseService {
* Gets the master key for the function app and stores a reference in the APIM instance
* @param functionAppUrl The host name for the Azure function app
*/
async ensureFunctionAppKeys(functionApp) {
private async ensureFunctionAppKeys(functionApp) {
this.serverless.cli.log('-> Deploying API keys')
try {
const masterKey = await this.functionAppService.getMasterKey(functionApp);
const keyName = `${this.serviceName}-key`;
Expand Down
22 changes: 21 additions & 1 deletion src/services/baseService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Serverless from 'serverless';
import Serverless from 'serverless';
import axios from 'axios';
import request from 'request'
import fs from 'fs';

export abstract class BaseService {
protected baseUrl: string;
Expand Down Expand Up @@ -63,4 +65,22 @@ export abstract class BaseService {
}, interval);
});
}

/**
* Uploads the specified file via HTTP request
* @param requestOptions The HTTP request options
* @param filePath The local file path
*/
protected sendFile(requestOptions, filePath) {
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(request(requestOptions, (err, response) => {
if (err) {
this.serverless.cli.log(JSON.stringify(err, null, 4));
return reject(err);
}
resolve(response);
}));
});
}
}
Loading