yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/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 cloudpods 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 21 api "yunion.io/x/onecloud/pkg/apis/compute" 22 modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" 23 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SWire struct { 29 CloudpodsTags 30 multicloud.SResourceBase 31 32 vpc *SVpc 33 34 api.WireDetails 35 } 36 37 func (self *SWire) GetId() string { 38 return self.Id 39 } 40 41 func (self *SWire) GetGlobalId() string { 42 return self.Id 43 } 44 45 func (self *SWire) GetName() string { 46 return self.Name 47 } 48 49 func (self *SWire) GetStatus() string { 50 return self.Status 51 } 52 53 func (self *SWire) GetBandwidth() int { 54 return self.Bandwidth 55 } 56 57 func (self *SWire) IsEmulated() bool { 58 return self.SWire.IsEmulated 59 } 60 61 func (self *SWire) GetIVpc() cloudprovider.ICloudVpc { 62 return self.vpc 63 } 64 65 func (self *SWire) GetIZone() cloudprovider.ICloudZone { 66 if len(self.ZoneId) == 0 { 67 return nil 68 } 69 zone, err := self.vpc.region.GetZone(self.ZoneId) 70 if err != nil { 71 return nil 72 } 73 return zone 74 } 75 76 func (self *SWire) Refresh() error { 77 wire, err := self.vpc.region.GetWire(self.Id) 78 if err != nil { 79 return err 80 } 81 return jsonutils.Update(self, wire) 82 } 83 84 func (self *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) { 85 wire, err := self.region.GetWire(id) 86 if err != nil { 87 return nil, errors.Wrapf(err, "GetWire(%s)", id) 88 } 89 wire.vpc = self 90 return wire, nil 91 } 92 93 func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) { 94 wires, err := self.region.GetWires(self.Id, "") 95 if err != nil { 96 return nil, errors.Wrapf(err, "GetWires") 97 } 98 ret := []cloudprovider.ICloudWire{} 99 for i := range wires { 100 wires[i].vpc = self 101 ret = append(ret, &wires[i]) 102 } 103 return ret, nil 104 } 105 106 func (self *SRegion) GetWires(vpcId, hostId string) ([]SWire, error) { 107 wires := []SWire{} 108 params := map[string]interface{}{"cloud_env": ""} 109 if len(vpcId) > 0 { 110 params["vpc_id"] = vpcId 111 } 112 if len(hostId) > 0 { 113 params["host_id"] = hostId 114 } 115 return wires, self.list(&modules.Wires, params, &wires) 116 } 117 118 func (self *SRegion) GetWire(id string) (*SWire, error) { 119 wire := &SWire{} 120 return wire, self.cli.get(&modules.Wires, id, nil, wire) 121 }