images.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 cloudprovider
  15. import (
  16. "fmt"
  17. "io"
  18. "time"
  19. )
  20. type TImageType string
  21. const (
  22. IMAGE_STATUS_ACTIVE = "active"
  23. IMAGE_STATUS_QUEUED = "queued"
  24. IMAGE_STATUS_SAVING = "saving"
  25. IMAGE_STATUS_KILLED = "killed"
  26. IMAGE_STATUS_DELETED = "deleted"
  27. ImageTypeSystem = TImageType("system")
  28. ImageTypeCustomized = TImageType("customized")
  29. ImageTypeShared = TImageType("shared")
  30. ImageTypeMarket = TImageType("market")
  31. )
  32. type SImage struct {
  33. Checksum string
  34. // ContainerFormat string
  35. CreatedAt time.Time
  36. Deleted bool
  37. DiskFormat string
  38. Id string
  39. IsPublic bool
  40. MinDiskMB int `json:"min_disk"`
  41. MinRamMB int `json:"min_ram"`
  42. Name string
  43. Owner string
  44. Properties map[string]string
  45. Protected bool
  46. SizeBytes int64 `json:"size"`
  47. Status string
  48. // UpdatedAt time.Time
  49. PublicScope string
  50. ExternalId string
  51. // SubImages record the subImages of the guest image.
  52. // For normal image, it's nil.
  53. SubImages []SSubImage
  54. // EncryptKeyId
  55. EncryptKeyId string
  56. }
  57. type SSubImage struct {
  58. Index int
  59. MinDiskMB int
  60. MinRamMb int
  61. SizeBytes int64
  62. }
  63. type SaveImageOptions struct {
  64. Name string
  65. Notes string
  66. }
  67. func CloudImage2Image(image ICloudImage) SImage {
  68. uefiSupport := false
  69. if image.GetBios() == UEFI {
  70. uefiSupport = true
  71. }
  72. return SImage{
  73. CreatedAt: image.GetCreatedAt(),
  74. Deleted: false,
  75. DiskFormat: image.GetImageFormat(),
  76. Id: image.GetId(),
  77. IsPublic: image.GetImageType() != ImageTypeCustomized,
  78. MinDiskMB: image.GetMinOsDiskSizeGb() * 1024,
  79. MinRamMB: image.GetMinRamSizeMb(),
  80. Name: image.GetName(),
  81. Properties: map[string]string{
  82. "os_full_name": image.GetFullOsName(),
  83. "os_type": string(image.GetOsType()),
  84. "os_distribution": image.GetOsDist(),
  85. "os_version": image.GetOsVersion(),
  86. "os_arch": image.GetOsArch(),
  87. "os_language": image.GetOsLang(),
  88. "uefi_support": fmt.Sprintf("%v", uefiSupport),
  89. },
  90. Protected: true,
  91. SizeBytes: image.GetSizeByte(),
  92. Status: image.GetImageStatus(),
  93. SubImages: image.GetSubImages(),
  94. }
  95. }
  96. type SImageCreateOption struct {
  97. ImageId string
  98. ExternalId string
  99. ImageName string
  100. Description string
  101. MinDiskMb int
  102. MinRamMb int
  103. Checksum string
  104. OsType string
  105. OsArch string
  106. OsDistribution string
  107. OsVersion string
  108. OsFullVersion string
  109. GetReader func(imageId, format string) (io.Reader, int64, error)
  110. // 镜像临时存储位置
  111. TmpPath string
  112. }
  113. type SImageExportOptions struct {
  114. BucketName string
  115. }
  116. type SImageExportInfo struct {
  117. DownloadUrl string
  118. Name string
  119. CompressFormat string
  120. }