yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/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 ctyun
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  	"yunion.io/x/pkg/util/netutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SWire struct {
    29  	multicloud.SResourceBase
    30  	CtyunTags
    31  	region *SRegion
    32  	vpc    *SVpc
    33  
    34  	inetworks []cloudprovider.ICloudNetwork
    35  }
    36  
    37  func (self *SWire) GetId() string {
    38  	return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.region.GetId())
    39  }
    40  
    41  func (self *SWire) GetName() string {
    42  	return self.GetId()
    43  }
    44  
    45  func (self *SWire) GetGlobalId() string {
    46  	return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.region.GetGlobalId())
    47  }
    48  
    49  func (self *SWire) GetStatus() string {
    50  	return api.WIRE_STATUS_AVAILABLE
    51  }
    52  
    53  func (self *SWire) Refresh() error {
    54  	return nil
    55  }
    56  
    57  func (self *SWire) IsEmulated() bool {
    58  	return true
    59  }
    60  
    61  //  http://ctyun-api-url/apiproxy/v3/queryVPCDetail
    62  func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
    63  	return self.vpc
    64  }
    65  
    66  func (self *SWire) GetIZone() cloudprovider.ICloudZone {
    67  	return nil
    68  }
    69  
    70  // http://ctyun-api-url/apiproxy/v3/getSubnets
    71  func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    72  	if self.inetworks == nil {
    73  		err := self.vpc.fetchNetworks()
    74  		if err != nil {
    75  			return nil, err
    76  		}
    77  	}
    78  	return self.inetworks, nil
    79  }
    80  
    81  func (self *SWire) GetBandwidth() int {
    82  	return 10000
    83  }
    84  
    85  func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    86  	networks, err := self.GetINetworks()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	for i := 0; i < len(networks); i += 1 {
    91  		if networks[i].GetGlobalId() == netid {
    92  			return networks[i], nil
    93  		}
    94  	}
    95  	return nil, cloudprovider.ErrNotFound
    96  }
    97  
    98  func getDefaultGateWay(cidr string) (string, error) {
    99  	pref, err := netutils.NewIPV4Prefix(cidr)
   100  	if err != nil {
   101  		return "", errors.Wrap(err, "getDefaultGateWay.NewIPV4Prefix")
   102  	}
   103  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   104  	startIp = startIp.StepUp()                    // 1
   105  	return startIp.String(), nil
   106  }
   107  
   108  func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
   109  	var ZoneId string
   110  	if len(self.inetworks) > 0 {
   111  		ZoneId = self.inetworks[0].(*SNetwork).ZoneID
   112  	} else {
   113  		if len(self.region.izones) > 0 {
   114  			ZoneId = self.region.izones[0].GetId()
   115  		} else {
   116  			return nil, fmt.Errorf("SWire.CreateINetwork region %s zone is empty", self.region.GetName())
   117  		}
   118  	}
   119  	network, err := self.region.CreateNetwork(self.vpc.GetId(), ZoneId, opts.Name, opts.Cidr, "true")
   120  	if err != nil {
   121  		return nil, errors.Wrap(err, "SWire.CreateINetwork.CreateNetwork")
   122  	}
   123  
   124  	return network, nil
   125  }
   126  
   127  func (self *SWire) addNetwork(network *SNetwork) {
   128  	if self.inetworks == nil {
   129  		self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
   130  	}
   131  	find := false
   132  	for i := 0; i < len(self.inetworks); i += 1 {
   133  		if self.inetworks[i].GetId() == network.GetId() {
   134  			find = true
   135  			break
   136  		}
   137  	}
   138  	if !find {
   139  		self.inetworks = append(self.inetworks, network)
   140  	}
   141  }