yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 "yunion.io/x/onecloud/pkg/util/imagetools" 28 ) 29 30 type SImage struct { 31 multicloud.SImageBase 32 UcloudTags 33 storageCache *SStoragecache 34 35 // normalized image info 36 imgInfo *imagetools.ImageInfo 37 38 Zone string `json:"Zone"` 39 ImageDescription string `json:"ImageDescription"` 40 OSName string `json:"OsName"` 41 ImageID string `json:"ImageId"` 42 State string `json:"State"` 43 ImageName string `json:"ImageName"` 44 OSType string `json:"OsType"` 45 CreateTime int64 `json:"CreateTime"` 46 ImageType string `json:"ImageType"` 47 ImageSizeGB int64 `json:"ImageSize"` 48 } 49 50 func (self *SImage) GetMinRamSizeMb() int { 51 return 0 52 } 53 54 func (self *SImage) GetId() string { 55 return self.ImageID 56 } 57 58 func (self *SImage) GetName() string { 59 if len(self.ImageName) == 0 { 60 return self.GetId() 61 } 62 63 return self.ImageName 64 } 65 66 func (self *SImage) GetGlobalId() string { 67 return self.GetId() 68 } 69 70 // 镜像状态, 可用:Available,制作中:Making, 不可用:Unavailable 71 func (self *SImage) GetStatus() string { 72 switch self.State { 73 case "Available": 74 return api.CACHED_IMAGE_STATUS_ACTIVE 75 case "Making": 76 return api.CACHED_IMAGE_STATUS_CACHING 77 case "Unavailable": 78 return api.CACHED_IMAGE_STATUS_CACHE_FAILED 79 default: 80 return api.CACHED_IMAGE_STATUS_CACHE_FAILED 81 } 82 } 83 84 func (self *SImage) Refresh() error { 85 new, err := self.storageCache.region.GetImage(self.GetId()) 86 if err != nil { 87 return err 88 } 89 return jsonutils.Update(self, new) 90 } 91 92 func (self *SImage) IsEmulated() bool { 93 return false 94 } 95 96 func (self *SImage) Delete(ctx context.Context) error { 97 return self.storageCache.region.DeleteImage(self.GetId()) 98 } 99 100 func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache { 101 return self.storageCache 102 } 103 104 func (self *SImage) GetSizeByte() int64 { 105 return self.ImageSizeGB * 1024 * 1024 * 1024 106 } 107 108 // 镜像类型。标准镜像:Base,镜像市场:Business, 自定义镜像:Custom,默认返回所有类型 109 func (self *SImage) GetImageType() cloudprovider.TImageType { 110 switch self.ImageType { 111 case "Base": 112 return cloudprovider.ImageTypeSystem 113 case "Custom": 114 return cloudprovider.ImageTypeCustomized 115 case "Business": 116 return cloudprovider.ImageTypeShared 117 default: 118 return cloudprovider.ImageTypeCustomized 119 } 120 } 121 122 func (self *SImage) GetImageStatus() string { 123 switch self.State { 124 case "Available": 125 return cloudprovider.IMAGE_STATUS_ACTIVE 126 case "Making": 127 return cloudprovider.IMAGE_STATUS_QUEUED 128 case "Unavailable": 129 return cloudprovider.IMAGE_STATUS_KILLED 130 default: 131 return cloudprovider.IMAGE_STATUS_KILLED 132 } 133 } 134 135 func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { 136 if self.imgInfo == nil { 137 imgInfo := imagetools.NormalizeImageInfo(self.ImageName, "", "", "", "") 138 self.imgInfo = &imgInfo 139 } 140 141 return self.imgInfo 142 } 143 144 func (self *SImage) GetOsType() cloudprovider.TOsType { 145 return cloudprovider.TOsType(self.getNormalizedImageInfo().OsType) 146 } 147 148 func (self *SImage) GetOsDist() string { 149 return self.getNormalizedImageInfo().OsDistro 150 } 151 152 func (self *SImage) GetOsVersion() string { 153 return self.getNormalizedImageInfo().OsVersion 154 } 155 156 func (self *SImage) GetOsArch() string { 157 return self.getNormalizedImageInfo().OsArch 158 } 159 160 func (img *SImage) GetOsLang() string { 161 return img.getNormalizedImageInfo().OsLang 162 } 163 164 func (img *SImage) GetBios() cloudprovider.TBiosType { 165 return cloudprovider.ToBiosType(img.getNormalizedImageInfo().OsBios) 166 } 167 168 func (img *SImage) GetFullOsName() string { 169 return img.ImageName 170 } 171 172 func (self *SImage) GetMinOsDiskSizeGb() int { 173 return int(self.ImageSizeGB) 174 } 175 176 func (self *SImage) GetImageFormat() string { 177 return "" 178 } 179 180 func (self *SImage) GetCreatedAt() time.Time { 181 return time.Unix(self.CreateTime, 0) 182 } 183 184 // https://docs.ucloud.cn/api/uhost-api/describe_image 185 func (self *SRegion) GetImage(imageId string) (SImage, error) { 186 params := NewUcloudParams() 187 params.Set("ImageId", imageId) 188 189 images := make([]SImage, 0) 190 err := self.DoListAll("DescribeImage", params, &images) 191 if err != nil { 192 return SImage{}, err 193 } 194 195 if len(images) == 1 { 196 return images[0], nil 197 } else if len(images) == 0 { 198 return SImage{}, cloudprovider.ErrNotFound 199 } else { 200 return SImage{}, fmt.Errorf("GetImage %s %d found.", imageId, len(images)) 201 } 202 } 203 204 // https://docs.ucloud.cn/api/uhost-api/describe_image 205 // ImageType 标准镜像:Base,镜像市场:Business, 自定义镜像:Custom,默认返回所有类型 206 func (self *SRegion) GetImages(imageType string, imageId string) ([]SImage, error) { 207 params := NewUcloudParams() 208 209 if len(imageId) > 0 { 210 params.Set("ImageId", imageId) 211 } 212 213 if len(imageType) > 0 { 214 params.Set("ImageType", imageType) 215 } 216 217 images := make([]SImage, 0) 218 err := self.DoListAll("DescribeImage", params, &images) 219 return images, err 220 } 221 222 // https://docs.ucloud.cn/api/uhost-api/terminate_custom_image 223 func (self *SRegion) DeleteImage(imageId string) error { 224 params := NewUcloudParams() 225 params.Set("ImageId", imageId) 226 227 return self.DoAction("TerminateCustomImage", params, nil) 228 }