Merge pull request #12678 from chlins/fix/preheat-delete-instance-precheck

fix(preheat): add precheck when delete instance
This commit is contained in:
Chlins Zhang 2020-08-08 12:54:00 +08:00 committed by GitHub
commit 6b9929d848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -887,7 +887,7 @@ paths:
description: List preheat policies success
headers:
X-Total-Count:
description: The total count of tags
description: The total count of policies
type: integer
Link:
description: Link refers to the previous page and next page
@ -1032,7 +1032,7 @@ paths:
description: List executions success
headers:
X-Total-Count:
description: The total count of tags
description: The total count of executions
type: integer
Link:
description: Link refers to the previous page and next page
@ -1128,7 +1128,7 @@ paths:
description: List tasks success
headers:
X-Total-Count:
description: The total count of tags
description: The total count of tasks
type: integer
Link:
description: Link refers to the previous page and next page

View File

@ -81,6 +81,22 @@ func (api *preheatAPI) DeleteInstance(ctx context.Context, params operation.Dele
return api.SendError(ctx, err)
}
// delete instance should check the instance whether be used by policies
policies, err := api.preheatCtl.ListPolicies(ctx, &q.Query{
Keywords: map[string]interface{}{
"provider_id": instance.ID,
},
})
if err != nil {
return api.SendError(ctx, err)
}
if len(policies) > 0 {
return api.SendError(ctx, liberrors.New(nil).
WithCode(liberrors.PreconditionCode).
WithMessage("Can't delete instance %s, %d preheat policies use it as provider", instance.Name, len(policies)))
}
err = api.preheatCtl.DeleteInstance(ctx, instance.ID)
if err != nil {
return api.SendError(ctx, err)
@ -293,7 +309,7 @@ func (api *preheatAPI) DeletePolicy(ctx context.Context, params operation.Delete
// Detecting running tasks under the policy
if err = detectRunningExecutions(executions); err != nil {
return api.SendError(ctx, err)
return api.SendError(ctx, liberrors.New(err).WithCode(liberrors.PreconditionCode))
}
err = api.preheatCtl.DeletePolicy(ctx, policy.ID)