yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/storage.go (about)

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