fix(preheat): add precheck when delete instance

Signed-off-by: chlins <chlins.zhang@gmail.com>
This commit is contained in:
chlins 2020-08-07 14:06:35 +08:00
parent 19234cdb46
commit 5c433c0875
2 changed files with 20 additions and 4 deletions

View File

@ -887,7 +887,7 @@ paths:
description: List preheat policies success description: List preheat policies success
headers: headers:
X-Total-Count: X-Total-Count:
description: The total count of tags description: The total count of policies
type: integer type: integer
Link: Link:
description: Link refers to the previous page and next page description: Link refers to the previous page and next page
@ -1032,7 +1032,7 @@ paths:
description: List executions success description: List executions success
headers: headers:
X-Total-Count: X-Total-Count:
description: The total count of tags description: The total count of executions
type: integer type: integer
Link: Link:
description: Link refers to the previous page and next page description: Link refers to the previous page and next page
@ -1128,7 +1128,7 @@ paths:
description: List tasks success description: List tasks success
headers: headers:
X-Total-Count: X-Total-Count:
description: The total count of tags description: The total count of tasks
type: integer type: integer
Link: Link:
description: Link refers to the previous page and next page 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) 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) err = api.preheatCtl.DeleteInstance(ctx, instance.ID)
if err != nil { if err != nil {
return api.SendError(ctx, err) 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 // Detecting running tasks under the policy
if err = detectRunningExecutions(executions); err != nil { 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) err = api.preheatCtl.DeletePolicy(ctx, policy.ID)