fix: 支持批量释放镜像缓存

This commit is contained in:
Qu Xuan
2020-01-16 19:09:32 +08:00
parent 7c31d1a7da
commit ad57f66781
2 changed files with 45 additions and 0 deletions
+25
View File
@@ -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"`
}
+20
View File
@@ -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