image.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 ctyun
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/imagetools"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SImage struct {
  26. multicloud.SImageBase
  27. CtyunTags
  28. storageCache *SStoragecache
  29. // normalized image info
  30. imgInfo *imagetools.ImageInfo
  31. Architecture string
  32. AzName string
  33. BootMode string
  34. ContainerFormat string
  35. CreatedTime int
  36. Description string
  37. DestinationUser string
  38. DiskFormat string
  39. DiskId string
  40. DiskSize int
  41. ImageClass string
  42. ImageId string
  43. ImageName string
  44. ImageType string
  45. MaximumRAM string
  46. MinimumRAM string
  47. OsDistro string
  48. OsType string
  49. OsVersion string
  50. ProjectId string
  51. SharedListLength string
  52. Size int64
  53. SourceServerId string
  54. SourceUser string
  55. Status string
  56. Tags string
  57. UpdatedTime string
  58. Visibility string
  59. }
  60. func (self *SImage) GetCreatedAt() time.Time {
  61. return time.Time{}
  62. }
  63. func (self *SImage) GetId() string {
  64. return self.ImageId
  65. }
  66. func (self *SImage) GetName() string {
  67. return self.ImageName
  68. }
  69. func (self *SImage) GetGlobalId() string {
  70. return self.GetId()
  71. }
  72. func (self *SImage) GetStatus() string {
  73. switch self.Status {
  74. case "accepted", "active":
  75. return api.CACHED_IMAGE_STATUS_ACTIVE
  76. case "deactivated", "deactivating", "deleted", "deleting", "pending_delete", "rejected":
  77. return api.CACHED_IMAGE_STATUS_DELETING
  78. case "error", "killed":
  79. return api.CACHED_IMAGE_STATUS_UNKNOWN
  80. case "importing", "queued", "reactivating", "saving", "syncing", "uploading", "waiting":
  81. return api.CACHED_IMAGE_STATUS_CACHING
  82. }
  83. return api.CACHED_IMAGE_STATUS_UNKNOWN
  84. }
  85. func (self *SImage) Refresh() error {
  86. image, err := self.storageCache.region.GetImage(self.GetId())
  87. if err != nil {
  88. return err
  89. }
  90. return jsonutils.Update(self, image)
  91. }
  92. func (self *SImage) IsEmulated() bool {
  93. return false
  94. }
  95. func (self *SImage) Delete(ctx context.Context) error {
  96. return cloudprovider.ErrNotSupported
  97. }
  98. func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  99. return self.storageCache
  100. }
  101. func (self *SImage) GetSizeByte() int64 {
  102. return self.Size
  103. }
  104. func (self *SImage) GetImageType() cloudprovider.TImageType {
  105. switch self.Visibility {
  106. case "private":
  107. return cloudprovider.ImageTypeCustomized
  108. case "public":
  109. return cloudprovider.ImageTypeSystem
  110. case "shared":
  111. return cloudprovider.ImageTypeShared
  112. case "safe", "community":
  113. return cloudprovider.ImageTypeMarket
  114. default:
  115. return cloudprovider.ImageTypeCustomized
  116. }
  117. }
  118. func (self *SImage) GetImageStatus() string {
  119. switch self.Status {
  120. case "accepted", "active":
  121. return cloudprovider.IMAGE_STATUS_ACTIVE
  122. case "deactivated", "deactivating", "deleted", "deleting", "pending_delete", "rejected":
  123. return cloudprovider.IMAGE_STATUS_DELETED
  124. case "error", "killed":
  125. return cloudprovider.IMAGE_STATUS_KILLED
  126. case "importing", "queued", "reactivating", "saving", "syncing", "uploading", "waiting":
  127. return cloudprovider.IMAGE_STATUS_QUEUED
  128. }
  129. return cloudprovider.IMAGE_STATUS_KILLED
  130. }
  131. func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
  132. if self.imgInfo == nil {
  133. imgInfo := imagetools.NormalizeImageInfo(self.ImageName, self.Architecture, self.OsType, self.OsDistro, self.OsVersion)
  134. self.imgInfo = &imgInfo
  135. }
  136. return self.imgInfo
  137. }
  138. func (self *SImage) GetFullOsName() string {
  139. return self.getNormalizedImageInfo().GetFullOsName()
  140. }
  141. func (self *SImage) GetOsType() cloudprovider.TOsType {
  142. return cloudprovider.TOsType(self.getNormalizedImageInfo().OsType)
  143. }
  144. func (self *SImage) GetOsDist() string {
  145. return self.getNormalizedImageInfo().OsDistro
  146. }
  147. func (self *SImage) GetOsVersion() string {
  148. return self.getNormalizedImageInfo().OsVersion
  149. }
  150. func (self *SImage) GetOsLang() string {
  151. return self.getNormalizedImageInfo().OsLang
  152. }
  153. func (self *SImage) GetOsArch() string {
  154. return self.getNormalizedImageInfo().OsArch
  155. }
  156. func (self *SImage) GetBios() cloudprovider.TBiosType {
  157. return cloudprovider.ToBiosType(self.getNormalizedImageInfo().OsBios)
  158. }
  159. func (self *SImage) GetMinOsDiskSizeGb() int {
  160. return self.DiskSize
  161. }
  162. func (self *SImage) GetMinRamSizeMb() int {
  163. return 0
  164. }
  165. func (self *SImage) GetImageFormat() string {
  166. return self.DiskFormat
  167. }
  168. func (self *SImage) GetCreateTime() time.Time {
  169. return time.Time{}
  170. }
  171. func (self *SRegion) GetImages(imageType string) ([]SImage, error) {
  172. pageNo := 1
  173. params := map[string]interface{}{
  174. "pageNo": pageNo,
  175. "pageSize": 50,
  176. }
  177. switch imageType {
  178. case "private":
  179. params["visibility"] = 0
  180. case "public":
  181. params["visibility"] = 1
  182. case "shared":
  183. params["visibility"] = 2
  184. case "safe":
  185. params["visibility"] = 3
  186. case "community":
  187. params["visibility"] = 4
  188. }
  189. ret := []SImage{}
  190. for {
  191. resp, err := self.list(SERVICE_IMAGE, "/v4/image/list", params)
  192. if err != nil {
  193. return nil, err
  194. }
  195. part := struct {
  196. ReturnObj struct {
  197. Images []SImage
  198. }
  199. TotalCount int
  200. }{}
  201. err = resp.Unmarshal(&part)
  202. if err != nil {
  203. return nil, errors.Wrapf(err, "Unmarshal")
  204. }
  205. ret = append(ret, part.ReturnObj.Images...)
  206. if len(part.ReturnObj.Images) == 0 || len(ret) >= part.TotalCount {
  207. break
  208. }
  209. pageNo++
  210. params["pageNo"] = pageNo
  211. }
  212. return ret, nil
  213. }
  214. func (self *SRegion) GetImage(imageId string) (*SImage, error) {
  215. params := map[string]interface{}{
  216. "imageID": imageId,
  217. }
  218. resp, err := self.list(SERVICE_IMAGE, "/v4/image/detail", params)
  219. if err != nil {
  220. return nil, err
  221. }
  222. ret := []SImage{}
  223. err = resp.Unmarshal(&ret, "returnObj", "images")
  224. if err != nil {
  225. return nil, err
  226. }
  227. for i := range ret {
  228. if ret[i].ImageId == imageId {
  229. return &ret[i], nil
  230. }
  231. }
  232. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", imageId)
  233. }