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

Commit d1fcfc9

Browse files
wbrezatbarlow12
authored andcommitted
fix: Removed service name from storage account name generator (#232)
When the storage account name is generated it includes a hash of the sls service name. By default if you deploy multiple services/apps into the same resource group this causes multiple storage accounts to be created vs. reusing the same one. By removing the service name from the storage account generation logic it simplifies the case to reuse the same account. This value can still be overridden by specifying your own name in the sls yaml configuration.
1 parent d2d9a2f commit d1fcfc9

File tree

11 files changed

+95
-75
lines changed

11 files changed

+95
-75
lines changed

package-lock.json

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"js-yaml": "^3.13.1",
5151
"jsonpath": "^1.0.1",
5252
"lodash": "^4.16.6",
53-
"md5": "^2.2.1",
5453
"open": "^6.3.0",
5554
"request": "^2.81.0",
5655
"rimraf": "^2.6.3",

src/armTemplates/resources/apim.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { AzureNamingService } from "../../services/namingService";
55

66
export class ApimResource implements ArmResourceTemplateGenerator {
77
public static getResourceName(config: ServerlessAzureConfig) {
8-
return AzureNamingService.getResourceName(
9-
config,
10-
config.provider.apim,
11-
"apim"
12-
);
8+
return AzureNamingService.getResourceName(config, config.provider.apim, "apim");
139
}
1410

1511
public getTemplate(): ArmResourceTemplate {

src/armTemplates/resources/appInsights.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { AzureNamingService } from "../../services/namingService";
44

55
export class AppInsightsResource implements ArmResourceTemplateGenerator {
66
public static getResourceName(config: ServerlessAzureConfig) {
7-
return AzureNamingService.getResourceName(
8-
config,
9-
config.provider.appInsights,
10-
"appinsights"
11-
);
7+
return AzureNamingService.getResourceName(config, config.provider.appInsights, "appinsights");
128
}
139

1410
public getTemplate(): ArmResourceTemplate {

src/armTemplates/resources/appServicePlan.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { AzureNamingService } from "../../services/namingService";
44

55
export class AppServicePlanResource implements ArmResourceTemplateGenerator {
66
public static getResourceName(config: ServerlessAzureConfig) {
7-
return AzureNamingService.getResourceName(
8-
config,
9-
config.provider.appInsights,
10-
"asp"
11-
);
7+
return AzureNamingService.getResourceName(config, config.provider.appInsights, "asp");
128
}
139

1410
public getTemplate(): ArmResourceTemplate {

src/armTemplates/resources/functionApp.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import { AzureNamingService } from "../../services/namingService";
55
export class FunctionAppResource implements ArmResourceTemplateGenerator {
66
public static getResourceName(config: ServerlessAzureConfig) {
77
const safeServiceName = config.service.replace(/\s/g, "-");
8-
return AzureNamingService.getResourceName(
9-
config,
10-
config.provider.appInsights,
11-
safeServiceName
12-
);
8+
9+
return AzureNamingService.getResourceName(config, config.provider.appInsights, safeServiceName);
1310
}
1411

1512
public getTemplate(): ArmResourceTemplate {

src/armTemplates/resources/hostingEnvironment.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { AzureNamingService } from "../../services/namingService";
44

55
export class HostingEnvironmentResource implements ArmResourceTemplateGenerator {
66
public static getResourceName(config: ServerlessAzureConfig) {
7-
return AzureNamingService.getResourceName(
8-
config,
9-
config.provider.hostingEnvironment,
10-
"ase"
11-
);
7+
return AzureNamingService.getResourceName(config, config.provider.hostingEnvironment, "ase");
128
}
139

1410
public getTemplate(): ArmResourceTemplate {

src/armTemplates/resources/storageAccount.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import md5 from "md5";
21
import { ServerlessAzureConfig } from "../../models/serverless";
32
import { AzureNamingService } from "../../services/namingService";
43
import { StorageAccountResource } from "./storageAccount";
@@ -35,8 +34,7 @@ describe("Storage Account Resource", () => {
3534
prefix: "my-long-test-prefix-name",
3635
region: "Australia Southeast",
3736
stage: "development"
38-
},
39-
service: "my-long-test-api",
37+
}
4038
};
4139

4240
const result = StorageAccountResource.getResourceName(testConfig);
@@ -145,10 +143,9 @@ describe("Storage Account Resource", () => {
145143
expect(value).toContain(AzureNamingService.createShortAzureRegionName(config.provider.region));
146144
expect(value).toContain(createSafeString(config.provider.prefix));
147145
expect(value).toContain(createSafeString(config.provider.stage));
148-
expect(value).toContain(md5(config.service).substr(0, 3));
149146
}
150147

151148
function createSafeString(value: string) {
152-
return value.replace(/\W+/g, "").toLocaleLowerCase().substr(0, 3);
149+
return value.replace(/\W+/g, "").toLowerCase().substr(0, 3);
153150
};
154151
});

src/armTemplates/resources/storageAccount.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import configConstants from "../../config";
55

66
export class StorageAccountResource implements ArmResourceTemplateGenerator {
77
public static getResourceName(config: ServerlessAzureConfig) {
8-
return AzureNamingService.getSafeResourceName(
9-
config,
10-
configConstants.naming.maxLength.storageAccount,
11-
config.provider.storageAccount
12-
);
8+
return AzureNamingService.getSafeResourceName(config, configConstants.naming.maxLength.storageAccount, config.provider.storageAccount);
139
}
1410

1511
public getTemplate(): ArmResourceTemplate {

src/services/namingService.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AzureNamingService } from "./namingService"
2+
import { ServerlessAzureConfig } from "../models/serverless";
23

34
describe("Naming Service", () => {
4-
55
it("Creates a short name for an azure region", () => {
66
const expected = "ausse";
77
const actual = AzureNamingService.createShortAzureRegionName("australiasoutheast");
@@ -86,4 +86,57 @@ describe("Naming Service", () => {
8686
const actual = AzureNamingService.getNormalizedRegionName(expected);
8787
expect(actual).toEqual(expected);
8888
});
89+
90+
it("deployment name is generated correctly", () => {
91+
const config: ServerlessAzureConfig = {
92+
functions: [],
93+
plugins: [],
94+
provider: {
95+
prefix: "sls",
96+
name: "azure",
97+
region: "westus",
98+
stage: "dev",
99+
},
100+
service: "test-api"
101+
};
102+
103+
const timestamp = Date.now();
104+
const deploymentName = AzureNamingService.getDeploymentName(config, `t${timestamp}`);
105+
106+
expect(deploymentName).toEqual(`slswusdevtestapi-DEPLOYMENT-t${timestamp}`);
107+
assertValidDeploymentName(config, deploymentName, timestamp);
108+
});
109+
110+
it("deployment name with long suffix or service name generated correctly", () => {
111+
const config: ServerlessAzureConfig = {
112+
functions: [],
113+
plugins: [],
114+
provider: {
115+
prefix: "sls-long-prefix-name",
116+
name: "azure",
117+
region: "westus",
118+
stage: "multicloud",
119+
},
120+
service: "extra-long-service-name"
121+
};
122+
123+
const timestamp = Date.now();
124+
const deploymentName = AzureNamingService.getDeploymentName(config, `t${timestamp}`);
125+
126+
expect(deploymentName).toEqual(`slswusmulext-DEPLOYMENT-t${timestamp}`);
127+
assertValidDeploymentName(config, deploymentName, timestamp);
128+
});
129+
130+
function assertValidDeploymentName(config: ServerlessAzureConfig, value: string, timestamp: number) {
131+
expect(value.length).toBeLessThanOrEqual(64);
132+
expect(value).toContain(timestamp);
133+
expect(value).toContain(AzureNamingService.createShortAzureRegionName(config.provider.region));
134+
expect(value).toContain(createSafeString(config.provider.prefix));
135+
expect(value).toContain(createSafeString(config.provider.stage));
136+
expect(value).toContain(createSafeString(config.service));
137+
}
138+
139+
function createSafeString(value: string) {
140+
return value.replace(/\W+/g, "").toLowerCase().substr(0, 3);
141+
};
89142
});

0 commit comments

Comments
 (0)