yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    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 SStorage struct {
    31  	multicloud.SStorageBase
    32  	zone        *SZone
    33  	storageType string
    34  	available   bool
    35  }
    36  
    37  func (self *SStorage) GetId() string {
    38  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), strings.ToLower(self.storageType))
    39  }
    40  
    41  func (self *SStorage) GetName() string {
    42  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), strings.ToLower(self.storageType))
    43  }
    44  
    45  func (self *SStorage) GetGlobalId() string {
    46  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), strings.ToLower(self.storageType))
    47  }
    48  
    49  func (self *SStorage) IsEmulated() bool {
    50  	return true
    51  }
    52  
    53  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    54  	return self.zone
    55  }
    56  
    57  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    58  	disks := make([]SDisk, 0)
    59  	for {
    60  		parts, total, err := self.zone.region.GetDisks("", self.zone.GetId(), self.storageType, nil, len(disks), 50)
    61  		if err != nil {
    62  			log.Errorf("GetDisks fail %s", err)
    63  			return nil, err
    64  		}
    65  		disks = append(disks, parts...)
    66  		if len(disks) >= total {
    67  			break
    68  		}
    69  	}
    70  	idisks := make([]cloudprovider.ICloudDisk, len(disks))
    71  	for i := 0; i < len(disks); i++ {
    72  		disks[i].storage = self
    73  		idisks[i] = &disks[i]
    74  	}
    75  	return idisks, nil
    76  }
    77  
    78  func (self *SStorage) GetStorageType() string {
    79  	return strings.ToLower(self.storageType)
    80  }
    81  
    82  func (self *SStorage) GetMediumType() string {
    83  	if strings.HasSuffix(self.storageType, "_BASIC") {
    84  		return api.DISK_TYPE_ROTATE
    85  	}
    86  	return api.DISK_TYPE_SSD
    87  }
    88  
    89  func (self *SStorage) GetCapacityMB() int64 {
    90  	return 0 // unlimited
    91  }
    92  
    93  func (self *SStorage) GetCapacityUsedMB() int64 {
    94  	return 0
    95  }
    96  
    97  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
    98  	conf := jsonutils.NewDict()
    99  	return conf
   100  }
   101  
   102  func (self *SStorage) GetStatus() string {
   103  	if !self.available {
   104  		return api.STORAGE_OFFLINE
   105  	}
   106  	return api.STORAGE_ONLINE
   107  }
   108  
   109  func (self *SStorage) Refresh() error {
   110  	// do nothing
   111  	return nil
   112  }
   113  
   114  func (self *SStorage) GetEnabled() bool {
   115  	return self.available == true
   116  }
   117  
   118  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   119  	return self.zone.region.getStoragecache()
   120  }
   121  
   122  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   123  	diskId, err := self.zone.region.CreateDisk(self.zone.Zone, self.storageType, conf.Name, conf.SizeGb, conf.Desc, conf.ProjectId)
   124  	if err != nil {
   125  		log.Errorf("createDisk fail %s", err)
   126  		return nil, err
   127  	}
   128  	//腾讯刚创建完成的磁盘,需要稍微等待才能查询
   129  	for i := 0; i < 3; i++ {
   130  		disk, err := self.zone.region.GetDisk(diskId)
   131  		if err == nil {
   132  			disk.storage = self
   133  			return disk, nil
   134  		}
   135  		time.Sleep(time.Second * 3)
   136  	}
   137  	log.Errorf("getDisk fail %s id %s", err, diskId)
   138  	return nil, cloudprovider.ErrNotFound
   139  }
   140  
   141  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   142  	disk, err := self.zone.region.GetDisk(idStr)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	disk.storage = self
   147  	return disk, nil
   148  }
   149  
   150  func (self *SStorage) GetMountPoint() string {
   151  	return ""
   152  }
   153  
   154  func (self *SStorage) IsSysDiskStore() bool {
   155  	return strings.ToLower(self.storageType) != api.STORAGE_CLOUD_HSSD
   156  }