yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/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 ctyun
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SZone struct {
    27  	multicloud.SResourceBase
    28  	CtyunTags
    29  	region *SRegion
    30  	host   *SHost
    31  
    32  	iwires    []cloudprovider.ICloudWire
    33  	istorages []cloudprovider.ICloudStorage
    34  	/* 支持的磁盘种类集合 */
    35  	storageTypes []string
    36  
    37  	RegionID string `json:"regionId"`
    38  	ZoneID   string `json:"zoneId"`
    39  	ZoneName string `json:"zoneName"`
    40  	ZoneType string `json:"zoneType"`
    41  }
    42  
    43  func getZoneGlobalId(region *SRegion, zoneId string) string {
    44  	return fmt.Sprintf("%s/%s", region.GetGlobalId(), zoneId)
    45  }
    46  
    47  func (self *SZone) addWire(wire *SWire) {
    48  	if self.iwires == nil {
    49  		self.iwires = make([]cloudprovider.ICloudWire, 0)
    50  	}
    51  	self.iwires = append(self.iwires, wire)
    52  }
    53  
    54  func (self *SZone) GetId() string {
    55  	return self.ZoneID
    56  }
    57  
    58  func (self *SZone) GetName() string {
    59  	return fmt.Sprintf("%s %s", CLOUD_PROVIDER_CTYUN_CN, self.ZoneID)
    60  }
    61  
    62  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
    63  	table := cloudprovider.SModelI18nTable{}
    64  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(self.ZoneID)
    65  	return table
    66  }
    67  
    68  func (self *SZone) GetGlobalId() string {
    69  	return getZoneGlobalId(self.region, self.GetId())
    70  }
    71  
    72  func (self *SZone) GetStatus() string {
    73  	return "enable"
    74  }
    75  
    76  func (self *SZone) Refresh() error {
    77  	return nil
    78  }
    79  
    80  func (self *SZone) IsEmulated() bool {
    81  	return false
    82  }
    83  
    84  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
    85  	return self.region
    86  }
    87  
    88  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
    89  	return []cloudprovider.ICloudHost{self.getHost()}, nil
    90  }
    91  
    92  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
    93  	host := self.getHost()
    94  	if host.GetGlobalId() == id {
    95  		return host, nil
    96  	}
    97  	return nil, cloudprovider.ErrNotFound
    98  }
    99  
   100  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   101  	if self.istorages == nil {
   102  		err := self.fetchStorages()
   103  		if err != nil {
   104  			return nil, errors.Wrapf(err, "fetchStorages")
   105  		}
   106  	}
   107  	return self.istorages, nil
   108  }
   109  
   110  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   111  	if self.istorages == nil {
   112  		err := self.fetchStorages()
   113  		if err != nil {
   114  			return nil, errors.Wrapf(err, "fetchStorages")
   115  		}
   116  	}
   117  	for i := 0; i < len(self.istorages); i += 1 {
   118  		if self.istorages[i].GetGlobalId() == id {
   119  			return self.istorages[i], nil
   120  		}
   121  	}
   122  	return nil, cloudprovider.ErrNotFound
   123  }
   124  
   125  func (self *SZone) getHost() *SHost {
   126  	if self.host == nil {
   127  		self.host = &SHost{zone: self}
   128  	}
   129  	return self.host
   130  }
   131  
   132  func (self *SZone) fetchStorages() error {
   133  	self.getStorageType()
   134  	self.istorages = make([]cloudprovider.ICloudStorage, len(self.storageTypes))
   135  
   136  	for i, sc := range self.storageTypes {
   137  		storage := SStorage{zone: self, storageType: sc}
   138  		self.istorages[i] = &storage
   139  	}
   140  	return nil
   141  }
   142  
   143  func (self *SZone) getStorageType() {
   144  	if len(self.storageTypes) == 0 {
   145  		self.storageTypes = StorageTypes
   146  	}
   147  }
   148  
   149  func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
   150  	return self.iwires, nil
   151  }