| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package remotefile
- import (
- "fmt"
- "yunion.io/x/pkg/errors"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- )
- type SRegion struct {
- SResourceBase
- multicloud.SRegion
- multicloud.SRegionSecurityGroupBase
- multicloud.SRegionOssBase
- multicloud.SRegionLbBase
- client *SRemoteFileClient
- }
- func (self *SRegion) GetGlobalId() string {
- return fmt.Sprintf("%s/%s", self.client.GetCloudRegionExternalIdPrefix(), self.Id)
- }
- func (self *SRegion) GetProvider() string {
- return self.client.cpcfg.Vendor
- }
- func (self *SRegion) GetCloudEnv() string {
- return ""
- }
- func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
- return cloudprovider.SGeographicInfo{}
- }
- func (self *SRegion) GetStatus() string {
- return api.CLOUD_REGION_STATUS_INSERVER
- }
- func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
- zones, err := self.client.GetZones()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudZone{}
- for i := range zones {
- if zones[i].RegionId != self.GetId() {
- continue
- }
- zones[i].region = self
- ret = append(ret, &zones[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
- zones, err := self.client.GetZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(zones); i += 1 {
- if zones[i].RegionId != self.GetId() {
- continue
- }
- if zones[i].GetGlobalId() == id {
- zones[i].region = self
- return &zones[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (region *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
- instances, err := region.client.GetInstances()
- if err != nil {
- return nil, err
- }
- instance := SInstance{}
- for _, v := range instances {
- if v.Id == id {
- return &instance, nil
- }
- }
- return nil, errors.Wrapf(errors.ErrNotFound, "GetIVMById:%s", id)
- }
- func (self *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
- storages, err := self.GetIStorages()
- if err != nil {
- return nil, err
- }
- for i := range storages {
- disk, err := storages[i].GetIDiskById(id)
- if err == nil {
- return disk, nil
- }
- if errors.Cause(err) != cloudprovider.ErrNotFound {
- return nil, err
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
- vpcs, err := self.client.GetVpcs()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudVpc{}
- for i := range vpcs {
- if vpcs[i].RegionId != self.GetId() {
- continue
- }
- vpcs[i].region = self
- ret = append(ret, &vpcs[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
- ivpcs, err := self.GetIVpcs()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(ivpcs); i += 1 {
- if ivpcs[i].GetGlobalId() == id {
- return ivpcs[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
- hosts, err := self.GetIHosts()
- if err != nil {
- return nil, err
- }
- for i := range hosts {
- if hosts[i].GetGlobalId() == id {
- return hosts[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
- storages, err := self.GetIStorages()
- if err != nil {
- return nil, err
- }
- for i := range storages {
- if storages[i].GetGlobalId() == id {
- return storages[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
- zones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudHost{}
- for i := range zones {
- hosts, err := zones[i].GetIHosts()
- if err != nil {
- return nil, err
- }
- ret = append(ret, hosts...)
- }
- return ret, nil
- }
- func (self *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
- zones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudStorage{}
- for i := range zones {
- storages, err := zones[i].GetIStorages()
- if err != nil {
- return nil, err
- }
- ret = append(ret, storages...)
- }
- return ret, nil
- }
- func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
- eips, err := self.client.GetEips()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudEIP{}
- for i := range eips {
- if eips[i].RegionId != self.GetId() {
- continue
- }
- ret = append(ret, &eips[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
- eips, err := self.GetIEips()
- if err != nil {
- return nil, err
- }
- for i := range eips {
- if eips[i].GetGlobalId() == id {
- return eips[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
- secgroups, err := self.client.GetSecgroups()
- if err != nil {
- return nil, err
- }
- for i := range secgroups {
- if secgroups[i].GetGlobalId() == secgroupId {
- return &secgroups[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
- lbs, err := self.client.GetLoadbalancers()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudLoadbalancer{}
- for i := range lbs {
- if lbs[i].RegionId != self.GetId() {
- continue
- }
- lbs[i].region = self
- ret = append(ret, &lbs[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetILoadBalancerById(id string) (cloudprovider.ICloudLoadbalancer, error) {
- lbs, err := self.GetILoadBalancers()
- if err != nil {
- return nil, err
- }
- for i := range lbs {
- if lbs[i].GetGlobalId() == id {
- return lbs[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalancerCreateOptions) (cloudprovider.ICloudLoadbalancer, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) CreateILoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (cloudprovider.ICloudLoadbalancerAcl, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SRegion) GetIBuckets() ([]cloudprovider.ICloudBucket, error) {
- buckets, err := self.client.GetBuckets()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudBucket{}
- for i := range buckets {
- if buckets[i].RegionId != self.GetId() {
- continue
- }
- buckets[i].region = self
- ret = append(ret, &buckets[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetIBucketById(id string) (cloudprovider.ICloudBucket, error) {
- buckets, err := self.GetIBuckets()
- if err != nil {
- return nil, err
- }
- for i := range buckets {
- if buckets[i].GetId() == id {
- return buckets[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIBucketByName(name string) (cloudprovider.ICloudBucket, error) {
- buckets, err := self.GetIBuckets()
- if err != nil {
- return nil, err
- }
- for i := range buckets {
- if buckets[i].GetGlobalId() == name {
- return buckets[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) CreateIBucket(name string, storageClassStr string, acl string) error {
- return cloudprovider.ErrNotSupported
- }
- func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) {
- return nil, cloudprovider.ErrNotImplemented
- }
- func (self *SRegion) GetIMiscResources() ([]cloudprovider.ICloudMiscResource, error) {
- misc, err := self.client.GetMisc()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudMiscResource{}
- for i := range misc {
- ret = append(ret, &misc[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetCapabilities() []string {
- return self.client.GetCapabilities()
- }
- func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
- vms, err := region.client.GetInstances()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudVM{}
- for i := range vms {
- ret = append(ret, &vms[i])
- }
- return ret, nil
- }
|