image.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 proxmox
  15. import (
  16. "context"
  17. "fmt"
  18. "net/url"
  19. "yunion.io/x/pkg/util/imagetools"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SImage struct {
  25. multicloud.SImageBase
  26. ProxmoxTags
  27. cache *SStoragecache
  28. imageInfo *imagetools.ImageInfo
  29. Volid string
  30. Size int64
  31. Ctime int64
  32. Content string
  33. Format string
  34. }
  35. func (self *SImage) GetMinRamSizeMb() int {
  36. return 0
  37. }
  38. func (self *SImage) GetId() string {
  39. return self.Volid
  40. }
  41. func (self *SImage) GetName() string {
  42. return self.Volid
  43. }
  44. func (self *SImage) Delete(ctx context.Context) error {
  45. return cloudprovider.ErrNotImplemented
  46. }
  47. func (self *SImage) GetGlobalId() string {
  48. return self.GetId()
  49. }
  50. func (self *SImage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  51. return self.cache
  52. }
  53. func (self *SImage) GetStatus() string {
  54. return api.CACHED_IMAGE_STATUS_ACTIVE
  55. }
  56. func (self *SImage) GetImageStatus() string {
  57. return cloudprovider.IMAGE_STATUS_ACTIVE
  58. }
  59. func (self *SImage) GetImageType() cloudprovider.TImageType {
  60. return cloudprovider.ImageTypeSystem
  61. }
  62. func (self *SImage) GetSizeByte() int64 {
  63. return self.Size
  64. }
  65. func (img *SImage) getNormalizedImageInfo() *imagetools.ImageInfo {
  66. if img.imageInfo == nil {
  67. imgInfo := imagetools.NormalizeImageInfo(img.Volid, "", "", "", "")
  68. img.imageInfo = &imgInfo
  69. }
  70. return img.imageInfo
  71. }
  72. func (img *SImage) GetOsType() cloudprovider.TOsType {
  73. return cloudprovider.TOsType(img.getNormalizedImageInfo().OsType)
  74. }
  75. func (img *SImage) GetOsDist() string {
  76. return img.getNormalizedImageInfo().OsDistro
  77. }
  78. func (img *SImage) GetOsVersion() string {
  79. return img.getNormalizedImageInfo().OsVersion
  80. }
  81. func (img *SImage) GetOsArch() string {
  82. return img.getNormalizedImageInfo().OsArch
  83. }
  84. func (img *SImage) GetOsLang() string {
  85. return img.getNormalizedImageInfo().OsLang
  86. }
  87. func (img *SImage) GetFullOsName() string {
  88. return ""
  89. }
  90. func (img *SImage) GetBios() cloudprovider.TBiosType {
  91. return cloudprovider.BIOS
  92. }
  93. func (self *SImage) GetMinOsDiskSizeGb() int {
  94. if self.GetOsType() == "windows" {
  95. return 40
  96. }
  97. return 30
  98. }
  99. func (self *SImage) GetImageFormat() string {
  100. return self.Format
  101. }
  102. func (self *SProxmoxClient) GetImages(node, storageName string) ([]SImage, error) {
  103. images := []SImage{}
  104. params := url.Values{}
  105. params.Set("content", "iso")
  106. path := fmt.Sprintf("/nodes/%s/storage/%s/content", node, storageName)
  107. err := self.get(path, params, &images)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return images, nil
  112. }
  113. func (self *SRegion) GetImages(node, storageName string) ([]SImage, error) {
  114. return self.client.GetImages(node, storageName)
  115. }