yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/storage.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  	"yunion.io/x/jsonutils"
    19  	"yunion.io/x/pkg/errors"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SStorage struct {
    27  	multicloud.STagBase
    28  	multicloud.SStorageBase
    29  	cluster *SCluster
    30  
    31  	StorageId    string `json:"storageId"`
    32  	Location     string `json:"location"`
    33  	UsedBy       string `json:"usedBy"`
    34  	SpaceUsed    int64  `json:"spaceUsed"`
    35  	StorageName  string `json:"storageName"`
    36  	FileFormat   string `json:"fileFormat"`
    37  	Disabled     string `json:"disabled"`
    38  	SpaceMax     int64  `json:"spaceMax"`
    39  	StorageType  string `json:"storageType"`
    40  	DrCloudId    string `json:"drCloudId"`
    41  	ParameterSet []struct {
    42  		Name  string
    43  		Value string
    44  	} `json:"parameterSet"`
    45  	ClusterId    string `json:"clusterId"`
    46  	IsDRStorage  string `json:"isDRStorage"`
    47  	ResUsage     string `json:"resUsage"`
    48  	ScheduleTags string `json:"scheduleTags"`
    49  }
    50  
    51  func (self *SStorage) GetName() string {
    52  	return self.StorageName
    53  }
    54  
    55  func (self *SStorage) GetId() string {
    56  	return self.StorageId
    57  }
    58  
    59  func (self *SStorage) GetGlobalId() string {
    60  	return self.GetId()
    61  }
    62  
    63  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
    64  	return nil, cloudprovider.ErrNotImplemented
    65  }
    66  
    67  func (self *SStorage) DisableSync() bool {
    68  	return false
    69  }
    70  
    71  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    72  	return &SStoragecache{storageId: self.StorageId, storageName: self.StorageName, region: self.cluster.region}
    73  }
    74  
    75  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    76  	return self.cluster
    77  }
    78  
    79  func (self *SStorage) GetStorageType() string {
    80  	return self.StorageType
    81  }
    82  
    83  func (self *SStorage) GetMediumType() string {
    84  	return api.DISK_TYPE_SSD
    85  }
    86  
    87  func (self *SStorage) GetCapacityMB() int64 {
    88  	return self.SpaceMax * 1024
    89  }
    90  
    91  func (self *SStorage) GetCapacityUsedMB() int64 {
    92  	return self.SpaceUsed * 1024
    93  }
    94  
    95  func (self *SStorage) GetMountPoint() string {
    96  	return ""
    97  }
    98  
    99  func (self *SStorage) GetStatus() string {
   100  	return api.STORAGE_ONLINE
   101  }
   102  
   103  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   104  	return jsonutils.Marshal(self.ParameterSet)
   105  }
   106  
   107  func (self *SStorage) GetEnabled() bool {
   108  	return self.Disabled == "false"
   109  }
   110  
   111  func (self *SStorage) IsSysDiskStore() bool {
   112  	return true
   113  }
   114  
   115  func (self *SRegion) GetStorages(nextToken string) ([]SStorage, string, error) {
   116  	params := map[string]string{}
   117  	if len(nextToken) > 0 {
   118  		params["nextToken"] = nextToken
   119  	}
   120  	resp, err := self.invoke("DescribeStorages", params)
   121  	if err != nil {
   122  		return nil, "", err
   123  	}
   124  	ret := struct {
   125  		NextToken  string
   126  		StorageSet []SStorage
   127  	}{}
   128  	resp.Unmarshal(&ret)
   129  	return ret.StorageSet, ret.NextToken, nil
   130  }
   131  
   132  func (self *SCluster) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   133  	storages, err := self.GetIStorages()
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	for i := range storages {
   138  		if storages[i].GetGlobalId() == id {
   139  			return storages[i], nil
   140  		}
   141  	}
   142  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   143  }
   144  
   145  func (self *SCluster) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   146  	storages, err := self.region.getStorages()
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	ret := []cloudprovider.ICloudStorage{}
   151  	for i := range storages {
   152  		storages[i].cluster = self
   153  		ret = append(ret, &storages[i])
   154  	}
   155  	return ret, nil
   156  }
   157  
   158  func (self *SRegion) getStorages() ([]SStorage, error) {
   159  	storages := []SStorage{}
   160  	part, nextToken, err := self.GetStorages("")
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	storages = append(storages, part...)
   165  	for len(nextToken) > 0 {
   166  		part, nextToken, err = self.GetStorages(nextToken)
   167  		if err != nil {
   168  			return nil, err
   169  		}
   170  		storages = append(storages, part...)
   171  	}
   172  	return storages, nil
   173  }