yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud
    16  
    17  import (
    18  	"fmt"
    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  // 子网在整个region可用
    26  type SWire struct {
    27  	multicloud.SResourceBase
    28  	UcloudTags
    29  	region *SRegion
    30  	vpc    *SVPC
    31  
    32  	inetworks []cloudprovider.ICloudNetwork
    33  }
    34  
    35  func (self *SWire) GetId() string {
    36  	return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.region.GetId())
    37  }
    38  
    39  func (self *SWire) GetName() string {
    40  	return self.GetId()
    41  }
    42  
    43  func (self *SWire) GetGlobalId() string {
    44  	return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.region.GetGlobalId())
    45  }
    46  
    47  func (self *SWire) GetStatus() string {
    48  	return api.WIRE_STATUS_AVAILABLE
    49  }
    50  
    51  func (self *SWire) Refresh() error {
    52  	return nil
    53  }
    54  
    55  func (self *SWire) IsEmulated() bool {
    56  	return true
    57  }
    58  
    59  func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
    60  	return self.vpc
    61  }
    62  
    63  func (self *SWire) GetIZone() cloudprovider.ICloudZone {
    64  	return nil
    65  }
    66  
    67  func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    68  	if self.inetworks == nil {
    69  		err := self.vpc.fetchNetworks()
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  	}
    74  	return self.inetworks, nil
    75  }
    76  
    77  func (self *SWire) GetBandwidth() int {
    78  	return 10000
    79  }
    80  
    81  func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    82  	networks, err := self.GetINetworks()
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	for i := 0; i < len(networks); i += 1 {
    87  		if networks[i].GetGlobalId() == netid {
    88  			return networks[i], nil
    89  		}
    90  	}
    91  	return nil, cloudprovider.ErrNotFound
    92  }
    93  
    94  // https://docs.ucloud.cn/api/vpc2.0-api/create_subnet
    95  func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
    96  	return self.region.CreateNetwork(self.vpc.GetId(), opts.Name, opts.Cidr, opts.Desc)
    97  }
    98  
    99  func (self *SWire) addNetwork(network *SNetwork) {
   100  	if self.inetworks == nil {
   101  		self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
   102  	}
   103  	find := false
   104  	for i := 0; i < len(self.inetworks); i += 1 {
   105  		if self.inetworks[i].GetId() == network.GetId() {
   106  			find = true
   107  			break
   108  		}
   109  	}
   110  	if !find {
   111  		self.inetworks = append(self.inetworks, network)
   112  	}
   113  }