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

Commit 506ebc6

Browse files
authored
fix: Use stage, region, resourceGroup and prefix from CLI options (#284)
Fixes a regression in accepting `region` and `stage` options from CLI to form resource group name. Adds acceptance of `prefix` as well. Does so by configuring each of them in `serverless.service` (`ServerlessAzureConfig`) as one of the first steps of the `BaseService` constructor. Also puts `location` into `region` inside the config if `location` is specified, but `region` is not. Resolves #280, #282 and #281
1 parent b9b6765 commit 506ebc6

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

src/models/serverless.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export interface ServerlessCommandMap {
9999
}
100100

101101
export interface ServerlessAzureOptions extends Serverless.Options {
102+
prefix?: string;
102103
resourceGroup?: string;
103104
}
104105

src/services/baseService.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs";
22
import mockFs from "mock-fs";
33
import Serverless from "serverless";
4-
import { ServerlessAzureOptions } from "../models/serverless";
4+
import { ServerlessAzureOptions, ServerlessAzureConfig } from "../models/serverless";
55
import { MockFactory } from "../test/mockFactory";
66
import { BaseService } from "./baseService";
77
import { AzureNamingService } from "./namingService";
@@ -40,9 +40,10 @@ class MockService extends BaseService {
4040
describe("Base Service", () => {
4141
let service: MockService;
4242
let sls: Serverless;
43+
const serviceName = "my-custom-service"
4344

4445
const slsConfig = {
45-
service: "my custom service",
46+
service: serviceName,
4647
provider: {
4748
resourceGroup: "My-Resource-Group",
4849
deploymentName: "My-Deployment",
@@ -54,7 +55,6 @@ describe("Base Service", () => {
5455
});
5556

5657
function createMockService(options?: Serverless.Options) {
57-
sls = MockFactory.createTestServerless();
5858
sls.variables["azureCredentials"] = MockFactory.createTestAzureCredentials();
5959
sls.variables["subscriptionId"] = "ABC123";
6060
Object.assign(sls.service, slsConfig);
@@ -63,6 +63,7 @@ describe("Base Service", () => {
6363
}
6464

6565
beforeEach(() => {
66+
sls = MockFactory.createTestServerless();
6667
service = createMockService();
6768
});
6869

@@ -198,4 +199,39 @@ describe("Base Service", () => {
198199

199200
readStreamSpy.mockRestore();
200201
});
202+
203+
it("sets stage name from CLI", async () => {
204+
const stage = "test";
205+
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
206+
expect(sls.service.provider.stage).not.toEqual(stage);
207+
service = new MockService(sls, { stage } as any);
208+
expect(service.getStage()).toEqual(stage);
209+
expect(service.getResourceGroupName()).toEqual(`sls-wus-${stage}-${serviceName}-rg`);
210+
});
211+
212+
it("sets region name from CLI", async () => {
213+
const region = "East US";
214+
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
215+
expect(sls.service.provider.region).not.toEqual(region);
216+
service = new MockService(sls, { region } as any);
217+
expect(service.getRegion()).toEqual(region);
218+
expect(service.getResourceGroupName()).toEqual(`sls-eus-dev-${serviceName}-rg`);
219+
});
220+
221+
it("sets prefix from CLI", async () => {
222+
const prefix = "prefix";
223+
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
224+
expect(sls.service.provider["prefix"]).not.toEqual(prefix);
225+
service = new MockService(sls, { prefix } as any);
226+
expect(service.getPrefix()).toEqual(prefix);
227+
expect(service.getResourceGroupName()).toEqual(`${prefix}-wus-dev-${serviceName}-rg`);
228+
});
229+
230+
it("sets resource group from CLI", async () => {
231+
const resourceGroup = "resourceGroup";
232+
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
233+
expect(sls.service.provider["resourceGroup"]).not.toEqual(resourceGroup);
234+
service = new MockService(sls, { resourceGroup } as any);
235+
expect(service.getResourceGroupName()).toEqual(resourceGroup);
236+
});
201237
});

src/services/baseService.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ export abstract class BaseService {
3535
) {
3636
Guard.null(serverless);
3737
this.setDefaultValues();
38+
this.config = serverless.service as any;
39+
this.setConfigFromCli();
3840

3941
this.baseUrl = "https://management.azure.com";
4042
this.serviceName = this.getServiceName();
41-
this.config = serverless.service as any;
4243
this.credentials = serverless.variables["azureCredentials"];
4344
this.subscriptionId = serverless.variables["subscriptionId"];
4445
this.resourceGroup = this.getResourceGroupName();
45-
this.config.provider.resourceGroup = this.resourceGroup;
4646
this.deploymentConfig = this.getDeploymentConfig();
4747
this.deploymentName = this.getDeploymentName();
4848
this.artifactName = this.getArtifactName(this.deploymentName);
@@ -59,16 +59,19 @@ export abstract class BaseService {
5959
* Name of Azure Region for deployment
6060
*/
6161
public getRegion(): string {
62-
return this.options.region || this.config.provider.region;
62+
return this.config.provider.region;
6363
}
6464

6565
/**
6666
* Name of current deployment stage
6767
*/
6868
public getStage(): string {
69-
return this.options.stage || this.config.provider.stage;
69+
return this.config.provider.stage;
7070
}
7171

72+
/**
73+
* Prefix for service
74+
*/
7275
public getPrefix(): string {
7376
return this.config.provider.prefix;
7477
}
@@ -77,9 +80,7 @@ export abstract class BaseService {
7780
* Name of current resource group
7881
*/
7982
public getResourceGroupName(): string {
80-
return this.options.resourceGroup
81-
|| this.config.provider.resourceGroup
82-
|| AzureNamingService.getResourceName(this.config, null, `${this.serviceName}-rg`);
83+
return this.config.provider.resourceGroup;
8384
}
8485

8586
/**
@@ -243,4 +244,24 @@ export abstract class BaseService {
243244
}
244245
return timestamp;
245246
}
247+
248+
/**
249+
* Overwrite values for resourceGroup, prefix, region and stage
250+
* in config if passed through CLI
251+
*/
252+
private setConfigFromCli() {
253+
const { prefix, region, stage } = this.config.provider;
254+
this.config.provider = {
255+
...this.config.provider,
256+
prefix: this.getOption("prefix") || prefix,
257+
stage: this.getOption("stage") || stage,
258+
region: this.getOption("region") || region,
259+
}
260+
if (!this.config.provider.region && this.config.provider["location"]) {
261+
this.config.provider.region = this.config.provider["location"];
262+
}
263+
this.config.provider.resourceGroup = (
264+
this.getOption("resourceGroup", this.config.provider.resourceGroup)
265+
) || AzureNamingService.getResourceName(this.config, null, `${this.getServiceName()}-rg`);
266+
}
246267
}

src/services/namingService.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ export class AzureNamingService {
123123
* @param stageName The stage name
124124
*/
125125
public static createShortStageName(stageName: string) {
126-
Guard.empty(stageName);
126+
Guard.empty(stageName, "stageName");
127127

128128
const stageMap = {
129129
"dogfood": "df",
130130
"production": "prod",
131+
"prod": "prod",
131132
"development": "dev",
132-
"testing": "test"
133+
"testing": "test",
134+
"test": "test"
133135
};
134136

135137
return this.createShortName(stageName, stageMap);
@@ -140,7 +142,7 @@ export class AzureNamingService {
140142
* @param regionName The region name
141143
*/
142144
public static getNormalizedRegionName(regionName: string) {
143-
Guard.empty(regionName);
145+
Guard.empty(regionName, "regionName");
144146
return regionName.replace(/\W/g, "").toLowerCase();
145147
}
146148

@@ -149,7 +151,7 @@ export class AzureNamingService {
149151
* @param regionName The azure region name
150152
*/
151153
public static createShortAzureRegionName(regionName: string) {
152-
Guard.empty(regionName);
154+
Guard.empty(regionName, "regionName");
153155

154156
const locationMap = {
155157
"north": "n",

0 commit comments

Comments
 (0)