yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/data_disk.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  	"context"
    19  	"strings"
    20  
    21  	"yunion.io/x/pkg/errors"
    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 SDataDisk struct {
    29  	multicloud.SDisk
    30  	AzureTags
    31  	region *SRegion
    32  
    33  	Lun            int32
    34  	Name           string                 `json:"name,omitempty"`
    35  	DiskName       string                 `json:"diskName,omitempty"`
    36  	Vhd            *VirtualHardDisk       `json:"vhd,omitempty"`
    37  	Caching        string                 `json:"caching,omitempty"`
    38  	DiskSizeGB     TAzureInt32            `json:"diskSizeGB,omitempty"`
    39  	IoType         string                 `json:"ioType,omitempty"`
    40  	CreateOption   string                 `json:"createOption,omitempty"`
    41  	ManagedDisk    *ManagedDiskParameters `json:"managedDisk,omitempty"`
    42  	VhdUri         string                 `json:"vhdUri,omitempty"`
    43  	StorageAccount *SubResource           `json:"storageAccount,omitempty"`
    44  }
    45  
    46  func (self *SDataDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
    47  	if self.ManagedDisk != nil {
    48  		snapshot, err := self.region.CreateSnapshot(self.ManagedDisk.ID, name, desc)
    49  		if err != nil {
    50  			return nil, errors.Wrapf(err, "CreateSnapshot")
    51  		}
    52  		return snapshot, nil
    53  	}
    54  	return nil, cloudprovider.ErrNotSupported
    55  }
    56  
    57  func (self *SDataDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
    58  	return []cloudprovider.ICloudSnapshot{}, nil
    59  }
    60  
    61  func (self *SDataDisk) Delete(ctx context.Context) error {
    62  	if self.ManagedDisk != nil {
    63  		return self.region.del(self.ManagedDisk.ID)
    64  	}
    65  	return cloudprovider.ErrNotSupported
    66  }
    67  
    68  func (self *SDataDisk) GetStatus() string {
    69  	return api.DISK_READY
    70  }
    71  
    72  func (self *SDataDisk) GetId() string {
    73  	if self.ManagedDisk != nil {
    74  		return strings.ToLower(self.ManagedDisk.ID)
    75  	}
    76  	return self.Vhd.Uri
    77  }
    78  
    79  func (self *SDataDisk) GetGlobalId() string {
    80  	return self.GetId()
    81  }
    82  
    83  func (self *SDataDisk) GetName() string {
    84  	return self.Name
    85  }
    86  
    87  func (self *SDataDisk) Resize(ctx context.Context, sizeMb int64) error {
    88  	if self.ManagedDisk != nil {
    89  		return self.region.ResizeDisk(self.ManagedDisk.ID, int32(sizeMb/1024))
    90  	}
    91  	return cloudprovider.ErrNotSupported
    92  }
    93  
    94  func (self *SDataDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
    95  	storageType := "Standard_LRS"
    96  	if self.ManagedDisk != nil && len(self.ManagedDisk.StorageAccountType) > 0 {
    97  		storageType = self.ManagedDisk.StorageAccountType
    98  	}
    99  	return &SStorage{storageType: storageType, zone: self.region.getZone()}, nil
   100  }
   101  
   102  func (self *SDataDisk) GetFsFormat() string {
   103  	return ""
   104  }
   105  
   106  func (self *SDataDisk) GetIsNonPersistent() bool {
   107  	return false
   108  }
   109  
   110  func (self *SDataDisk) GetDriver() string {
   111  	return "scsi"
   112  }
   113  
   114  func (self *SDataDisk) GetCacheMode() string {
   115  	return "none"
   116  }
   117  
   118  func (self *SDataDisk) GetMountpoint() string {
   119  	return ""
   120  }
   121  
   122  func (self *SDataDisk) GetDiskFormat() string {
   123  	return "vhd"
   124  }
   125  
   126  func (self *SDataDisk) GetDiskSizeMB() int {
   127  	if self.ManagedDisk != nil {
   128  		disk, err := self.region.GetDisk(self.ManagedDisk.ID)
   129  		if err != nil {
   130  			return 0
   131  		}
   132  		return disk.GetDiskSizeMB()
   133  	}
   134  	return int(self.DiskSizeGB.Int32()) * 1024
   135  }
   136  
   137  func (self *SDataDisk) GetIsAutoDelete() bool {
   138  	return true
   139  }
   140  
   141  func (self *SDataDisk) GetTemplateId() string {
   142  	if self.ManagedDisk != nil {
   143  		disk, err := self.region.GetDisk(self.ManagedDisk.ID)
   144  		if err == nil {
   145  			return disk.GetTemplateId()
   146  		}
   147  	}
   148  	return ""
   149  }
   150  
   151  func (self *SDataDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
   152  	return "", cloudprovider.ErrNotSupported
   153  }
   154  
   155  func (self *SDataDisk) GetDiskType() string {
   156  	return api.DISK_TYPE_DATA
   157  }
   158  
   159  func (disk *SDataDisk) GetAccessPath() string {
   160  	return ""
   161  }
   162  
   163  func (self *SDataDisk) Rebuild(ctx context.Context) error {
   164  	return cloudprovider.ErrNotSupported
   165  }
   166  
   167  func (self *SDataDisk) GetProjectId() string {
   168  	if self.ManagedDisk != nil {
   169  		return getResourceGroup(self.ManagedDisk.ID)
   170  	}
   171  	return ""
   172  }