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

Commit

Permalink
fix: Use stage, region, resourceGroup and prefix from CLI options (#284)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tbarlow12 authored Aug 29, 2019
1 parent b9b6765 commit 506ebc6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/models/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface ServerlessCommandMap {
}

export interface ServerlessAzureOptions extends Serverless.Options {
prefix?: string;
resourceGroup?: string;
}

Expand Down
42 changes: 39 additions & 3 deletions src/services/baseService.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import mockFs from "mock-fs";
import Serverless from "serverless";
import { ServerlessAzureOptions } from "../models/serverless";
import { ServerlessAzureOptions, ServerlessAzureConfig } from "../models/serverless";
import { MockFactory } from "../test/mockFactory";
import { BaseService } from "./baseService";
import { AzureNamingService } from "./namingService";
Expand Down Expand Up @@ -40,9 +40,10 @@ class MockService extends BaseService {
describe("Base Service", () => {
let service: MockService;
let sls: Serverless;
const serviceName = "my-custom-service"

const slsConfig = {
service: "my custom service",
service: serviceName,
provider: {
resourceGroup: "My-Resource-Group",
deploymentName: "My-Deployment",
Expand All @@ -54,7 +55,6 @@ describe("Base Service", () => {
});

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

beforeEach(() => {
sls = MockFactory.createTestServerless();
service = createMockService();
});

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

readStreamSpy.mockRestore();
});

it("sets stage name from CLI", async () => {
const stage = "test";
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
expect(sls.service.provider.stage).not.toEqual(stage);
service = new MockService(sls, { stage } as any);
expect(service.getStage()).toEqual(stage);
expect(service.getResourceGroupName()).toEqual(`sls-wus-${stage}-${serviceName}-rg`);
});

it("sets region name from CLI", async () => {
const region = "East US";
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
expect(sls.service.provider.region).not.toEqual(region);
service = new MockService(sls, { region } as any);
expect(service.getRegion()).toEqual(region);
expect(service.getResourceGroupName()).toEqual(`sls-eus-dev-${serviceName}-rg`);
});

it("sets prefix from CLI", async () => {
const prefix = "prefix";
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
expect(sls.service.provider["prefix"]).not.toEqual(prefix);
service = new MockService(sls, { prefix } as any);
expect(service.getPrefix()).toEqual(prefix);
expect(service.getResourceGroupName()).toEqual(`${prefix}-wus-dev-${serviceName}-rg`);
});

it("sets resource group from CLI", async () => {
const resourceGroup = "resourceGroup";
delete (sls.service as any as ServerlessAzureConfig).provider.resourceGroup;
expect(sls.service.provider["resourceGroup"]).not.toEqual(resourceGroup);
service = new MockService(sls, { resourceGroup } as any);
expect(service.getResourceGroupName()).toEqual(resourceGroup);
});
});
35 changes: 28 additions & 7 deletions src/services/baseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export abstract class BaseService {
) {
Guard.null(serverless);
this.setDefaultValues();
this.config = serverless.service as any;
this.setConfigFromCli();

this.baseUrl = "https://management.azure.com";
this.serviceName = this.getServiceName();
this.config = serverless.service as any;
this.credentials = serverless.variables["azureCredentials"];
this.subscriptionId = serverless.variables["subscriptionId"];
this.resourceGroup = this.getResourceGroupName();
this.config.provider.resourceGroup = this.resourceGroup;
this.deploymentConfig = this.getDeploymentConfig();
this.deploymentName = this.getDeploymentName();
this.artifactName = this.getArtifactName(this.deploymentName);
Expand All @@ -59,16 +59,19 @@ export abstract class BaseService {
* Name of Azure Region for deployment
*/
public getRegion(): string {
return this.options.region || this.config.provider.region;
return this.config.provider.region;
}

/**
* Name of current deployment stage
*/
public getStage(): string {
return this.options.stage || this.config.provider.stage;
return this.config.provider.stage;
}

/**
* Prefix for service
*/
public getPrefix(): string {
return this.config.provider.prefix;
}
Expand All @@ -77,9 +80,7 @@ export abstract class BaseService {
* Name of current resource group
*/
public getResourceGroupName(): string {
return this.options.resourceGroup
|| this.config.provider.resourceGroup
|| AzureNamingService.getResourceName(this.config, null, `${this.serviceName}-rg`);
return this.config.provider.resourceGroup;
}

/**
Expand Down Expand Up @@ -243,4 +244,24 @@ export abstract class BaseService {
}
return timestamp;
}

/**
* Overwrite values for resourceGroup, prefix, region and stage
* in config if passed through CLI
*/
private setConfigFromCli() {
const { prefix, region, stage } = this.config.provider;
this.config.provider = {
...this.config.provider,
prefix: this.getOption("prefix") || prefix,
stage: this.getOption("stage") || stage,
region: this.getOption("region") || region,
}
if (!this.config.provider.region && this.config.provider["location"]) {
this.config.provider.region = this.config.provider["location"];
}
this.config.provider.resourceGroup = (
this.getOption("resourceGroup", this.config.provider.resourceGroup)
) || AzureNamingService.getResourceName(this.config, null, `${this.getServiceName()}-rg`);
}
}
10 changes: 6 additions & 4 deletions src/services/namingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ export class AzureNamingService {
* @param stageName The stage name
*/
public static createShortStageName(stageName: string) {
Guard.empty(stageName);
Guard.empty(stageName, "stageName");

const stageMap = {
"dogfood": "df",
"production": "prod",
"prod": "prod",
"development": "dev",
"testing": "test"
"testing": "test",
"test": "test"
};

return this.createShortName(stageName, stageMap);
Expand All @@ -140,7 +142,7 @@ export class AzureNamingService {
* @param regionName The region name
*/
public static getNormalizedRegionName(regionName: string) {
Guard.empty(regionName);
Guard.empty(regionName, "regionName");
return regionName.replace(/\W/g, "").toLowerCase();
}

Expand All @@ -149,7 +151,7 @@ export class AzureNamingService {
* @param regionName The azure region name
*/
public static createShortAzureRegionName(regionName: string) {
Guard.empty(regionName);
Guard.empty(regionName, "regionName");

const locationMap = {
"north": "n",
Expand Down

0 comments on commit 506ebc6

Please sign in to comment.