yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/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 nutanix
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  
    23  	"yunion.io/x/jsonutils"
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type DiskStats struct {
    32  	HypervisorAvgIoLatencyUsecs                     string `json:"hypervisor_avg_io_latency_usecs"`
    33  	HypervisorWriteIoBandwidthKBps                  string `json:"hypervisor_write_io_bandwidth_kBps"`
    34  	ControllerRandomOpsPpm                          string `json:"controller.random_ops_ppm"`
    35  	ControllerStorageTierSsdUsageBytes              string `json:"controller.storage_tier.ssd.usage_bytes"`
    36  	ReadIoPpm                                       string `json:"read_io_ppm"`
    37  	ControllerFrontendReadLatencyHistogram1000Us    string `json:"controller.frontend_read_latency_histogram_1000us"`
    38  	ControllerNumIops                               string `json:"controller_num_iops"`
    39  	ControllerFrontendWriteOps                      string `json:"controller.frontend_write_ops"`
    40  	ControllerFrontendWriteLatencyHistogram10000Us  string `json:"controller.frontend_write_latency_histogram_10000us"`
    41  	ControllerReadSizeHistogram1024KB               string `json:"controller.read_size_histogram_1024kB"`
    42  	TotalReadIoTimeUsecs                            string `json:"total_read_io_time_usecs"`
    43  	ControllerTotalReadIoTimeUsecs                  string `json:"controller_total_read_io_time_usecs"`
    44  	ControllerWss3600SWriteMB                       string `json:"controller.wss_3600s_write_MB"`
    45  	ControllerFrontendReadLatencyHistogram50000Us   string `json:"controller.frontend_read_latency_histogram_50000us"`
    46  	ControllerFrontendReadLatencyHistogram2000Us    string `json:"controller.frontend_read_latency_histogram_2000us"`
    47  	ControllerNumWriteIo                            string `json:"controller_num_write_io"`
    48  	ControllerReadSourceCacheSsdBytes               string `json:"controller.read_source_cache_ssd_bytes"`
    49  	ControllerReadSourceOplogBytes                  string `json:"controller.read_source_oplog_bytes"`
    50  	ControllerReadSourceCacheDramBytes              string `json:"controller.read_source_cache_dram_bytes"`
    51  	ControllerRandomReadOps                         string `json:"controller.random_read_ops"`
    52  	ControllerTotalIoTimeUsecs                      string `json:"controller_total_io_time_usecs"`
    53  	ControllerNumSeqIo                              string `json:"controller_num_seq_io"`
    54  	ControllerTotalIoSizeKbytes                     string `json:"controller_total_io_size_kbytes"`
    55  	ControllerWss120SWriteMB                        string `json:"controller.wss_120s_write_MB"`
    56  	ControllerReadSourceBlockStoreBytes             string `json:"controller.read_source_block_store_bytes"`
    57  	ControllerNumIo                                 string `json:"controller_num_io"`
    58  	ControllerReadSourceEstoreZeroBytes             string `json:"controller.read_source_estore_zero_bytes"`
    59  	ControllerNumRandomIo                           string `json:"controller_num_random_io"`
    60  	HypervisorNumReadIo                             string `json:"hypervisor_num_read_io"`
    61  	HypervisorTotalReadIoTimeUsecs                  string `json:"hypervisor_total_read_io_time_usecs"`
    62  	NumIo                                           string `json:"num_io"`
    63  	HypervisorNumWriteIo                            string `json:"hypervisor_num_write_io"`
    64  	ControllerWriteSizeHistogram32KB                string `json:"controller.write_size_histogram_32kB"`
    65  	ControllerFrontendReadLatencyHistogram20000Us   string `json:"controller.frontend_read_latency_histogram_20000us"`
    66  	ControllerReadSizeHistogram32KB                 string `json:"controller.read_size_histogram_32kB"`
    67  	HypervisorNumWriteIops                          string `json:"hypervisor_num_write_iops"`
    68  	AvgIoLatencyUsecs                               string `json:"avg_io_latency_usecs"`
    69  	ControllerWriteIoPpm                            string `json:"controller_write_io_ppm"`
    70  	ControllerReadSourceEstoreSsdBytes              string `json:"controller.read_source_estore_ssd_bytes"`
    71  	HypervisorTotalReadIoSizeKbytes                 string `json:"hypervisor_total_read_io_size_kbytes"`
    72  	ControllerNumWriteIops                          string `json:"controller_num_write_iops"`
    73  	TotalIoTimeUsecs                                string `json:"total_io_time_usecs"`
    74  	ControllerWss3600SReadMB                        string `json:"controller.wss_3600s_read_MB"`
    75  	ControllerSummaryReadSourceSsdBytesPerSec       string `json:"controller.summary_read_source_ssd_bytes_per_sec"`
    76  	ControllerWriteSizeHistogram16KB                string `json:"controller.write_size_histogram_16kB"`
    77  	TotalTransformedUsageBytes                      string `json:"total_transformed_usage_bytes"`
    78  	AvgWriteIoLatencyUsecs                          string `json:"avg_write_io_latency_usecs"`
    79  	ControllerCseTarget90PercentWriteMB             string `json:"controller.cse_target_90_percent_write_MB"`
    80  	NumReadIo                                       string `json:"num_read_io"`
    81  	HypervisorReadIoBandwidthKBps                   string `json:"hypervisor_read_io_bandwidth_kBps"`
    82  	HypervisorTotalIoTimeUsecs                      string `json:"hypervisor_total_io_time_usecs"`
    83  	NumRandomIo                                     string `json:"num_random_io"`
    84  	ControllerWriteDestEstoreBytes                  string `json:"controller.write_dest_estore_bytes"`
    85  	ControllerFrontendWriteLatencyHistogram5000Us   string `json:"controller.frontend_write_latency_histogram_5000us"`
    86  	ControllerStorageTierDasSataPinnedUsageBytes    string `json:"controller.storage_tier.das-sata.pinned_usage_bytes"`
    87  	NumWriteIo                                      string `json:"num_write_io"`
    88  	ControllerFrontendWriteLatencyHistogram2000Us   string `json:"controller.frontend_write_latency_histogram_2000us"`
    89  	ControllerRandomWriteOpsPerSec                  string `json:"controller.random_write_ops_per_sec"`
    90  	ControllerFrontendWriteLatencyHistogram20000Us  string `json:"controller.frontend_write_latency_histogram_20000us"`
    91  	IoBandwidthKBps                                 string `json:"io_bandwidth_kBps"`
    92  	ControllerWriteSizeHistogram512KB               string `json:"controller.write_size_histogram_512kB"`
    93  	ControllerReadSizeHistogram16KB                 string `json:"controller.read_size_histogram_16kB"`
    94  	WriteIoPpm                                      string `json:"write_io_ppm"`
    95  	ControllerAvgWriteIoLatencyUsecs                string `json:"controller_avg_write_io_latency_usecs"`
    96  	ControllerFrontendReadLatencyHistogram100000Us  string `json:"controller.frontend_read_latency_histogram_100000us"`
    97  	NumReadIops                                     string `json:"num_read_iops"`
    98  	ControllerSummaryReadSourceHddBytesPerSec       string `json:"controller.summary_read_source_hdd_bytes_per_sec"`
    99  	ControllerReadSourceExtentCacheBytes            string `json:"controller.read_source_extent_cache_bytes"`
   100  	TimespanUsecs                                   string `json:"timespan_usecs"`
   101  	ControllerNumReadIops                           string `json:"controller_num_read_iops"`
   102  	ControllerFrontendReadLatencyHistogram10000Us   string `json:"controller.frontend_read_latency_histogram_10000us"`
   103  	ControllerWriteSizeHistogram64KB                string `json:"controller.write_size_histogram_64kB"`
   104  	ControllerFrontendWriteLatencyHistogram0Us      string `json:"controller.frontend_write_latency_histogram_0us"`
   105  	ControllerFrontendWriteLatencyHistogram100000Us string `json:"controller.frontend_write_latency_histogram_100000us"`
   106  	HypervisorNumIo                                 string `json:"hypervisor_num_io"`
   107  	ControllerTotalTransformedUsageBytes            string `json:"controller_total_transformed_usage_bytes"`
   108  	AvgReadIoLatencyUsecs                           string `json:"avg_read_io_latency_usecs"`
   109  	ControllerTotalReadIoSizeKbytes                 string `json:"controller_total_read_io_size_kbytes"`
   110  	ControllerReadIoPpm                             string `json:"controller_read_io_ppm"`
   111  	ControllerFrontendOps                           string `json:"controller.frontend_ops"`
   112  	ControllerWss120SReadMB                         string `json:"controller.wss_120s_read_MB"`
   113  	ControllerReadSizeHistogram512KB                string `json:"controller.read_size_histogram_512kB"`
   114  	HypervisorAvgReadIoLatencyUsecs                 string `json:"hypervisor_avg_read_io_latency_usecs"`
   115  	ControllerWriteSizeHistogram1024KB              string `json:"controller.write_size_histogram_1024kB"`
   116  	ControllerWriteDestBlockStoreBytes              string `json:"controller.write_dest_block_store_bytes"`
   117  	ControllerReadSizeHistogram4KB                  string `json:"controller.read_size_histogram_4kB"`
   118  	NumWriteIops                                    string `json:"num_write_iops"`
   119  	ControllerRandomOpsPerSec                       string `json:"controller.random_ops_per_sec"`
   120  	NumIops                                         string `json:"num_iops"`
   121  	ControllerStorageTierCloudPinnedUsageBytes      string `json:"controller.storage_tier.cloud.pinned_usage_bytes"`
   122  	ControllerAvgIoLatencyUsecs                     string `json:"controller_avg_io_latency_usecs"`
   123  	ControllerReadSizeHistogram8KB                  string `json:"controller.read_size_histogram_8kB"`
   124  	ControllerNumReadIo                             string `json:"controller_num_read_io"`
   125  	ControllerSeqIoPpm                              string `json:"controller_seq_io_ppm"`
   126  	ControllerReadIoBandwidthKBps                   string `json:"controller_read_io_bandwidth_kBps"`
   127  	ControllerIoBandwidthKBps                       string `json:"controller_io_bandwidth_kBps"`
   128  	ControllerReadSizeHistogram0KB                  string `json:"controller.read_size_histogram_0kB"`
   129  	ControllerRandomOps                             string `json:"controller.random_ops"`
   130  	HypervisorTimespanUsecs                         string `json:"hypervisor_timespan_usecs"`
   131  	TotalReadIoSizeKbytes                           string `json:"total_read_io_size_kbytes"`
   132  	HypervisorTotalIoSizeKbytes                     string `json:"hypervisor_total_io_size_kbytes"`
   133  	ControllerFrontendOpsPerSec                     string `json:"controller.frontend_ops_per_sec"`
   134  	ControllerWriteDestOplogBytes                   string `json:"controller.write_dest_oplog_bytes"`
   135  	ControllerFrontendWriteLatencyHistogram1000Us   string `json:"controller.frontend_write_latency_histogram_1000us"`
   136  	HypervisorNumReadIops                           string `json:"hypervisor_num_read_iops"`
   137  	ControllerSummaryReadSourceCacheBytesPerSec     string `json:"controller.summary_read_source_cache_bytes_per_sec"`
   138  	ControllerWriteIoBandwidthKBps                  string `json:"controller_write_io_bandwidth_kBps"`
   139  	ControllerUserBytes                             string `json:"controller_user_bytes"`
   140  	HypervisorAvgWriteIoLatencyUsecs                string `json:"hypervisor_avg_write_io_latency_usecs"`
   141  	ControllerStorageTierSsdPinnedUsageBytes        string `json:"controller.storage_tier.ssd.pinned_usage_bytes"`
   142  	ReadIoBandwidthKBps                             string `json:"read_io_bandwidth_kBps"`
   143  	ControllerFrontendReadOps                       string `json:"controller.frontend_read_ops"`
   144  	HypervisorNumIops                               string `json:"hypervisor_num_iops"`
   145  	HypervisorIoBandwidthKBps                       string `json:"hypervisor_io_bandwidth_kBps"`
   146  	ControllerWss120SUnionMB                        string `json:"controller.wss_120s_union_MB"`
   147  	ControllerReadSourceEstoreHddBytes              string `json:"controller.read_source_estore_hdd_bytes"`
   148  	ControllerRandomIoPpm                           string `json:"controller_random_io_ppm"`
   149  	ControllerCseTarget90PercentReadMB              string `json:"controller.cse_target_90_percent_read_MB"`
   150  	ControllerStorageTierDasSataUsageBytes          string `json:"controller.storage_tier.das-sata.usage_bytes"`
   151  	ControllerFrontendReadLatencyHistogram5000Us    string `json:"controller.frontend_read_latency_histogram_5000us"`
   152  	ControllerAvgReadIoSizeKbytes                   string `json:"controller_avg_read_io_size_kbytes"`
   153  	WriteIoBandwidthKBps                            string `json:"write_io_bandwidth_kBps"`
   154  	ControllerRandomReadOpsPerSec                   string `json:"controller.random_read_ops_per_sec"`
   155  	ControllerReadSizeHistogram64KB                 string `json:"controller.read_size_histogram_64kB"`
   156  	ControllerWss3600SUnionMB                       string `json:"controller.wss_3600s_union_MB"`
   157  	RandomIoPpm                                     string `json:"random_io_ppm"`
   158  	TotalUntransformedUsageBytes                    string `json:"total_untransformed_usage_bytes"`
   159  	ControllerFrontendReadLatencyHistogram0Us       string `json:"controller.frontend_read_latency_histogram_0us"`
   160  	ControllerRandomWriteOps                        string `json:"controller.random_write_ops"`
   161  	ControllerAvgWriteIoSizeKbytes                  string `json:"controller_avg_write_io_size_kbytes"`
   162  	ControllerAvgReadIoLatencyUsecs                 string `json:"controller_avg_read_io_latency_usecs"`
   163  	TotalIoSizeKbytes                               string `json:"total_io_size_kbytes"`
   164  	ControllerStorageTierCloudUsageBytes            string `json:"controller.storage_tier.cloud.usage_bytes"`
   165  	ControllerFrontendWriteLatencyHistogram50000Us  string `json:"controller.frontend_write_latency_histogram_50000us"`
   166  	ControllerWriteSizeHistogram8KB                 string `json:"controller.write_size_histogram_8kB"`
   167  	ControllerTimespanUsecs                         string `json:"controller_timespan_usecs"`
   168  	NumSeqIo                                        string `json:"num_seq_io"`
   169  	ControllerWriteSizeHistogram4KB                 string `json:"controller.write_size_histogram_4kB"`
   170  	SeqIoPpm                                        string `json:"seq_io_ppm"`
   171  	ControllerWriteSizeHistogram0KB                 string `json:"controller.write_size_histogram_0kB"`
   172  }
   173  
   174  type SDisk struct {
   175  	multicloud.STagBase
   176  	multicloud.SDisk
   177  
   178  	storage *SStorage
   179  
   180  	VirtualDiskID         string    `json:"virtual_disk_id"`
   181  	UUID                  string    `json:"uuid"`
   182  	DeviceUUID            string    `json:"device_uuid"`
   183  	NutanixNfsfilePath    string    `json:"nutanix_nfsfile_path"`
   184  	DiskAddress           string    `json:"disk_address"`
   185  	AttachedVMID          string    `json:"attached_vm_id"`
   186  	AttachedVMUUID        string    `json:"attached_vm_uuid"`
   187  	AttachedVmname        string    `json:"attached_vmname"`
   188  	AttachedVolumeGroupID string    `json:"attached_volume_group_id"`
   189  	DiskCapacityInBytes   int64     `json:"disk_capacity_in_bytes"`
   190  	ClusterUUID           string    `json:"cluster_uuid"`
   191  	StorageContainerID    string    `json:"storage_container_id"`
   192  	StorageContainerUUID  string    `json:"storage_container_uuid"`
   193  	FlashModeEnabled      string    `json:"flash_mode_enabled"`
   194  	DataSourceURL         string    `json:"data_source_url"`
   195  	Stats                 DiskStats `json:"stats"`
   196  
   197  	isSys bool
   198  }
   199  
   200  func (self *SDisk) GetName() string {
   201  	return self.DiskAddress
   202  }
   203  
   204  func (self *SDisk) GetId() string {
   205  	return self.UUID
   206  }
   207  
   208  func (self *SDisk) GetGlobalId() string {
   209  	return self.UUID
   210  }
   211  
   212  func (self *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
   213  	return nil, cloudprovider.ErrNotSupported
   214  }
   215  
   216  func (self *SDisk) Delete(ctx context.Context) error {
   217  	return cloudprovider.ErrNotSupported
   218  }
   219  
   220  func (self *SDisk) GetAccessPath() string {
   221  	return self.NutanixNfsfilePath
   222  }
   223  
   224  func (self *SDisk) GetCacheMode() string {
   225  	return "none"
   226  }
   227  
   228  func (self *SDisk) GetFsFormat() string {
   229  	return ""
   230  }
   231  
   232  func (self *SDisk) GetIsNonPersistent() bool {
   233  	return false
   234  }
   235  
   236  func (self *SDisk) GetDriver() string {
   237  	if info := strings.Split(self.DiskAddress, "."); len(info) > 0 {
   238  		return info[0]
   239  	}
   240  	return "scsi"
   241  }
   242  
   243  func (self *SDisk) GetDiskType() string {
   244  	if self.isSys {
   245  		return api.DISK_TYPE_SYS
   246  	}
   247  	ins, err := self.storage.zone.region.GetInstance(self.AttachedVMUUID)
   248  	if err != nil {
   249  		return api.DISK_TYPE_DATA
   250  	}
   251  	for _, disk := range ins.VMDiskInfo {
   252  		if disk.IsCdrom {
   253  			continue
   254  		}
   255  		if disk.DiskAddress.VmdiskUUID == self.UUID {
   256  			return api.DISK_TYPE_SYS
   257  		} else {
   258  			return api.DISK_TYPE_DATA
   259  		}
   260  	}
   261  	return api.DISK_TYPE_DATA
   262  }
   263  
   264  func (self *SDisk) GetDiskFormat() string {
   265  	return "raw"
   266  }
   267  
   268  func (self *SDisk) GetDiskSizeMB() int {
   269  	return int(self.DiskCapacityInBytes / 1024 / 1024)
   270  }
   271  
   272  func (self *SDisk) GetIsAutoDelete() bool {
   273  	return true
   274  }
   275  
   276  func (self *SDisk) GetMountpoint() string {
   277  	return ""
   278  }
   279  
   280  func (self *SDisk) GetStatus() string {
   281  	return api.DISK_READY
   282  }
   283  
   284  func (self *SDisk) Rebuild(ctx context.Context) error {
   285  	return cloudprovider.ErrNotSupported
   286  }
   287  
   288  func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
   289  	return "", cloudprovider.ErrNotSupported
   290  }
   291  
   292  func (self *SDisk) Resize(ctx context.Context, sizeMb int64) error {
   293  	ins, err := self.storage.zone.region.GetInstance(self.AttachedVMUUID)
   294  	if err != nil {
   295  		return errors.Wrapf(err, "GetInstance(%s)", self.AttachedVMUUID)
   296  	}
   297  	for _, disk := range ins.VMDiskInfo {
   298  		if disk.DiskAddress.VmdiskUUID == self.UUID {
   299  			params := map[string]interface{}{
   300  				"vm_disks": []map[string]interface{}{
   301  					{
   302  						"disk_address":       disk.DiskAddress,
   303  						"flash_mode_enabled": disk.FlashModeEnabled,
   304  						"is_cdrom":           disk.IsCdrom,
   305  						"is_empty":           disk.IsEmpty,
   306  						"vm_disk_create": map[string]interface{}{
   307  							"storage_container_uuid": disk.StorageContainerUUID,
   308  							"size":                   sizeMb * 1024 * 1024,
   309  						},
   310  					},
   311  				},
   312  			}
   313  			return self.storage.zone.region.update("vms", fmt.Sprintf("%s/disks/update", self.AttachedVMUUID), jsonutils.Marshal(params), nil)
   314  		}
   315  	}
   316  	return cloudprovider.ErrNotSupported
   317  }
   318  
   319  func (self *SDisk) GetTemplateId() string {
   320  	return ""
   321  }
   322  
   323  func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
   324  	return self.storage, nil
   325  }
   326  
   327  func (self *SDisk) GetISnapshot(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
   328  	return nil, cloudprovider.ErrNotImplemented
   329  }
   330  
   331  func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
   332  	return nil, cloudprovider.ErrNotImplemented
   333  }
   334  
   335  func (self *SRegion) GetDisks(storageId, vmId string) ([]SDisk, error) {
   336  	disks := []SDisk{}
   337  	params := url.Values{}
   338  	filter := []string{}
   339  	if len(storageId) > 0 {
   340  		filter = append(filter, "container_uuid=="+storageId)
   341  	}
   342  	if len(vmId) > 0 {
   343  		filter = append(filter, "vm_uuid=="+vmId)
   344  		filter = append(filter, "attach_vm_id=="+vmId)
   345  	}
   346  	if len(filter) > 0 {
   347  		params.Set("filter_criteria", strings.Join(filter, ","))
   348  	}
   349  	return disks, self.listAll("virtual_disks", params, &disks)
   350  }
   351  
   352  func (self *SRegion) GetDisk(id string) (*SDisk, error) {
   353  	disk := &SDisk{}
   354  	return disk, self.get("virtual_disks", id, url.Values{}, disk)
   355  }