From 8d21e622ec9b148b1f46559d6f43a446e1915588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E9=9B=A8?= Date: Mon, 9 Dec 2019 20:26:23 +0800 Subject: [PATCH] fix(region): Guest template & Service catalog Add the common create input. Not allow to delete the guest template used by some service catalog. --- pkg/apis/compute/guesttemplate.go | 9 +-------- .../{service_compute.go => service_catalog.go} | 9 +-------- pkg/apis/sharablevirtualresource.go | 6 ++++++ pkg/apis/standaloneresource.go | 11 +++++++++++ pkg/apis/virtualresource.go | 5 +++++ pkg/compute/models/guest_template.go | 13 +++++++++++++ 6 files changed, 37 insertions(+), 16 deletions(-) rename pkg/apis/compute/{service_compute.go => service_catalog.go} (84%) diff --git a/pkg/apis/compute/guesttemplate.go b/pkg/apis/compute/guesttemplate.go index 936e7518c2..48cc8be90a 100644 --- a/pkg/apis/compute/guesttemplate.go +++ b/pkg/apis/compute/guesttemplate.go @@ -7,14 +7,7 @@ import ( ) type GuesttemplateCreateInput struct { - apis.Meta - - // description: guest template name - // unique: true - // required: true - // example: hello - Name string `json:"name"` - + apis.SharableVirutalResourceCreateInput // description: the content of guest template // required: true Content jsonutils.JSONObject `json:"content"` diff --git a/pkg/apis/compute/service_compute.go b/pkg/apis/compute/service_catalog.go similarity index 84% rename from pkg/apis/compute/service_compute.go rename to pkg/apis/compute/service_catalog.go index c73dc9bb16..14fbde673b 100644 --- a/pkg/apis/compute/service_compute.go +++ b/pkg/apis/compute/service_catalog.go @@ -3,14 +3,7 @@ package compute import "yunion.io/x/onecloud/pkg/apis" type ServiceCatalogCreateInput struct { - apis.Meta - - // description: service catalog name - // uniqure: true - // required: true - // example: hello - Name string `json:"name` - + apis.SharableVirutalResourceCreateInput // description: service catalog icon url // example: https://yunion.io/files/hello.png IconUrl string `json:"icon_url"` diff --git a/pkg/apis/sharablevirtualresource.go b/pkg/apis/sharablevirtualresource.go index 7d4d82a219..9d2ee9fcc6 100644 --- a/pkg/apis/sharablevirtualresource.go +++ b/pkg/apis/sharablevirtualresource.go @@ -27,3 +27,9 @@ type SharableVirtualResourceDetails struct { type SharableVirtualResourceListInput struct { StandaloneResourceListInput } + +type SharableVirutalResourceCreateInput struct { + VirtualResourceCreateInput + IsPublic bool `json:"is_public"` + PublicScope string `json:"public_scope"` +} diff --git a/pkg/apis/standaloneresource.go b/pkg/apis/standaloneresource.go index 55360d2a72..b7abd35829 100644 --- a/pkg/apis/standaloneresource.go +++ b/pkg/apis/standaloneresource.go @@ -27,3 +27,14 @@ type StandaloneResourceListInput struct { Tags []string `json:"tags"` WithoutUserMeta bool `json:"without_user_meta"` } + +type StandaloneResourceCreatInput struct { + Meta + // description: resource name + // unique: true + // required: true + // example: yunion + Name string `json:"name"` + Description string `json:"description"` + IsEmulated bool `json:"is_emulated"` +} diff --git a/pkg/apis/virtualresource.go b/pkg/apis/virtualresource.go index 2df8b7ebea..2a36788fa8 100644 --- a/pkg/apis/virtualresource.go +++ b/pkg/apis/virtualresource.go @@ -17,3 +17,8 @@ package apis type VirtualResourceDetails struct { ModelBaseDetails } + +type VirtualResourceCreateInput struct { + StandaloneResourceCreatInput + IsSystem bool `json:"is_system"` +} diff --git a/pkg/compute/models/guest_template.go b/pkg/compute/models/guest_template.go index ae7bd559bb..79f53d51d5 100644 --- a/pkg/compute/models/guest_template.go +++ b/pkg/compute/models/guest_template.go @@ -395,3 +395,16 @@ func (gt *SGuestTemplate) genForbiddenError(resourceName, resourceStr, scope str } return httperrors.NewForbiddenError(msg) } + +func (gt *SGuestTemplate) ValidateDeleteCondition(ctx context.Context) error { + q := ServiceCatalogManager.Query("name").Equals("guest_template_id", gt.Id) + names := make([]string, 0, 1) + err := q.All(&names) + if err != nil { + return errors.Wrap(err, "SQuery.All") + } + if len(names) > 0 { + return httperrors.NewForbiddenError("guest template %s used by service catalog %s", gt.Id, names[0]) + } + return nil +}