yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/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 proxmox
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SStorage struct {
    30  	multicloud.SStorageBase
    31  	ProxmoxTags
    32  
    33  	zone *SZone
    34  
    35  	Id   string
    36  	Node string
    37  
    38  	Total        int64   `json:"total"`
    39  	Storage      string  `json:"storage"`
    40  	Shared       int     `json:"shared"`
    41  	Used         int64   `json:"used"`
    42  	Content      string  `json:"content"`
    43  	Active       int     `json:"active"`
    44  	UsedFraction float64 `json:"used_fraction"`
    45  	Avail        int64   `json:"avail"`
    46  	Enabled      int     `json:"enabled"`
    47  	Type         string  `json:"type"`
    48  }
    49  
    50  func (self *SStorage) GetName() string {
    51  	return self.Storage
    52  }
    53  
    54  func (self *SStorage) GetId() string {
    55  	return self.Id
    56  }
    57  
    58  func (self *SStorage) GetGlobalId() string {
    59  	return self.GetId()
    60  }
    61  
    62  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    63  	disks, err := self.zone.region.GetDisks(self.Id)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	ret := []cloudprovider.ICloudDisk{}
    68  	for i := range disks {
    69  		disks[i].region = self.zone.region
    70  		ret = append(ret, &disks[i])
    71  	}
    72  	return ret, nil
    73  }
    74  
    75  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
    76  	return nil, cloudprovider.ErrNotFound
    77  }
    78  
    79  func (self *SStorage) GetCapacityMB() int64 {
    80  	return int64(self.Total / 1024 / 1024)
    81  }
    82  
    83  func (self *SStorage) GetCapacityUsedMB() int64 {
    84  	return int64(self.Used / 1024 / 1024)
    85  }
    86  
    87  func (self *SStorage) GetEnabled() bool {
    88  	return true
    89  }
    90  
    91  func (self *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
    92  	disk, err := self.zone.region.GetDisk(id)
    93  	if err != nil {
    94  		return nil, cloudprovider.ErrNotFound
    95  	}
    96  
    97  	return disk, nil
    98  }
    99  
   100  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   101  	cache := &SStoragecache{zone: self.zone}
   102  	return cache
   103  }
   104  
   105  func (self *SStorage) GetMediumType() string {
   106  	return api.DISK_TYPE_SSD
   107  }
   108  
   109  func (self *SStorage) GetMountPoint() string {
   110  	return ""
   111  }
   112  
   113  func (self *SStorage) GetStatus() string {
   114  	return api.STORAGE_ONLINE
   115  }
   116  
   117  func (self *SStorage) Refresh() error {
   118  	ret, err := self.zone.region.GetStorage(self.GetGlobalId())
   119  	if err != nil {
   120  		return err
   121  	}
   122  	return jsonutils.Update(self, ret)
   123  }
   124  
   125  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
   126  	return self.zone
   127  }
   128  
   129  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   130  	return jsonutils.NewDict()
   131  }
   132  
   133  func (self *SStorage) GetStorageType() string {
   134  	return strings.ToLower(self.Type)
   135  }
   136  
   137  func (self *SStorage) IsSysDiskStore() bool {
   138  	return true
   139  }
   140  
   141  func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   142  	storage, err := self.GetStorage(id)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	zone, err := self.GetZone()
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	storage.zone = zone
   151  	return storage, nil
   152  }
   153  
   154  func (self *SRegion) GetStorages() ([]SStorage, error) {
   155  	storages := []SStorage{}
   156  	resources, err := self.GetClusterStoragesResources()
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	for _, res := range resources {
   162  		storage := &SStorage{}
   163  		status := fmt.Sprintf("%s/status", res.Path)
   164  		err := self.get(status, url.Values{}, storage)
   165  		if err != nil {
   166  			return nil, err
   167  		}
   168  
   169  		storage.Id = res.Id
   170  		storage.Node = res.Node
   171  		// not support storageCache, so chanege the type name.
   172  		if storage.Type == "rbd" {
   173  			storage.Type = "cephrbd"
   174  		}
   175  		if storage.Storage == "" {
   176  			storage.Storage = res.Name
   177  		}
   178  
   179  		storages = append(storages, *storage)
   180  	}
   181  
   182  	return storages, nil
   183  }
   184  
   185  func (self *SRegion) GetStoragesByHost(hostId string) ([]SStorage, error) {
   186  	storages := []SStorage{}
   187  	nodeName := ""
   188  	splited := strings.Split(hostId, "/")
   189  	nodeName = splited[1]
   190  
   191  	res := fmt.Sprintf("/nodes/%s/storage", nodeName)
   192  	err := self.get(res, url.Values{}, &storages)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  
   197  	for i := range storages {
   198  		id := fmt.Sprintf("storage/%s/%s", nodeName, storages[i].Storage)
   199  		storages[i].Node = nodeName
   200  		storages[i].Id = id
   201  		if storages[i].Type == "rbd" {
   202  			storages[i].Type = "cephrbd"
   203  		}
   204  	}
   205  
   206  	return storages, nil
   207  }
   208  
   209  func (self *SRegion) GetStorage(id string) (*SStorage, error) {
   210  	ret := &SStorage{}
   211  
   212  	//"id": "storage/nodeNAME/strogeNAME",
   213  	splited := strings.Split(id, "/")
   214  	nodeName := ""
   215  	storageName := ""
   216  
   217  	if len(splited) == 3 {
   218  		nodeName, storageName = splited[1], splited[2]
   219  	}
   220  
   221  	status := fmt.Sprintf("/nodes/%s/storage/%s/status", nodeName, storageName)
   222  	err := self.get(status, url.Values{}, ret)
   223  	ret.Id = id
   224  	ret.Node = nodeName
   225  	if ret.Type == "rbd" {
   226  		ret.Type = "cephrbd"
   227  	}
   228  
   229  	return ret, err
   230  }