Repository: serverless/serverless-azure-functions
Pull request #211 (2019-07-19 22:12:37)

fix: apim uses shortened region names
When generating an APIM resource name, the service now
uses the shortened resource name, consistent with other resources.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/24687355/badge)](https://coveralls.io/builds/24687355)

Coverage increased (+0.2%) to 84.463% when pulling **528a2d31f2223a95e2979c8bf09be4d106146cb0 on pjlittle/fix-apim-name** into **2b97f75ca9dd5a6b29e823c4aa46e34bf3195d52 on dev**.

pjlittle: Bumped prerelease version in `package.json`

Patch:
diff --git a/package-lock.json b/package-lock.json
index 8f27d8f..0fb1b9b 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "serverless-azure-functions",
-  "version": "1.0.0-6",
+  "version": "1.0.0-7",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index f8d38ce..7f84b17 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "serverless-azure-functions",
-  "version": "1.0.0-6",
+  "version": "1.0.0-7",
   "description": "Provider plugin for the Serverless Framework v1.x which adds support for Azure Functions.",
   "license": "MIT",
   "main": "./lib/index.js",
diff --git a/src/services/apimService.ts b/src/services/apimService.ts
index 6126a70..9257b13 100644
--- a/src/services/apimService.ts
+++ b/src/services/apimService.ts
@@ -10,6 +10,7 @@ import {
 } from "@azure/arm-apimanagement/esm/models";
 import { Site } from "@azure/arm-appservice/esm/models";
 import { Guard } from "../shared/guard";
+import { ApimResource } from "../armTemplates/resources/apim";
 
 /**
  * APIM Service handles deployment and integration with Azure API Management
@@ -28,7 +29,7 @@ export class ApimService extends BaseService {
     }
 
     if (!this.apimConfig.name) {
-      this.apimConfig.name = `${this.config.provider.prefix}-${this.config.provider.region}-${this.config.provider.stage}-apim`
+      this.apimConfig.name = ApimResource.getResourceName(this.config);
     }
 
     if (!this.apimConfig.backend) {
@@ -325,4 +326,4 @@ export class ApimService extends BaseService {
 
     return xml(policy, { indent: "\t" });
   }
-}
\ No newline at end of file
+}


Test patch:
diff --git a/src/services/apimService.test.ts b/src/services/apimService.test.ts
index 028b8ec..50131f2 100644
--- a/src/services/apimService.test.ts
+++ b/src/services/apimService.test.ts
@@ -19,6 +19,7 @@ import {
   OperationContract,
   ApiPolicyCreateOrUpdateResponse,
 } from "@azure/arm-apimanagement/esm/models";
+import { Utils } from "../shared/utils";
 
 describe("APIM Service", () => {
   const apimConfig = MockFactory.createTestApimConfig();
@@ -46,7 +47,6 @@ describe("APIM Service", () => {
       subscriptionId: "ABC123",
     };
   });
-
   it("is defined", () => {
     expect(ApimService).toBeDefined();
   });
@@ -56,6 +56,16 @@ describe("APIM Service", () => {
     expect(service).not.toBeNull();
   });
 
+  it("when generated, has a shorted region name in the resource name", () => {
+    // if APIM name is ommited, it should auto-generate one that contains a shortened region name
+    const apimConfigName = MockFactory.createTestApimConfig(true);
+    (serverless.service.provider as any).apim = apimConfigName;
+
+    const service = new ApimService(serverless);
+    const expectedRegionName = Utils.createShortAzureRegionName(service.getRegion());
+    expect(apimConfigName.name.includes(expectedRegionName)).toBeTruthy();
+  });
+
   describe("Get service reference", () => {
     it("returns null when service doesn't exist", async () => {
       axios.request = jest.fn((requestConfig) => MockFactory.createTestAxiosResponse(requestConfig, apimGetService404, 404));
diff --git a/src/test/mockFactory.ts b/src/test/mockFactory.ts
index e453e20..2d48e2f 100644
--- a/src/test/mockFactory.ts
+++ b/src/test/mockFactory.ts
@@ -267,9 +267,9 @@ export class MockFactory {
     return (asYaml) ? yaml.dump(data) : data;
   }
 
-  public static createTestApimConfig(): ApiManagementConfig {
+  public static createTestApimConfig(generateName: boolean = false): ApiManagementConfig {
     return {
-      name: "test-apim-resource",
+      name: generateName ? null : "test-apim-resource",
       api: {
         name: "test-apim-api1",
         subscriptionRequired: false,