@@ -39,6 +39,28 @@ export default class ServiceEntity {
3939 return result as never ;
4040 }
4141
42+ /**
43+ * 获取 API 网关列表
44+ * @param options 参数
45+ * @returns 网关列表
46+ */
47+ async list ( options ?: { offset ?: number ; limit ?: number } ) {
48+ options = {
49+ ...{ limit : 10 , offset : 0 } ,
50+ ...( options || { } ) ,
51+ } ;
52+ try {
53+ const res : { TotalCount : number ; ServiceSet : any [ ] } = await this . request ( {
54+ Action : 'DescribeServicesStatus' ,
55+ Offset : options . offset ,
56+ Limit : options . limit ,
57+ } ) ;
58+ return res . ServiceSet || [ ] ;
59+ } catch ( e ) {
60+ return [ ] ;
61+ }
62+ }
63+
4264 async getById ( serviceId : string ) {
4365 try {
4466 const detail : Detail = await this . request ( {
@@ -52,6 +74,112 @@ export default class ServiceEntity {
5274 }
5375 }
5476
77+ async removeApiUsagePlan ( ServiceId : string ) {
78+ const { ApiUsagePlanList = [ ] } = await this . request ( {
79+ Action : 'DescribeApiUsagePlan' ,
80+ ServiceId,
81+ } ) ;
82+
83+ for ( let i = 0 ; i < ApiUsagePlanList . length ; i ++ ) {
84+ const { UsagePlanId, Environment, ApiId } = ApiUsagePlanList [ i ] ;
85+ console . log ( `APIGW - Removing api usage plan: ${ UsagePlanId } ` ) ;
86+ const { AccessKeyList = [ ] } = await this . request ( {
87+ Action : 'DescribeUsagePlanSecretIds' ,
88+ UsagePlanId : UsagePlanId ,
89+ Limit : 100 ,
90+ } ) ;
91+
92+ const AccessKeyIds = AccessKeyList . map ( ( item : { SecretId : string } ) => item . SecretId ) ;
93+
94+ if ( AccessKeyIds && AccessKeyIds . length > 0 ) {
95+ await this . request ( {
96+ Action : 'UnBindSecretIds' ,
97+ UsagePlanId : UsagePlanId ,
98+ AccessKeyIds : AccessKeyIds ,
99+ } ) ;
100+ // delelet all created api key
101+ for ( let sIdx = 0 ; sIdx < AccessKeyIds . length ; sIdx ++ ) {
102+ await this . request ( {
103+ Action : 'DisableApiKey' ,
104+ AccessKeyId : AccessKeyIds [ sIdx ] ,
105+ } ) ;
106+ }
107+ }
108+
109+ // unbind environment
110+ await this . request ( {
111+ Action : 'UnBindEnvironment' ,
112+ ServiceId,
113+ UsagePlanIds : [ UsagePlanId ] ,
114+ Environment : Environment ,
115+ BindType : 'API' ,
116+ ApiIds : [ ApiId ] ,
117+ } ) ;
118+
119+ await this . request ( {
120+ Action : 'DeleteUsagePlan' ,
121+ UsagePlanId : UsagePlanId ,
122+ } ) ;
123+ }
124+ }
125+
126+ async removeById ( serviceId : string ) {
127+ try {
128+ const { ApiIdStatusSet = [ ] } = await this . request ( {
129+ Action : 'DescribeApisStatus' ,
130+ ServiceId : serviceId ,
131+ Limit : 100 ,
132+ } ) ;
133+
134+ // remove all apis
135+ for ( let i = 0 ; i < ApiIdStatusSet . length ; i ++ ) {
136+ const { ApiId } = ApiIdStatusSet [ i ] ;
137+
138+ await this . removeApiUsagePlan ( serviceId ) ;
139+
140+ console . log ( `APIGW - Removing api: ${ ApiId } ` ) ;
141+
142+ await this . request ( {
143+ Action : 'DeleteApi' ,
144+ ServiceId : serviceId ,
145+ ApiId,
146+ } ) ;
147+ }
148+
149+ // unrelease service
150+ // get environment list
151+ const { EnvironmentList = [ ] } = await this . request ( {
152+ Action : 'DescribeServiceEnvironmentList' ,
153+ ServiceId : serviceId ,
154+ } ) ;
155+
156+ for ( let i = 0 ; i < EnvironmentList . length ; i ++ ) {
157+ const { EnvironmentName, Status } = EnvironmentList [ i ] ;
158+ if ( Status === 1 ) {
159+ try {
160+ console . log (
161+ `APIGW - Unreleasing service: ${ serviceId } , environment: ${ EnvironmentName } ` ,
162+ ) ;
163+ await this . request ( {
164+ Action : 'UnReleaseService' ,
165+ ServiceId : serviceId ,
166+ EnvironmentName,
167+ } ) ;
168+ } catch ( e ) { }
169+ }
170+ }
171+
172+ // delete service
173+ console . log ( `APIGW - Removing service: ${ serviceId } ` ) ;
174+ await this . request ( {
175+ Action : 'DeleteService' ,
176+ ServiceId : serviceId ,
177+ } ) ;
178+ } catch ( e ) {
179+ console . error ( e ) ;
180+ }
181+ }
182+
55183 /** 创建 API 网关服务 */
56184 async create ( serviceConf : ApigwCreateServiceInputs ) : Promise < ApigwCreateOrUpdateServiceOutputs > {
57185 const {
0 commit comments