@@ -549,6 +549,30 @@ class Apigw {
549
549
return outputs ;
550
550
}
551
551
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
+
552
576
async createOrUpdateApi ( { serviceId, endpoint, environment, created } ) {
553
577
// compatibility for secret auth config depends on auth & usagePlan
554
578
const authType = endpoint . auth ? 'SECRET' : endpoint . authType || 'NONE' ;
@@ -588,48 +612,33 @@ class Apigw {
588
612
output . authRelationApiId = endpoint . authRelationApiId ;
589
613
}
590
614
591
- let exist = false ;
592
- let apiDetail = null ;
615
+ this . marshalApiInput ( endpoint , apiInputs ) ;
593
616
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 ,
599
621
} ) ;
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
- }
612
622
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' ,
618
629
apiId : endpoint . apiId ,
630
+ ...apiInputs ,
619
631
} ) ;
620
632
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 {
628
638
const { ApiId } = await this . request ( {
629
639
Action : 'CreateApi' ,
630
640
...apiInputs ,
631
641
} ) ;
632
-
633
642
output . apiId = ApiId ;
634
643
output . created = true ;
635
644
@@ -640,18 +649,6 @@ class Apigw {
640
649
apiId : output . apiId ,
641
650
} ) ;
642
651
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` ) ;
655
652
}
656
653
657
654
output . apiName = apiInputs . apiName ;
0 commit comments