yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/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 nutanix 16 17 import ( 18 "context" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/log" 22 "yunion.io/x/pkg/errors" 23 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/onecloud/pkg/esxi/options" 26 "yunion.io/x/onecloud/pkg/mcclient" 27 "yunion.io/x/onecloud/pkg/mcclient/auth" 28 modules "yunion.io/x/onecloud/pkg/mcclient/modules/image" 29 "yunion.io/x/cloudmux/pkg/multicloud" 30 "yunion.io/x/onecloud/pkg/util/qemuimg" 31 ) 32 33 type SStoragecache struct { 34 multicloud.SResourceBase 35 multicloud.STagBase 36 37 storage *SStorage 38 region *SRegion 39 } 40 41 func (self *SStoragecache) GetName() string { 42 return self.storage.GetName() 43 } 44 45 func (self *SStoragecache) GetId() string { 46 return self.storage.GetId() 47 } 48 49 func (self *SStoragecache) GetGlobalId() string { 50 return self.storage.GetGlobalId() 51 } 52 53 func (self *SStoragecache) GetStatus() string { 54 return "available" 55 } 56 57 func (self *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) { 58 images, err := self.region.GetImages() 59 if err != nil { 60 return nil, errors.Wrapf(err, "GetImages") 61 } 62 ret := []cloudprovider.ICloudImage{} 63 for i := range images { 64 if images[i].StorageContainerUUID != self.storage.GetGlobalId() { 65 continue 66 } 67 images[i].cache = self 68 ret = append(ret, &images[i]) 69 } 70 return ret, nil 71 } 72 73 func (self *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) { 74 return nil, cloudprovider.ErrNotSupported 75 } 76 77 func (self *SStoragecache) GetIImageById(id string) (cloudprovider.ICloudImage, error) { 78 image, err := self.region.GetImage(id) 79 if err != nil { 80 return nil, err 81 } 82 image.cache = self 83 return image, nil 84 } 85 86 func (self *SStoragecache) GetPath() string { 87 return "" 88 } 89 90 func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc string) (cloudprovider.ICloudImage, error) { 91 return nil, cloudprovider.ErrNotImplemented 92 } 93 94 func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { 95 return nil, cloudprovider.ErrNotImplemented 96 } 97 98 func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { 99 s := auth.GetAdminSession(ctx, options.Options.Region) 100 101 meta, reader, size, err := modules.Images.Download(s, opts.ImageId, string(qemuimg.QCOW2), false) 102 if err != nil { 103 return "", err 104 } 105 log.Infof("meta data %s", meta) 106 image, err := self.region.CreateImage(self.storage.StorageContainerUUID, opts, size, reader, callback) 107 if err != nil { 108 return "", err 109 } 110 if callback != nil { 111 callback(100.0) 112 } 113 image.cache = self 114 return image.GetGlobalId(), nil 115 } 116 117 func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) { 118 storages, err := self.GetStorages() 119 if err != nil { 120 return nil, err 121 } 122 ret := []cloudprovider.ICloudStoragecache{} 123 for i := range storages { 124 cache := &SStoragecache{storage: &storages[i], region: self} 125 ret = append(ret, cache) 126 } 127 return ret, nil 128 } 129 130 func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) { 131 storage, err := self.GetStorage(id) 132 if err != nil { 133 return nil, errors.Wrapf(err, "GetStorage") 134 } 135 return &SStoragecache{region: self, storage: storage}, nil 136 }