image.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. package ksyun
  15. import (
  16. "context"
  17. "time"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/util/imagetools"
  23. )
  24. /*
  25. {
  26. "ImageId": "IMG-8990a317-b0ff-4319-bf41-6dfc1732455c",
  27. "ContainerFormat": "ovf",
  28. "Type": "CommonImage",
  29. "Name": "ubuntu-16.04-gpu-20180102203641",
  30. "ImageState": "active",
  31. "CreationDate": "2018-02-27T13:48:32Z",
  32. "Platform": "ubuntu-16.04",
  33. "IsPublic": true,
  34. "IsNpe": true,
  35. "UserCategory": "common",
  36. "SysDisk": 20,
  37. "Progress": "100",
  38. "ImageSource": "system",
  39. "CloudInitSupport": false,
  40. "Ipv6Support": false,
  41. "IsModifyType": false,
  42. "FastBoot": false,
  43. "IsCloudMarket": false,
  44. "RealImageId": "ab836124-d008-404e-8e78-f3160e3b8df3",
  45. "OnlineExpansion": true,
  46. "BootMode": "BIOS(Legacy)",
  47. "ImageOpenstackDefinedPagePerVq": "false"
  48. }
  49. */
  50. type SImage struct {
  51. multicloud.SImageBase
  52. SKsyunTags
  53. storageCache *SStoragecache
  54. // normalized image info
  55. imgInfo *imagetools.ImageInfo
  56. ImageId string
  57. ContainerFormat string
  58. Type string
  59. Name string
  60. ImageState string
  61. CreationDate time.Time
  62. Platform string
  63. IsPublic string
  64. IsNpe string
  65. UserCategory string
  66. SysDisk int
  67. Progress string
  68. ImageSource string
  69. CloudInitSupport string
  70. Ipv6Support string
  71. IsModifyType string
  72. FastBoot string
  73. IsCloudMarket string
  74. RealImageId string
  75. OnlineExpansion string
  76. BootMode string
  77. ImageOpenstackDefinedPagePerVq string
  78. Architecture string
  79. }
  80. func (image *SImage) GetMinRamSizeMb() int {
  81. return 0
  82. }
  83. func (image *SImage) GetId() string {
  84. return image.ImageId
  85. }
  86. func (image *SImage) GetName() string {
  87. return image.Name
  88. }
  89. func (image *SImage) Delete(ctx context.Context) error {
  90. return image.storageCache.region.DeleteImage(image.ImageId)
  91. }
  92. func (image *SImage) GetGlobalId() string {
  93. return image.ImageId
  94. }
  95. func (image *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  96. return image.storageCache
  97. }
  98. func (image *SImage) GetStatus() string {
  99. switch image.ImageState {
  100. case "creating":
  101. return api.CACHED_IMAGE_STATUS_SAVING
  102. case "active":
  103. return api.CACHED_IMAGE_STATUS_ACTIVE
  104. default:
  105. return api.CACHED_IMAGE_STATUS_CACHE_FAILED
  106. }
  107. }
  108. func (image *SImage) GetImageStatus() string {
  109. switch image.ImageState {
  110. case "creating":
  111. return cloudprovider.IMAGE_STATUS_QUEUED
  112. case "active":
  113. return cloudprovider.IMAGE_STATUS_ACTIVE
  114. default:
  115. return cloudprovider.IMAGE_STATUS_KILLED
  116. }
  117. }
  118. func (image *SImage) Refresh() error {
  119. ret, err := image.storageCache.region.GetImage(image.ImageId)
  120. if err != nil {
  121. return err
  122. }
  123. return jsonutils.Update(image, ret)
  124. }
  125. func (image *SImage) GetImageType() cloudprovider.TImageType {
  126. switch image.Type {
  127. case "CommonImage":
  128. return cloudprovider.ImageTypeSystem
  129. case "CustomImage":
  130. return cloudprovider.ImageTypeCustomized
  131. case "MarketImage":
  132. return cloudprovider.ImageTypeMarket
  133. default:
  134. return cloudprovider.ImageTypeCustomized
  135. }
  136. }
  137. func (image *SImage) GetSizeByte() int64 {
  138. return int64(image.SysDisk) * 1024 * 1024 * 1024
  139. }
  140. func (self *SImage) GetOsType() cloudprovider.TOsType {
  141. return cloudprovider.TOsType(self.getNormalizedImageInfo().OsType)
  142. }
  143. func (self *SImage) GetOsDist() string {
  144. return self.getNormalizedImageInfo().OsDistro
  145. }
  146. func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
  147. if self.imgInfo == nil {
  148. imgInfo := imagetools.NormalizeImageInfo(self.Platform, self.Architecture, "", "", "")
  149. self.imgInfo = &imgInfo
  150. }
  151. return self.imgInfo
  152. }
  153. func (self *SImage) GetFullOsName() string {
  154. return self.Name
  155. }
  156. func (self *SImage) GetOsVersion() string {
  157. return self.getNormalizedImageInfo().OsVersion
  158. }
  159. func (self *SImage) GetOsLang() string {
  160. return self.getNormalizedImageInfo().OsLang
  161. }
  162. func (self *SImage) GetOsArch() string {
  163. return self.getNormalizedImageInfo().OsArch
  164. }
  165. func (self *SImage) GetBios() cloudprovider.TBiosType {
  166. return cloudprovider.ToBiosType(self.getNormalizedImageInfo().OsBios)
  167. }
  168. func (self *SImage) GetMinOsDiskSizeGb() int {
  169. return self.SysDisk
  170. }
  171. func (self *SImage) GetImageFormat() string {
  172. return self.ContainerFormat
  173. }
  174. func (self *SImage) GetCreatedAt() time.Time {
  175. return self.CreationDate
  176. }
  177. func (region *SRegion) GetImage(imageId string) (*SImage, error) {
  178. images, err := region.GetImages(imageId, "")
  179. if err != nil {
  180. return nil, err
  181. }
  182. for i := range images {
  183. storageCache := region.getStoragecache()
  184. images[i].storageCache = storageCache
  185. if images[i].ImageId == imageId {
  186. return &images[i], nil
  187. }
  188. }
  189. return nil, cloudprovider.ErrNotFound
  190. }
  191. func (region *SRegion) GetImages(id string, imageType string) ([]SImage, error) {
  192. params := map[string]interface{}{}
  193. if len(id) > 0 {
  194. params["ImageId"] = id
  195. }
  196. if len(imageType) > 0 {
  197. params["ImageType"] = imageType
  198. }
  199. resp, err := region.ecsRequest("DescribeImages", params)
  200. if err != nil {
  201. return nil, err
  202. }
  203. ret := struct {
  204. ImagesSet []SImage
  205. NextToken string
  206. }{}
  207. err = resp.Unmarshal(&ret)
  208. if err != nil {
  209. return nil, err
  210. }
  211. return ret.ImagesSet, nil
  212. }
  213. func (region *SRegion) DeleteImage(imageId string) error {
  214. params := map[string]interface{}{
  215. "ImageId": imageId,
  216. }
  217. _, err := region.ecsRequest("DeleteImage", params)
  218. return err
  219. }