yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud 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 QcloudTags 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) GetBandwidth() int { 69 return 10000 70 } 71 72 func (self *SWire) GetNetworkById(networkId string) *SNetwork { 73 networks, err := self.GetINetworks() 74 if err != nil { 75 return nil 76 } 77 log.Debugf("search for networks %d", len(networks)) 78 for i := 0; i < len(networks); i += 1 { 79 log.Debugf("search %s", networks[i].GetName()) 80 network := networks[i].(*SNetwork) 81 if network.SubnetId == networkId { 82 return network 83 } 84 } 85 return nil 86 } 87 88 func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) { 89 networkId, err := self.zone.region.CreateNetwork(self.zone.Zone, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc) 90 if err != nil { 91 log.Errorf("CreateNetwork error %s", err) 92 return nil, err 93 } 94 self.inetworks = nil 95 network := self.GetNetworkById(networkId) 96 if network == nil { 97 log.Errorf("cannot find vswitch after create????") 98 return nil, cloudprovider.ErrNotFound 99 } 100 return network, nil 101 } 102 103 func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) { 104 networks, err := self.GetINetworks() 105 if err != nil { 106 return nil, err 107 } 108 for i := 0; i < len(networks); i += 1 { 109 if networks[i].GetGlobalId() == netid { 110 return networks[i], nil 111 } 112 } 113 return nil, cloudprovider.ErrNotFound 114 } 115 116 func (self *SWire) getNetworkById(networkId string) *SNetwork { 117 networks, err := self.GetINetworks() 118 if err != nil { 119 return nil 120 } 121 log.Debugf("search for networks %d", len(networks)) 122 for i := 0; i < len(networks); i += 1 { 123 log.Debugf("search %s", networks[i].GetName()) 124 network := networks[i].(*SNetwork) 125 if network.SubnetId == networkId { 126 return network 127 } 128 } 129 return nil 130 } 131 132 func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) { 133 if self.inetworks == nil { 134 err := self.vpc.fetchNetworks() 135 if err != nil { 136 return nil, err 137 } 138 } 139 return self.inetworks, nil 140 } 141 142 func (self *SWire) addNetwork(network *SNetwork) { 143 if self.inetworks == nil { 144 self.inetworks = make([]cloudprovider.ICloudNetwork, 0) 145 } 146 find := false 147 for i := 0; i < len(self.inetworks); i += 1 { 148 if self.inetworks[i].GetId() == network.SubnetId { 149 find = true 150 break 151 } 152 } 153 if !find { 154 self.inetworks = append(self.inetworks, network) 155 } 156 }