From 3b0cf1549ac1f17009016b05c34a5c9f599fe7f7 Mon Sep 17 00:00:00 2001 From: Sampsa Saarela Date: Thu, 25 Apr 2019 12:55:40 +0800 Subject: [PATCH 1/3] fetch exports from cloudformation if used Fn::ImportValue --- index.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index ed8f0471..8168008d 100644 --- a/index.ts +++ b/index.ts @@ -527,14 +527,54 @@ class ServerlessCustomDomain { } } + /** + * Gets value by name from cloudformation exports + */ + public async getImportValue(importValueName, nextToken = null): Promise { + const params = nextToken ? { NextToken: nextToken } : {}; + const result = await this.cloudformation.listExports(params).promise(); + const importValue = result.Exports.find((val: any) => val.Name === importValueName); + if (importValue) { + return importValue.Value; + } + + if (result.NextToken) { + return this.getImportValue(importValueName, result.NextToken); + } + + return null; + } + /** * Gets rest API id from CloudFormation stack */ public async getRestApiId(): Promise { if (this.serverless.service.provider.apiGateway && this.serverless.service.provider.apiGateway.restApiId) { + const restApiId = this.serverless.service.provider.apiGateway.restApiId; + + if (typeof restApiId === "object" && restApiId["Fn::ImportValue"]) { + try { + const importValue = await this.getImportValue(restApiId["Fn::ImportValue"]); + if (importValue) { + return importValue; + } + + throw new Error(`Error: CloudFormation ImportValue not found + by ${restApiId["Fn::ImportValue"]}\n`); + } catch (err) { + this.logIfDebug(err); + throw new Error(`Error: Failed to find CloudFormation ImportValue + by ${restApiId["Fn::ImportValue"]}\n`); + } + } + + if (typeof restApiId === "object") { + throw new Error("Error: Unsupported restApiId object"); + } + this.serverless.cli.log(`Mapping custom domain to existing API ${this.serverless.service.provider.apiGateway.restApiId}.`); - return this.serverless.service.provider.apiGateway.restApiId; + return restApiId; } const stackName = this.serverless.service.provider.stackName || `${this.serverless.service.service}-${this.stage}`; From 896e1b239da3bd94a9061d0c76754812c737a66e Mon Sep 17 00:00:00 2001 From: Sampsa Saarela Date: Thu, 25 Apr 2019 13:26:16 +0800 Subject: [PATCH 2/3] fixed lint error --- index.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 8168008d..3614b188 100644 --- a/index.ts +++ b/index.ts @@ -550,31 +550,31 @@ class ServerlessCustomDomain { */ public async getRestApiId(): Promise { if (this.serverless.service.provider.apiGateway && this.serverless.service.provider.apiGateway.restApiId) { - const restApiId = this.serverless.service.provider.apiGateway.restApiId; - - if (typeof restApiId === "object" && restApiId["Fn::ImportValue"]) { + if (typeof this.serverless.service.provider.apiGateway.restApiId === "object" && + this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]) { try { - const importValue = await this.getImportValue(restApiId["Fn::ImportValue"]); + const importValueName = this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]; + const importValue = await this.getImportValue(importValueName); if (importValue) { return importValue; } throw new Error(`Error: CloudFormation ImportValue not found - by ${restApiId["Fn::ImportValue"]}\n`); + by ${this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]}\n`); } catch (err) { this.logIfDebug(err); throw new Error(`Error: Failed to find CloudFormation ImportValue - by ${restApiId["Fn::ImportValue"]}\n`); + by ${this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]}\n`); } } - if (typeof restApiId === "object") { + if (typeof this.serverless.service.provider.apiGateway.restApiId === "object") { throw new Error("Error: Unsupported restApiId object"); } this.serverless.cli.log(`Mapping custom domain to existing API ${this.serverless.service.provider.apiGateway.restApiId}.`); - return restApiId; + return this.serverless.service.provider.apiGateway.restApiId; } const stackName = this.serverless.service.provider.stackName || `${this.serverless.service.service}-${this.stage}`; From 9093e8c9b13f6a869873e515b2d7fc344be32f4d Mon Sep 17 00:00:00 2001 From: Sampsa Saarela Date: Thu, 25 Apr 2019 13:31:16 +0800 Subject: [PATCH 3/3] bumb version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4d22bb3b..5248f774 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-domain-manager", - "version": "3.2.1", + "version": "3.2.2", "engines": { "node": ">=4.0" },