yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/storage_local.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 zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SLocalStorage struct {
    31  	multicloud.SStorageBase
    32  	ZStackTags
    33  	region *SRegion
    34  
    35  	primaryStorageID          string
    36  	HostUUID                  string `json:"hostUuid"`
    37  	TotalCapacity             int64  `json:"totalCapacity"`
    38  	AvailableCapacity         int64  `json:"availableCapacity"`
    39  	TotalPhysicalCapacity     int64  `json:"totalPhysicalCapacity"`
    40  	AvailablePhysicalCapacity int64  `json:"availablePhysicalCapacity"`
    41  }
    42  
    43  func (region *SRegion) GetLocalStorage(storageId string, hostId string) (*SLocalStorage, error) {
    44  	storages, err := region.GetLocalStorages(storageId, hostId)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if len(storages) == 1 {
    49  		if storages[0].HostUUID == hostId {
    50  			return &storages[0], nil
    51  		}
    52  		return nil, cloudprovider.ErrNotFound
    53  	}
    54  	if len(storages) == 0 || len(storageId) == 0 {
    55  		return nil, cloudprovider.ErrNotFound
    56  	}
    57  	return nil, cloudprovider.ErrDuplicateId
    58  }
    59  
    60  func (region *SRegion) GetLocalStorages(storageId string, hostId string) ([]SLocalStorage, error) {
    61  	localStorage := []SLocalStorage{}
    62  	params := url.Values{}
    63  	if len(hostId) > 0 {
    64  		params.Set("hostUuid", hostId)
    65  	}
    66  	err := region.client.listAll(fmt.Sprintf("primary-storage/local-storage/%s/capacities", storageId), params, &localStorage)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	for i := 0; i < len(localStorage); i++ {
    71  		localStorage[i].region = region
    72  		localStorage[i].primaryStorageID = storageId
    73  	}
    74  	return localStorage, nil
    75  }
    76  
    77  func (region *SRegion) getILocalStorages(storageId, hostId string) ([]cloudprovider.ICloudStorage, error) {
    78  	storages, err := region.GetLocalStorages(storageId, hostId)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	istorage := []cloudprovider.ICloudStorage{}
    83  	for i := 0; i < len(storages); i++ {
    84  		istorage = append(istorage, &storages[i])
    85  	}
    86  	return istorage, nil
    87  }
    88  
    89  func (storage *SLocalStorage) GetId() string {
    90  	return storage.primaryStorageID
    91  }
    92  
    93  func (storage *SLocalStorage) GetName() string {
    94  	primaryStorage, err := storage.region.GetStorage(storage.primaryStorageID)
    95  	if err != nil {
    96  		return "Unknown"
    97  	}
    98  	host, err := storage.region.GetHost(storage.HostUUID)
    99  	if err != nil {
   100  		return "Unknown"
   101  	}
   102  	return fmt.Sprintf("%s/%s", primaryStorage.Name, host.Name)
   103  }
   104  
   105  func (storage *SLocalStorage) GetGlobalId() string {
   106  	return fmt.Sprintf("%s/%s", storage.primaryStorageID, storage.HostUUID)
   107  }
   108  
   109  func (storage *SLocalStorage) IsEmulated() bool {
   110  	return false
   111  }
   112  
   113  func (storage *SLocalStorage) GetIZone() cloudprovider.ICloudZone {
   114  	host, err := storage.region.GetHost(storage.HostUUID)
   115  	if err != nil {
   116  		log.Errorf("failed get host info %s error: %v", storage.HostUUID, err)
   117  		return nil
   118  	}
   119  	zone, err := storage.region.GetZone(host.ZoneUUID)
   120  	if err != nil {
   121  		log.Errorf("failed get zone info %s error: %v", host.ZoneUUID, err)
   122  	}
   123  	return zone
   124  }
   125  
   126  func (storage *SLocalStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
   127  	tags, err := storage.region.GetResourceSysTags("", "VolumeVO", "", "localStorage::hostUuid::"+storage.HostUUID)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	diskIds := []string{}
   132  	for i := 0; i < len(tags); i++ {
   133  		diskIds = append(diskIds, tags[i].ResourceUUID)
   134  	}
   135  	idisks := []cloudprovider.ICloudDisk{}
   136  	if len(diskIds) == 0 {
   137  		return idisks, nil
   138  	}
   139  	disks, err := storage.region.GetDisks(storage.primaryStorageID, diskIds, "")
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  	for i := 0; i < len(disks); i++ {
   144  		disks[i].localStorage = storage
   145  		disks[i].region = storage.region
   146  		idisks = append(idisks, &disks[i])
   147  	}
   148  	return idisks, nil
   149  }
   150  
   151  func (storage *SLocalStorage) GetStorageType() string {
   152  	return strings.ToLower(string(StorageTypeLocal))
   153  }
   154  
   155  func (storage *SLocalStorage) GetMediumType() string {
   156  	return api.DISK_TYPE_ROTATE
   157  }
   158  
   159  func (storage *SLocalStorage) GetCapacityMB() int64 {
   160  	return storage.TotalCapacity / 1024 / 1024
   161  }
   162  
   163  func (storage *SLocalStorage) GetCapacityUsedMB() int64 {
   164  	return 0
   165  }
   166  
   167  func (storage *SLocalStorage) GetStorageConf() jsonutils.JSONObject {
   168  	conf := jsonutils.NewDict()
   169  	return conf
   170  }
   171  
   172  func (storage *SLocalStorage) GetStatus() string {
   173  	primaryStorage, err := storage.region.GetStorage(storage.primaryStorageID)
   174  	if err != nil {
   175  		return api.STORAGE_OFFLINE
   176  	}
   177  	return primaryStorage.GetStatus()
   178  }
   179  
   180  func (storage *SLocalStorage) Refresh() error {
   181  	// do nothing
   182  	return nil
   183  }
   184  
   185  func (storage *SLocalStorage) GetEnabled() bool {
   186  	return true
   187  }
   188  
   189  func (storage *SLocalStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   190  	cache := &SStoragecache{region: storage.region}
   191  	host, _ := storage.region.GetHost(storage.HostUUID)
   192  	if host != nil {
   193  		cache.ZoneId = host.ZoneUUID
   194  	} else {
   195  		_storage, _ := storage.region.GetStorage(storage.primaryStorageID)
   196  		if _storage != nil {
   197  			cache.ZoneId = _storage.ZoneUUID
   198  		}
   199  	}
   200  	return cache
   201  }
   202  
   203  func (storage *SLocalStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   204  	disk, err := storage.region.CreateDisk(conf.Name, storage.primaryStorageID, storage.HostUUID, "", conf.SizeGb, conf.Desc)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	disk.localStorage = storage
   209  	return disk, nil
   210  }
   211  
   212  func (storage *SLocalStorage) GetIDiskById(diskId string) (cloudprovider.ICloudDisk, error) {
   213  	disk, err := storage.region.GetDisk(diskId)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  	if disk.PrimaryStorageUUID != storage.primaryStorageID {
   218  		return nil, cloudprovider.ErrNotFound
   219  	}
   220  	disk.localStorage = storage
   221  	disk.region = storage.region
   222  	return disk, nil
   223  }
   224  
   225  func (storage *SLocalStorage) GetMountPoint() string {
   226  	return ""
   227  }
   228  
   229  func (storage *SLocalStorage) IsSysDiskStore() bool {
   230  	return true
   231  }