yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    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 SDisk struct {
    29  	storage *SStorage
    30  	// TODO instance
    31  
    32  	multicloud.SDisk
    33  	EcloudTags
    34  	multicloud.SBillingBase
    35  	SZoneRegionBase
    36  	SCreateTime
    37  
    38  	ManualAttr SDiskManualAttr
    39  
    40  	// 硬盘可挂载主机类型
    41  	AttachServerTypes []string
    42  	AvailabilityZone  string
    43  	BackupId          string
    44  	Description       string
    45  	ID                string
    46  	IsDelete          bool
    47  	IsShare           bool
    48  	// 磁盘所在集群的ID
    49  	Metadata      string
    50  	Name          string
    51  	OperationFlag string
    52  	// 硬盘挂在主机ID列表
    53  	ServerId       []string
    54  	SizeGB         int `json:"size"`
    55  	SourceVolumeId string
    56  	Status         string
    57  	Type           string
    58  	VolumeType     string
    59  	Iscsi          bool
    60  	ProductType    string
    61  }
    62  
    63  type SDiskManualAttr struct {
    64  	IsVirtual  bool
    65  	TempalteId string
    66  	ServerId   string
    67  }
    68  
    69  func (d *SDisk) GetBillingType() string {
    70  	return billing_api.BILLING_TYPE_POSTPAID
    71  }
    72  
    73  func (d *SDisk) GetExpiredAt() time.Time {
    74  	return time.Time{}
    75  }
    76  
    77  func (d *SDisk) GetId() string {
    78  	return d.ID
    79  }
    80  
    81  func (d *SDisk) GetName() string {
    82  	return d.Name
    83  }
    84  
    85  func (d *SDisk) GetGlobalId() string {
    86  	return d.ID
    87  }
    88  
    89  func (d *SDisk) GetCreatedAt() time.Time {
    90  	return d.SCreateTime.GetCreatedAt()
    91  }
    92  
    93  func (d *SDisk) GetStatus() string {
    94  	if d.IsDelete {
    95  		// TODO
    96  		return ""
    97  	}
    98  	switch d.Status {
    99  	case "available", "in-use":
   100  		return api.DISK_READY
   101  	case "attaching":
   102  		return api.DISK_ATTACHING
   103  	case "backing_up":
   104  		return api.DISK_BACKUP_STARTALLOC
   105  	case "creating", "downloading":
   106  		return api.DISK_ALLOCATING
   107  	case "deleting":
   108  		return api.DISK_DEALLOC
   109  	case "uploading":
   110  		return api.DISK_SAVING
   111  	case "error":
   112  		return api.DISK_ALLOC_FAILED
   113  	case "error_deleting":
   114  		return api.DISK_DEALLOC_FAILED
   115  	case "restoring_backup":
   116  		return api.DISK_REBUILD
   117  	case "detaching":
   118  		return api.DISK_DETACHING
   119  	case "extending":
   120  		return api.DISK_RESIZING
   121  	case "error_extending":
   122  		return api.DISK_RESIZE_FAILED
   123  	case "error_restoring", "unrecognized":
   124  		return api.DISK_UNKNOWN
   125  	default:
   126  		return api.DISK_UNKNOWN
   127  	}
   128  }
   129  
   130  func (d *SDisk) Refresh() error {
   131  	return nil
   132  }
   133  
   134  func (d *SDisk) IsEmulated() bool {
   135  	return false
   136  }
   137  
   138  func (s *SDisk) GetSysTags() map[string]string {
   139  	data := map[string]string{}
   140  	data["hypervisor"] = api.HYPERVISOR_ECLOUD
   141  	return data
   142  }
   143  
   144  func (s *SDisk) GetProjectId() string {
   145  	return ""
   146  }
   147  
   148  func (s *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
   149  	return s.storage, nil
   150  }
   151  
   152  func (s *SDisk) GetDiskFormat() string {
   153  	return "vhd"
   154  }
   155  
   156  func (s *SDisk) GetDiskSizeMB() int {
   157  	return s.SizeGB * 1024
   158  }
   159  
   160  func (s *SDisk) GetIsAutoDelete() bool {
   161  	if s.GetDiskType() == api.DISK_TYPE_SYS {
   162  		return true
   163  	}
   164  	return false
   165  }
   166  
   167  func (s *SDisk) GetTemplateId() string {
   168  	return s.ManualAttr.TempalteId
   169  }
   170  
   171  func (s *SDisk) GetDiskType() string {
   172  	if s.ManualAttr.IsVirtual {
   173  		return api.DISK_TYPE_SYS
   174  	}
   175  	return api.DISK_TYPE_DATA
   176  }
   177  
   178  func (s *SDisk) GetFsFormat() string {
   179  	return ""
   180  }
   181  
   182  func (s *SDisk) GetIsNonPersistent() bool {
   183  	return false
   184  }
   185  
   186  func (s *SDisk) GetDriver() string {
   187  	return "scsi"
   188  }
   189  
   190  func (s *SDisk) GetCacheMode() string {
   191  	return "none"
   192  }
   193  
   194  func (s *SDisk) GetMountpoint() string {
   195  	return ""
   196  }
   197  
   198  func (s *SDisk) GetAccessPath() string {
   199  	return ""
   200  }
   201  
   202  func (s *SDisk) Delete(ctx context.Context) error {
   203  	return cloudprovider.ErrNotImplemented
   204  }
   205  
   206  func (s *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) {
   207  	return nil, cloudprovider.ErrNotImplemented
   208  }
   209  
   210  func (s *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error) {
   211  	parentId, isSystem := s.ID, false
   212  	if s.ManualAttr.IsVirtual {
   213  		parentId, isSystem = s.ManualAttr.ServerId, true
   214  	}
   215  	snapshots, err := s.storage.zone.region.GetSnapshots(id, parentId, isSystem)
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  	if len(snapshots) == 0 {
   220  		return nil, cloudprovider.ErrNotFound
   221  	}
   222  	return &snapshots[0], nil
   223  }
   224  
   225  func (s *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
   226  	parentId, isSystem := s.ID, false
   227  	if s.ManualAttr.IsVirtual {
   228  		parentId, isSystem = s.ManualAttr.ServerId, true
   229  	}
   230  	snapshots, err := s.storage.zone.region.GetSnapshots("", parentId, isSystem)
   231  	if err != nil {
   232  		return nil, err
   233  	}
   234  	isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots))
   235  	for i := range snapshots {
   236  		isnapshots[i] = &snapshots[i]
   237  	}
   238  	return isnapshots, nil
   239  }
   240  
   241  func (s *SDisk) GetExtSnapshotPolicyIds() ([]string, error) {
   242  	return []string{}, nil
   243  }
   244  
   245  func (s *SDisk) Resize(ctx context.Context, newSizeMB int64) error {
   246  	return cloudprovider.ErrNotImplemented
   247  }
   248  
   249  func (s *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
   250  	return "", cloudprovider.ErrNotImplemented
   251  }
   252  
   253  func (s *SDisk) Rebuild(ctx context.Context) error {
   254  	return cloudprovider.ErrNotImplemented
   255  }
   256  
   257  func (s *SRegion) GetDisks() ([]SDisk, error) {
   258  	request := NewNovaRequest(NewApiRequest(s.ID, "/api/v2/volume/volume/volume/list/with/server", nil, nil))
   259  	disks := make([]SDisk, 0, 5)
   260  	err := s.client.doList(context.Background(), request, &disks)
   261  	if err != nil {
   262  		return nil, err
   263  	}
   264  	return disks, nil
   265  }
   266  
   267  func (s *SRegion) GetDisk(id string) (*SDisk, error) {
   268  	// TODO
   269  	request := NewNovaRequest(NewApiRequest(s.ID, fmt.Sprintf("/api/v2/volume/volume/volumeDetail/%s", id), nil, nil))
   270  	var disk SDisk
   271  	err := s.client.doGet(context.Background(), request, &disk)
   272  	if err != nil {
   273  		return nil, err
   274  	}
   275  	return &disk, nil
   276  }