yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/remotefile/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 remotefile
    16  
    17  import (
    18  	"yunion.io/x/pkg/errors"
    19  
    20  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    21  )
    22  
    23  type SZone struct {
    24  	SResourceBase
    25  	region *SRegion
    26  
    27  	RegionId string
    28  }
    29  
    30  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
    31  	return self.region
    32  }
    33  
    34  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
    35  	hosts, err := self.region.client.GetHosts()
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	ret := []cloudprovider.ICloudHost{}
    40  	for i := range hosts {
    41  		if hosts[i].ZoneId != self.GetId() {
    42  			continue
    43  		}
    44  		hosts[i].zone = self
    45  		ret = append(ret, &hosts[i])
    46  	}
    47  	return ret, nil
    48  }
    49  
    50  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
    51  	hosts, err := self.GetIHosts()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	for i := range hosts {
    56  		if hosts[i].GetGlobalId() == id {
    57  			return hosts[i], nil
    58  		}
    59  	}
    60  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
    61  }
    62  
    63  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
    64  	storages, err := self.region.client.GetStorages()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	ret := []cloudprovider.ICloudStorage{}
    69  	for i := range storages {
    70  		if storages[i].ZoneId != self.GetId() {
    71  			continue
    72  		}
    73  		storages[i].zone = self
    74  		ret = append(ret, &storages[i])
    75  	}
    76  	return ret, nil
    77  }
    78  
    79  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
    80  	storages, err := self.GetIStorages()
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	for i := range storages {
    85  		if storages[i].GetGlobalId() == id {
    86  			return storages[i], nil
    87  		}
    88  	}
    89  	return nil, cloudprovider.ErrNotFound
    90  }