image.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 jdcloud
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "time"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/apis"
  21. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/client"
  22. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/models"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/util/imagetools"
  26. api "yunion.io/x/cloudmux/pkg/apis/compute"
  27. "yunion.io/x/cloudmux/pkg/cloudprovider"
  28. "yunion.io/x/cloudmux/pkg/multicloud"
  29. )
  30. type SImage struct {
  31. multicloud.SImageBase
  32. JdcloudTags
  33. storageCache *SStoragecache
  34. imageInfo *imagetools.ImageInfo
  35. models.Image
  36. }
  37. func (i *SImage) GetCreatedAt() time.Time {
  38. return parseTime(i.CreateTime)
  39. }
  40. func (i *SImage) GetId() string {
  41. return i.ImageId
  42. }
  43. func (i *SImage) GetName() string {
  44. return i.Name
  45. }
  46. func (i *SImage) GetDescription() string {
  47. return i.Desc
  48. }
  49. func (i *SImage) GetGlobalId() string {
  50. return i.GetId()
  51. }
  52. func (i *SImage) GetStatus() string {
  53. switch i.Status {
  54. case "pending":
  55. return api.CACHED_IMAGE_STATUS_SAVING
  56. case "ready":
  57. return api.CACHED_IMAGE_STATUS_ACTIVE
  58. case "deleting":
  59. return api.CACHED_IMAGE_STATUS_DELETING
  60. case "error":
  61. return api.CACHED_IMAGE_STATUS_UNKNOWN
  62. default:
  63. return api.CACHED_IMAGE_STATUS_UNKNOWN
  64. }
  65. }
  66. func (i *SImage) Refresh() error {
  67. return nil
  68. }
  69. func (i *SImage) IsEmulated() bool {
  70. return false
  71. }
  72. func (i *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
  73. if i.imageInfo == nil {
  74. imgInfo := imagetools.NormalizeImageInfo(i.Name, i.Architecture, i.OsType, i.Platform, i.OsVersion)
  75. i.imageInfo = &imgInfo
  76. }
  77. return i.imageInfo
  78. }
  79. func (i *SImage) GetFullOsName() string {
  80. return i.Name
  81. }
  82. func (i *SImage) GetOsType() cloudprovider.TOsType {
  83. return cloudprovider.TOsType(i.getNormalizedImageInfo().OsType)
  84. }
  85. func (i *SImage) GetOsDist() string {
  86. return i.getNormalizedImageInfo().OsDistro
  87. }
  88. func (i *SImage) GetOsVersion() string {
  89. return i.getNormalizedImageInfo().OsVersion
  90. }
  91. func (i *SImage) GetOsLang() string {
  92. return i.getNormalizedImageInfo().OsLang
  93. }
  94. func (i *SImage) GetOsArch() string {
  95. return i.getNormalizedImageInfo().OsArch
  96. }
  97. func (i *SImage) GetBios() cloudprovider.TBiosType {
  98. return cloudprovider.ToBiosType(i.getNormalizedImageInfo().OsBios)
  99. }
  100. func (i *SImage) GetMinOsDiskSizeGb() int {
  101. return i.SystemDiskSizeGB
  102. }
  103. func (i *SImage) GetMinRamSizeMb() int {
  104. return 0
  105. }
  106. func (i *SImage) GetImageFormat() string {
  107. return ""
  108. }
  109. func (i *SImage) Delete(ctx context.Context) error {
  110. return cloudprovider.ErrNotImplemented
  111. }
  112. func (i *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  113. return nil
  114. }
  115. func (i *SImage) GetSizeByte() int64 {
  116. return int64(i.SizeMB) * 1024 * 1024
  117. }
  118. func (i *SImage) GetImageType() cloudprovider.TImageType {
  119. switch i.ImageSource {
  120. case "jcloud":
  121. return cloudprovider.ImageTypeSystem
  122. case "marketplace":
  123. return cloudprovider.ImageTypeMarket
  124. case "self":
  125. return cloudprovider.ImageTypeCustomized
  126. case "shared":
  127. return cloudprovider.ImageTypeShared
  128. default:
  129. return cloudprovider.TImageType("")
  130. }
  131. }
  132. func (i *SImage) GetImageStatus() string {
  133. switch i.Status {
  134. case "pending":
  135. return cloudprovider.IMAGE_STATUS_QUEUED
  136. default:
  137. return cloudprovider.IMAGE_STATUS_ACTIVE
  138. }
  139. }
  140. func (r *SRegion) GetImage(imageId string) (*SImage, error) {
  141. req := apis.NewDescribeImageRequest(r.ID, imageId)
  142. client := client.NewVmClient(r.getCredential())
  143. client.Logger = Logger{debug: r.client.debug}
  144. resp, err := client.DescribeImage(req)
  145. if err != nil {
  146. return nil, err
  147. }
  148. if resp.Error.Code >= 400 {
  149. err = fmt.Errorf("%s", resp.Error.Message)
  150. return nil, err
  151. }
  152. return &SImage{
  153. Image: resp.Result.Image,
  154. }, nil
  155. }
  156. func (r *SRegion) GetImages(imageIds []string, imageSource string, pageNumber, pageSize int) ([]SImage, int, error) {
  157. req := apis.NewDescribeImagesRequestWithAllParams(r.ID, &imageSource, nil, nil, nil, imageIds, nil, nil, nil, nil, &pageNumber, &pageSize)
  158. client := client.NewVmClient(r.getCredential())
  159. client.Logger = Logger{debug: r.client.debug}
  160. resp, err := client.DescribeImages(req)
  161. if err != nil {
  162. log.Errorf("err: %v", err)
  163. return nil, 0, err
  164. }
  165. if resp.Error.Code >= 400 {
  166. if strings.Contains(resp.Error.Message, "secret key is nul") || strings.Contains(resp.Error.Message, "sign result is not same") {
  167. return nil, 0, errors.Wrapf(cloudprovider.ErrInvalidAccessKey, "%s", resp.Error.Message)
  168. }
  169. err = fmt.Errorf("%s", resp.Error.Message)
  170. return nil, 0, err
  171. }
  172. images := make([]SImage, len(resp.Result.Images))
  173. for i := range resp.Result.Images {
  174. images[i] = SImage{
  175. Image: resp.Result.Images[i],
  176. }
  177. }
  178. return images, resp.Result.TotalCount, nil
  179. }