yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type Capabilitie struct {
    29  	Name  string
    30  	Value string
    31  }
    32  
    33  const (
    34  	STORAGE_STD_LRS = "Standard_LRS"
    35  	STORAGE_PRE_LRS = "Premium_LRS"
    36  	STORAGE_STD_SSD = "StandardSSD_LRS"
    37  )
    38  
    39  var STORAGETYPES = []string{STORAGE_STD_LRS, STORAGE_PRE_LRS, STORAGE_STD_SSD}
    40  
    41  type SStorage struct {
    42  	multicloud.SStorageBase
    43  	AzureTags
    44  	zone *SZone
    45  
    46  	storageType  string
    47  	ResourceType string
    48  	Tier         string
    49  	Kind         string
    50  	Locations    []string
    51  	Capabilities []Capabilitie
    52  }
    53  
    54  func (self *SStorage) GetId() string {
    55  	return fmt.Sprintf("%s/%s", self.zone.GetGlobalId(), strings.ToLower(self.storageType))
    56  }
    57  
    58  func (self *SStorage) GetName() string {
    59  	return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, strings.ToLower(self.storageType))
    60  }
    61  
    62  func (self *SStorage) GetGlobalId() string {
    63  	return fmt.Sprintf("%s/%s/%s", self.zone.region.GetGlobalId(), self.zone.region.client.subscriptionId, strings.ToLower(self.storageType))
    64  }
    65  
    66  func (self *SStorage) IsEmulated() bool {
    67  	return true
    68  }
    69  
    70  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    71  	return self.zone
    72  }
    73  
    74  func (self *SStorage) GetEnabled() bool {
    75  	return true
    76  }
    77  
    78  func (self *SStorage) GetCapacityMB() int64 {
    79  	return 0 // unlimited
    80  }
    81  
    82  func (self *SStorage) GetCapacityUsedMB() int64 {
    83  	return 0
    84  }
    85  
    86  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
    87  	disk, err := self.zone.region.CreateDisk(self.storageType, conf.Name, int32(conf.SizeGb), "", "", conf.ProjectId)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	disk.storage = self
    92  	return disk, nil
    93  }
    94  
    95  func (self *SStorage) GetIDiskById(diskId string) (cloudprovider.ICloudDisk, error) {
    96  	if disk, err := self.zone.region.GetDisk(diskId); err != nil {
    97  		return nil, err
    98  	} else {
    99  		disk.storage = self
   100  		return disk, nil
   101  	}
   102  }
   103  
   104  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
   105  	disks, err := self.zone.region.GetDisks()
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	idisks := make([]cloudprovider.ICloudDisk, 0)
   110  	for i := 0; i < len(disks); i++ {
   111  		storageType := strings.ToLower(string(disks[i].Sku.Name))
   112  		if storageType == strings.ToLower(self.storageType) {
   113  			disks[i].storage = self
   114  			idisks = append(idisks, &disks[i])
   115  		}
   116  	}
   117  	return idisks, nil
   118  }
   119  
   120  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
   121  	return self.zone.region.getStoragecache()
   122  }
   123  
   124  func (self *SStorage) GetMediumType() string {
   125  	if strings.HasPrefix(self.storageType, "premium") {
   126  		return api.DISK_TYPE_SSD
   127  	}
   128  	return api.DISK_TYPE_ROTATE
   129  }
   130  
   131  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   132  	conf := jsonutils.NewDict()
   133  	return conf
   134  }
   135  
   136  func (self *SStorage) GetStatus() string {
   137  	return api.STORAGE_ONLINE
   138  }
   139  
   140  func (self *SStorage) GetStorageType() string {
   141  	return strings.ToLower(self.storageType)
   142  }
   143  
   144  func (self *SStorage) Refresh() error {
   145  	// do nothing
   146  	return nil
   147  }
   148  
   149  func (self *SStorage) GetMountPoint() string {
   150  	return ""
   151  }
   152  
   153  func (self *SStorage) IsSysDiskStore() bool {
   154  	return true
   155  }