yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/objectstore/xsky/bucket.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package xsky 16 17 import ( 18 "context" 19 "strconv" 20 "time" 21 22 "yunion.io/x/log" 23 "yunion.io/x/pkg/errors" 24 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud/objectstore" 27 ) 28 29 type SXskyBucket struct { 30 *objectstore.SBucket 31 32 client *SXskyClient 33 } 34 35 func (b *SXskyBucket) GetStats() cloudprovider.SBucketStats { 36 _, hdr, _ := b.GetIBucketProvider().S3Client().BucketExists(b.Name) 37 if hdr != nil { 38 sizeBytesStr := hdr.Get("X-Rgw-Bytes-Used") 39 sizeBytes, _ := strconv.ParseInt(sizeBytesStr, 10, 64) 40 objCntStr := hdr.Get("X-Rgw-Object-Count") 41 objCnt, _ := strconv.ParseInt(objCntStr, 10, 64) 42 return cloudprovider.SBucketStats{ 43 SizeBytes: sizeBytes, 44 ObjectCount: int(objCnt), 45 } 46 } 47 return b.SBucket.GetStats() 48 } 49 50 func (b *SXskyBucket) LimitSupport() cloudprovider.SBucketStats { 51 return cloudprovider.SBucketStats{ 52 SizeBytes: 1, 53 ObjectCount: 1, 54 } 55 } 56 57 func (b *SXskyBucket) GetLimit() cloudprovider.SBucketStats { 58 limit := cloudprovider.SBucketStats{} 59 bucket, err := b.client.adminApi.getBucketByName(context.Background(), b.Name) 60 if err != nil { 61 log.Errorf("b.client.adminApi.getBucketByName error %s", err) 62 } else { 63 limit.SizeBytes = bucket.QuotaMaxSize 64 limit.ObjectCount = bucket.QuotaMaxObjects 65 } 66 return limit 67 } 68 69 func (b *SXskyBucket) SetLimit(limit cloudprovider.SBucketStats) error { 70 bucket, err := b.client.adminApi.getBucketByName(context.Background(), b.Name) 71 if err != nil { 72 return errors.Wrap(err, "b.client.adminApi.getBucketByName") 73 } 74 input := sBucketQuotaInput{} 75 input.OsBucket.QuotaMaxObjects = limit.ObjectCount 76 input.OsBucket.QuotaMaxSize = limit.SizeBytes 77 err = b.client.adminApi.setBucketQuota(context.Background(), bucket.Id, input) 78 if err != nil { 79 return errors.Wrap(err, "b.client.adminApi.setBucketQuota") 80 } 81 cloudprovider.Wait(time.Second, 30*time.Second, func() (bool, error) { 82 target := b.GetLimit() 83 if target.SizeBytes == limit.SizeBytes && target.ObjectCount == limit.ObjectCount { 84 return true, nil 85 } 86 return false, nil 87 }) 88 return nil 89 }