Skip to content

Commit aad3160

Browse files
committed
fix: add deleting compatibility for all resources
1 parent c2594ba commit aad3160

File tree

6 files changed

+83
-55
lines changed

6 files changed

+83
-55
lines changed

src/baas/apigw/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class Apigw {
2323
}
2424
}
2525

26+
async deleteRequest(inputs) {
27+
inputs.Region = this.region
28+
try {
29+
await this.apigwClient.request(inputs)
30+
} catch (e) {
31+
console.log(e)
32+
}
33+
}
34+
2635
async createOrUpdateService(serviceConf) {
2736
const {
2837
serviceId,
@@ -570,7 +579,7 @@ class Apigw {
570579
// 1.1 unbind secrete ids
571580
const { secrets } = curApi.usagePlan
572581
if (secrets && secrets.secretIds) {
573-
await this.request({
582+
await this.deleteRequest({
574583
Action: 'UnBindSecretIds',
575584
secretIds: secrets.secretIds,
576585
usagePlanId: curApi.usagePlan.usagePlanId
@@ -581,11 +590,11 @@ class Apigw {
581590
if (curApi.usagePlan.secrets.created === true) {
582591
for (let sIdx = 0; sIdx < secrets.secretIds.length; sIdx++) {
583592
const secretId = secrets.secretIds[sIdx]
584-
await this.request({
593+
await this.deleteRequest({
585594
Action: 'DisableApiKey',
586595
secretId
587596
})
588-
await this.request({
597+
await this.deleteRequest({
589598
Action: 'DeleteApiKey',
590599
secretId
591600
})
@@ -595,7 +604,7 @@ class Apigw {
595604
}
596605

597606
// 1.2 unbind environment
598-
await this.request({
607+
await this.deleteRequest({
599608
Action: 'UnBindEnvironment',
600609
serviceId,
601610
usagePlanIds: [curApi.usagePlan.usagePlanId],
@@ -612,7 +621,7 @@ class Apigw {
612621
console.log(
613622
`Removing any previously deployed usage plan ids ${curApi.usagePlan.usagePlanId}`
614623
)
615-
await this.request({
624+
await this.deleteRequest({
616625
Action: 'DeleteUsagePlan',
617626
usagePlanId: curApi.usagePlan.usagePlanId
618627
})
@@ -622,7 +631,7 @@ class Apigw {
622631
// 2. delete only apis created by serverless framework
623632
if (curApi.apiId && curApi.created === true) {
624633
console.log(`Removing api: ${curApi.apiId}`)
625-
await this.request({
634+
await this.deleteRequest({
626635
Action: 'DeleteApi',
627636
apiId: curApi.apiId,
628637
serviceId
@@ -636,7 +645,7 @@ class Apigw {
636645
const curDomain = customDomains[i]
637646
if (curDomain.subDomain && curDomain.created === true) {
638647
console.log(`Unbinding custom domain: ${curDomain.subDomain}`)
639-
await this.request({
648+
await this.deleteRequest({
640649
Action: 'UnBindSubDomain',
641650
serviceId,
642651
subDomain: curDomain.subDomain
@@ -647,7 +656,7 @@ class Apigw {
647656

648657
// 3. unrelease service
649658
console.log(`Unreleasing service: ${serviceId}, environment: ${environment}`)
650-
await this.request({
659+
await this.deleteRequest({
651660
Action: 'UnReleaseService',
652661
serviceId,
653662
environmentName: environment,
@@ -657,7 +666,7 @@ class Apigw {
657666
if (created === true) {
658667
// delete service
659668
console.log(`Removing service: ${serviceId}`)
660-
await this.request({
669+
await this.deleteRequest({
661670
Action: 'DeleteService',
662671
serviceId
663672
})

src/baas/cdn/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ class Cdn {
211211
// disable first
212212
await StopCdnDomain(this.capi, { Domain: domain })
213213
} else if (Status === 'processing') {
214-
throw new Error(`Status is not operational for ${domain}`)
214+
console.log(`Status is not operational for ${domain}`)
215+
return {}
215216
}
216217
console.log(`Waiting for offline ${domain}...`)
217218
await waitResponse({

src/baas/cos/index.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,10 @@ class Cos {
329329

330330
async getBucket(inputs = {}) {
331331
const getBucketHandler = util.promisify(this.cosClient.getBucket.bind(this.cosClient))
332-
try {
333-
return await getBucketHandler({
334-
Bucket: inputs.bucket,
335-
Region: this.region
336-
})
337-
} catch (e) {
338-
throw new Error(JSON.stringify(e))
339-
}
332+
return getBucketHandler({
333+
Bucket: inputs.bucket,
334+
Region: this.region
335+
})
340336
}
341337

342338
async upload(inputs = {}) {
@@ -495,41 +491,49 @@ class Cos {
495491
console.log(`Removing bucket from ${this.region} ...`)
496492

497493
// 获取全部文件
498-
const fileListResult = await this.getBucket(inputs)
499-
494+
let detail
500495
try {
501-
const fileList = []
502-
if (fileListResult && fileListResult.Contents && fileListResult.Contents.length > 0) {
503-
// delete files
504-
for (let i = 0; i < fileListResult.Contents.length; i++) {
505-
fileList.push({
506-
Key: fileListResult.Contents[i].Key
507-
})
496+
detail = await this.getBucket(inputs)
497+
} catch (e) {
498+
if (e.error && e.error.Code && e.error.Code === 'NoSuchBucket') {
499+
console.log(`Bucket ${inputs.bucket} not exist`)
500+
return
501+
}
502+
}
503+
504+
if (detail && detail.Contents) {
505+
// delete files
506+
const objectList = (detail.Contents || []).map((item) => {
507+
return {
508+
Key: item.Key
508509
}
510+
})
511+
512+
try {
509513
const deleteMultipleObjectHandler = util.promisify(
510514
this.cosClient.deleteMultipleObject.bind(this.cosClient)
511515
)
512-
try {
513-
await deleteMultipleObjectHandler({
514-
Region: this.region,
515-
Bucket: inputs.bucket,
516-
Objects: fileList
517-
})
518-
} catch (e) {
519-
throw new Error(JSON.stringify(e))
520-
}
516+
await deleteMultipleObjectHandler({
517+
Region: this.region,
518+
Bucket: inputs.bucket,
519+
Objects: objectList
520+
})
521+
} catch (e) {
522+
console.log(e)
523+
}
524+
try {
521525
const deleteBucketHandler = util.promisify(this.cosClient.deleteBucket.bind(this.cosClient))
522-
try {
523-
await deleteBucketHandler({
524-
Region: this.region,
525-
Bucket: inputs.bucket
526-
})
527-
} catch (e) {
528-
throw new Error(JSON.stringify(e))
526+
await deleteBucketHandler({
527+
Region: this.region,
528+
Bucket: inputs.bucket
529+
})
530+
} catch (e) {
531+
if (e.error && e.error.Code && e.error.Code === 'NoSuchBucket') {
532+
console.log(`Bucket ${inputs.bucket} not exist`)
533+
} else {
534+
throw e
529535
}
530536
}
531-
} catch (e) {
532-
throw new Error(JSON.stringify(e))
533537
}
534538
}
535539
}

src/baas/scf/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,33 @@ class Scf {
497497

498498
// 移除函数的主逻辑
499499
async remove(inputs = {}) {
500-
console.log(`Deleteing funtion ${inputs.functionName || inputs.FunctionName} ...`)
500+
console.log(`Deleteing function ${inputs.functionName || inputs.FunctionName} ...`)
501501
const functionName = inputs.functionName || inputs.FunctionName
502502
const namespace = inputs.namespace || inputs.Namespace || defaultNamespace
503503

504504
// check function exist, then delete
505-
const func = await this.getFunction(functionName, namespace)
505+
const func = await this.getFunction(namespace, functionName)
506506

507507
if (!func) {
508508
console.log(`Funtion ${functionName} not exist...`)
509509
return
510510
}
511511

512+
if (func.Status === 'Updating' || func.Status === 'Creating') {
513+
console.log(`Funtion ${functionName} status is ${func.Status}, can not delete...`)
514+
return
515+
}
516+
512517
await this.deleteFunction(functionName, namespace)
513518

514519
if (inputs.Triggers) {
515520
for (let i = 0; i < inputs.Triggers.length; i++) {
516521
if (inputs.Triggers[i].serviceId) {
517-
await this.deleteAPIGW(inputs.Triggers[i])
522+
try {
523+
await this.deleteAPIGW(inputs.Triggers[i])
524+
} catch (e) {
525+
console.log(e)
526+
}
518527
}
519528
}
520529
}

src/baas/tag/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ class Tag {
2929

3030
console.log(`Modify tags ... `)
3131
try {
32-
const tagsResult = await this.tagClient.request(tagsInputs)
33-
if (tagsResult.Response && tagsResult.Response.Error) {
34-
throw new Error(JSON.stringify(tagsResult.Response))
35-
}
32+
await this.tagClient.request(tagsInputs)
3633
} catch (e) {
37-
throw e
34+
console.log(e)
3835
}
3936
console.log(`Modified tags.`)
4037

src/baas/vpc/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,20 @@ class Vpc {
139139
const { vpcId, subnetId } = inputs
140140
if (subnetId) {
141141
console.log(`Start removing subnet ${subnetId}`)
142-
await utils.deleteSubnet(this.capi, subnetId)
142+
try {
143+
await utils.deleteSubnet(this.capi, subnetId)
144+
} catch (e) {
145+
console.log(e)
146+
}
143147
console.log(`Removed subnet ${subnetId}`)
144148
}
145149
if (vpcId) {
146150
console.log(`Start removing vpc ${vpcId}`)
147-
await utils.deleteVpc(this.capi, vpcId)
151+
try {
152+
await utils.deleteVpc(this.capi, vpcId)
153+
} catch (e) {
154+
console.log(e)
155+
}
148156
console.log(`Removed vpc ${vpcId}`)
149157
}
150158

0 commit comments

Comments
 (0)