yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SStorage struct {
    28  	zone *SZone
    29  	SResourceBase
    30  	GoogleTags
    31  
    32  	CreationTimestamp time.Time
    33  	Description       string
    34  	ValidDiskSize     string
    35  	Zone              string
    36  	DefaultDiskSizeGb string
    37  	Kind              string
    38  }
    39  
    40  func (region *SRegion) GetStorages(zone string, maxResults int, pageToken string) ([]SStorage, error) {
    41  	storages := []SStorage{}
    42  	if len(zone) == 0 {
    43  		return nil, fmt.Errorf("zone params can not be empty")
    44  	}
    45  	resource := fmt.Sprintf("zones/%s/diskTypes", zone)
    46  	params := map[string]string{}
    47  	return storages, region.List(resource, params, maxResults, pageToken, &storages)
    48  }
    49  
    50  func (region *SRegion) GetStorage(id string) (*SStorage, error) {
    51  	storage := &SStorage{}
    52  	return storage, region.GetBySelfId(id, storage)
    53  }
    54  
    55  func (storage *SStorage) GetName() string {
    56  	return storage.Description
    57  }
    58  
    59  func (storage *SStorage) GetStatus() string {
    60  	return api.STORAGE_ONLINE
    61  }
    62  
    63  func (storage *SStorage) IsEmulated() bool {
    64  	return true
    65  }
    66  
    67  func (self *SStorage) GetCreatedAt() time.Time {
    68  	return time.Time{}
    69  }
    70  
    71  func (storage *SStorage) Refresh() error {
    72  	_storage, err := storage.zone.region.GetStorage(storage.SelfLink)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	return jsonutils.Update(storage, _storage)
    77  }
    78  
    79  func (storage *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    80  	return storage.zone.region.getStoragecache()
    81  }
    82  
    83  func (storage *SStorage) GetIZone() cloudprovider.ICloudZone {
    84  	return storage.zone
    85  }
    86  
    87  func (storage *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    88  	disks, err := storage.zone.region.GetDisks(storage.zone.Name, storage.Name, 0, "")
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	idisks := []cloudprovider.ICloudDisk{}
    93  	for i := range disks {
    94  		disks[i].storage = storage
    95  		idisks = append(idisks, &disks[i])
    96  	}
    97  	return idisks, nil
    98  }
    99  
   100  func (storage *SStorage) GetStorageType() string {
   101  	return storage.Name
   102  }
   103  
   104  func (storage *SStorage) GetMediumType() string {
   105  	return api.DISK_TYPE_SSD
   106  }
   107  
   108  func (storage *SStorage) GetCapacityMB() int64 {
   109  	return 0
   110  }
   111  
   112  func (self *SStorage) GetCapacityUsedMB() int64 {
   113  	return 0
   114  }
   115  
   116  func (storage *SStorage) GetStorageConf() jsonutils.JSONObject {
   117  	return jsonutils.Marshal(map[string]string{
   118  		"ValidDiskSize":     storage.ValidDiskSize,
   119  		"DefaultDiskSizeGb": storage.DefaultDiskSizeGb,
   120  	})
   121  }
   122  
   123  func (storage *SStorage) GetEnabled() bool {
   124  	return true
   125  }
   126  
   127  func (storage *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
   128  	disk, err := storage.zone.region.GetDisk(id)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	disk.storage = storage
   133  	return disk, nil
   134  }
   135  
   136  func (storage *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   137  	disk, err := storage.zone.region.CreateDisk(conf.Name, conf.SizeGb, storage.zone.Name, storage.Name, "", conf.Desc)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	disk.storage = storage
   142  	return disk, nil
   143  }
   144  
   145  func (storage *SStorage) GetMountPoint() string {
   146  	return ""
   147  }
   148  
   149  func (storage *SStorage) IsSysDiskStore() bool {
   150  	return storage.Name != api.STORAGE_GOOGLE_LOCAL_SSD
   151  }
   152  
   153  func (storage *SStorage) DisableSync() bool {
   154  	return false
   155  }