yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/wire.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 bingocloud
    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 SWire struct {
    28  	multicloud.STagBase
    29  	multicloud.SResourceBase
    30  
    31  	cluster *SCluster
    32  	vpc     *SVpc
    33  }
    34  
    35  func (self *SWire) GetId() string {
    36  	return fmt.Sprintf("%s/%s", self.vpc.GetGlobalId(), self.cluster.GetGlobalId())
    37  }
    38  
    39  func (self *SWire) GetGlobalId() string {
    40  	return self.GetId()
    41  }
    42  
    43  func (self *SWire) GetName() string {
    44  	return fmt.Sprintf("%s-%s", self.vpc.GetName(), self.cluster.GetName())
    45  }
    46  
    47  func (self *SWire) GetBandwidth() int {
    48  	return 1000
    49  }
    50  
    51  func (self *SWire) GetStatus() string {
    52  	return api.WIRE_STATUS_AVAILABLE
    53  }
    54  
    55  func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
    56  	return self.vpc
    57  }
    58  
    59  func (self *SWire) IsEmulated() bool {
    60  	return true
    61  }
    62  
    63  func (self *SWire) GetIZone() cloudprovider.ICloudZone {
    64  	return self.cluster
    65  }
    66  
    67  func (self *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) {
    68  	wires, err := self.GetIWires()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	for i := range wires {
    73  		if wires[i].GetGlobalId() == id {
    74  			return wires[i], nil
    75  		}
    76  	}
    77  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
    78  }
    79  
    80  func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
    81  	clusters, err := self.region.GetClusters()
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	ret := []cloudprovider.ICloudWire{}
    86  	for i := range clusters {
    87  		wire := &SWire{
    88  			vpc:     self,
    89  			cluster: &clusters[i],
    90  		}
    91  		ret = append(ret, wire)
    92  	}
    93  	return ret, nil
    94  }