yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/remotefile/region.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  	"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 SRegion struct {
    28  	SResourceBase
    29  	multicloud.SRegion
    30  	multicloud.SRegionSecurityGroupBase
    31  	multicloud.SRegionOssBase
    32  	multicloud.SRegionLbBase
    33  
    34  	client *SRemoteFileClient
    35  }
    36  
    37  func (self *SRegion) GetGlobalId() string {
    38  	return fmt.Sprintf("%s%s", self.client.GetCloudRegionExternalIdPrefix(), self.Id)
    39  }
    40  
    41  func (self *SRegion) GetProvider() string {
    42  	return self.client.cpcfg.Vendor
    43  }
    44  
    45  func (self *SRegion) GetCloudEnv() string {
    46  	return ""
    47  }
    48  
    49  func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
    50  	return cloudprovider.SGeographicInfo{}
    51  }
    52  
    53  func (self *SRegion) GetStatus() string {
    54  	return api.CLOUD_REGION_STATUS_INSERVER
    55  }
    56  
    57  func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
    58  	zones, err := self.client.GetZones()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	ret := []cloudprovider.ICloudZone{}
    63  	for i := range zones {
    64  		if zones[i].RegionId != self.GetId() {
    65  			continue
    66  		}
    67  		zones[i].region = self
    68  		ret = append(ret, &zones[i])
    69  	}
    70  	return ret, nil
    71  }
    72  
    73  func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
    74  	zones, err := self.client.GetZones()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	for i := 0; i < len(zones); i += 1 {
    79  		if zones[i].RegionId != self.GetId() {
    80  			continue
    81  		}
    82  		if zones[i].GetGlobalId() == id {
    83  			zones[i].region = self
    84  			return &zones[i], nil
    85  		}
    86  	}
    87  	return nil, cloudprovider.ErrNotFound
    88  }
    89  
    90  func (region *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
    91  	instances, err := region.client.GetInstances()
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	instance := SInstance{}
    96  	for _, v := range instances {
    97  		if v.Id == id {
    98  			return &instance, nil
    99  		}
   100  	}
   101  	return nil, errors.Wrapf(errors.ErrNotFound, "GetIVMById:%s", id)
   102  }
   103  
   104  func (self *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
   105  	return nil, cloudprovider.ErrNotImplemented
   106  }
   107  
   108  func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
   109  	vpcs, err := self.client.GetVpcs()
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	ret := []cloudprovider.ICloudVpc{}
   114  	for i := range vpcs {
   115  		if vpcs[i].RegionId != self.GetId() {
   116  			continue
   117  		}
   118  		vpcs[i].region = self
   119  		ret = append(ret, &vpcs[i])
   120  	}
   121  	return ret, nil
   122  }
   123  
   124  func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
   125  	ivpcs, err := self.GetIVpcs()
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	for i := 0; i < len(ivpcs); i += 1 {
   130  		if ivpcs[i].GetGlobalId() == id {
   131  			return ivpcs[i], nil
   132  		}
   133  	}
   134  	return nil, cloudprovider.ErrNotFound
   135  }
   136  
   137  func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   138  	return nil, cloudprovider.ErrNotImplemented
   139  }
   140  
   141  func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   142  	return nil, cloudprovider.ErrNotImplemented
   143  }
   144  
   145  func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   146  	return nil, cloudprovider.ErrNotImplemented
   147  }
   148  
   149  func (self *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   150  	return nil, cloudprovider.ErrNotImplemented
   151  }
   152  
   153  func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
   154  	eips, err := self.client.GetEips()
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	ret := []cloudprovider.ICloudEIP{}
   159  	for i := range eips {
   160  		if eips[i].RegionId != self.GetId() {
   161  			continue
   162  		}
   163  		ret = append(ret, &eips[i])
   164  	}
   165  	return ret, nil
   166  }
   167  
   168  func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
   169  	eips, err := self.GetIEips()
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	for i := range eips {
   174  		if eips[i].GetGlobalId() == id {
   175  			return eips[i], nil
   176  		}
   177  	}
   178  	return nil, cloudprovider.ErrNotFound
   179  }
   180  
   181  func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
   182  	return nil, cloudprovider.ErrNotSupported
   183  }
   184  
   185  func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
   186  	secgroups, err := self.client.GetSecgroups()
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	for i := range secgroups {
   191  		if secgroups[i].GetGlobalId() == secgroupId {
   192  			return &secgroups[i], nil
   193  		}
   194  	}
   195  	return nil, cloudprovider.ErrNotFound
   196  }
   197  
   198  func (self *SRegion) GetISecurityGroupByName(opts *cloudprovider.SecurityGroupFilterOptions) (cloudprovider.ICloudSecurityGroup, error) {
   199  	secgroups, err := self.client.GetSecgroups()
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  	for i := range secgroups {
   204  		if secgroups[i].GetName() == opts.Name {
   205  			return &secgroups[i], nil
   206  		}
   207  	}
   208  	return nil, cloudprovider.ErrNotFound
   209  }
   210  
   211  func (self *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
   212  	lbs, err := self.client.GetLoadbalancers()
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  	ret := []cloudprovider.ICloudLoadbalancer{}
   217  	for i := range lbs {
   218  		if lbs[i].RegionId != self.GetId() {
   219  			continue
   220  		}
   221  		lbs[i].region = self
   222  		ret = append(ret, &lbs[i])
   223  	}
   224  	return ret, nil
   225  }
   226  
   227  func (self *SRegion) GetILoadBalancerById(id string) (cloudprovider.ICloudLoadbalancer, error) {
   228  	lbs, err := self.GetILoadBalancers()
   229  	if err != nil {
   230  		return nil, err
   231  	}
   232  	for i := range lbs {
   233  		if lbs[i].GetGlobalId() == id {
   234  			return lbs[i], nil
   235  		}
   236  	}
   237  	return nil, cloudprovider.ErrNotFound
   238  }
   239  
   240  func (self *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalancer) (cloudprovider.ICloudLoadbalancer, error) {
   241  	return nil, cloudprovider.ErrNotSupported
   242  }
   243  
   244  func (self *SRegion) CreateILoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (cloudprovider.ICloudLoadbalancerAcl, error) {
   245  	return nil, cloudprovider.ErrNotSupported
   246  }
   247  
   248  func (self *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) {
   249  	return nil, cloudprovider.ErrNotSupported
   250  }
   251  
   252  func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
   253  	return nil, cloudprovider.ErrNotSupported
   254  }
   255  
   256  func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
   257  	return nil, cloudprovider.ErrNotSupported
   258  }
   259  
   260  func (self *SRegion) GetIBuckets() ([]cloudprovider.ICloudBucket, error) {
   261  	buckets, err := self.client.GetBuckets()
   262  	if err != nil {
   263  		return nil, err
   264  	}
   265  	ret := []cloudprovider.ICloudBucket{}
   266  	for i := range buckets {
   267  		if buckets[i].RegionId != self.GetId() {
   268  			continue
   269  		}
   270  		buckets[i].region = self
   271  		ret = append(ret, &buckets[i])
   272  	}
   273  	return ret, nil
   274  }
   275  
   276  func (self *SRegion) GetIBucketById(name string) (cloudprovider.ICloudBucket, error) {
   277  	buckets, err := self.GetIBuckets()
   278  	if err != nil {
   279  		return nil, err
   280  	}
   281  	for i := range buckets {
   282  		if buckets[i].GetName() == name {
   283  			return buckets[i], nil
   284  		}
   285  	}
   286  	return nil, cloudprovider.ErrNotFound
   287  }
   288  
   289  func (self *SRegion) GetIBucketByName(name string) (cloudprovider.ICloudBucket, error) {
   290  	buckets, err := self.GetIBuckets()
   291  	if err != nil {
   292  		return nil, err
   293  	}
   294  	for i := range buckets {
   295  		if buckets[i].GetGlobalId() == name {
   296  			return buckets[i], nil
   297  		}
   298  	}
   299  	return nil, cloudprovider.ErrNotFound
   300  }
   301  
   302  func (self *SRegion) CreateIBucket(name string, storageClassStr string, acl string) error {
   303  	return cloudprovider.ErrNotSupported
   304  }
   305  
   306  func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) {
   307  	return nil, cloudprovider.ErrNotImplemented
   308  }
   309  
   310  func (self *SRegion) GetIMiscResources() ([]cloudprovider.ICloudMiscResource, error) {
   311  	misc, err := self.client.GetMisc()
   312  	if err != nil {
   313  		return nil, err
   314  	}
   315  	ret := []cloudprovider.ICloudMiscResource{}
   316  	for i := range misc {
   317  		ret = append(ret, &misc[i])
   318  	}
   319  	return ret, nil
   320  }
   321  
   322  func (self *SRegion) GetCapabilities() []string {
   323  	return self.client.GetCapabilities()
   324  }