yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/storagecache.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 zstack 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/log" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/onecloud/pkg/compute/options" 28 "yunion.io/x/onecloud/pkg/mcclient" 29 "yunion.io/x/onecloud/pkg/mcclient/auth" 30 modules "yunion.io/x/onecloud/pkg/mcclient/modules/image" 31 "yunion.io/x/cloudmux/pkg/multicloud" 32 "yunion.io/x/onecloud/pkg/util/qemuimg" 33 ) 34 35 type SStoragecache struct { 36 multicloud.SResourceBase 37 ZStackTags 38 ZoneId string 39 region *SRegion 40 } 41 42 func (scache *SStoragecache) GetId() string { 43 return fmt.Sprintf("%s-%s/%s", scache.region.client.cpcfg.Id, scache.region.GetId(), scache.ZoneId) 44 } 45 46 func (scache *SStoragecache) GetName() string { 47 return fmt.Sprintf("%s-%s/%s", scache.region.client.cpcfg.Name, scache.region.GetId(), scache.ZoneId) 48 } 49 50 func (scache *SStoragecache) GetStatus() string { 51 return "available" 52 } 53 54 func (scache *SStoragecache) Refresh() error { 55 return nil 56 } 57 58 func (scache *SStoragecache) GetGlobalId() string { 59 return scache.GetId() 60 } 61 62 func (scache *SStoragecache) IsEmulated() bool { 63 return false 64 } 65 66 func (scache *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) { 67 return nil, cloudprovider.ErrNotImplemented 68 } 69 70 func (scache *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) { 71 images, err := scache.region.GetImages(scache.ZoneId, "") 72 if err != nil { 73 return nil, err 74 } 75 ret := []cloudprovider.ICloudImage{} 76 for i := 0; i < len(images); i++ { 77 images[i].storageCache = scache 78 ret = append(ret, &images[i]) 79 } 80 return ret, nil 81 } 82 83 func (scache *SStoragecache) GetIImageById(extId string) (cloudprovider.ICloudImage, error) { 84 images, err := scache.region.GetImages(scache.ZoneId, extId) 85 if err != nil { 86 return nil, err 87 } 88 if len(images) == 1 { 89 images[0].storageCache = scache 90 return &images[0], nil 91 } 92 if len(images) == 0 { 93 return nil, cloudprovider.ErrNotFound 94 } 95 return nil, cloudprovider.ErrDuplicateId 96 } 97 98 func (scache *SStoragecache) GetPath() string { 99 return "" 100 } 101 102 func (scache *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { 103 return scache.uploadImage(ctx, userCred, image, callback) 104 } 105 106 func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { 107 s := auth.GetAdminSession(ctx, options.Options.Region) 108 109 meta, reader, size, err := modules.Images.Download(s, image.ImageId, string(qemuimg.QCOW2), false) 110 if err != nil { 111 return "", err 112 } 113 log.Infof("meta data %s", meta) 114 115 // size, _ := meta.Int("size") 116 img, err := self.region.CreateImage(self.ZoneId, image.ImageName, string(qemuimg.QCOW2), image.OsType, "", reader, size, callback) 117 if err != nil { 118 return "", err 119 } 120 img.storageCache = self 121 err = cloudprovider.WaitStatus(img, api.CACHED_IMAGE_STATUS_ACTIVE, time.Second*5, time.Minute*20) //windows镜像转换比较慢,等待时间稍微设长一些 122 if err != nil { 123 log.Errorf("waitting for image %s(%s) status ready timeout", img.Name, img.UUID) 124 } 125 if callback != nil { 126 callback(100) 127 } 128 return img.UUID, err 129 } 130 131 func (scache *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageDesc string) (cloudprovider.ICloudImage, error) { 132 return nil, cloudprovider.ErrNotImplemented 133 } 134 135 func (scache *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { 136 return nil, cloudprovider.ErrNotImplemented 137 }