From 5c433c087517ed8555e7dc7ea291ff7430e66b2c Mon Sep 17 00:00:00 2001 From: chlins Date: Fri, 7 Aug 2020 14:06:35 +0800 Subject: [PATCH] fix(preheat): add precheck when delete instance Signed-off-by: chlins --- api/v2.0/swagger.yaml | 6 +++--- src/server/v2.0/handler/preheat.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index 26ad8bc07..060ffee4a 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -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 diff --git a/src/server/v2.0/handler/preheat.go b/src/server/v2.0/handler/preheat.go index ca3d442c4..e7cd12262 100644 --- a/src/server/v2.0/handler/preheat.go +++ b/src/server/v2.0/handler/preheat.go @@ -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)