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