Skip to content

Commit

Permalink
feat: Set GET as default HTTP method for APIM inferred configuration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarlow12 authored Sep 12, 2019
1 parent f6d642a commit 1ad3ce2
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/armTemplates/compositeArmTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArmResourceTemplate, ArmResourceTemplateGenerator, ArmParameters } from "../models/armTemplates";
import { ArmParameters, ArmResourceTemplate, ArmResourceTemplateGenerator } from "../models/armTemplates";
import { ServerlessAzureConfig } from "../models/serverless";
import { AzureNamingService } from "../services/namingService";
import { Guard } from "../shared/guard";
Expand Down
11 changes: 2 additions & 9 deletions src/models/azureProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export interface FunctionEvent {
http?: boolean;
"x-azure-settings"?: {
authLevel?: string;
direction?: string;
name?: string;
};
}
import { ServerlessAzureFunctionBindingConfig } from "./serverless";

export interface ServicePrincipalEnvVariables {
AZURE_SUBSCRIPTION_ID: string;
Expand All @@ -16,7 +9,7 @@ export interface ServicePrincipalEnvVariables {

export interface FunctionMetadata {
handler: string;
events: FunctionEvent[];
events: ServerlessAzureFunctionBindingConfig[];
}

export interface DeploymentExtendedError {
Expand Down
1 change: 1 addition & 0 deletions src/models/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface ServerlessExtraAzureSettingsConfig {
route?: string;
name?: string;
authLevel?: string;
methods?: string[];
}

export interface ServerlessCommand {
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/invoke/azureInvokePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import fs from "fs";
import { isAbsolute } from "path";
import path, { isAbsolute } from "path";
import Serverless from "serverless";
import { InvokeService } from "../../services/invokeService";
import { AzureBasePlugin } from "../azureBasePlugin";
import path from "path";

export class AzureInvokePlugin extends AzureBasePlugin {

Expand Down
70 changes: 70 additions & 0 deletions src/services/apimService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,76 @@ describe("APIM Service", () => {
}
);
});

it("uses GET as default HTTP method with inferred APIM operation", async () => {
const functions = MockFactory.createTestSlsFunctionConfig();
functions.hello.events.forEach((event) => delete event["x-azure-settings"].methods);
functions.goodbye.events.forEach((event) => delete event["x-azure-settings"].methods);
Object.assign(serverless.service, { functions });

let apimResource: ApiManagementServiceResource = {
name: apimConfig.name,
location: "West US",
gatewayUrl: "https://you.url.com",
publisherEmail: "someone@example.com",
publisherName: "Someone",
sku: {
capacity: 1,
name: "Consumption",
},
};

ApiManagementService.prototype.get =
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiManagementServiceGetResponse>(apimResource, 200));
Api.prototype.createOrUpdate =
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiCreateOrUpdateResponse>(expectedApiResult, 201));
Backend.prototype.createOrUpdate =
jest.fn(() => MockFactory.createTestArmSdkResponse<BackendCreateOrUpdateResponse>(expectedBackend, 201));
Property.prototype.createOrUpdate =
jest.fn(() => MockFactory.createTestArmSdkResponse<PropertyCreateOrUpdateResponse>(expectedProperty, 201));
ApiPolicy.prototype.createOrUpdate =
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiPolicyCreateOrUpdateResponse>(expectedProperty, 201));
ApiOperation.prototype.createOrUpdate =
jest.fn((resourceGroup, serviceName, apiName, operationName, operationContract) => {
const response = MockFactory.createTestArmSdkResponse<ApiOperationCreateOrUpdateResponse>(operationContract, 201);
return Promise.resolve(response);
});

const apimService = new ApimService(serverless);
await apimService.deploy();

expect(ApiOperation.prototype.createOrUpdate).toBeCalledTimes(2)
expect(ApiOperation.prototype.createOrUpdate).toBeCalledWith(
resourceGroupName,
serviceName,
apiName,
"hello",
{
displayName: "hello",
name: "hello",
description: "",
method: "get",
urlTemplate: "hello",
templateParameters: [],
responses: [],
}
);
expect(ApiOperation.prototype.createOrUpdate).toBeCalledWith(
resourceGroupName,
serviceName,
apiName,
"goodbye",
{
displayName: "goodbye",
name: "goodbye",
description: "",
method: "get",
urlTemplate: "goodbye",
templateParameters: [],
responses: [],
}
);
});
});

describe("Deploying Functions", () => {
Expand Down
7 changes: 4 additions & 3 deletions src/services/apimService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Site } from "@azure/arm-appservice/esm/models";
import { Guard } from "../shared/guard";
import { ApimResource } from "../armTemplates/resources/apim";
import { ServerlessExtraAzureSettingsConfig } from "../models/serverless";

/**
* APIM Service handles deployment and integration with Azure API Management
Expand Down Expand Up @@ -142,11 +143,11 @@ export class ApimService extends BaseService {
return;
}

const httpConfig = httpEvent["x-azure-settings"];
const httpConfig: ServerlessExtraAzureSettingsConfig = httpEvent["x-azure-settings"];

// Set to GET method by default
// Default to GET method if not specified
if (!httpConfig.methods) {
httpConfig.methods = ["GET"];
httpConfig.methods = [ "GET" ]
}

// Infer APIM operation configuration from HTTP event if not already set
Expand Down
3 changes: 1 addition & 2 deletions src/services/armService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ describe("Arm Service", () => {
};
deployment.parameters.param1.value = "3"
const { code, message, details } = previousDeploymentError;
let errorPattern = [
const errorPattern = [
code,
message,
details[0].code,
Expand Down Expand Up @@ -412,7 +412,6 @@ describe("Arm Service", () => {
template: MockFactory.createTestArmTemplate()
};
deployment.parameters.param1.value = "3"

await expect(service.deployTemplate(deployment))
.rejects
.toThrowError(originalError);
Expand Down
6 changes: 3 additions & 3 deletions src/test/mockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ApiCorsPolicy, ApiManagementConfig } from "../models/apiManagement";
import { ArmDeployment, ArmResourceTemplate, ArmTemplateProvisioningState, ArmParameters, ArmParamType } from "../models/armTemplates";
import { ServicePrincipalEnvVariables } from "../models/azureProvider";
import { Logger } from "../models/generic";
import { ServerlessAzureConfig, ServerlessAzureProvider, ServerlessAzureFunctionConfig, ServerlessCliCommand } from "../models/serverless";
import { ServerlessAzureConfig, ServerlessAzureProvider, ServerlessAzureFunctionConfig, ServerlessCliCommand, ServerlessAzureFunctionBindingConfig } from "../models/serverless";
import configConstants from "../config";

function getAttribute(object: any, prop: string, defaultValue: any): any {
Expand Down Expand Up @@ -328,14 +328,14 @@ export class MockFactory {
};
}

public static createTestFunctionMetadata(name: string) {
public static createTestFunctionMetadata(name: string): ServerlessAzureFunctionConfig {
return {
"handler": `${name}.handler`,
"events": MockFactory.createTestFunctionEvents(),
}
}

public static createTestFunctionEvents() {
public static createTestFunctionEvents(): ServerlessAzureFunctionBindingConfig[] {
return [
{
"http": true,
Expand Down

0 comments on commit 1ad3ce2

Please sign in to comment.