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

Commit 1ad3ce2

Browse files
authored
feat: Set GET as default HTTP method for APIM inferred configuration (#328)
1 parent f6d642a commit 1ad3ce2

File tree

8 files changed

+83
-20
lines changed

8 files changed

+83
-20
lines changed

src/armTemplates/compositeArmTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArmResourceTemplate, ArmResourceTemplateGenerator, ArmParameters } from "../models/armTemplates";
1+
import { ArmParameters, ArmResourceTemplate, ArmResourceTemplateGenerator } from "../models/armTemplates";
22
import { ServerlessAzureConfig } from "../models/serverless";
33
import { AzureNamingService } from "../services/namingService";
44
import { Guard } from "../shared/guard";

src/models/azureProvider.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
export interface FunctionEvent {
2-
http?: boolean;
3-
"x-azure-settings"?: {
4-
authLevel?: string;
5-
direction?: string;
6-
name?: string;
7-
};
8-
}
1+
import { ServerlessAzureFunctionBindingConfig } from "./serverless";
92

103
export interface ServicePrincipalEnvVariables {
114
AZURE_SUBSCRIPTION_ID: string;
@@ -16,7 +9,7 @@ export interface ServicePrincipalEnvVariables {
169

1710
export interface FunctionMetadata {
1811
handler: string;
19-
events: FunctionEvent[];
12+
events: ServerlessAzureFunctionBindingConfig[];
2013
}
2114

2215
export interface DeploymentExtendedError {

src/models/serverless.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export interface ServerlessExtraAzureSettingsConfig {
145145
route?: string;
146146
name?: string;
147147
authLevel?: string;
148+
methods?: string[];
148149
}
149150

150151
export interface ServerlessCommand {

src/plugins/invoke/azureInvokePlugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import fs from "fs";
2-
import { isAbsolute } from "path";
2+
import path, { isAbsolute } from "path";
33
import Serverless from "serverless";
44
import { InvokeService } from "../../services/invokeService";
55
import { AzureBasePlugin } from "../azureBasePlugin";
6-
import path from "path";
76

87
export class AzureInvokePlugin extends AzureBasePlugin {
98

src/services/apimService.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,76 @@ describe("APIM Service", () => {
457457
}
458458
);
459459
});
460+
461+
it("uses GET as default HTTP method with inferred APIM operation", async () => {
462+
const functions = MockFactory.createTestSlsFunctionConfig();
463+
functions.hello.events.forEach((event) => delete event["x-azure-settings"].methods);
464+
functions.goodbye.events.forEach((event) => delete event["x-azure-settings"].methods);
465+
Object.assign(serverless.service, { functions });
466+
467+
let apimResource: ApiManagementServiceResource = {
468+
name: apimConfig.name,
469+
location: "West US",
470+
gatewayUrl: "https://you.url.com",
471+
publisherEmail: "someone@example.com",
472+
publisherName: "Someone",
473+
sku: {
474+
capacity: 1,
475+
name: "Consumption",
476+
},
477+
};
478+
479+
ApiManagementService.prototype.get =
480+
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiManagementServiceGetResponse>(apimResource, 200));
481+
Api.prototype.createOrUpdate =
482+
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiCreateOrUpdateResponse>(expectedApiResult, 201));
483+
Backend.prototype.createOrUpdate =
484+
jest.fn(() => MockFactory.createTestArmSdkResponse<BackendCreateOrUpdateResponse>(expectedBackend, 201));
485+
Property.prototype.createOrUpdate =
486+
jest.fn(() => MockFactory.createTestArmSdkResponse<PropertyCreateOrUpdateResponse>(expectedProperty, 201));
487+
ApiPolicy.prototype.createOrUpdate =
488+
jest.fn(() => MockFactory.createTestArmSdkResponse<ApiPolicyCreateOrUpdateResponse>(expectedProperty, 201));
489+
ApiOperation.prototype.createOrUpdate =
490+
jest.fn((resourceGroup, serviceName, apiName, operationName, operationContract) => {
491+
const response = MockFactory.createTestArmSdkResponse<ApiOperationCreateOrUpdateResponse>(operationContract, 201);
492+
return Promise.resolve(response);
493+
});
494+
495+
const apimService = new ApimService(serverless);
496+
await apimService.deploy();
497+
498+
expect(ApiOperation.prototype.createOrUpdate).toBeCalledTimes(2)
499+
expect(ApiOperation.prototype.createOrUpdate).toBeCalledWith(
500+
resourceGroupName,
501+
serviceName,
502+
apiName,
503+
"hello",
504+
{
505+
displayName: "hello",
506+
name: "hello",
507+
description: "",
508+
method: "get",
509+
urlTemplate: "hello",
510+
templateParameters: [],
511+
responses: [],
512+
}
513+
);
514+
expect(ApiOperation.prototype.createOrUpdate).toBeCalledWith(
515+
resourceGroupName,
516+
serviceName,
517+
apiName,
518+
"goodbye",
519+
{
520+
displayName: "goodbye",
521+
name: "goodbye",
522+
description: "",
523+
method: "get",
524+
urlTemplate: "goodbye",
525+
templateParameters: [],
526+
responses: [],
527+
}
528+
);
529+
});
460530
});
461531

