diff --git a/pkg/apis/compute/cachedimage.go b/pkg/apis/compute/cachedimage.go new file mode 100644 index 0000000000..51142bd8ec --- /dev/null +++ b/pkg/apis/compute/cachedimage.go @@ -0,0 +1,25 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package compute + +type CachedImageUncacheImageInput struct { + // 存储缓存名Id + // required: true + StoragecacheId string `json:"storagecache_id"` + + // 是否强制清除缓存 + // default: false + IsForce bool `json:"is_force"` +} diff --git a/pkg/compute/models/cachedimages.go b/pkg/compute/models/cachedimages.go index f8edcd5649..d2a2870754 100644 --- a/pkg/compute/models/cachedimages.go +++ b/pkg/compute/models/cachedimages.go @@ -336,6 +336,26 @@ func (self *SCachedimage) PerformRefresh(ctx context.Context, userCred mcclient. return jsonutils.Marshal(img), nil } +func (self *SCachedimage) AllowPerformUncacheImage(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { + return db.IsAdminAllowPerform(userCred, self, "uncache-image") +} + +// 清除镜像缓存 +func (self *SCachedimage) PerformUncacheImage(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.CachedImageUncacheImageInput) (jsonutils.JSONObject, error) { + if len(input.StoragecacheId) == 0 { + return nil, httperrors.NewMissingParameterError("storagecache_id") + } + _storagecache, err := StoragecacheManager.FetchById(input.StoragecacheId) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, httperrors.NewResourceNotFoundError("failed to found storagecache %s", input.StoragecacheId) + } + return nil, errors.Wrap(err, "StoragecacheManager.FetchById") + } + storagecache := _storagecache.(*SStoragecache) + return storagecache.PerformUncacheImage(ctx, userCred, query, jsonutils.Marshal(map[string]interface{}{"image": self.Id, "is_force": input.IsForce})) +} + func (self *SCachedimage) addRefCount() { if self.GetStatus() != "active" { return