Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
fix: Remove dead, outdated AzureProvider code (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarlow12 committed Sep 13, 2019
1 parent 9c606de commit 54d1296
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 218 deletions.
12 changes: 12 additions & 0 deletions src/provider/azureProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MockFactory } from "../test/mockFactory";
import AzureProvider from "./azureProvider"

describe("Azure Provider", () => {

it("sets the provider name in initialization", () => {
const sls = MockFactory.createTestServerless();
new AzureProvider(sls);
expect(sls.setProvider).toBeCalledWith("azure", expect.anything());
expect(AzureProvider.getProviderName()).toEqual("azure");
});
});
218 changes: 0 additions & 218 deletions src/provider/azureProvider.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import fs from "fs";
import { join } from "path";
import request from "request";
import Serverless from "serverless";
import config from "../config";

let functionAppName;
let functionsAdminKey;
let invocationId;

export default class AzureProvider {
public credentials: any;
private serverless: any;

public static getProviderName() {
Expand All @@ -20,214 +12,4 @@ export default class AzureProvider {
this.serverless = serverless;
this.serverless.setProvider(config.providerName, this);
}

public getAdminKey(): Promise<any> {
const options = {
url: `https://${functionAppName}${config.scmDomain}${config.masterKeyApiPath}`,
json: true,
headers: {
Authorization: config.bearer + this.credentials.tokenCache._entries[0].accessToken
}
};

return new Promise((resolve, reject) => {
request(options, (err, response, body) => {
if (err) return reject(err);
if (response.statusCode !== 200) return reject(body);

functionsAdminKey = body.masterKey;

resolve(body.masterKey);
});
});
}

public pingHostStatus(functionName): Promise<any> {
const requestUrl = `https://${functionAppName}${config.functionAppDomain}/admin/functions/${functionName}/status`;
const options = {
host: functionAppName + config.functionAppDomain,
method: "get",
url: requestUrl,
json: true,
headers: {
"x-functions-key": functionsAdminKey,
Accept: "application/json,*/*"
}
};

return new Promise((resolve, reject) => {
this.serverless.cli.log("Pinging host status...");
request(options, (err, res, body) => {
if (err) return reject(err);
if (body && body.Error) return reject(body.Error);

if (res.statusCode !== 200) {
return body && body.Error ? reject(body.Error) : reject(body);
}

resolve(res);
});
});
}

public getLogsStream(functionName) {
const logOptions = {
url: `https://${functionAppName}${config.scmDomain}${config.logStreamApiPath}${functionName}`,
headers: {
Authorization: config.bearer + this.credentials.tokenCache._entries[0].accessToken,
Accept: "*/*"
}
};

request
.get(logOptions)
.on("error", () => {
console.error("Disconnected from log streaming.");
})
.on("end", () => this.getLogsStream(functionName))
.pipe(process.stdout);
}

public getInvocationId(functionName): Promise<any> {
const options = {
url: `https://${functionAppName}${config.scmDomain}${config.logInvocationsApiPath + functionAppName}-${functionName}/invocations?limit=5`,
method: "GET",
json: true,
headers: {
Authorization: config.bearer + this.credentials.tokenCache._entries[0].accessToken
}
};

return new Promise((resolve, reject) => {
request(options, (err, response, body) => {
if (err) return reject(err);
if (response.statusCode !== 200) return reject(body);

invocationId = body.entries[0].id;

resolve(body.entries[0].id);
});
});
}

public getLogsForInvocationId(): Promise<any> {
this.serverless.cli.log(`Logs for InvocationId: ${invocationId}`);
const options = {
url: `https://${functionAppName}${config.scmDomain}${config.logOutputApiPath}${invocationId}`,
method: "GET",
json: true,
headers: {
Authorization: config.bearer + this.credentials.tokenCache._entries[0].accessToken
}
};

return new Promise((resolve, reject) => {
request(options, (err, response, body) => {
if (err) return reject(err);
if (response.statusCode !== 200) return reject(body);

resolve(body);
});
});
}

public invoke(functionName, eventType, eventData): Promise<any> {
if (eventType === "http") {
let queryString = "";

if (eventData) {
if (typeof eventData === "string") {
try {
eventData = JSON.parse(eventData);
}
catch (error) {
return Promise.reject("The specified input data isn't a valid JSON string. " +
"Please correct it and try invoking the function again.");
}
}

queryString = Object.keys(eventData)
.map((key) => `${key}=${eventData[key]}`)
.join("&");
}

return new Promise((resolve, reject) => {
const options = {
headers: {
"x-functions-key": functionsAdminKey
},
url: `http://${functionAppName}${config.functionAppDomain}${config.functionAppApiPath + functionName}?${queryString}`,
method: "GET",
json: true,
};

this.serverless.cli.log(`Invoking function "${functionName}"`);
request(options, (err, response, body) => {
if (err) return reject(err);
if (response.statusCode !== 200) return reject(body);

console.log(body);

resolve(body);
});
});
}

const requestUrl = `https://${functionAppName}${config.functionAppDomain}${config.functionsAdminApiPath}${functionName}`;

const options = {
host: config.functionAppDomain,
method: "post",
body: eventData,
url: requestUrl,
json: true,
headers: {
"x-functions-key": functionsAdminKey,
Accept: "application/json,*/*"
}
};

return new Promise((resolve, reject) => {
request(options, (err, res) => {
if (err) {
reject(err);
}
this.serverless.cli.log(`Invoked function at: ${requestUrl}. \nResponse statuscode: ${res.statusCode}`);
resolve(res);
});
});
}

public uploadPackageJson(): Promise<any> {
const packageJsonFilePath = join(this.serverless.config.servicePath, "package.json");
this.serverless.cli.log("Uploading package.json...");

const requestUrl = `https://${functionAppName}${config.scmDomain}${config.scmVfsPath}package.json`;
const options = {
host: functionAppName + config.scmDomain,
method: "put",
url: requestUrl,
json: true,
headers: {
Authorization: config.bearer + this.credentials.tokenCache._entries[0].accessToken,
Accept: "*/*"
}
};

return new Promise((resolve, reject) => {
if (fs.existsSync(packageJsonFilePath)) {
fs.createReadStream(packageJsonFilePath)
.pipe(request.put(options, (err) => {
if (err) {
reject(err);
} else {
resolve("Package.json file uploaded");
}
}));
}
else {
resolve("Package.json file does not exist");
}
});
}
}
1 change: 1 addition & 0 deletions src/test/mockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class MockFactory {
sls.variables = getAttribute(config, "variables", MockFactory.createTestVariables());
sls.service = getAttribute(config, "service", MockFactory.createTestService());
sls.config.servicePath = "";
sls.setProvider = jest.fn();
return sls;
}

Expand Down

0 comments on commit 54d1296

Please sign in to comment.