yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs
    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  type SZone struct {
    28  	multicloud.SResourceBase
    29  	HcsTags
    30  
    31  	region    *SRegion
    32  	ZoneName  string
    33  	ZoneState struct {
    34  		Available bool
    35  	}
    36  }
    37  
    38  func (self *SRegion) GetZones() ([]SZone, error) {
    39  	zones := []SZone{}
    40  	return zones, self.ecsList("os-availability-zone", nil, &zones)
    41  }
    42  
    43  func (self *SZone) getStorageType() ([]string, error) {
    44  	return self.region.GetZoneSupportedDiskTypes(self.GetId())
    45  }
    46  
    47  func (self *SRegion) GetZoneSupportedDiskTypes(zoneId string) ([]string, error) {
    48  	dts, err := self.GetDiskTypes()
    49  	if err != nil {
    50  		return nil, errors.Wrap(err, "GetDiskTypes")
    51  	}
    52  
    53  	ret := []string{}
    54  	for i := range dts {
    55  		if dts[i].IsAvaliableInZone(zoneId) {
    56  			ret = append(ret, dts[i].Name)
    57  		}
    58  	}
    59  	return ret, nil
    60  }
    61  
    62  func (self *SZone) GetId() string {
    63  	return self.ZoneName
    64  }
    65  
    66  func (self *SZone) GetName() string {
    67  	return fmt.Sprintf("%s %s", CLOUD_PROVIDER_HCS_CN, self.ZoneName)
    68  }
    69  
    70  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
    71  	en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_HCS_EN, self.ZoneName)
    72  	table := cloudprovider.SModelI18nTable{}
    73  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
    74  	return table
    75  }
    76  
    77  func (self *SZone) GetGlobalId() string {
    78  	return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.ZoneName)
    79  }
    80  
    81  func (self *SZone) GetStatus() string {
    82  	return api.ZONE_ENABLE
    83  }
    84  
    85  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
    86  	return self.region
    87  }
    88  
    89  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
    90  	host := &SHost{zone: self}
    91  	return []cloudprovider.ICloudHost{host}, nil
    92  }
    93  
    94  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
    95  	host := &SHost{zone: self}
    96  	if host.GetGlobalId() == id {
    97  		return host, nil
    98  	}
    99  	return nil, cloudprovider.ErrNotFound
   100  }
   101  
   102  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   103  	storages, err := self.getStorageType()
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	ret := []cloudprovider.ICloudStorage{}
   108  	for i := range storages {
   109  		ret = append(ret, &SStorage{zone: self, storageType: storages[i]})
   110  	}
   111  	return ret, nil
   112  }
   113  
   114  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   115  	storages, err := self.GetIStorages()
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	for i := 0; i < len(storages); i += 1 {
   120  		if storages[i].GetGlobalId() == id {
   121  			return storages[i], nil
   122  		}
   123  	}
   124  	return nil, cloudprovider.ErrNotFound
   125  }
   126  
   127  func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
   128  	vpcs, err := self.region.GetVpcs()
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	ret := []cloudprovider.ICloudWire{}
   133  	for i := range vpcs {
   134  		wire := &SWire{region: self.region, vpc: &vpcs[i]}
   135  		ret = append(ret, wire)
   136  	}
   137  	return ret, nil
   138  }
   139  
   140  func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
   141  	zones, err := self.GetZones()
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	ret := []cloudprovider.ICloudZone{}
   146  	for i := 0; i < len(zones); i++ {
   147  		zones[i].region = self
   148  		ret = append(ret, &zones[i])
   149  	}
   150  	if len(zones) == 0 {
   151  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, "no availabe zone found")
   152  	}
   153  	return ret, nil
   154  }
   155  
   156  func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
   157  	izones, err := self.GetIZones()
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	for i := 0; i < len(izones); i += 1 {
   162  		if izones[i].GetGlobalId() == id {
   163  			return izones[i], nil
   164  		}
   165  	}
   166  	return nil, cloudprovider.ErrNotFound
   167  }
   168  
   169  func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   170  	izones, err := self.GetIZones()
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	for i := 0; i < len(izones); i += 1 {
   175  		ihost, err := izones[i].GetIHostById(id)
   176  		if err == nil {
   177  			return ihost, nil
   178  		} else if errors.Cause(err) != cloudprovider.ErrNotFound {
   179  			return nil, err
   180  		}
   181  	}
   182  	return nil, cloudprovider.ErrNotFound
   183  }
   184  
   185  func (self *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   186  	iStores := make([]cloudprovider.ICloudStorage, 0)
   187  
   188  	izones, err := self.GetIZones()
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	for i := 0; i < len(izones); i += 1 {
   193  		iZoneStores, err := izones[i].GetIStorages()
   194  		if err != nil {
   195  			return nil, err
   196  		}
   197  		iStores = append(iStores, iZoneStores...)
   198  	}
   199  	return iStores, nil
   200  }
   201  
   202  func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   203  	izones, err := self.GetIZones()
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  	for i := 0; i < len(izones); i += 1 {
   208  		istore, err := izones[i].GetIStorageById(id)
   209  		if err == nil {
   210  			return istore, nil
   211  		} else if errors.Cause(err) != cloudprovider.ErrNotFound {
   212  			return nil, err
   213  		}
   214  	}
   215  	return nil, cloudprovider.ErrNotFound
   216  }
   217  
   218  func (self *SRegion) GetZoneById(id string) (*SZone, error) {
   219  	zones, err := self.GetZones()
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  	for i := 0; i < len(zones); i += 1 {
   224  		zones[i].region = self
   225  		if zones[i].GetId() == id || zones[i].GetGlobalId() == id {
   226  			return &zones[i], nil
   227  		}
   228  	}
   229  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   230  }