yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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 azure
    16  
    17  import (
    18  	"yunion.io/x/pkg/errors"
    19  
    20  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud"
    23  )
    24  
    25  type SZone struct {
    26  	multicloud.SResourceBase
    27  	AzureTags
    28  	region *SRegion
    29  
    30  	Name string
    31  }
    32  
    33  func (self *SZone) GetId() string {
    34  	return self.region.client.cpcfg.Id
    35  }
    36  
    37  func (self *SZone) GetName() string {
    38  	return self.region.GetName()
    39  }
    40  
    41  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
    42  	return self.region.GetI18n()
    43  }
    44  
    45  func (self *SZone) GetGlobalId() string {
    46  	return self.region.GetGlobalId()
    47  }
    48  
    49  func (self *SZone) IsEmulated() bool {
    50  	return true
    51  }
    52  
    53  func (self *SZone) GetStatus() string {
    54  	return api.ZONE_ENABLE
    55  }
    56  
    57  func (self *SZone) Refresh() error {
    58  	// do nothing
    59  	return nil
    60  }
    61  
    62  func (self *SZone) getHost() *SHost {
    63  	return &SHost{zone: self}
    64  }
    65  
    66  func (self *SZone) getClassicHost() *SClassicHost {
    67  	return &SClassicHost{zone: self}
    68  }
    69  
    70  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
    71  	return self.region
    72  }
    73  
    74  func (self *SZone) ListStorageTypes() []SStorage {
    75  	storages := []SStorage{}
    76  	for _, storageType := range STORAGETYPES {
    77  		storage := SStorage{zone: self, storageType: storageType}
    78  		storages = append(storages, storage)
    79  	}
    80  	return storages
    81  }
    82  
    83  func (self *SRegion) ListClassicStorageTypes() []SClassicStorage {
    84  	storages := []SClassicStorage{}
    85  	for _, storageType := range []string{STORAGE_LRS, STORAGE_GRS} {
    86  		storage := SClassicStorage{AccountType: storageType, region: self}
    87  		storages = append(storages, storage)
    88  	}
    89  	return storages
    90  }
    91  
    92  func (self *SZone) GetIClassicStorages() []cloudprovider.ICloudStorage {
    93  	ret := []cloudprovider.ICloudStorage{}
    94  	storages := self.region.ListClassicStorageTypes()
    95  	for i := range storages {
    96  		ret = append(ret, &storages[i])
    97  	}
    98  	return ret
    99  }
   100  
   101  func (self *SZone) getIStorages() []cloudprovider.ICloudStorage {
   102  	ret := []cloudprovider.ICloudStorage{}
   103  	storages := self.ListStorageTypes()
   104  	for i := range storages {
   105  		storages[i].zone = self
   106  		ret = append(ret, &storages[i])
   107  	}
   108  	return ret
   109  }
   110  
   111  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   112  	ret := self.getIStorages()
   113  	ret = append(ret, self.GetIClassicStorages()...)
   114  	return ret, nil
   115  }
   116  
   117  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   118  	storages, err := self.GetIStorages()
   119  	if err != nil {
   120  		return nil, errors.Wrapf(err, "GetIStorages")
   121  	}
   122  	for i := range storages {
   123  		if storages[i].GetGlobalId() == id {
   124  			return storages[i], nil
   125  		}
   126  	}
   127  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   128  }
   129  
   130  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   131  	hosts, err := self.GetIHosts()
   132  	if err != nil {
   133  		return nil, errors.Wrapf(err, "GetIHosts")
   134  	}
   135  	for i := range hosts {
   136  		if hosts[i].GetGlobalId() == id {
   137  			return hosts[i], nil
   138  		}
   139  	}
   140  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   141  }
   142  
   143  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   144  	return []cloudprovider.ICloudHost{self.getHost(), self.getClassicHost()}, nil
   145  }
   146  
   147  func (self *SZone) GetIClassicWires() ([]cloudprovider.ICloudWire, error) {
   148  	wires := []cloudprovider.ICloudWire{}
   149  	classicVpcs, err := self.region.ListClassicVpcs()
   150  	if err != nil {
   151  		return nil, errors.Wrapf(err, "ListClassicVpcs")
   152  	}
   153  	for i := range classicVpcs {
   154  		classicVpcs[i].region = self.region
   155  		wire := &SClassicWire{vpc: &classicVpcs[i], zone: self}
   156  		wires = append(wires, wire)
   157  	}
   158  	return wires, nil
   159  }
   160  
   161  func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
   162  	vpcs, err := self.region.ListVpcs()
   163  	if err != nil {
   164  		return nil, errors.Wrapf(err, "ListVpcs")
   165  	}
   166  	wires := []cloudprovider.ICloudWire{}
   167  	for i := range vpcs {
   168  		vpcs[i].region = self.region
   169  		wire := &SWire{vpc: &vpcs[i], zone: self}
   170  		wires = append(wires, wire)
   171  	}
   172  	classWires, err := self.GetIClassicWires()
   173  	if err != nil {
   174  		return nil, errors.Wrapf(err, "GetIClassicWires")
   175  	}
   176  	wires = append(wires, classWires...)
   177  	return wires, nil
   178  }