yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/image.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 bingocloud 16 17 import ( 18 "context" 19 "time" 20 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 "yunion.io/x/onecloud/pkg/util/imagetools" 27 "yunion.io/x/onecloud/pkg/util/rbacutils" 28 ) 29 30 type SImage struct { 31 multicloud.SResourceBase 32 BingoTags 33 cache *SStoragecache 34 35 imageInfo *imagetools.ImageInfo 36 37 Architecture string `json:"architecture"` 38 BlockDeviceMapping string `json:"blockDeviceMapping"` 39 Bootloader string `json:"bootloader"` 40 Clonemode string `json:"clonemode"` 41 ClusterId string `json:"clusterId"` 42 Contentmode string `json:"contentmode"` 43 DefaultStorageId string `json:"defaultStorageId"` 44 Description string `json:"description"` 45 DiskBus string `json:"diskBus"` 46 ExtendDisk string `json:"extendDisk"` 47 Features string `json:"features"` 48 Hypervisor string `json:"hypervisor"` 49 ImageId string `json:"imageId"` 50 ImageLocation string `json:"imageLocation"` 51 ImageOwnerId string `json:"imageOwnerId"` 52 ImagePath string `json:"imagePath"` 53 ImageSize int64 `json:"imageSize"` 54 ImageState string `json:"imageState"` 55 ImageType string `json:"imageType"` 56 IsBareMetal string `json:"isBareMetal"` 57 IsPublic bool `json:"isPublic"` 58 KernelId string `json:"kernelId"` 59 Name string `json:"name"` 60 OsId string `json:"osId"` 61 OSName string `json:"osName"` 62 Platform string `json:"platform"` 63 RamdiskId string `json:"ramdiskId"` 64 RootDeviceName string `json:"rootDeviceName"` 65 RootDeviceType string `json:"rootDeviceType"` 66 ScheduleTags string `json:"scheduleTags"` 67 Shared string `json:"shared"` 68 Sharemode string `json:"sharemode"` 69 StateReason string `json:"stateReason"` 70 StorageId string `json:"storageId"` 71 } 72 73 func (self *SImage) GetId() string { 74 return self.ImageId 75 } 76 77 func (self *SImage) GetGlobalId() string { 78 return self.GetId() 79 } 80 81 func (self *SImage) GetName() string { 82 return self.Name 83 } 84 85 func (self *SImage) Delete(ctx context.Context) error { 86 return cloudprovider.ErrNotImplemented 87 } 88 89 func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache { 90 return self.cache 91 } 92 93 func (self *SImage) GetSizeByte() int64 { 94 return self.ImageSize 95 } 96 97 func (self *SImage) GetImageType() cloudprovider.TImageType { 98 if self.IsPublic { 99 return cloudprovider.ImageTypeSystem 100 } 101 return cloudprovider.ImageTypeCustomized 102 } 103 104 func (self *SImage) GetImageStatus() string { 105 return "" 106 } 107 108 func (i *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { 109 if i.imageInfo == nil { 110 imgInfo := imagetools.NormalizeImageInfo(i.OSName, i.Architecture, i.Platform, "", "") 111 i.imageInfo = &imgInfo 112 } 113 return i.imageInfo 114 } 115 116 func (i *SImage) GetOsType() cloudprovider.TOsType { 117 return cloudprovider.TOsType(i.getNormalizedImageInfo().OsType) 118 } 119 120 func (i *SImage) GetFullOsName() string { 121 return i.OSName 122 } 123 124 func (i *SImage) GetOsArch() string { 125 return i.getNormalizedImageInfo().OsArch 126 } 127 128 func (i *SImage) GetOsDist() string { 129 return i.getNormalizedImageInfo().OsDistro 130 } 131 132 func (i *SImage) GetOsVersion() string { 133 return i.getNormalizedImageInfo().OsVersion 134 } 135 136 func (i *SImage) GetOsLang() string { 137 return i.getNormalizedImageInfo().OsLang 138 } 139 140 func (i *SImage) GetBios() cloudprovider.TBiosType { 141 return cloudprovider.ToBiosType(i.getNormalizedImageInfo().OsBios) 142 } 143 144 func (self *SImage) GetMinOsDiskSizeGb() int { 145 return 0 146 } 147 148 func (self *SImage) GetMinRamSizeMb() int { 149 return 0 150 } 151 152 func (self *SImage) GetImageFormat() string { 153 return "raw" 154 } 155 156 func (self *SImage) GetCreatedAt() time.Time { 157 return time.Time{} 158 } 159 160 func (self *SImage) UEFI() bool { 161 return self.Bootloader == "uefi" 162 } 163 164 func (self *SImage) GetPublicScope() rbacutils.TRbacScope { 165 if self.Shared == "true" { 166 return rbacutils.ScopeSystem 167 } 168 return rbacutils.ScopeDomain 169 } 170 171 func (self *SImage) GetSubImages() []cloudprovider.SSubImage { 172 return []cloudprovider.SSubImage{} 173 } 174 175 func (self *SImage) GetProjectId() string { 176 return "" 177 } 178 179 func (self *SImage) GetStatus() string { 180 switch self.ImageState { 181 case "available": 182 return api.CACHED_IMAGE_STATUS_ACTIVE 183 default: 184 return self.ImageState 185 } 186 } 187 188 func (self *SRegion) GetImages(id, nextToken string) ([]SImage, string, error) { 189 params := map[string]string{} 190 if len(id) > 0 { 191 params["imageId"] = id 192 } 193 if len(nextToken) > 0 { 194 params["nextToken"] = nextToken 195 } 196 resp, err := self.invoke("DescribeImages", params) 197 if err != nil { 198 return nil, "", err 199 } 200 ret := struct { 201 NextToken string 202 ImagesSet []SImage 203 }{} 204 resp.Unmarshal(&ret) 205 return ret.ImagesSet, ret.NextToken, nil 206 } 207 208 func (self *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) { 209 part, nextToken, err := self.region.GetImages("", "") 210 if err != nil { 211 return nil, err 212 } 213 images := []SImage{} 214 images = append(images, part...) 215 for len(nextToken) > 0 { 216 part, nextToken, err = self.region.GetImages("", nextToken) 217 if err != nil { 218 return nil, err 219 } 220 images = append(images, part...) 221 } 222 ret := []cloudprovider.ICloudImage{} 223 for i := range images { 224 if images[i].StorageId == self.storageId { 225 images[i].cache = self 226 ret = append(ret, &images[i]) 227 } 228 } 229 return ret, nil 230 } 231 232 func (self *SStoragecache) GetIImageById(id string) (cloudprovider.ICloudImage, error) { 233 images, _, err := self.region.GetImages(id, "") 234 if err != nil { 235 return nil, err 236 } 237 for i := range images { 238 if images[i].GetGlobalId() == id && images[i].StorageId == self.storageId { 239 images[i].cache = self 240 return &images[i], nil 241 } 242 } 243 244 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 245 }