yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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  //
    16  // Licensed under the Apache License, Version 2.0 (the "License");
    17  // you may not use this file except in compliance with the License.
    18  // You may obtain a copy of the License at
    19  //
    20  //     http://www.apache.org/licenses/LICENSE-2.0
    21  //
    22  // Unless required by applicable law or agreed to in writing, software
    23  // distributed under the License is distributed on an "AS IS" BASIS,
    24  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    25  // See the License for the specific language governing permissions and
    26  // limitations under the License.
    27  
    28  package aliyun
    29  
    30  import (
    31  	"fmt"
    32  	"strings"
    33  
    34  	"yunion.io/x/jsonutils"
    35  	"yunion.io/x/log"
    36  	"yunion.io/x/pkg/errors"
    37  	"yunion.io/x/pkg/utils"
    38  
    39  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    40  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    41  	"yunion.io/x/cloudmux/pkg/multicloud"
    42  )
    43  
    44  type SStorage struct {
    45  	multicloud.SStorageBase
    46  	AliyunTags
    47  	zone        *SZone
    48  	storageType string
    49  }
    50  
    51  func (self *SStorage) GetId() string {
    52  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
    53  }
    54  
    55  func (self *SStorage) GetName() string {
    56  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
    57  }
    58  
    59  func (self *SStorage) GetGlobalId() string {
    60  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), self.storageType)
    61  }
    62  
    63  func (self *SStorage) IsEmulated() bool {
    64  	return true
    65  }
    66  
    67  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    68  	return self.zone
    69  }
    70  
    71  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    72  	disks := make([]SDisk, 0)
    73  	offset := 0
    74  	storageType := self.storageType
    75  	if self.storageType == api.STORAGE_CLOUD_ESSD_PL2 || self.storageType == api.STORAGE_CLOUD_ESSD_PL3 {
    76  		storageType = api.STORAGE_CLOUD_ESSD
    77  	}
    78  	for {
    79  		parts, total, err := self.zone.region.GetDisks("", self.zone.GetId(), storageType, nil, offset, 50)
    80  		if err != nil {
    81  			return nil, errors.Wrapf(err, "GetDisks")
    82  		}
    83  		performanceLevel := ""
    84  		switch self.storageType {
    85  		case api.STORAGE_CLOUD_ESSD:
    86  			performanceLevel = "PL1"
    87  		case api.STORAGE_CLOUD_ESSD_PL2:
    88  			performanceLevel = "PL2"
    89  		case api.STORAGE_CLOUD_ESSD_PL3:
    90  			performanceLevel = "PL3"
    91  		}
    92  		for _, disk := range parts {
    93  			if disk.PerformanceLevel == performanceLevel {
    94  				disks = append(disks, disk)
    95  			}
    96  		}
    97  
    98  		offset += len(parts)
    99  
   100  		if offset >= total {
   101  			break
   102  		}
   103  	}
   104  	idisks := make([]cloudprovider.ICloudDisk, len(disks))
   105  	for i := 0; i < len(disks); i += 1 {
   106  		disks[i].storage = self
   107  		idisks[i] = &disks[i]
   108  	}
   109  	return idisks, nil
   110  }
   111  
   112  func (self *SStorage) GetStorageType() string {
   113  	//return models.STORAGE_PUBLIC_CLOUD
   114  	return self.storageType
   115  }
   116  
   117  func (self *SStorage) GetMediumType() string {
   118  	if strings.Contains(self.storageType, "_ssd") {
   119  		return api.DISK_TYPE_SSD
   120  	} else {
   121  		return api.DISK_TYPE_ROTATE
   122  	}
   123  }
   124  
   125  func (self *SStorage) GetCapacityMB() int64 {
   126  	return 0 // unlimited
   127  }
   128  
   129  func (self *SStorage) GetCapacityUsedMB() int64 {
   130  	return 0
   131  }
   132  
   133  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   134  	conf := jsonutils.NewDict()
   135  	return conf
   136  }
   137  
   138  func (self *SStorage) GetStatus() string {
   139  	return api.STORAGE_ONLINE
   140  }
   141  
   142  func (self *SStorage) Refresh() error {
   143  	// do nothing
   144  	return nil
   145  }
   146  
   147  func (self *SStorage) GetEnabled() bool {
   148  	if utils.IsInStringArray(self.storageType, LOCAL_STORAGES) {
   149  		return false
   150  	}
   151  	return true
   152  }
   153  
   154  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   155  	return self.zone.region.getStoragecache()
   156  }
   157  
   158  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   159  	diskId, err := self.zone.region.CreateDisk(self.zone.ZoneId, self.storageType, conf.Name, conf.SizeGb, conf.Desc, conf.ProjectId)
   160  	if err != nil {
   161  		log.Errorf("createDisk fail %s", err)
   162  		return nil, err
   163  	}
   164  	disk, err := self.zone.region.getDisk(diskId)
   165  	if err != nil {
   166  		log.Errorf("getDisk fail %s", err)
   167  		return nil, err
   168  	}
   169  	disk.storage = self
   170  	return disk, nil
   171  }
   172  
   173  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   174  	disk, err := self.zone.region.getDisk(idStr)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  	disk.storage = self
   179  	return disk, nil
   180  }
   181  
   182  func (self *SStorage) GetMountPoint() string {
   183  	return ""
   184  }
   185  
   186  func (self *SStorage) IsSysDiskStore() bool {
   187  	if utils.IsInStringArray(self.storageType, self.zone.getSysDiskCategories()) {
   188  		return true
   189  	}
   190  	return false
   191  }