yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/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 apsara
    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  	ApsaraTags
    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 false
    65  }
    66  
    67  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    68  	return self.zone
    69  }
    70  
    71  func (self *SStorage) getDisks() ([]SDisk, error) {
    72  	if len(self.zone.disks) > 0 {
    73  		return self.zone.disks, nil
    74  	}
    75  	self.zone.disks = []SDisk{}
    76  	for {
    77  		part, total, err := self.zone.region.GetDisks("", self.zone.GetId(), "", nil, len(self.zone.disks), 100)
    78  		if err != nil {
    79  			return nil, errors.Wrapf(err, "GetDisks")
    80  		}
    81  		self.zone.disks = append(self.zone.disks, part...)
    82  		if len(self.zone.disks) >= total {
    83  			break
    84  		}
    85  	}
    86  	return self.zone.disks, nil
    87  }
    88  
    89  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    90  	disks, err := self.getDisks()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	ret := []cloudprovider.ICloudDisk{}
    95  	for i := 0; i < len(disks); i += 1 {
    96  		if disks[i].Category == self.storageType {
    97  			disks[i].storage = self
    98  			ret = append(ret, &disks[i])
    99  		}
   100  	}
   101  	return ret, nil
   102  }
   103  
   104  func (self *SStorage) GetStorageType() string {
   105  	return self.storageType
   106  }
   107  
   108  func (self *SStorage) GetMediumType() string {
   109  	if strings.Contains(self.storageType, "_ssd") {
   110  		return api.DISK_TYPE_SSD
   111  	} else {
   112  		return api.DISK_TYPE_ROTATE
   113  	}
   114  }
   115  
   116  func (self *SStorage) GetCapacityMB() int64 {
   117  	return 0 // unlimited
   118  }
   119  
   120  func (self *SStorage) GetCapacityUsedMB() int64 {
   121  	return 0
   122  }
   123  
   124  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   125  	conf := jsonutils.NewDict()
   126  	return conf
   127  }
   128  
   129  func (self *SStorage) GetStatus() string {
   130  	return api.STORAGE_ONLINE
   131  }
   132  
   133  func (self *SStorage) Refresh() error {
   134  	// do nothing
   135  	return nil
   136  }
   137  
   138  func (self *SStorage) GetEnabled() bool {
   139  	return true
   140  }
   141  
   142  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   143  	return self.zone.region.getStoragecache()
   144  }
   145  
   146  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   147  	diskId, err := self.zone.region.CreateDisk(self.zone.ZoneId, self.storageType, conf.Name, conf.SizeGb, conf.Desc, conf.ProjectId)
   148  	if err != nil {
   149  		log.Errorf("createDisk fail %s", err)
   150  		return nil, err
   151  	}
   152  	disk, err := self.zone.region.getDisk(diskId)
   153  	if err != nil {
   154  		log.Errorf("getDisk fail %s", err)
   155  		return nil, err
   156  	}
   157  	disk.storage = self
   158  	return disk, nil
   159  }
   160  
   161  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   162  	if disk, err := self.zone.region.getDisk(idStr); err != nil {
   163  		return nil, err
   164  	} else {
   165  		disk.storage = self
   166  		return disk, nil
   167  	}
   168  }
   169  
   170  func (self *SStorage) GetMountPoint() string {
   171  	return ""
   172  }
   173  
   174  func (self *SStorage) IsSysDiskStore() bool {
   175  	if utils.IsInStringArray(self.storageType, self.zone.getSysDiskCategories()) {
   176  		return true
   177  	}
   178  	return false
   179  }