Skip to content
Prev Previous commit
Next Next commit
feat(apigw): only modify exact changed custom domain
  • Loading branch information
wwwzbwcom committed Feb 22, 2021
commit 7e39b8c3df30a0d550d37b097806c9bab045df7b
54 changes: 37 additions & 17 deletions src/modules/apigw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ import {
ApigwBindCustomDomainInputs,
ApigwBindUsagePlanOutputs,
ApigwCustomDomain,
ApigwCustomDomainFormatted,
} from './interface';

interface FormattedApigwCustomDomain {
domain: string;
protocols: string;

certificateId?: string;
isDefaultMapping: boolean;
pathMappingSetDict: Record<string, string>;
netType: string;
isForcedHttps: boolean;
}

function getProtocolString(protocols: string | ('http' | 'https')[]) {
if (!protocols || protocols.length < 1) {
return 'http';
Expand All @@ -40,7 +50,7 @@ function getProtocolString(protocols: string | ('http' | 'https')[]) {
}

function getCustomDomainFormattedDict(domains: ApigwCustomDomain[]) {
const domainDict: Record<string, ApigwCustomDomainFormatted> = {};
const domainDict: Record<string, FormattedApigwCustomDomain> = {};
domains.forEach((d) => {
const pmDict: Record<string, string> = {};
for (const pm of d.pathMappingSet ?? []) {
Expand Down Expand Up @@ -414,10 +424,10 @@ export default class Apigw {
}
| undefined;

const domainDict: Record<string, ApigwCustomDomainFormatted> = {};
const domainDict: Record<string, FormattedApigwCustomDomain> = {};

for (const d of res?.DomainSet ?? []) {
const domain: ApigwCustomDomainFormatted = {
const domain: FormattedApigwCustomDomain = {
domain: d.DomainName,
protocols: d.Protocol,
certificateId: d.CertificateId,
Expand Down Expand Up @@ -450,10 +460,15 @@ export default class Apigw {
}

/**
* 解绑 API 网关所有自定义域名
* 解绑 API 网关所有自定义域名,不解绑当前已有并且需要配置的域名
* @param serviceId API 网关 ID
*/
async unbindCustomDomain(serviceId: string, oldCustomDomains: ApigwCustomDomain[]) {
async unbindCustomDomain(
serviceId: string,
oldCustomDomains: ApigwCustomDomain[],
currentDict: Record<string, FormattedApigwCustomDomain> = {},
newDict: Record<string, FormattedApigwCustomDomain> = {},
) {
const customDomainDetail = (await this.request({
Action: 'DescribeServiceSubDomains',
ServiceId: serviceId,
Expand All @@ -469,6 +484,15 @@ export default class Apigw {
const stateDomains = oldCustomDomains ?? [];
for (let i = 0; i < DomainSet.length; i++) {
const domainItem = DomainSet[i];
const domain = domainItem.DomainName ?? '';
// 当前绑定状态与新的绑定状态一致,不解绑
if (currentDict[domain] && deepEqual(currentDict[domain], newDict[domain])) {
console.log(
`Domain ${domainItem.DomainName} for service ${serviceId} unchanged, won't unbind`,
);
continue;
}

for (let j = 0; j < stateDomains.length; j++) {
// 只解绑由组件创建的域名
if (stateDomains[j].subDomain === domainItem.DomainName) {
Expand Down Expand Up @@ -505,16 +529,8 @@ export default class Apigw {
const currentDict = await this.getCurrentCustomDomainsDict(serviceId);
const newDict = getCustomDomainFormattedDict(inputs.customDomains ?? []);

let hasChange = true;
if (deepEqual(currentDict, newDict)) {
hasChange = false;
console.log("Custom domain unchange, won't unbind or bind");
}

if (hasChange) {
// 1. 解绑旧的自定义域名
await this.unbindCustomDomain(serviceId, oldState?.customDomains ?? []);
}
// 1. 解绑旧的自定义域名
await this.unbindCustomDomain(serviceId, oldState?.customDomains ?? [], currentDict, newDict);

// 2. bind user config domain
const customDomainOutput: ApigwBindCustomDomainOutputs[] = [];
Expand All @@ -540,13 +556,17 @@ export default class Apigw {
};

try {
if (hasChange) {
const { domain } = domainItem;
if (currentDict[domain] && deepEqual(currentDict[domain], newDict[domain])) {
await this.request({
Action: 'BindSubDomain',
...domainInputs,
});
console.log(`Custom domain for service ${serviceId} created successfullly.`);
console.log(`Please add CNAME record ${subDomain} for ${domainItem.domain}.`);
} else {
console.log(`Custom domain for service ${serviceId} unchanged, wont create.`);
console.log(`Please add CNAME record ${subDomain} for ${domainItem.domain}.`);
}

customDomainOutput.push({
Expand Down
11 changes: 0 additions & 11 deletions src/modules/apigw/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,6 @@ export interface ApiEndpoint {
};
}

export interface ApigwCustomDomainFormatted {
domain: string;
protocols: string;

certificateId?: string;
isDefaultMapping: boolean;
pathMappingSetDict: Record<string, string>;
netType: string;
isForcedHttps: boolean;
}

export interface ApigwCustomDomain {
domain: string;
protocols?: ('http' | 'https')[] | string;
Expand Down