Skip to content

Commit 9a56046

Browse files
committed
fix(apigw): judge api exist by path and method
1 parent faaf6bf commit 9a56046

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

src/modules/apigw/index.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,30 @@ class Apigw {
549549
return outputs;
550550
}
551551

552+
async getApiByPathAndMethod({ serviceId, path, method }) {
553+
const { ApiIdStatusSet } = await this.request({
554+
Action: 'DescribeApisStatus',
555+
ServiceId: serviceId,
556+
Filters: [{ Name: 'ApiType', Values: ['normal'] }],
557+
});
558+
let apiDetail;
559+
if (ApiIdStatusSet) {
560+
ApiIdStatusSet.forEach((item) => {
561+
if (item.Path === path && item.Method.toLowerCase() === method.toLowerCase()) {
562+
apiDetail = item;
563+
}
564+
});
565+
}
566+
if (apiDetail) {
567+
apiDetail = await this.request({
568+
Action: 'DescribeApi',
569+
serviceId: serviceId,
570+
apiId: apiDetail.ApiId,
571+
});
572+
}
573+
return apiDetail;
574+
}
575+
552576
async createOrUpdateApi({ serviceId, endpoint, environment, created }) {
553577
// compatibility for secret auth config depends on auth & usagePlan
554578
const authType = endpoint.auth ? 'SECRET' : endpoint.authType || 'NONE';
@@ -588,48 +612,33 @@ class Apigw {
588612
output.authRelationApiId = endpoint.authRelationApiId;
589613
}
590614

591-
let exist = false;
592-
let apiDetail = null;
615+
this.marshalApiInput(endpoint, apiInputs);
593616

594-
// check api method+path existance
595-
const pathAPIList = await this.request({
596-
Action: 'DescribeApisStatus',
597-
ServiceId: serviceId,
598-
Filters: [{ Name: 'ApiPath', Values: [endpoint.path] }],
617+
let apiDetail = await this.getApiByPathAndMethod({
618+
serviceId,
619+
path: endpoint.path,
620+
method: endpoint.method,
599621
});
600-
if (pathAPIList.ApiIdStatusSet) {
601-
for (let i = 0; i < pathAPIList.ApiIdStatusSet.length; i++) {
602-
if (
603-
pathAPIList.ApiIdStatusSet[i].Method.toLowerCase() === endpoint.method.toLowerCase() &&
604-
pathAPIList.ApiIdStatusSet[i].Path === endpoint.path
605-
) {
606-
console.log(`Api method ${endpoint.method}, path ${endpoint.path} already exist`);
607-
endpoint.apiId = pathAPIList.ApiIdStatusSet[i].ApiId;
608-
exist = true;
609-
}
610-
}
611-
}
612622

613-
// get API info after apiId confirmed
614-
if (endpoint.apiId) {
615-
apiDetail = await this.request({
616-
Action: 'DescribeApi',
617-
serviceId: serviceId,
623+
if (apiDetail) {
624+
console.log(`Api method ${endpoint.method}, path ${endpoint.path} already exist`);
625+
endpoint.apiId = apiDetail.ApiId;
626+
627+
await this.request({
628+
Action: 'ModifyApi',
618629
apiId: endpoint.apiId,
630+
...apiInputs,
619631
});
620632

621-
if (apiDetail && apiDetail.ApiId) {
622-
exist = true;
623-
}
624-
}
625-
626-
if (!exist) {
627-
this.marshalApiInput(endpoint, apiInputs);
633+
output.apiId = endpoint.apiId;
634+
output.created = !!created;
635+
output.internalDomain = apiDetail.InternalDomain || '';
636+
console.log(`Api ${output.apiId} updated`);
637+
} else {
628638
const { ApiId } = await this.request({
629639
Action: 'CreateApi',
630640
...apiInputs,
631641
});
632-
633642
output.apiId = ApiId;
634643
output.created = true;
635644

@@ -640,18 +649,6 @@ class Apigw {
640649
apiId: output.apiId,
641650
});
642651
output.internalDomain = apiDetail.InternalDomain || '';
643-
} else {
644-
console.log(`Updating api ${endpoint.apiId}.`);
645-
this.marshalApiInput(endpoint, apiInputs);
646-
await this.request({
647-
Action: 'ModifyApi',
648-
apiId: endpoint.apiId,
649-
...apiInputs,
650-
});
651-
output.apiId = endpoint.apiId;
652-
output.created = !!created;
653-
output.internalDomain = apiDetail.InternalDomain || '';
654-
console.log(`Api ${output.apiId} updated`);
655652
}
656653

657654
output.apiName = apiInputs.apiName;

0 commit comments

Comments
 (0)