462532
describe("Deploying Functions", () => {

src/services/apimService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { Site } from "@azure/arm-appservice/esm/models";
1212
import { Guard } from "../shared/guard";
1313
import { ApimResource } from "../armTemplates/resources/apim";
14+
import { ServerlessExtraAzureSettingsConfig } from "../models/serverless";
1415

1516
/**
1617
* APIM Service handles deployment and integration with Azure API Management
@@ -142,11 +143,11 @@ export class ApimService extends BaseService {
142143
return;
143144
}
144145

145-
const httpConfig = httpEvent["x-azure-settings"];
146+
const httpConfig: ServerlessExtraAzureSettingsConfig = httpEvent["x-azure-settings"];
146147

147-
// Set to GET method by default
148+
// Default to GET method if not specified
148149
if (!httpConfig.methods) {
149-
httpConfig.methods = ["GET"];
150+
httpConfig.methods = [ "GET" ]
150151
}
151152

152153
// Infer APIM operation configuration from HTTP event if not already set

src/services/armService.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ describe("Arm Service", () => {
352352
};
353353
deployment.parameters.param1.value = "3"
354354
const { code, message, details } = previousDeploymentError;
355-
let errorPattern = [
355+
const errorPattern = [
356356
code,
357357
message,
358358
details[0].code,
@@ -412,7 +412,6 @@ describe("Arm Service", () => {
412412
template: MockFactory.createTestArmTemplate()
413413
};
414414
deployment.parameters.param1.value = "3"
415-
416415
await expect(service.deployTemplate(deployment))
417416
.rejects
418417
.toThrowError(originalError);

src/test/mockFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ApiCorsPolicy, ApiManagementConfig } from "../models/apiManagement";
1515
import { ArmDeployment, ArmResourceTemplate, ArmTemplateProvisioningState, ArmParameters, ArmParamType } from "../models/armTemplates";
1616
import { ServicePrincipalEnvVariables } from "../models/azureProvider";
1717
import { Logger } from "../models/generic";
18-
import { ServerlessAzureConfig, ServerlessAzureProvider, ServerlessAzureFunctionConfig, ServerlessCliCommand } from "../models/serverless";
18+
import { ServerlessAzureConfig, ServerlessAzureProvider, ServerlessAzureFunctionConfig, ServerlessCliCommand, ServerlessAzureFunctionBindingConfig } from "../models/serverless";
1919
import configConstants from "../config";
2020

2121
function getAttribute(object: any, prop: string, defaultValue: any): any {
@@ -328,14 +328,14 @@ export class MockFactory {
328328
};
329329
}
330330

331-
public static createTestFunctionMetadata(name: string) {
331+
public static createTestFunctionMetadata(name: string): ServerlessAzureFunctionConfig {
332332
return {
333333
"handler": `${name}.handler`,
334334
"events": MockFactory.createTestFunctionEvents(),
335335
}
336336
}
337337

338-
public static createTestFunctionEvents() {
338+
public static createTestFunctionEvents(): ServerlessAzureFunctionBindingConfig[] {
339339
return [
340340
{
341341
"http": true,

0 commit comments

Comments
 (0)