yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere
    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 RelatedVms struct {
    32  	Id            string `json:"id"`
    33  	Text          string `json:"text"`
    34  	IconCls       string `json:"iconCls"`
    35  	State         string `json:"state"`
    36  	Children      string `json:"children"`
    37  	Object        string `json:"object"`
    38  	Name          string `json:"name"`
    39  	ViewId        string `json:"viewId"`
    40  	TargetType    string `json:"targetType"`
    41  	InnerName     string `json:"innerName"`
    42  	ServiceType   string `json:"serviceType"`
    43  	StorageType   string `json:"storageType"`
    44  	HciType       string `json:"hciType"`
    45  	VMType        string `json:"vmType"`
    46  	HostId        string `json:"hostId"`
    47  	ParentId      string `json:"parentId"`
    48  	DatastoreRole string `json:"datastoreRole"`
    49  	Vlan          string `json:"vlan"`
    50  	DhcpEnabled   bool   `json:"dhcpEnabled"`
    51  	ConnectMode   string `json:"connectMode"`
    52  	DataStoreType string `json:"dataStoreType"`
    53  	Hypervisor    string `json:"hypervisor"`
    54  }
    55  
    56  type SDisk struct {
    57  	multicloud.SDisk
    58  	InCloudSphereTags
    59  
    60  	region *SRegion
    61  
    62  	Id                 string       `json:"id"`
    63  	UUID               string       `json:"uuid"`
    64  	Size               float64      `json:"size"`
    65  	RealSize           float64      `json:"realSize"`
    66  	Name               string       `json:"name"`
    67  	FileName           string       `json:"fileName"`
    68  	Offset             int          `json:"offset"`
    69  	Shared             bool         `json:"shared"`
    70  	DeleteModel        string       `json:"deleteModel"`
    71  	VolumePolicy       string       `json:"volumePolicy"`
    72  	Format             string       `json:"format"`
    73  	BlockDeviceId      string       `json:"blockDeviceId"`
    74  	DiskType           string       `json:"diskType"`
    75  	DataStoreId        string       `json:"dataStoreId"`
    76  	DataStoreName      string       `json:"dataStoreName"`
    77  	DataStoreSize      float64      `json:"dataStoreSize"`
    78  	FreeStorage        float64      `json:"freeStorage"`
    79  	DataStoreType      string       `json:"dataStoreType"`
    80  	DataStoreReplicate int          `json:"dataStoreReplicate"`
    81  	VMName             string       `json:"vmName"`
    82  	VMStatus           string       `json:"vmStatus"`
    83  	Type               string       `json:"type"`
    84  	Description        string       `json:"description"`
    85  	Bootable           bool         `json:"bootable"`
    86  	VolumeStatus       string       `json:"volumeStatus"`
    87  	MountedHostIds     string       `json:"mountedHostIds"`
    88  	Md5                string       `json:"md5"`
    89  	DataSize           int          `json:"dataSize"`
    90  	OpenStackId        string       `json:"openStackId"`
    91  	VvSourceDto        string       `json:"vvSourceDto"`
    92  	FormatDisk         bool         `json:"formatDisk"`
    93  	ToBeConverted      bool         `json:"toBeConverted"`
    94  	RelatedVms         []RelatedVms `json:"relatedVms"`
    95  	XactiveDataStoreId string       `json:"xactiveDataStoreId"`
    96  	ClusterSize        int          `json:"clusterSize"`
    97  	ScsiId             string       `json:"scsiId"`
    98  	SecondaryUUId      string       `json:"secondaryUuid"`
    99  	SecondaryVolumes   string       `json:"secondaryVolumes"`
   100  }
   101  
   102  func (self *SDisk) GetName() string {
   103  	return self.Name
   104  }
   105  
   106  func (self *SDisk) GetId() string {
   107  	return self.Id
   108  }
   109  
   110  func (self *SDisk) GetGlobalId() string {
   111  	return self.GetId()
   112  }
   113  
   114  func (self *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
   115  	return nil, cloudprovider.ErrNotSupported
   116  }
   117  
   118  func (self *SDisk) Delete(ctx context.Context) error {
   119  	return cloudprovider.ErrNotImplemented
   120  }
   121  
   122  func (self *SDisk) GetCacheMode() string {
   123  	return "none"
   124  }
   125  
   126  func (self *SDisk) GetFsFormat() string {
   127  	return ""
   128  }
   129  
   130  func (self *SDisk) GetIsNonPersistent() bool {
   131  	return false
   132  }
   133  
   134  func (self *SDisk) GetDriver() string {
   135  	return "scsi"
   136  }
   137  
   138  func (self *SDisk) GetDiskType() string {
   139  	if strings.HasSuffix(self.Name, "1") || strings.HasSuffix(self.Name, "0") {
   140  		return api.DISK_TYPE_SYS
   141  	}
   142  	return api.DISK_TYPE_DATA
   143  }
   144  
   145  func (self *SDisk) GetDiskFormat() string {
   146  	return strings.ToLower(self.Format)
   147  }
   148  
   149  func (self *SDisk) GetDiskSizeMB() int {
   150  	return int(self.Size * 1024)
   151  }
   152  
   153  func (self *SDisk) GetIsAutoDelete() bool {
   154  	return true
   155  }
   156  
   157  func (self *SDisk) GetMountpoint() string {
   158  	return ""
   159  }
   160  
   161  func (self *SDisk) GetStatus() string {
   162  	return api.DISK_READY
   163  }
   164  
   165  func (self *SDisk) Rebuild(ctx context.Context) error {
   166  	return cloudprovider.ErrNotSupported
   167  }
   168  
   169  func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
   170  	return "", cloudprovider.ErrNotSupported
   171  }
   172  
   173  func (self *SDisk) Resize(ctx context.Context, sizeMb int64) error {
   174  	return self.region.ResizeDisk(self.Id, int(sizeMb/1024))
   175  }
   176  
   177  func (self *SDisk) GetTemplateId() string {
   178  	return ""
   179  }
   180  
   181  func (self *SDisk) GetAccessPath() string {
   182  	return self.FileName
   183  }
   184  
   185  func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
   186  	return self.region.GetStorage(self.DataStoreId)
   187  }
   188  
   189  func (self *SDisk) GetISnapshot(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
   190  	return nil, cloudprovider.ErrNotSupported
   191  }
   192  
   193  func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
   194  	return []cloudprovider.ICloudSnapshot{}, nil
   195  }
   196  
   197  func (self *SRegion) GetDisks(storageId string) ([]SDisk, error) {
   198  	ret := []SDisk{}
   199  	res := fmt.Sprintf("/storages/%s/volumes", storageId)
   200  	return ret, self.list(res, url.Values{}, &ret)
   201  }
   202  
   203  func (self *SRegion) GetDisk(id string) (*SDisk, error) {
   204  	ret := &SDisk{region: self}
   205  	res := fmt.Sprintf("/volumes/%s", id)
   206  	return ret, self.get(res, nil, ret)
   207  }
   208  
   209  func (self *SRegion) ResizeDisk(id string, sizeGb int) error {
   210  	disk, err := self.GetDisk(id)
   211  	if err != nil {
   212  		return errors.Wrapf(err, "GetDisk(%s)", id)
   213  	}
   214  	body := map[string]interface{}{
   215  		"size":          sizeGb,
   216  		"name":          disk.Name,
   217  		"bootable":      disk.Bootable,
   218  		"volumePolicy":  disk.VolumePolicy,
   219  		"dataStoreType": disk.DataStoreType,
   220  	}
   221  	return self.put("/volumes/"+disk.Id, nil, jsonutils.Marshal(body), nil)
   222  }
   223  
   224  func (self *SRegion) CreateDisk(name, storageId string, sizeGb int) (*SDisk, error) {
   225  	body := map[string]interface{}{
   226  		"dataStoreId":  storageId,
   227  		"format":       "RAW",
   228  		"name":         name,
   229  		"replicate":    1,
   230  		"size":         sizeGb,
   231  		"volumePolicy": "THIN",
   232  	}
   233  	resp, err := self.client.post("/volumes", jsonutils.Marshal(body))
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	taskId, err := resp.GetString("taskId")
   238  	if err != nil {
   239  		return nil, err
   240  	}
   241  	diskId, err := self.client.waitTask(taskId)
   242  	if err != nil {
   243  		return nil, errors.Wrapf(err, "waitTask(%s)", taskId)
   244  	}
   245  	return self.GetDisk(diskId)
   246  }