yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/zone.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 ucloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  // https://docs.ucloud.cn/api/udisk-api/create_udisk
    28  // UDisk 类型: DataDisk(普通数据盘),SSDDataDisk(SSD数据盘),默认值(DataDisk)
    29  var StorageTypes = []string{
    30  	api.STORAGE_UCLOUD_CLOUD_NORMAL,
    31  	api.STORAGE_UCLOUD_CLOUD_SSD,
    32  	api.STORAGE_UCLOUD_LOCAL_NORMAL, // 本地盘
    33  	api.STORAGE_UCLOUD_LOCAL_SSD,    // 本地SSD盘
    34  }
    35  
    36  type SZone struct {
    37  	multicloud.SResourceBase
    38  	UcloudTags
    39  	region *SRegion
    40  	host   *SHost
    41  
    42  	iwires    []cloudprovider.ICloudWire
    43  	istorages []cloudprovider.ICloudStorage
    44  
    45  	RegionId string
    46  	ZoneId   string
    47  
    48  	/* 支持的磁盘种类集合 */
    49  	storageTypes []string
    50  }
    51  
    52  func (self *SZone) addWire(wire *SWire) {
    53  	if self.iwires == nil {
    54  		self.iwires = make([]cloudprovider.ICloudWire, 0)
    55  	}
    56  	self.iwires = append(self.iwires, wire)
    57  }
    58  
    59  func (self *SZone) getHost() *SHost {
    60  	if self.host == nil {
    61  		self.host = &SHost{zone: self, projectId: self.region.client.projectId}
    62  	}
    63  	return self.host
    64  }
    65  
    66  func (self *SZone) getStorageType() {
    67  	if len(self.storageTypes) == 0 {
    68  		self.storageTypes = StorageTypes
    69  	}
    70  }
    71  
    72  func (self *SZone) fetchStorages() error {
    73  	self.getStorageType()
    74  	self.istorages = make([]cloudprovider.ICloudStorage, len(self.storageTypes))
    75  
    76  	for i, sc := range self.storageTypes {
    77  		storage := SStorage{zone: self, storageType: sc}
    78  		self.istorages[i] = &storage
    79  	}
    80  	return nil
    81  }
    82  
    83  func (self *SZone) GetId() string {
    84  	return self.ZoneId
    85  }
    86  
    87  func (self *SZone) GetName() string {
    88  	if name, exists := UCLOUD_ZONE_NAMES[self.GetId()]; exists {
    89  		return name
    90  	}
    91  
    92  	return self.GetId()
    93  }
    94  
    95  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
    96  	var en string
    97  	if name, exists := UCLOUD_ZONE_NAMES_EN[self.GetId()]; exists {
    98  		en = name
    99  	} else {
   100  		en = self.GetId()
   101  	}
   102  
   103  	table := cloudprovider.SModelI18nTable{}
   104  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
   105  	return table
   106  }
   107  
   108  func (self *SZone) GetGlobalId() string {
   109  	return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.GetId())
   110  }
   111  
   112  func (self *SZone) GetStatus() string {
   113  	return api.ZONE_ENABLE
   114  }
   115  
   116  func (self *SZone) Refresh() error {
   117  	return nil
   118  }
   119  
   120  func (self *SZone) IsEmulated() bool {
   121  	return false
   122  }
   123  
   124  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
   125  	return self.region
   126  }
   127  
   128  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   129  	return []cloudprovider.ICloudHost{self.getHost()}, nil
   130  }
   131  
   132  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   133  	host := self.getHost()
   134  	if host.GetGlobalId() == id {
   135  		return host, nil
   136  	}
   137  	return nil, cloudprovider.ErrNotFound
   138  }
   139  
   140  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   141  	if self.istorages == nil {
   142  		err := self.fetchStorages()
   143  		if err != nil {
   144  			return nil, errors.Wrapf(err, "fetchStorages")
   145  		}
   146  	}
   147  	return self.istorages, nil
   148  }
   149  
   150  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   151  	if self.istorages == nil {
   152  		err := self.fetchStorages()
   153  		if err != nil {
   154  			return nil, errors.Wrapf(err, "fetchStorages")
   155  		}
   156  	}
   157  	for i := 0; i < len(self.istorages); i += 1 {
   158  		if self.istorages[i].GetGlobalId() == id {
   159  			return self.istorages[i], nil
   160  		}
   161  	}
   162  	return nil, cloudprovider.ErrNotFound
   163  }
   164  
   165  // https://docs.ucloud.cn/api/uhost-api/describe_uhost_instance
   166  func (self *SZone) GetInstances() ([]SInstance, error) {
   167  	return self.region.GetInstances(self.GetId(), "")
   168  }
   169  
   170  func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
   171  	return self.iwires, nil
   172  }
   173  
   174  // https://docs.ucloud.cn/api/uhost-api/describe_uhost_instance
   175  func (self *SRegion) GetInstances(zoneId string, instanceId string) ([]SInstance, error) {
   176  	instances := make([]SInstance, 0)
   177  
   178  	params := NewUcloudParams()
   179  	if len(zoneId) > 0 {
   180  		params.Set("Zone", zoneId)
   181  	}
   182  
   183  	if len(instanceId) > 0 {
   184  		params.Set("UHostIds.0", instanceId)
   185  	}
   186  
   187  	err := self.DoListAll("DescribeUHostInstance", params, &instances)
   188  	return instances, err
   189  }
   190  
   191  func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
   192  	storages, err := self.GetIStorages()
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  	for i := 0; i < len(storages); i += 1 {
   197  		storage := storages[i].(*SStorage)
   198  		if storage.storageType == category {
   199  			return storage, nil
   200  		}
   201  	}
   202  	return nil, fmt.Errorf("No such storage %s", category)
   203  }