image.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 baidu
  15. import (
  16. "context"
  17. "fmt"
  18. "net/url"
  19. "strings"
  20. "time"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. "yunion.io/x/jsonutils"
  25. "yunion.io/x/pkg/util/imagetools"
  26. )
  27. type SImage struct {
  28. multicloud.SImageBase
  29. SBaiduTag
  30. cache *SStoragecache
  31. imgInfo *imagetools.ImageInfo
  32. Id string
  33. CreateTime time.Time
  34. Name string
  35. Type string
  36. OsType string
  37. OsVersion string
  38. OsLang string
  39. OsName string
  40. OsBuild string
  41. OsArch string
  42. Status string
  43. Desc string
  44. MinDiskGb int64
  45. }
  46. func (self *SImage) GetMinRamSizeMb() int {
  47. return 0
  48. }
  49. func (self *SImage) GetId() string {
  50. return self.Id
  51. }
  52. func (self *SImage) GetName() string {
  53. return self.Name
  54. }
  55. func (self *SImage) Delete(ctx context.Context) error {
  56. return self.cache.region.DeleteImage(self.Id)
  57. }
  58. func (self *SImage) GetGlobalId() string {
  59. return self.Id
  60. }
  61. func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  62. return self.cache
  63. }
  64. func (self *SImage) GetStatus() string {
  65. switch self.Status {
  66. case "Creating":
  67. return api.CACHED_IMAGE_STATUS_SAVING
  68. case "Available":
  69. return api.CACHED_IMAGE_STATUS_ACTIVE
  70. case "CreatedFailed", "NotAvailable", "Error":
  71. return api.CACHED_IMAGE_STATUS_CACHE_FAILED
  72. default:
  73. return strings.ToLower(self.Status)
  74. }
  75. }
  76. func (self *SImage) GetImageStatus() string {
  77. switch self.Status {
  78. case "Creating":
  79. return cloudprovider.IMAGE_STATUS_QUEUED
  80. case "Available":
  81. return cloudprovider.IMAGE_STATUS_ACTIVE
  82. case "CreatedFailed", "NotAvailable", "Error":
  83. return cloudprovider.IMAGE_STATUS_DELETED
  84. default:
  85. return cloudprovider.IMAGE_STATUS_KILLED
  86. }
  87. }
  88. func (self *SImage) Refresh() error {
  89. image, err := self.cache.region.GetImage(self.Id)
  90. if err != nil {
  91. return err
  92. }
  93. return jsonutils.Update(self, image)
  94. }
  95. func (self *SImage) GetImageType() cloudprovider.TImageType {
  96. switch self.Type {
  97. case "System":
  98. return cloudprovider.ImageTypeSystem
  99. case "Custom":
  100. return cloudprovider.ImageTypeCustomized
  101. case "Integration":
  102. return cloudprovider.ImageTypeMarket
  103. default:
  104. return cloudprovider.ImageTypeCustomized
  105. }
  106. }
  107. func (self *SImage) GetSizeByte() int64 {
  108. return int64(self.GetMinOsDiskSizeGb()) * 1024 * 1024 * 1024
  109. }
  110. func (self *SImage) GetOsType() cloudprovider.TOsType {
  111. return cloudprovider.TOsType(self.getNormalizedImageInfo().OsType)
  112. }
  113. func (self *SImage) GetOsDist() string {
  114. return self.getNormalizedImageInfo().OsDistro
  115. }
  116. func getOsArch(osArch string) string {
  117. for _, key := range []string{"x86_64", "i386", "amd64", "aarch64"} {
  118. if strings.Contains(osArch, key) {
  119. return key
  120. }
  121. }
  122. return osArch
  123. }
  124. func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
  125. if self.imgInfo == nil {
  126. imgInfo := imagetools.NormalizeImageInfo(self.OsName, getOsArch(self.OsArch), self.OsType, self.OsName, self.OsVersion)
  127. self.imgInfo = &imgInfo
  128. }
  129. return self.imgInfo
  130. }
  131. func (self *SImage) GetFullOsName() string {
  132. return self.Name
  133. }
  134. func (self *SImage) GetOsVersion() string {
  135. return self.getNormalizedImageInfo().OsVersion
  136. }
  137. func (self *SImage) GetOsLang() string {
  138. return self.getNormalizedImageInfo().OsLang
  139. }
  140. func (self *SImage) GetOsArch() string {
  141. return self.getNormalizedImageInfo().OsArch
  142. }
  143. func (self *SImage) GetBios() cloudprovider.TBiosType {
  144. return cloudprovider.ToBiosType(self.getNormalizedImageInfo().OsBios)
  145. }
  146. func (self *SImage) GetMinOsDiskSizeGb() int {
  147. if self.MinDiskGb > 0 {
  148. return int(self.MinDiskGb)
  149. }
  150. image, err := self.cache.region.GetImage(self.Id)
  151. if err != nil {
  152. if strings.EqualFold(self.OsType, "windows") {
  153. return 40
  154. }
  155. return 20
  156. }
  157. return int(image.MinDiskGb)
  158. }
  159. func (self *SImage) GetImageFormat() string {
  160. return "raw"
  161. }
  162. func (self *SImage) GetCreatedAt() time.Time {
  163. return self.CreateTime
  164. }
  165. func (region *SRegion) GetImages(imageType string) ([]SImage, error) {
  166. params := url.Values{}
  167. if len(imageType) > 0 {
  168. params.Set("imageType", imageType)
  169. }
  170. ret := []SImage{}
  171. for {
  172. resp, err := region.bccList("v2/image", params)
  173. if err != nil {
  174. return nil, err
  175. }
  176. part := struct {
  177. NextMarker string
  178. Images []SImage
  179. }{}
  180. err = resp.Unmarshal(&part)
  181. ret = append(ret, part.Images...)
  182. if len(part.NextMarker) == 0 {
  183. break
  184. }
  185. params.Set("marker", part.NextMarker)
  186. }
  187. return ret, nil
  188. }
  189. func (region *SRegion) GetImage(id string) (*SImage, error) {
  190. resp, err := region.bccList(fmt.Sprintf("v2/image/%s", id), nil)
  191. if err != nil {
  192. return nil, err
  193. }
  194. ret := &SImage{}
  195. err = resp.Unmarshal(ret, "image")
  196. if err != nil {
  197. return nil, err
  198. }
  199. return ret, nil
  200. }
  201. func (region *SRegion) DeleteImage(id string) error {
  202. _, err := region.bccDelete(fmt.Sprintf("v2/image/%s", id), nil)
  203. return err
  204. }