yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/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 bingocloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    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 SDisk struct {
    29  	BingoTags
    30  	multicloud.SDisk
    31  
    32  	storage *SStorage
    33  
    34  	AttachmentSet    []AttachmentSet `json:"attachmentSet"`
    35  	AvailabilityZone string          `json:"availabilityZone"`
    36  	CreateTime       string          `json:"createTime"`
    37  	Description      string          `json:"description"`
    38  	DetachBehavior   string          `json:"detachBehavior"`
    39  	Goal             string          `json:"goal"`
    40  	Iops             string          `json:"iops"`
    41  	IsDeductQuota    string          `json:"isDeductQuota"`
    42  	IsEncrypt        string          `json:"isEncrypt"`
    43  	IsForSleepInst   string          `json:"isForSleepInst"`
    44  	IsMirrorVolume   string          `json:"isMirrorVolume"`
    45  	IsMultiAttach    string          `json:"isMultiAttach"`
    46  	IsOneInst        string          `json:"isOneInst"`
    47  	IsRoot           string          `json:"isRoot"`
    48  	Location         string          `json:"location"`
    49  	MirrorFrom       string          `json:"mirrorFrom"`
    50  	MirrorProcess    string          `json:"mirrorProcess"`
    51  	MirrorStatus     string          `json:"mirrorStatus"`
    52  	NodeId           string          `json:"nodeId"`
    53  	Owner            string          `json:"owner"`
    54  	Passphrase       string          `json:"passphrase"`
    55  	Readonly         string          `json:"readonly"`
    56  	Size             int             `json:"size"`
    57  	SnapshotId       string          `json:"snapshotId"`
    58  	Status           string          `json:"status"`
    59  	StorageId        string          `json:"storageId"`
    60  	VolumeId         string          `json:"volumeId"`
    61  	VolumeName       string          `json:"volumeName"`
    62  }
    63  
    64  type AttachmentSet struct {
    65  	AttachTime          string `json:"attachTime"`
    66  	Cache               string `json:"cache"`
    67  	DeleteOnTermination string `json:"deleteOnTermination"`
    68  	Device              string `json:"device"`
    69  	InstanceId          string `json:"instanceId"`
    70  	Status              string `json:"status"`
    71  	VolumeId            string `json:"volumeId"`
    72  }
    73  
    74  func (self *SDisk) GetName() string {
    75  	return self.VolumeName
    76  }
    77  
    78  func (self *SDisk) GetId() string {
    79  	return self.VolumeId
    80  }
    81  
    82  func (self *SDisk) GetGlobalId() string {
    83  	return self.GetId()
    84  }
    85  
    86  func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
    87  	return self.storage, nil
    88  }
    89  
    90  func (self *SDisk) GetIStorageId() string {
    91  	return self.StorageId
    92  }
    93  
    94  func (self *SDisk) GetDiskFormat() string {
    95  	return "raw"
    96  }
    97  
    98  func (self *SDisk) GetDiskSizeMB() int {
    99  	return self.Size * 1024
   100  }
   101  
   102  func (self *SDisk) GetIsAutoDelete() bool {
   103  	return self.IsRoot == "true"
   104  }
   105  
   106  func (self *SDisk) GetTemplateId() string {
   107  	return ""
   108  }
   109  
   110  func (self *SDisk) GetDiskType() string {
   111  	if self.IsRoot == "true" {
   112  		return api.DISK_TYPE_SYS
   113  	}
   114  	return api.DISK_TYPE_DATA
   115  }
   116  
   117  func (self *SDisk) GetFsFormat() string {
   118  	return ""
   119  }
   120  
   121  func (self *SDisk) GetIsNonPersistent() bool {
   122  	return false
   123  }
   124  
   125  func (self *SDisk) GetDriver() string {
   126  	return "virtio"
   127  }
   128  
   129  func (self *SDisk) GetCacheMode() string {
   130  	return "none"
   131  }
   132  
   133  func (self *SDisk) GetMountpoint() string {
   134  	for _, att := range self.AttachmentSet {
   135  		return att.Device
   136  	}
   137  	return ""
   138  }
   139  
   140  func (self *SDisk) GetAccessPath() string {
   141  	return ""
   142  }
   143  
   144  func (self *SDisk) Delete(ctx context.Context) error {
   145  	return cloudprovider.ErrNotImplemented
   146  }
   147  
   148  func (self *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) {
   149  	return nil, cloudprovider.ErrNotImplemented
   150  }
   151  
   152  func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
   153  	return nil, cloudprovider.ErrNotImplemented
   154  }
   155  
   156  func (self *SDisk) GetExtSnapshotPolicyIds() ([]string, error) {
   157  	return []string{}, nil
   158  }
   159  
   160  func (self *SDisk) Resize(ctx context.Context, newSizeMB int64) error {
   161  	return cloudprovider.ErrNotImplemented
   162  }
   163  
   164  func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
   165  	return "", cloudprovider.ErrNotImplemented
   166  }
   167  
   168  func (self *SDisk) Rebuild(ctx context.Context) error {
   169  	return cloudprovider.ErrNotImplemented
   170  }
   171  
   172  func (self *SDisk) GetStatus() string {
   173  	switch self.Status {
   174  	case "available", "in-use":
   175  		return api.DISK_READY
   176  	default:
   177  		return self.Status
   178  	}
   179  }
   180  
   181  func (self *SRegion) GetDisks(id string, maxResult int, nextToken string) ([]SDisk, string, error) {
   182  	params := map[string]string{}
   183  	idx := 1
   184  	if len(id) > 0 {
   185  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "volume-id"
   186  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = id
   187  		idx++
   188  	}
   189  
   190  	if len(nextToken) > 0 {
   191  		params["nextToken"] = nextToken
   192  	}
   193  	if maxResult > 0 {
   194  		params["maxRecords"] = fmt.Sprintf("%d", maxResult)
   195  	}
   196  	resp, err := self.invoke("DescribeVolumes", params)
   197  	if err != nil {
   198  		return nil, "", err
   199  	}
   200  	ret := struct {
   201  		NextToken string
   202  		VolumeSet []SDisk
   203  	}{}
   204  	resp.Unmarshal(&ret)
   205  	return ret.VolumeSet, ret.NextToken, nil
   206  }
   207  
   208  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
   209  	part, nextToken, err := self.cluster.region.GetDisks("", MAX_RESULT, "")
   210  	if err != nil {
   211  		return nil, err
   212  	}
   213  	disks := []SDisk{}
   214  	disks = append(disks, part...)
   215  	for len(nextToken) > 0 {
   216  		part, nextToken, err = self.cluster.region.GetDisks("", MAX_RESULT, nextToken)
   217  		if err != nil {
   218  			return nil, err
   219  		}
   220  		disks = append(disks, part...)
   221  	}
   222  	ret := []cloudprovider.ICloudDisk{}
   223  	for i := range disks {
   224  		if disks[i].StorageId == self.StorageId {
   225  			disks[i].storage = self
   226  			ret = append(ret, &disks[i])
   227  		}
   228  	}
   229  	return ret, nil
   230  }
   231  
   232  func (self *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
   233  	disk, err := self.cluster.region.GetDisk(id)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	if disk.StorageId != self.StorageId {
   238  		return nil, cloudprovider.ErrNotFound
   239  	}
   240  	disk.storage = self
   241  	return disk, nil
   242  }
   243  
   244  func (self *SRegion) GetDisk(id string) (*SDisk, error) {
   245  	disks, _, err := self.GetDisks(id, 1, "")
   246  	if err != nil {
   247  		return nil, err
   248  	}
   249  	for i := range disks {
   250  		if disks[i].GetGlobalId() == id {
   251  			return &disks[i], nil
   252  		}
   253  	}
   254  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   255  }