yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/log"
    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.SResourceBase
    29  	AliyunTags
    30  	zone *SZone
    31  	vpc  *SVpc
    32  
    33  	inetworks []cloudprovider.ICloudNetwork
    34  }
    35  
    36  func (self *SWire) GetId() string {
    37  	return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.zone.GetId())
    38  }
    39  
    40  func (self *SWire) GetName() string {
    41  	return self.GetId()
    42  }
    43  
    44  func (self *SWire) IsEmulated() bool {
    45  	return true
    46  }
    47  
    48  func (self *SWire) GetStatus() string {
    49  	return api.WIRE_STATUS_AVAILABLE
    50  }
    51  
    52  func (self *SWire) Refresh() error {
    53  	return nil
    54  }
    55  
    56  func (self *SWire) GetGlobalId() string {
    57  	return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.zone.GetGlobalId())
    58  }
    59  
    60  func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
    61  	return self.vpc
    62  }
    63  
    64  func (self *SWire) GetIZone() cloudprovider.ICloudZone {
    65  	return self.zone
    66  }
    67  
    68  func (self *SWire) addNetwork(vswitch *SVSwitch) {
    69  	if self.inetworks == nil {
    70  		self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
    71  	}
    72  	find := false
    73  	for i := 0; i < len(self.inetworks); i += 1 {
    74  		if self.inetworks[i].GetId() == vswitch.VSwitchId {
    75  			find = true
    76  			break
    77  		}
    78  	}
    79  	if !find {
    80  		self.inetworks = append(self.inetworks, vswitch)
    81  	}
    82  }
    83  
    84  func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    85  	if self.inetworks == nil {
    86  		err := self.vpc.fetchVSwitches()
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  	}
    91  	return self.inetworks, nil
    92  }
    93  
    94  func (self *SWire) getNetworkById(vswitchId string) *SVSwitch {
    95  	networks, err := self.GetINetworks()
    96  	if err != nil {
    97  		return nil
    98  	}
    99  	log.Debugf("search for networks %d", len(networks))
   100  	for i := 0; i < len(networks); i += 1 {
   101  		log.Debugf("search %s", networks[i].GetName())
   102  		network := networks[i].(*SVSwitch)
   103  		if network.VSwitchId == vswitchId {
   104  			return network
   105  		}
   106  	}
   107  	return nil
   108  }
   109  
   110  func (self *SWire) GetBandwidth() int {
   111  	return 10000
   112  }
   113  
   114  func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
   115  	vswitchId, err := self.zone.region.createVSwitch(self.zone.ZoneId, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
   116  	if err != nil {
   117  		log.Errorf("createVSwitch error %s", err)
   118  		return nil, err
   119  	}
   120  	self.inetworks = nil
   121  	vswitch := self.getNetworkById(vswitchId)
   122  	if vswitch == nil {
   123  		log.Errorf("cannot find vswitch after create????")
   124  		return nil, cloudprovider.ErrNotFound
   125  	}
   126  	return vswitch, nil
   127  }
   128  
   129  func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
   130  	networks, err := self.GetINetworks()
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	for i := 0; i < len(networks); i += 1 {
   135  		if networks[i].GetGlobalId() == netid {
   136  			return networks[i], nil
   137  		}
   138  	}
   139  	return nil, cloudprovider.ErrNotFound
   140  }