storage.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SStorage struct {
  24. multicloud.SStorageBase
  25. region *SRegion
  26. api.StorageDetails
  27. }
  28. func (self *SStorage) GetId() string {
  29. return self.Id
  30. }
  31. func (self *SStorage) GetGlobalId() string {
  32. return self.Id
  33. }
  34. func (self *SStorage) GetName() string {
  35. return self.Name
  36. }
  37. func (self *SStorage) GetStatus() string {
  38. return self.Status
  39. }
  40. func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
  41. zone, err := self.region.GetZone(self.ZoneId)
  42. if err != nil {
  43. return nil
  44. }
  45. return zone
  46. }
  47. func (self *SStorage) GetMediumType() string {
  48. return self.MediumType
  49. }
  50. func (self *SStorage) GetCapacityMB() int64 {
  51. return self.Capacity
  52. }
  53. func (self *SStorage) GetStorageType() string {
  54. return self.StorageType
  55. }
  56. func (self *SStorage) GetCapacityUsedMB() int64 {
  57. return self.ActualCapacityUsed
  58. }
  59. func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
  60. return self.StorageConf
  61. }
  62. func (self *SStorage) GetEnabled() bool {
  63. return self.Enabled != nil && *self.Enabled
  64. }
  65. func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
  66. if len(self.StoragecacheId) == 0 {
  67. return nil
  68. }
  69. cache, _ := self.region.GetStoragecache(self.StoragecacheId)
  70. return cache
  71. }
  72. func (self *SStorage) GetMountPoint() string {
  73. return ""
  74. }
  75. func (self *SStorage) IsSysDiskStore() bool {
  76. return self.SStorage.IsSysDiskStore != nil && *self.SStorage.IsSysDiskStore
  77. }
  78. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  79. storages, err := self.region.GetStorages(self.Id, "")
  80. if err != nil {
  81. return nil, errors.Wrapf(err, "GetStorages")
  82. }
  83. ret := []cloudprovider.ICloudStorage{}
  84. for i := range storages {
  85. storages[i].region = self.region
  86. ret = append(ret, &storages[i])
  87. }
  88. return ret, nil
  89. }
  90. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  91. return self.region.GetIStorageById(id)
  92. }
  93. func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  94. storage, err := self.GetStorage(id)
  95. if err != nil {
  96. return nil, err
  97. }
  98. storage.region = self
  99. return storage, nil
  100. }
  101. func (self *SRegion) GetStorage(id string) (*SStorage, error) {
  102. storage := &SStorage{region: self}
  103. return storage, self.cli.get(&modules.Storages, id, nil, storage)
  104. }
  105. func (self *SRegion) GetStorages(zoneId, hostId string) ([]SStorage, error) {
  106. params := map[string]interface{}{}
  107. if len(zoneId) > 0 {
  108. params["zone_id"] = zoneId
  109. }
  110. if len(hostId) > 0 {
  111. params["host_id"] = hostId
  112. }
  113. storages := []SStorage{}
  114. return storages, self.list(&modules.Storages, params, &storages)
  115. }