storagecache.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 cloudpods
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  25. )
  26. type SStoragecache struct {
  27. multicloud.SResourceBase
  28. CloudpodsTags
  29. region *SRegion
  30. api.StoragecacheDetails
  31. }
  32. func (self *SStoragecache) GetName() string {
  33. return self.Name
  34. }
  35. func (self *SStoragecache) GetId() string {
  36. return self.Id
  37. }
  38. func (self *SStoragecache) GetGlobalId() string {
  39. return self.Id
  40. }
  41. func (self *SStoragecache) GetStatus() string {
  42. return apis.STATUS_AVAILABLE
  43. }
  44. func (self *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) {
  45. return nil, cloudprovider.ErrNotImplemented
  46. }
  47. func (self *SStoragecache) GetIImageById(id string) (cloudprovider.ICloudImage, error) {
  48. image, err := self.region.GetImage(id)
  49. if err != nil {
  50. return nil, err
  51. }
  52. image.cache = self
  53. return image, nil
  54. }
  55. func (self *SStoragecache) GetPath() string {
  56. return self.Path
  57. }
  58. func (self *SStoragecache) CreateIImage(snapshotId, name, osType, desc string) (cloudprovider.ICloudImage, error) {
  59. return nil, cloudprovider.ErrNotImplemented
  60. }
  61. func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) {
  62. return nil, cloudprovider.ErrNotImplemented
  63. }
  64. func (self *SStoragecache) UploadImage(ctx context.Context, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) {
  65. id, err := self.region.UploadImage(ctx, opts, callback)
  66. if err != nil {
  67. return "", errors.Wrapf(err, "UploadImage")
  68. }
  69. image, err := self.GetIImageById(id)
  70. if err != nil {
  71. return "", errors.Wrapf(err, "GetIImageById(%s)", id)
  72. }
  73. err = cloudprovider.WaitStatus(image, cloudprovider.IMAGE_STATUS_ACTIVE, time.Second*5, time.Minute*10)
  74. if err != nil {
  75. return "", errors.Wrapf(err, "WaitStatus")
  76. }
  77. if callback != nil {
  78. callback(100)
  79. }
  80. return id, nil
  81. }
  82. func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
  83. caches, err := self.GetStoragecaches()
  84. if err != nil {
  85. return nil, err
  86. }
  87. ret := []cloudprovider.ICloudStoragecache{}
  88. for i := range caches {
  89. caches[i].region = self
  90. ret = append(ret, &caches[i])
  91. }
  92. return ret, nil
  93. }
  94. func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
  95. cache, err := self.GetStoragecache(id)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return cache, nil
  100. }
  101. func (self *SRegion) GetStoragecaches() ([]SStoragecache, error) {
  102. caches := []SStoragecache{}
  103. return caches, self.list(&compute.Storagecaches, nil, &caches)
  104. }
  105. func (self *SRegion) GetStoragecache(id string) (*SStoragecache, error) {
  106. cache := &SStoragecache{region: self}
  107. return cache, self.cli.get(&compute.Storagecaches, id, nil, cache)
  108. }